1 16 package org.apache.commons.math.distribution; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.math.MathException; 21 import org.apache.commons.math.special.Gamma; 22 import org.apache.commons.math.util.MathUtils; 23 24 29 public class PoissonDistributionImpl extends AbstractIntegerDistribution 30 implements PoissonDistribution, Serializable { 31 32 33 static final long serialVersionUID = -3349935121172596109L; 34 35 38 private double mean; 39 40 48 public PoissonDistributionImpl(double p) { 49 super(); 50 setMean(p); 51 } 52 53 58 public double getMean() { 59 return this.mean; 60 } 61 62 70 public void setMean(double p) { 71 if (p <= 0) { 72 throw new IllegalArgumentException ( 73 "The Poisson mean must be positive"); 74 } 75 this.mean = p; 76 } 77 78 84 public double probability(int x) { 85 if (x < 0 || x == Integer.MAX_VALUE) { 86 return 0; 87 } 88 return Math.pow(getMean(), x) / 89 MathUtils.factorialDouble(x) * Math.exp(-mean); 90 } 91 92 100 public double cumulativeProbability(int x) throws MathException { 101 if (x < 0) { 102 return 0; 103 } 104 if (x == Integer.MAX_VALUE) { 105 return 1; 106 } 107 return Gamma.regularizedGammaQ((double)x + 1, mean, 108 1E-12, Integer.MAX_VALUE); 109 } 110 111 123 public double normalApproximateProbability(int x) throws MathException { 124 NormalDistribution normal = DistributionFactory.newInstance() 125 .createNormalDistribution(getMean(), 126 Math.sqrt(getMean())); 127 128 return normal.cumulativeProbability(x + 0.5); 130 } 131 132 140 protected int getDomainLowerBound(double p) { 141 return 0; 142 } 143 144 152 protected int getDomainUpperBound(double p) { 153 return Integer.MAX_VALUE; 154 } 155 156 } | Popular Tags |