KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.statistics;
2
3 import JSci.maths.ExtraMath;
4
5 /**
6 * The PoissonDistribution class provides an object for encapsulating Poisson distributions.
7 * @version 0.3
8 * @author Mark Hale
9 */

10 public final class PoissonDistribution extends ProbabilityDistribution {
11         private double lambda;
12
13         /**
14         * Constructs a Poisson distribution.
15         * @param interval the interval.
16         */

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

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

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

37         public double getVariance() {
38                 return lambda;
39         }
40         /**
41         * Probability density function of a Poisson distribution.
42         * P(X) = <img border=0 alt="lambda" SRC="../doc-files/lambda.gif"><sup>X</sup>e<sup>-<img border=0 alt="lambda" SRC="../doc-files/lambda.gif"></sup>/X!.
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 Math.exp(X*Math.log(lambda)-lambda-ExtraMath.logFactorial(X));
49         }
50         /**
51         * Cumulative Poisson 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                 double sum=0.0;
58                 for(double i=0.0;i<=X;i++)
59                         sum+=probability(i);
60                 return sum;
61         }
62         /**
63     * Inverse of the cumulative Poisson distribution function.
64         * @return the value X for which P(x&lt;X).
65         */

66         public double inverse(double probability) {
67                 checkRange(probability);
68                 return Math.round(findRoot(probability,lambda,0.0,Double.MAX_VALUE));
69         }
70 }
71
72
Popular Tags