Debian Codegraph @

src/dr/math/distributions/NegativeBinomialDistribution.java

raw symbols SCIP
1 /*
2 * NegativeBinomialDistribution.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Trevor Bedford
36 * @version $Id$
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 // TB - I'm having trouble implementing this
62 // LM - A first stab using simple minimisation to invert the function (under absolute loss)
63 // Implementation based on the qnbinom.c function used in R
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() { // 20% window should cut it. Probably too large even...
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 // double r = -1 * (mean*mean) / (mean - stdev*stdev);
122 // double p = mean / (stdev*stdev);
123 // return Math.log(Math.pow(1-p,x)) + Math.log(Math.pow(p, r)) + GammaFunction.lnGamma(r+x) - GammaFunction.lnGamma(r) - GammaFunction.lnGamma(x+1);
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 // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime
137 // exceptions is bad. Better to return NaN and let the calling code deal with it.
138 return Double.NaN;
139 // throw MathRuntimeException.createIllegalArgumentException(
140 // "Couldn't calculate beta cdf for alpha = " + alpha + ", beta = " + beta + ": " +e.getMessage());
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 // double r = -1 * (mean*mean) / (mean - stdev*stdev);
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 }