KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.statistics;
2
3 /**
4 * The ProbabilityDistribution superclass provides an object for encapsulating probability distributions.
5 * @version 1.0
6 * @author Jaco van Kooten
7 */

8 public abstract class ProbabilityDistribution extends Object JavaDoc {
9         /**
10         * Constructs a probability distribution.
11         */

12         public ProbabilityDistribution() {}
13         /**
14         * Probability density function.
15         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
16         */

17         public abstract double probability(double X);
18         /**
19         * Cumulative distribution function.
20     * @return the probability that a stochastic variable x is less then X, i.e. P(x<X).
21         */

22         public abstract double cumulative(double X);
23         /**
24     * Inverse of the cumulative distribution function.
25         * @return the value X for which P(x<X).
26         */

27         public abstract double inverse(double probability);
28         /**
29     * Check if the range of the argument of the distribution method is between <code>lo</code> and <code>hi</code>.
30         * @exception OutOfRangeException If the argument is out of range.
31         */

32         protected final void checkRange(double x, double lo, double hi) {
33                 if(x<lo || x>hi)
34                         throw new OutOfRangeException("The argument of the distribution method should be between "+lo+" and "+hi+".");
35         }
36         /**
37     * Check if the range of the argument of the distribution method is between 0.0 and 1.0.
38         * @exception OutOfRangeException If the argument is out of range.
39         */

40         protected final void checkRange(double x) {
41             if(x<0.0 || x>1.0)
42                 throw new OutOfRangeException("The argument of the distribution method should be between 0.0 and 1.0.");
43         }
44
45         private static final double FINDROOT_ACCURACY = 1.0e-15;
46         private static final int FINDROOT_MAX_ITERATIONS = 150;
47         /**
48         * This method approximates the value of X for which P(x&lt;X)=<I>prob</I>.
49         * It applies a combination of a Newton-Raphson procedure and bisection method
50         * with the value <I>guess</I> as a starting point. Furthermore, to ensure convergency
51         * and stability, one should supply an inverval [<I>xLo</I>,<I>xHi</I>] in which the probalility
52         * distribution reaches the value <I>prob</I>. The method does no checking, it will produce
53         * bad results if wrong values for the parameters are supplied - use it with care.
54         */

55         protected final double findRoot(double prob,double guess,double xLo,double xHi) {
56                 double x=guess,xNew=guess;
57                 double error,pdf,dx=1.0;
58                 int i=0;
59                 while(Math.abs(dx)>FINDROOT_ACCURACY && i++<FINDROOT_MAX_ITERATIONS) {
60 // Apply Newton-Raphson step
61
error=cumulative(x)-prob;
62                         if(error<0.0)
63                                 xLo=x;
64                         else
65                                 xHi=x;
66                         pdf=probability(x);
67                         if(pdf!=0.0) { // Avoid division by zero
68
dx=error/pdf;
69                                 xNew=x-dx;
70                         }
71 // If the Newton-Raphson fails to converge (which for example may be the
72
// case if the initial guess is to rough) we apply a bisection
73
// step to determine a more narrow interval around the root.
74
if(xNew<xLo || xNew>xHi || pdf==0.0) {
75                                 xNew=(xLo+xHi)/2.0;
76                                 dx=xNew-x;
77                         }
78                         x=xNew;
79                 }
80                 return x;
81         }
82 }
83
84
Popular Tags