KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.statistics;
2
3 import JSci.maths.SpecialMath;
4
5 /**
6 * The WeibullDistribution class provides an object for encapsulating Weibull distributions.
7 * @version 0.2
8 * @author Mark Hale
9 */

10 public final class WeibullDistribution extends ProbabilityDistribution {
11         private double shape;
12
13         /**
14         * Constructs a Weibull distribution.
15         * @param sh the shape.
16         */

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

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

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

37         public double getVariance() {
38                 return SpecialMath.gamma(1.0+2.0/shape)-getMean()*getMean();
39         }
40         /**
41         * Probability density function of a Weibull distribution.
42         * P(X) = s X<sup>s-1</sup> exp(-X<sup>s</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                 final double XpowShape=Math.pow(X,shape);
49                 return shape*XpowShape/X*Math.exp(-XpowShape);
50         }
51         /**
52         * Cumulative Weibull distribution function.
53         * @param X should be integer-valued.
54     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
55         */

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

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