| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.math.distributions; |
| 27 |
|
| 28 |
import dr.math.Poisson; |
| 29 |
import dr.math.UnivariateFunction; |
| 30 |
import org.apache.commons.math.MathException; |
| 31 |
import org.apache.commons.math.distribution.PoissonDistributionImpl; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class PoissonDistribution implements Distribution { |
| 38 |
|
| 39 |
org.apache.commons.math.distribution.PoissonDistribution distribution; |
| 40 |
|
| 41 |
public PoissonDistribution(double mean) { |
| 42 |
distribution = new org.apache.commons.math.distribution.PoissonDistributionImpl(mean); |
| 43 |
} |
| 44 |
|
| 45 |
public double pdf(double x) { |
| 46 |
return distribution.probability(x); |
| 47 |
} |
| 48 |
|
| 49 |
public double logPdf(double x) { |
| 50 |
|
| 51 |
double pdf = distribution.probability(x); |
| 52 |
if (pdf == 0 || Double.isNaN(pdf)) { |
| 53 |
final double mean = mean(); |
| 54 |
return x * Math.log(mean) - Poisson.gammln(x + 1) - mean; |
| 55 |
} |
| 56 |
return Math.log(pdf); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
public double cdf(double x) { |
| 61 |
try { |
| 62 |
return distribution.cumulativeProbability(x); |
| 63 |
} catch (MathException e) { |
| 64 |
throw new RuntimeException(e); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public double quantile(double y) { |
| 69 |
try { |
| 70 |
return distribution.inverseCumulativeProbability(y); |
| 71 |
} catch (MathException e) { |
| 72 |
throw new RuntimeException(e); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public double mean() { |
| 77 |
return distribution.getMean(); |
| 78 |
} |
| 79 |
|
| 80 |
public double variance() { |
| 81 |
return distribution.getMean(); |
| 82 |
} |
| 83 |
|
| 84 |
public UnivariateFunction getProbabilityDensityFunction() { |
| 85 |
throw new RuntimeException(); |
| 86 |
} |
| 87 |
|
| 88 |
public double truncatedMean(int max) { |
| 89 |
|
| 90 |
double CDF = 0; |
| 91 |
double mean = 0; |
| 92 |
for(int i=0; i<=max; i++) { |
| 93 |
double p = distribution.probability(i); |
| 94 |
mean += i*p; |
| 95 |
CDF += p; |
| 96 |
} |
| 97 |
return mean / CDF; |
| 98 |
} |
| 99 |
|
| 100 |
public static double pdf(double x, double mean) { |
| 101 |
PoissonDistributionImpl dist = new PoissonDistributionImpl(mean); |
| 102 |
return dist.probability(x); |
| 103 |
} |
| 104 |
|
| 105 |
public static double logPdf(double x, double mean) { |
| 106 |
PoissonDistributionImpl dist = new PoissonDistributionImpl(mean); |
| 107 |
double pdf = dist.probability(x); |
| 108 |
if (pdf == 0 || Double.isNaN(pdf)) { |
| 109 |
return x * Math.log(mean) - Poisson.gammln(x + 1) - mean; |
| 110 |
} |
| 111 |
return Math.log(pdf); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
public static double cdf(double x, double mean) { |
| 116 |
try { |
| 117 |
PoissonDistributionImpl dist = new PoissonDistributionImpl(mean); |
| 118 |
return dist.cumulativeProbability(x); |
| 119 |
} catch (MathException e) { |
| 120 |
throw new RuntimeException(e); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public static double quantile(double y, double mean) { |
| 125 |
try { |
| 126 |
PoissonDistributionImpl dist = new PoissonDistributionImpl(mean); |
| 127 |
return dist.inverseCumulativeProbability(y); |
| 128 |
} catch (MathException e) { |
| 129 |
throw new RuntimeException(e); |
| 130 |
} |
| 131 |
} |
| 132 |
} |