Debian Codegraph @

src/dr/math/distributions/BetaDistribution.java

raw symbols SCIP
1 /*
2 * BetaDistribution.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
28 import dr.math.UnivariateFunction;
29 import org.apache.commons.math.MathException;
30 import org.apache.commons.math.distribution.AbstractContinuousDistribution;
31 import org.apache.commons.math.special.Beta;
32 import org.apache.commons.math.special.Gamma;
33
34 /**
35 * User: dkuh004
36 * Date: Mar 25, 2011
37 * Time: 11:32:25 AM
38 */
39 public class BetaDistribution extends AbstractContinuousDistribution implements Distribution {
40
41 // Default inverse cumulative probability accurac
42 public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
43
44 // first shape parameter
45 private double alpha;
46
47 // second shape parameter
48 private double beta;
49
50 // Normalizing factor used in density computations. updated whenever alpha or beta are changed.
51 private double z;
52
53 // Inverse cumulative probability accuracy
54 private final double solverAbsoluteAccuracy;
55
56
57 /**
58 * This general constructor creates a new beta distribution with a
59 * specified mean and scale
60 *
61 * @param alpha shape parameter
62 * @param beta shape parameter
63 */
64 public BetaDistribution(double alpha, double beta) {
65 this.alpha = alpha;
66 this.beta = beta;
67 z = Double.NaN;
68 solverAbsoluteAccuracy = DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
69 }
70
71
72 public double getAlpha() {
73 return alpha;
74 }
75
76 public double getBeta() {
77 return beta;
78 }
79
80 // Recompute the normalization factor.
81 private void recomputeZ() {
82 if (Double.isNaN(z)) {
83 z = Gamma.logGamma(alpha) + Gamma.logGamma(beta) - Gamma.logGamma(alpha + beta);
84 }
85 }
86
87 /**
88 * probability density function of the distribution
89 *
90 * @param x argument
91 * @return pdf value
92 */
93 public double pdf(double x) {
94 recomputeZ();
95 if (x < 0 || x > 1) {
96 return 0;
97 } else if (x == 0) {
98 if (alpha < 1) {
99 // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime
100 // exceptions is bad. Better to return NaN and let the calling code deal with it.
101 return Double.NaN;
102 // throw MathRuntimeException.createIllegalArgumentException(
103 // "Cannot compute beta density at 0 when alpha = {0,number}", alpha);
104 }
105 return 0;
106 } else if (x == 1) {
107 if (beta < 1) {
108 // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime
109 // exceptions is bad. Better to return NaN and let the calling code deal with it.
110 return Double.NaN;
111 // throw MathRuntimeException.createIllegalArgumentException(
112 // "Cannot compute beta density at 1 when beta = %.3g", beta);
113 }
114 return 0;
115 } else {
116 double logX = Math.log(x);
117 double log1mX = Math.log1p(-x);
118 return Math.exp((alpha - 1) * logX + (beta - 1) * log1mX - z);
119 }
120 }
121
122 /**
123 * the natural log of the probability density function of the distribution
124 *
125 * @param x argument
126 * @return log pdf value
127 */
128 public double logPdf(double x){
129 recomputeZ();
130 if (x < 0 || x > 1) {
131 return Double.NEGATIVE_INFINITY;
132 } else if (x == 0) {
133 if (alpha < 1) {
134 // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime
135 // exceptions is bad. Better to return NaN and let the calling code deal with it.
136 return Double.NaN;
137 // throw MathRuntimeException.createIllegalArgumentException(
138 // "Cannot compute beta density at 0 when alpha = {0,number}", alpha);
139 }
140 if (alpha == 1) {
141 return 0;
142 }
143 return Double.NEGATIVE_INFINITY;
144 } else if (x == 1) {
145 if (beta < 1) {
146 // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime
147 // exceptions is bad. Better to return NaN and let the calling code deal with it.
148 return Double.NaN;
149 // throw MathRuntimeException.createIllegalArgumentException(
150 // "Cannot compute beta density at 1 when beta = %.3g", beta);
151 }
152 if (beta == 1) {
153 return 0;
154 }
155 return Double.NEGATIVE_INFINITY;
156 } else {
157 double logX = Math.log(x);
158 double log1mX = Math.log1p(-x);
159 return (alpha - 1) * logX + (beta - 1) * log1mX - z;
160 }
161 }
162
163 /**
164 * quantile (inverse cumulative density function) of the distribution
165 *
166 * @param y argument
167 * @return icdf value
168 */
169 public double quantile(double y){
170 if (y == 0) {
171 return 0;
172 } else if (y == 1) {
173 return 1;
174 } else {
175 try{
176 return super.inverseCumulativeProbability(y);
177 } catch (MathException e) {
178 // throw MathRuntimeException.createIllegalArgumentException( // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime
179 // exceptions is bad. Better to return NaN and let the calling code deal with it.
180 return Double.NaN;
181
182 // "Couldn't calculate beta quantile for alpha = " + alpha + ", beta = " + beta + ": " +e.getMessage());
183 }
184 }
185 }
186
187 @Override
188 protected double getInitialDomain(double p) {
189 return p;
190 }
191
192 @Override
193 protected double getDomainLowerBound(double p) {
194 return 0;
195 }
196
197 @Override
198 protected double getDomainUpperBound(double p) {
199 return 1;
200 }
201
202 public double cdf(double x) {
203 if (x <= 0) {
204 return 0;
205 } else if (x >= 1) {
206 return 1;
207 } else {
208 try {
209 return Beta.regularizedBeta(x, alpha, beta);
210 } catch (MathException e) {
211 // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime
212 // exceptions is bad. Better to return NaN and let the calling code deal with it.
213 return Double.NaN;
214 // throw MathRuntimeException.createIllegalArgumentException(
215 // "Couldn't calculate beta cdf for alpha = " + alpha + ", beta = " + beta + ": " +e.getMessage());
216 }
217 }
218
219 }
220
221 public double cumulativeProbability(double x) throws MathException {
222 if (x <= 0) {
223 return 0;
224 } else if (x >= 1) {
225 return 1;
226 } else {
227 return Beta.regularizedBeta(x, alpha, beta);
228 }
229 }
230
231 @Override
232 public double cumulativeProbability(double x0, double x1) throws MathException {
233 return cumulativeProbability(x1) - cumulativeProbability(x0);
234 }
235
236 //Return the absolute accuracy setting of the solver used to estimate inverse cumulative probabilities.
237 protected double getSolverAbsoluteAccuracy() {
238 return solverAbsoluteAccuracy;
239 }
240
241
242 /**
243 * cumulative density function of the distribution
244 *
245 * @param x argument
246 * @return cdf value
247 */
248 // public double cdf(double x){
249 // throw new UnsupportedOperationException();
250 // }
251
252
253 /**
254 * mean of the distribution
255 *
256 * @return mean
257 */
258 public double mean(){
259 return (alpha / (alpha + beta));
260 }
261
262 /**
263 * variance of the distribution
264 *
265 * @return variance
266 */
267 public double variance(){
268 return (alpha * beta) / ((alpha + beta)* (alpha + beta) * ( alpha + beta + 1) );
269 }
270
271 /**
272 * @return a probability density function representing this distribution
273 */
274 public final UnivariateFunction getProbabilityDensityFunction() {
275 return pdfFunction;
276 }
277
278 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
279 public final double evaluate(double x) {
280 return pdf(x);
281 }
282
283 public final double getLowerBound() {
284 return 0.0;
285 }
286
287 public final double getUpperBound() {
288 return 1.0;
289 }
290 };
291
292
293 }
294
295