~ubuntu-branches/ubuntu/oneiric/commons-math/oneiric

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/distribution/ChiSquaredDistributionImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-04-05 23:33:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100405233302-gpqlceked76nw28a
Tags: 2.1-1
* New upstream release.
* Bump Standards-Version to 3.8.4: no changes needed
* Bump debhelper to >= 7
* Switch to 3.0 (quilt) source format:
  - Remove B-D on quilt
  - Add d/source/format
  - Remove d/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
/**
24
24
 * The default implementation of {@link ChiSquaredDistribution}
25
25
 *
26
 
 * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
 
26
 * @version $Revision: 925812 $ $Date: 2010-03-21 11:49:31 -0400 (Sun, 21 Mar 2010) $
27
27
 */
28
28
public class ChiSquaredDistributionImpl
29
29
    extends AbstractContinuousDistribution
30
30
    implements ChiSquaredDistribution, Serializable  {
31
 
    
 
31
 
 
32
    /**
 
33
     * Default inverse cumulative probability accuracy
 
34
     * @since 2.1
 
35
     */
 
36
    public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
 
37
 
32
38
    /** Serializable version identifier */
33
39
    private static final long serialVersionUID = -8352658048349159782L;
34
40
 
35
 
    /** Internal Gamma distribution. */    
 
41
    /** Internal Gamma distribution. */
36
42
    private GammaDistribution gamma;
37
 
    
 
43
 
 
44
    /** Inverse cumulative probability accuracy */
 
45
    private final double solverAbsoluteAccuracy;
 
46
 
38
47
    /**
39
48
     * Create a Chi-Squared distribution with the given degrees of freedom.
40
49
     * @param df degrees of freedom.
42
51
    public ChiSquaredDistributionImpl(double df) {
43
52
        this(df, new GammaDistributionImpl(df / 2.0, 2.0));
44
53
    }
45
 
    
 
54
 
46
55
    /**
47
56
     * Create a Chi-Squared distribution with the given degrees of freedom.
48
57
     * @param df degrees of freedom.
49
58
     * @param g the underlying gamma distribution used to compute probabilities.
50
59
     * @since 1.2
 
60
     * @deprecated as of 2.1 (to avoid possibly inconsistent state, the
 
61
     * "GammaDistribution" will be instantiated internally)
51
62
     */
 
63
    @Deprecated
52
64
    public ChiSquaredDistributionImpl(double df, GammaDistribution g) {
53
65
        super();
54
 
        setGamma(g);
55
 
        setDegreesOfFreedom(df);
56
 
    }
57
 
    
 
66
        setGammaInternal(g);
 
67
        setDegreesOfFreedomInternal(df);
 
68
        solverAbsoluteAccuracy = DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
 
69
    }
 
70
 
 
71
    /**
 
72
     * Create a Chi-Squared distribution with the given degrees of freedom and
 
73
     * inverse cumulative probability accuracy.
 
74
     * @param df degrees of freedom.
 
75
     * @param inverseCumAccuracy the maximum absolute error in inverse cumulative probability estimates
 
76
     * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY})
 
77
     * @since 2.1
 
78
     */
 
79
    public ChiSquaredDistributionImpl(double df, double inverseCumAccuracy) {
 
80
        super();
 
81
        gamma = new GammaDistributionImpl(df / 2.0, 2.0);
 
82
        setDegreesOfFreedomInternal(df);
 
83
        solverAbsoluteAccuracy = inverseCumAccuracy;
 
84
    }
 
85
 
58
86
    /**
59
87
     * Modify the degrees of freedom.
60
88
     * @param degreesOfFreedom the new degrees of freedom.
 
89
     * @deprecated as of 2.1 (class will become immutable in 3.0)
61
90
     */
 
91
    @Deprecated
62
92
    public void setDegreesOfFreedom(double degreesOfFreedom) {
63
 
        getGamma().setAlpha(degreesOfFreedom / 2.0);
64
 
    }
65
 
        
 
93
        setDegreesOfFreedomInternal(degreesOfFreedom);
 
94
    }
 
95
    /**
 
96
     * Modify the degrees of freedom.
 
97
     * @param degreesOfFreedom the new degrees of freedom.
 
98
     */
 
99
    private void setDegreesOfFreedomInternal(double degreesOfFreedom) {
 
100
        gamma.setAlpha(degreesOfFreedom / 2.0);
 
101
    }
 
102
 
66
103
    /**
67
104
     * Access the degrees of freedom.
68
105
     * @return the degrees of freedom.
69
106
     */
70
107
    public double getDegreesOfFreedom() {
71
 
        return getGamma().getAlpha() * 2.0;
 
108
        return gamma.getAlpha() * 2.0;
72
109
    }
73
110
 
74
111
    /**
76
113
     *
77
114
     * @param x The point at which the density should be computed.
78
115
     * @return The pdf at point x.
 
116
     * @deprecated
79
117
     */
80
118
    public double density(Double x) {
 
119
        return density(x.doubleValue());
 
120
    }
 
121
 
 
122
    /**
 
123
     * Return the probability density for a particular point.
 
124
     *
 
125
     * @param x The point at which the density should be computed.
 
126
     * @return The pdf at point x.
 
127
     * @since 2.1
 
128
     */
 
