KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 public final class FDistribution extends ProbabilityDistribution {
11         private double p,q;
12 // We make use of the fact that when x has an F-distibution then
13
// y = p*x/(q+p*x) has a beta distribution with parameters p/2 and q/2.
14
private BetaDistribution beta;
15
16         /**
17         * Constructs an F-distribution.
18         * @param dgrP degrees of freedom p.
19         * @param dgrQ degrees of freedom q.
20         */

21         public FDistribution(double dgrP, double dgrQ) {
22                 if(dgrP<=0.0 || dgrQ<=0.0)
23                         throw new OutOfRangeException("The degrees of freedom must be greater than zero.");
24                 p=dgrP;
25                 q=dgrQ;
26                 beta=new BetaDistribution(p/2.0, q/2.0);
27         }
28         /**
29         * Returns the degrees of freedom p.
30         */

31         public double getDegreesOfFreedomP() {
32                 return p;
33         }
34         /**
35         * Returns the degrees of freedom q.
36         */

37         public double getDegreesOfFreedomQ() {
38                 return q;
39         }
40         /**
41         * Probability density function of an F-distribution.
42         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
43         */

44         public double probability(double X) {
45                 checkRange(X,0.0,Double.MAX_VALUE);
46                 final double y = q/(q+(p*X));
47                 return beta.probability(1.0-y)*y*y*p/q;
48         }
49         /**
50         * Cumulative F-distribution function.
51     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
52         */

53         public double cumulative(double X) {
54                 checkRange(X,0.0,Double.MAX_VALUE);
55                 return beta.cumulative((p*X)/(q+(p*X)));
56         }
57         /**
58     * Inverse of the cumulative F-distribution function.
59         * @return the value X for which P(x&lt;X).
60         */

61         public double inverse(double probability) {
62                 checkRange(probability);
63                 if(probability==0.0)
64                         return 0.0;
65                 if(probability==1.0)
66                         return Double.MAX_VALUE;
67                 final double y=beta.inverse(probability);
68                 if(y<2.23e-308) //avoid overflow
69
return Double.MAX_VALUE;
70                 else
71                         return (q/p)*(y/(1.0-y));
72         }
73 }
74
75
Popular Tags