KickJava   Java API By Example, From Geeks To Geeks.

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


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

8 public final class ExponentialDistribution extends ProbabilityDistribution {
9         private double lambda;
10
11         /**
12         * Constructs the standard exponential distribution.
13         */

14         public ExponentialDistribution() {
15                 this(1.0);
16         }
17         /**
18         * Constructs an exponential distribution.
19         * @param decay the scale parameter.
20         */

21         public ExponentialDistribution(double decay) {
22                 if(decay<0.0)
23                         throw new OutOfRangeException("The scale parameter should be positive.");
24                 lambda=decay;
25         }
26         /**
27         * Constructs an exponential distribution from a data set.
28         * @param array a sample.
29         */

30         public ExponentialDistribution(double array[]) {
31                 double sumX=array[0];
32                 for(int i=1;i<array.length;i++)
33                         sumX+=array[i];
34                 lambda=sumX/array.length;
35         }
36         /**
37         * Returns the scale parameter.
38         */

39         public double getScaleParameter() {
40                 return lambda;
41         }
42         /**
43         * Returns the mean.
44         */

45         public double getMean() {
46                 return lambda;
47         }
48         /**
49         * Returns the variance.
50         */

51         public double getVariance() {
52                 return lambda*lambda;
53         }
54         /**
55         * Probability density function of an exponential distribution.
56         * P(X) = <img border=0 alt="lambda" SRC="../doc-files/lambda.gif">e<sup>-<img border=0 alt="lambda" SRC="../doc-files/lambda.gif">X</sup>.
57         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
58         */

59         public double probability(double X) {
60                 checkRange(X,0.0,Double.MAX_VALUE);
61                 return lambda*Math.exp(-lambda*X);
62         }
63         /**
64         * Cumulative exponential distribution function.
65     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
66         */

67         public double cumulative(double X) {
68                 checkRange(X,0.0,Double.MAX_VALUE);
69                 return 1.0-Math.exp(-lambda*X);
70         }
71         /**
72     * Inverse of the cumulative exponential distribution function.
73         * @return the value X for which P(x&lt;X).
74         */

75         public double inverse(double probability) {
76                 checkRange(probability);
77                 return -Math.log(1.0-probability)/lambda;
78         }
79 }
80
81
Popular Tags