129
    @Override
 
130
    public double density(double x) {
81
131
        return gamma.density(x);
82
132
    }
83
133
 
84
134
    /**
85
135
     * For this distribution, X, this method returns P(X < x).
86
136
     * @param x the value at which the CDF is evaluated.
87
 
     * @return CDF for this distribution. 
 
137
     * @return CDF for this distribution.
88
138
     * @throws MathException if the cumulative probability can not be
89
139
     *            computed due to convergence or other numerical errors.
90
140
     */
91
141
    public double cumulativeProbability(double x) throws MathException {
92
 
        return getGamma().cumulativeProbability(x);
 
142
        return gamma.cumulativeProbability(x);
93
143
    }
94
 
    
 
144
 
95
145
    /**
96
146
     * For this distribution, X, this method returns the critical point x, such
97
147
     * that P(X &lt; x) = <code>p</code>.
116
166
        }
117
167
        return super.inverseCumulativeProbability(p);
118
168
    }
119
 
        
 
169
 
120
170
    /**
121
171
     * Access the domain value lower bound, based on <code>p</code>, used to
122
172
     * bracket a CDF root.  This method is used by
123
173
     * {@link #inverseCumulativeProbability(double)} to find critical values.
124
 
     * 
 
174
     *
125
175
     * @param p the desired probability for the critical value
126
176
     * @return domain value lower bound, i.e.
127
 
     *         P(X &lt; <i>lower bound</i>) &lt; <code>p</code> 
 
177
     *         P(X &lt; <i>lower bound</i>) &lt; <code>p</code>
128
178
     */
129
179
    @Override
130
180
    protected double getDomainLowerBound(double p) {
131
 
        return Double.MIN_VALUE * getGamma().getBeta();
 
181
        return Double.MIN_VALUE * gamma.getBeta();
132
182
    }
133
183
 
134
184
    /**
135
185
     * Access the domain value upper bound, based on <code>p</code>, used to
136
186
     * bracket a CDF root.  This method is used by
137
187
     * {@link #inverseCumulativeProbability(double)} to find critical values.
138
 
     * 
 
188
     *
139
189
     * @param p the desired probability for the critical value
140
190
     * @return domain value upper bound, i.e.
141
 
     *         P(X &lt; <i>upper bound</i>) &gt; <code>p</code> 
 
191
     *         P(X &lt; <i>upper bound</i>) &gt; <code>p</code>
142
192
     */
143
193
    @Override
144
194
    protected double getDomainUpperBound(double p) {
154
204
            // use max
155
205
            ret = Double.MAX_VALUE;
156
206
        }
157
 
        
 
207
 
158
208
        return ret;
159
209
    }
160
210
 
162
212
     * Access the initial domain value, based on <code>p</code>, used to
163
213
     * bracket a CDF root.  This method is used by
164
214
     * {@link #inverseCumulativeProbability(double)} to find critical values.
165
 
     * 
 
215
     *
166
216
     * @param p the desired probability for the critical value
167
217
     * @return initial domain value
168
218
     */
170
220
    protected double getInitialDomain(double p) {
171
221
        // NOTE: chi squared is skewed to the left
172
222
        // NOTE: therefore, P(X < &mu;) > .5
173
 
        
 
223
 
174
224
        double ret;
175
225
 
176
226
        if (p < .5) {
180
230
            // use mean
181
231
            ret = getDegreesOfFreedom();
182
232
        }
183
 
        
 
233
 
184
234
        return ret;
185
235
    }
186
 
    
 
236
 
187
237
    /**
188
238
     * Modify the underlying gamma distribution.  The caller is responsible for
189
239
     * insuring the gamma distribution has the proper parameter settings.
190
240
     * @param g the new distribution.
191
241
     * @since 1.2 made public
 
242
     * @deprecated as of 2.1 (class will become immutable in 3.0)
192
243
     */
 
244
    @Deprecated
193
245
    public void setGamma(GammaDistribution g) {
 
246
        setGammaInternal(g);
 
247
    }
 
248
    /**
 
249
     * Modify the underlying gamma distribution.  The caller is responsible for
 
250
     * insuring the gamma distribution has the proper parameter settings.
 
251
     * @param g the new distribution.
 
252
     * @since 1.2 made public
 
253
     */
 
254
    private void setGammaInternal(GammaDistribution g) {
194
255
        this.gamma = g;
195
 
        
 
256
 
196
257
    }
197
258
 
 
259
 
198
260
    /**
199
 
     * Access the Gamma distribution.
200
 
     * @return the internal Gamma distribution.
 
261
     * Return the absolute accuracy setting of the solver used to estimate
 
262
     * inverse cumulative probabilities.
 
263
     *
 
264
     * @return the solver absolute accuracy
 
265
     * @since 2.1
201
266
     */
202
 
    private GammaDistribution getGamma() {
203
 
        return gamma;
 
267
    @Override
 
268
    protected double getSolverAbsoluteAccuracy() {
 
269
        return solverAbsoluteAccuracy;
204
270
    }
205
271
}