KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 public final class NormalDistribution extends ProbabilityDistribution implements NumericalConstants {
11         private double mean,variance;
12         private double pdfDenominator,cdfDenominator;
13
14         /**
15         * Constructs the standard normal distribution (zero mean and unity variance).
16         */

17         public NormalDistribution() {
18                 this(0.0,1.0);
19         }
20         /**
21         * Constructs a normal distribution.
22         * @param mu the mean.
23         * @param var the variance.
24         */

25         public NormalDistribution(double mu,double var) {
26                 mean=mu;
27                 if(var<=0.0)
28                         throw new OutOfRangeException("The variance should be (strictly) positive.");
29                 variance=var;
30                 pdfDenominator=SQRT2PI*Math.sqrt(variance);
31                 cdfDenominator=SQRT2*Math.sqrt(variance);
32         }
33         /**
34         * Constructs a normal distribution from a data set.
35         * @param array a sample.
36         * @author Mark Hale
37         */

38         public NormalDistribution(double array[]) {
39                 double sumX=array[0];
40                 double sumX2=array[0]*array[0];
41                 for(int i=1;i<array.length;i++) {
42                         sumX+=array[i];
43                         sumX2+=array[i]*array[i];
44                 }
45                 mean=sumX/array.length;
46                 variance=(sumX2 - array.length*mean*mean)/(array.length-1);
47                 pdfDenominator=SQRT2PI*Math.sqrt(variance);
48                 cdfDenominator=SQRT2*Math.sqrt(variance);
49         }
50         /**
51         * Returns the mean.
52         */

53         public double getMean() {
54                 return mean;
55         }
56         /**
57         * Returns the variance.
58         */

59         public double getVariance() {
60                 return variance;
61         }
62         /**
63         * Probability density function of a normal (Gaussian) distribution.
64         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
65         */

66         public double probability(double X) {
67                 return Math.exp(-(X-mean)*(X-mean)/(2*variance))/pdfDenominator;
68         }
69         /**
70         * Cumulative normal distribution function.
71     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
72         */

73         public double cumulative(double X) {
74                 return SpecialMath.complementaryError(-(X-mean)/cdfDenominator)/2;
75         }
76         /**
77     * Inverse of the cumulative normal distribution function.
78         * @return the value X for which P(x&lt;X).
79         */

80         public double inverse(double probability) {
81                 checkRange(probability);
82                 if(probability==0.0)
83                         return -Double.MAX_VALUE;
84                 if(probability==1.0)
85                         return Double.MAX_VALUE;
86                 if(probability==0.5)
87                         return mean;
88 // To ensure numerical stability we need to rescale the distribution
89
double meanSave=mean,varSave=variance;
90                 double pdfDSave=pdfDenominator,cdfDSave=cdfDenominator;
91                 mean=0.0;
92                 variance=1.0;
93                 pdfDenominator=Math.sqrt(TWO_PI);
94                 cdfDenominator=SQRT2;
95                 double X=findRoot(probability, 0.0, -100.0, 100.0);
96 // Scale back
97
mean=meanSave;
98                 variance=varSave;
99                 pdfDenominator=pdfDSave;
100                 cdfDenominator=cdfDSave;
101                 return X*Math.sqrt(variance)+mean;
102         }
103 }
104
105
Popular Tags