KickJava   Java API By Example, From Geeks To Geeks.

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


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

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("The degrees of freedom must be greater than zero.");
21                 p=dgrP;
22                 q=dgrQ;
23         }
24         /**
25         * Returns the degrees of freedom p.
26         */

27         public double getDegreesOfFreedomP() {
28                 return p;
29         }
30         /**
31         * Returns the degrees of freedom q.
32         */

33         public double getDegreesOfFreedomQ() {
34                 return q;
35         }
36         /**
37         * Probability density function of a beta distribution.
38         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
39         */

40         public double probability(double X) {
41                 checkRange(X);
42                 if(X==0.0 || X==1.0)
43                         return 0.0;
44                 return Math.exp(-SpecialMath.logBeta(p,q)+(p-1.0)*Math.log(X)+(q-1.0)*Math.log(1.0-X));
45         }
46         /**
47         * Cumulative beta distribution function.
48     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
49         */

50         public double cumulative(double X) {
51                 checkRange(X);
52                 return SpecialMath.incompleteBeta(X,p,q);
53         }
54         /**
55     * Inverse of the cumulative beta distribution function.
56         * @return the value X for which P(x&lt;X).
57         */

58         public double inverse(double probability) {
59                 checkRange(probability);
60                 if(probability==0.0)
61                         return 0.0;
62                 if(probability==1.0)
63                         return 1.0;
64                 return findRoot(probability, 0.5, 0.0, 1.0);
65         }
66 }
67
68
Popular Tags