1 package JSci.maths.statistics; 2 3 import JSci.maths.ExtraMath; 4 5 10 public final class BinomialDistribution extends ProbabilityDistribution { 11 private int n; 12 private double p; 13 14 19 public BinomialDistribution(int trials,double prob) { 20 if(trials<=0) 21 throw new OutOfRangeException("The number of trials should be (strictly) positive."); 22 n=trials; 23 if(prob<0.0 || prob>1.0) 24 throw new OutOfRangeException("The probability should be between 0 and 1."); 25 p=prob; 26 } 27 30 public int getTrialsParameter() { 31 return n; 32 } 33 36 public double getProbabilityParameter() { 37 return p; 38 } 39 42 public double getMean() { 43 return n*p; 44 } 45 48 public double getVariance() { 49 return n*p*(1.0-p); 50 } 51 56 public double probability(double X) { 57 checkRange(X,0.0,n); 58 return ExtraMath.binomial(n,X)*Math.pow(p,X)*Math.pow(1.0-p,n-X); 59 } 60 65 public double cumulative(double X) { 66 checkRange(X,0.0,n); 67 double sum=0.0; 68 for(double i=0.0;i<=X;i++) 69 sum+=probability(i); 70 return sum; 71 } 72 76 public double inverse(double probability) { 77 checkRange(probability); 78 return Math.floor(findRoot(probability,n/2.0,0.0,n)); 79 } 80 } 81 82 | Popular Tags |