01: package JSci.maths.statistics;
02:
03: import JSci.maths.SpecialMath;
04:
05: /**
06: * The GammaDistribution class provides an object for encapsulating gamma distributions.
07: * @version 1.0
08: * @author Jaco van Kooten
09: */
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(
20: "The shape parameter should be (strictly) positive.");
21: shape = s;
22: }
23:
24: /**
25: * Returns the shape parameter.
26: */
27: public double getShapeParameter() {
28: return shape;
29: }
30:
31: /**
32: * Returns the mean.
33: */
34: public double getMean() {
35: return shape;
36: }
37:
38: /**
39: * Returns the variance.
40: */
41: public double getVariance() {
42: return shape;
43: }
44:
45: /**
46: * Probability density function of a gamma distribution.
47: * P(X) = X<sup>s-1</sup> e<sup>-X</sup>/<img border=0 alt="Gamma" src="../doc-files/ugamma.gif">(s).
48: * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
49: */
50: public double probability(double X) {
51: checkRange(X, 0.0, Double.MAX_VALUE);
52: if (X == 0.0)
53: return 0.0;
54: else
55: return Math.exp(-SpecialMath.logGamma(shape) - X
56: + (shape - 1) * Math.log(X));
57: }
58:
59: /**
60: * Cumulative gamma distribution function.
61: * @return the probability that a stochastic variable x is less then X, i.e. P(x<X).
62: */
63: public double cumulative(double X) {
64: checkRange(X, 0.0, Double.MAX_VALUE);
65: return SpecialMath.incompleteGamma(shape, X);
66: }
67:
68: /**
69: * Inverse of the cumulative gamma distribution function.
70: * @return the value X for which P(x<X).
71: */
72: public double inverse(double probability) {
73: checkRange(probability);
74: if (probability == 0.0)
75: return 0.0;
76: if (probability == 1.0)
77: return Double.MAX_VALUE;
78: return findRoot(probability, shape, 0.0, Double.MAX_VALUE);
79: }
80: }
|