KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 public final class GammaDistribution extends ProbabilityDistribution {
11         private double shape;
12
13         /**
14         * Constructs a gamma distribution.
15         * @param s the shape parameter.
16         */

17         public GammaDistribution(double s) {
18                 if(s<=0.0)
19                         throw new OutOfRangeException("The shape parameter should be (strictly) positive.");
20                 shape=s;
21         }
22         /**
23         * Returns the shape parameter.
24         */

25         public double getShapeParameter() {
26                 return shape;
27         }
28         /**
29         * Returns the mean.
30         */

31         public double getMean() {
32                 return shape;
33         }
34         /**
35         * Returns the variance.
36         */

37         public double getVariance() {
38                 return shape;
39         }
40         /**
41         * Probability density function of a gamma distribution.
42         * P(X) = X<sup>s-1</sup> e<sup>-X</sup>/<img border=0 alt="Gamma" SRC="../doc-files/ugamma.gif">(s).
43         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
44         */

45         public double probability(double X) {
46                 checkRange(X,0.0,Double.MAX_VALUE);
47                 if(X==0.0)
48                         return 0.0;
49                 else
50                         return Math.exp(-SpecialMath.logGamma(shape)-X+(shape-1)*Math.log(X));
51         }
52         /**
53         * Cumulative gamma distribution function.
54     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
55         */

56         public double cumulative(double X) {
57                 checkRange(X,0.0,Double.MAX_VALUE);
58                 return SpecialMath.incompleteGamma(shape,X);
59         }
60         /**
61     * Inverse of the cumulative gamma distribution function.
62         * @return the value X for which P(x&lt;X).
63         */

64         public double inverse(double probability) {
65                 checkRange(probability);
66                 if(probability==0.0)
67                         return 0.0;
68                 if(probability==1.0)
69                         return Double.MAX_VALUE;
70                 return findRoot(probability, shape, 0.0, Double.MAX_VALUE);
71         }
72 }
73
74
Popular Tags