1 package JSci.maths.statistics; 2 3 8 public abstract class ProbabilityDistribution extends Object { 9 12 public ProbabilityDistribution() {} 13 17 public abstract double probability(double X); 18 22 public abstract double cumulative(double X); 23 27 public abstract double inverse(double probability); 28 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 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 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 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) { dx=error/pdf; 69 xNew=x-dx; 70 } 71 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 |