01: package JSci.maths.statistics;
02:
03: import JSci.maths.SpecialMath;
04:
05: /**
06: * The FDistribution class provides an object for encapsulating F-distributions.
07: * @version 1.0
08: * @author Jaco van Kooten
09: */
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(
24: "The degrees of freedom must be greater than zero.");
25: p = dgrP;
26: q = dgrQ;
27: beta = new BetaDistribution(p / 2.0, q / 2.0);
28: }
29:
30: /**
31: * Returns the degrees of freedom p.
32: */
33: public double getDegreesOfFreedomP() {
34: return p;
35: }
36:
37: /**
38: * Returns the degrees of freedom q.
39: */
40: public double getDegreesOfFreedomQ() {
41: return q;
42: }
43:
44: /**
45: * Probability density function of an F-distribution.
46: * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
47: */
48: public double probability(double X) {
49: checkRange(X, 0.0, Double.MAX_VALUE);
50: final double y = q / (q + (p * X));
51: return beta.probability(1.0 - y) * y * y * p / q;
52: }
53:
54: /**
55: * Cumulative F-distribution function.
56: * @return the probability that a stochastic variable x is less then X, i.e. P(x<X).
57: */
58: public double cumulative(double X) {
59: checkRange(X, 0.0, Double.MAX_VALUE);
60: return beta.cumulative((p * X) / (q + (p * X)));
61: }
62:
63: /**
64: * Inverse of the cumulative F-distribution function.
65: * @return the value X for which P(x<X).
66: */
67: public double inverse(double probability) {
68: checkRange(probability);
69: if (probability == 0.0)
70: return 0.0;
71: if (probability == 1.0)
72: return Double.MAX_VALUE;
73: final double y = beta.inverse(probability);
74: if (y < 2.23e-308) //avoid overflow
75: return Double.MAX_VALUE;
76: else
77: return (q / p) * (y / (1.0 - y));
78: }
79: }
|