24
24
* The default implementation of {@link ChiSquaredDistribution}
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) $
28
28
public class ChiSquaredDistributionImpl
29
29
extends AbstractContinuousDistribution
30
30
implements ChiSquaredDistribution, Serializable {
33
* Default inverse cumulative probability accuracy
36
public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
32
38
/** Serializable version identifier */
33
39
private static final long serialVersionUID = -8352658048349159782L;
35
/** Internal Gamma distribution. */
41
/** Internal Gamma distribution. */
36
42
private GammaDistribution gamma;
44
/** Inverse cumulative probability accuracy */
45
private final double solverAbsoluteAccuracy;
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));
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.
60
* @deprecated as of 2.1 (to avoid possibly inconsistent state, the
61
* "GammaDistribution" will be instantiated internally)
52
64
public ChiSquaredDistributionImpl(double df, GammaDistribution g) {
55
setDegreesOfFreedom(df);
67
setDegreesOfFreedomInternal(df);
68
solverAbsoluteAccuracy = DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
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})
79
public ChiSquaredDistributionImpl(double df, double inverseCumAccuracy) {
81
gamma = new GammaDistributionImpl(df / 2.0, 2.0);
82
setDegreesOfFreedomInternal(df);
83
solverAbsoluteAccuracy = inverseCumAccuracy;
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)
62
92
public void setDegreesOfFreedom(double degreesOfFreedom) {
63
getGamma().setAlpha(degreesOfFreedom / 2.0);
93
setDegreesOfFreedomInternal(degreesOfFreedom);
96
* Modify the degrees of freedom.
97
* @param degreesOfFreedom the new degrees of freedom.
99
private void setDegreesOfFreedomInternal(double degreesOfFreedom) {
100
gamma.setAlpha(degreesOfFreedom / 2.0);
67
104
* Access the degrees of freedom.
68
105
* @return the degrees of freedom.
70
107
public double getDegreesOfFreedom() {
71
return getGamma().getAlpha() * 2.0;
108
return gamma.getAlpha() * 2.0;
77
114
* @param x The point at which the density should be computed.
78
115
* @return The pdf at point x.
80
118
public double density(Double x) {
119
return density(x.doubleValue());
123
* Return the probability density for a particular point.
125
* @param x The point at which the density should be computed.
126
* @return The pdf at point x.
130
public double density(double x) {
81
131
return gamma.density(x);
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.
91
141
public double cumulativeProbability(double x) throws MathException {
92
return getGamma().cumulativeProbability(x);
142
return gamma.cumulativeProbability(x);
96
146
* For this distribution, X, this method returns the critical point x, such
97
147
* that P(X < x) = <code>p</code>.
117
167
return super.inverseCumulativeProbability(p);
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.
125
175
* @param p the desired probability for the critical value
126
176
* @return domain value lower bound, i.e.
127
* P(X < <i>lower bound</i>) < <code>p</code>
177
* P(X < <i>lower bound</i>) < <code>p</code>
130
180
protected double getDomainLowerBound(double p) {
131
return Double.MIN_VALUE * getGamma().getBeta();
181
return Double.MIN_VALUE * gamma.getBeta();
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.
139
189
* @param p the desired probability for the critical value
140
190
* @return domain value upper bound, i.e.
141
* P(X < <i>upper bound</i>) > <code>p</code>
191
* P(X < <i>upper bound</i>) > <code>p</code>
144
194
protected double getDomainUpperBound(double p) {
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.
166
216
* @param p the desired probability for the critical value
167
217
* @return initial domain value
181
231
ret = getDegreesOfFreedom();
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)
193
245
public void setGamma(GammaDistribution g) {
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
254
private void setGammaInternal(GammaDistribution g) {
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.
264
* @return the solver absolute accuracy
202
private GammaDistribution getGamma() {
268
protected double getSolverAbsoluteAccuracy() {
269
return solverAbsoluteAccuracy;