| 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 |
import dr.math.ErrorFunction; |
| 28 |
|
| 29 |
import org.apache.commons.math.MathException; |
| 30 |
import org.apache.commons.math.special.Beta; |
| 31 |
|
| 32 |
import dr.math.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class NegativeBinomialDistribution implements Distribution { |
| 39 |
|
| 40 |
double mean; |
| 41 |
double alpha; |
| 42 |
|
| 43 |
public NegativeBinomialDistribution(double mean, double alpha) { |
| 44 |
this.mean = mean; |
| 45 |
this.alpha = alpha; |
| 46 |
} |
| 47 |
|
| 48 |
public double pdf(double x) { |
| 49 |
return pdf(x, mean, alpha); |
| 50 |
} |
| 51 |
|
| 52 |
public double logPdf(double x) { |
| 53 |
return logPdf(x, mean, alpha); |
| 54 |
} |
| 55 |
|
| 56 |
public double cdf(double x) { |
| 57 |
return cdf(x, mean, alpha); |
| 58 |
} |
| 59 |
|
| 60 |
public double quantile(double y) { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
final double stdev = Math.sqrt(mean + (mean * mean * alpha)); |
| 65 |
final double r = -1 * (mean*mean) / (mean - stdev*stdev); |
| 66 |
final double p = mean / (stdev*stdev); |
| 67 |
final double prob = y; |
| 68 |
|
| 69 |
final double Q = 1.0 / p; |
| 70 |
final double P = (1.0 - p) * Q; |
| 71 |
final double gamma = (Q + P)/stdev; |
| 72 |
final double z = Math.sqrt(2.0) * ErrorFunction.inverseErf(2.0 * y - 1.0); |
| 73 |
final double crudeY = mean + stdev * (z + gamma * (z*z - 1) / 6); |
| 74 |
|
| 75 |
UnivariateFunction f = new UnivariateFunction() { |
| 76 |
double tent = Double.NaN; |
| 77 |
public double evaluate(final double argument) { |
| 78 |
try { |
| 79 |
tent = Beta.regularizedBeta(p, r, argument+1); |
| 80 |
} catch (MathException e) { |
| 81 |
return Double.NaN; |
| 82 |
} |
| 83 |
double score = Math.abs(tent-prob); |
| 84 |
return score; |
| 85 |
} |
| 86 |
public int getNumArguments() { |
| 87 |
return 1; |
| 88 |
} |
| 89 |
public double getLowerBound() { |
| 90 |
return Math.min(crudeY - .2*crudeY, 0); |
| 91 |
} |
| 92 |
public double getUpperBound() { |
| 93 |
return crudeY + .2*crudeY; |
| 94 |
} |
| 95 |
}; |
| 96 |
UnivariateMinimum minimum = new UnivariateMinimum(); |
| 97 |
double q = minimum.findMinimum(f); |
| 98 |
return Math.ceil(q); |
| 99 |
} |
| 100 |
|
| 101 |
public double mean() { |
| 102 |
return mean; |
| 103 |
} |
| 104 |
|
| 105 |
public double variance() { |
| 106 |
return mean + (mean * mean * alpha); |
| 107 |
} |
| 108 |
|
| 109 |
public UnivariateFunction getProbabilityDensityFunction() { |
| 110 |
throw new RuntimeException(); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
public static double pdf(double x, double mean, double alpha) { |
| 115 |
if (x < 0) return 0; |
| 116 |
return Math.exp(logPdf(x, mean, alpha)); |
| 117 |
} |
| 118 |
|
| 119 |
public static double logPdf(double x, double mean, double alpha) { |
| 120 |
if (x < 0) return Double.NEGATIVE_INFINITY; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
double theta = 1.0 / alpha; |
| 125 |
|
| 126 |
double p = theta / (theta + mean); |
| 127 |
return Math.log(1 - p) * x + Math.log(p) * theta + GammaFunction.lnGamma(theta + x) - GammaFunction.lnGamma(theta) - GammaFunction.lnGamma(x+1); |
| 128 |
} |
| 129 |
|
| 130 |
public static double cdf(double x, double mean, double alpha) { |
| 131 |
double theta = 1.0 / alpha; |
| 132 |
double p = theta / (theta + mean); |
| 133 |
try { |
| 134 |
return Beta.regularizedBeta(p, theta, x+1); |
| 135 |
} catch (MathException e) { |
| 136 |
|
| 137 |
|
| 138 |
return Double.NaN; |
| 139 |
|
| 140 |
|
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
public static void main(String[] args) { |
| 146 |
System.out.println("Test negative binomial"); |
| 147 |
double mean = 5; |
| 148 |
double stdev = 5; |
| 149 |
|
| 150 |
double alpha = (stdev * stdev - mean) / (mean * mean); |
| 151 |
|
| 152 |
NegativeBinomialDistribution dist = new NegativeBinomialDistribution(5, alpha); |
| 153 |
System.out.println("Mean 5, sd 5, x 5, pdf 0.074487, logPdf -2.59713, median 4"); |
| 154 |
System.out.println("pdf = " + dist.pdf(5)); |
| 155 |
System.out.println("quantile(0.5) aka median = " + dist.quantile(0.5)); |
| 156 |
System.out.println("logPdf = " + dist.logPdf(5)); |
| 157 |
} |
| 158 |
|
| 159 |
} |