KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.statistics;
2
3 /**
4 * The GeometricDistribution class provides an object for encapsulating geometric distributions.
5 * @version 0.2
6 * @author Mark Hale
7 */

8 public final class GeometricDistribution extends ProbabilityDistribution {
9         private double success;
10         private double failure;
11
12         /**
13         * Constructs a geometric distribution.
14         * @param prob the probability of success.
15         */

16         public GeometricDistribution(double prob) {
17                 if(prob<0.0 || prob>1.0)
18                         throw new OutOfRangeException("The probability should be between 0.0 and 1.0.");
19                 success=prob;
20                 failure=1.0-prob;
21         }
22         /**
23         * Returns the success parameter.
24         */

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

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

37         public double getVariance() {
38                 return failure/(success*success);
39         }
40         /**
41         * Probability density function of a geometric distribution.
42         * P(X) = p (1-p)<sup>X-1</sup>.
43         * @param X should be integer-valued.
44         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
45         */

46         public double probability(double X) {
47                 checkRange(X,0.0,Double.MAX_VALUE);
48                 return success*Math.pow(failure,X-1);
49         }
50         /**
51         * Cumulative geometric distribution function.
52         * @param X should be integer-valued.
53     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
54         */

55         public double cumulative(double X) {
56                 checkRange(X,0.0,Double.MAX_VALUE);
57                 return 1.0-Math.pow(failure,X);
58         }
59         /**
60     * Inverse of the cumulative geometric distribution function.
61         * @return the value X for which P(x&lt;X).
62         */

63         public double inverse(double probability) {
64                 checkRange(probability);
65                 return Math.log(1.0-probability)/Math.log(failure);
66         }
67 }
68
69
Popular Tags