KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > statistics > ChiSqrDistribution


1 package JSci.maths.statistics;
2
3 import JSci.maths.SpecialMath;
4
5 /**
6 * The ChiSqrDistribution class provides an object for encapsulating chi-squared distributions.
7 * @version 1.0
8 * @author Jaco van Kooten
9 */

10 public final class ChiSqrDistribution extends ProbabilityDistribution {
11         private double r;
12 // The ChiSqr and Gamma distributions are closely related.
13
private GammaDistribution gamma;
14
15         /**
16         * Constructs a chi-squared distribution.
17         * @param dgr degrees of freedom.
18         */

19         public ChiSqrDistribution(double dgr) {
20                 if(dgr<=0.0)
21                         throw new OutOfRangeException("The degrees of freedom must be greater than zero.");
22                 r=dgr;
23                 gamma=new GammaDistribution(0.5*r);
24         }
25         /**
26         * Returns the degrees of freedom.
27         */

28         public double getDegreesOfFreedom() {
29                 return r;
30         }
31         /**
32         * Probability density function of a chi-squared distribution.
33         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
34         */

35         public double probability(double X) {
36                 return 0.5*gamma.probability(0.5*X);
37         }
38         /**
39         * Cumulative chi-squared distribution function.
40     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
41         */

42         public double cumulative(double X) {
43                 checkRange(X,0.0,Double.MAX_VALUE);
44                 return SpecialMath.incompleteGamma(0.5*r,0.5*X);
45         }
46         /**
47     * Inverse of the cumulative chi-squared distribution function.
48         * @return the value X for which P(x&lt;X).
49         */

50         public double inverse(double probability) {
51                 if(probability==1.0)
52                         return Double.MAX_VALUE;
53                 else
54                         return 2.0*gamma.inverse(probability);
55         }
56 }
57
58
Popular Tags