01: package JSci.maths.statistics;
02:
03: import JSci.maths.SpecialMath;
04:
05: /**
06: * The BetaDistribution class provides an object for encapsulating beta distributions.
07: * @version 1.0
08: * @author Jaco van Kooten
09: */
10: public final class BetaDistribution extends ProbabilityDistribution {
11: private double p, q;
12:
13: /**
14: * Constructs a beta distribution.
15: * @param dgrP degrees of freedom p.
16: * @param dgrQ degrees of freedom q.
17: */
18: public BetaDistribution(double dgrP, double dgrQ) {
19: if (dgrP <= 0 || dgrQ <= 0)
20: throw new OutOfRangeException(
21: "The degrees of freedom must be greater than zero.");
22: p = dgrP;
23: q = dgrQ;
24: }
25:
26: /**
27: * Returns the degrees of freedom p.
28: */
29: public double getDegreesOfFreedomP() {
30: return p;
31: }
32:
33: /**
34: * Returns the degrees of freedom q.
35: */
36: public double getDegreesOfFreedomQ() {
37: return q;
38: }
39:
40: /**
41: * Probability density function of a beta 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);
46: if (X == 0.0 || X == 1.0)
47: return 0.0;
48: return Math.exp(-SpecialMath.logBeta(p, q) + (p - 1.0)
49: * Math.log(X) + (q - 1.0) * Math.log(1.0 - X));
50: }
51:
52: /**
53: * Cumulative beta distribution function.
54: * @return the probability that a stochastic variable x is less then X, i.e. P(x<X).
55: */
56: public double cumulative(double X) {
57: checkRange(X);
58: return SpecialMath.incompleteBeta(X, p, q);
59: }
60:
61: /**
62: * Inverse of the cumulative beta distribution function.
63: * @return the value X for which P(x<X).
64: */
65: public double inverse(double probability) {
66: checkRange(probability);
67: if (probability == 0.0)
68: return 0.0;
69: if (probability == 1.0)
70: return 1.0;
71: return findRoot(probability, 0.5, 0.0, 1.0);
72: }
73: }
|