Debian Codegraph @

src/dr/math/distributions/NormalDistribution.java

raw symbols SCIP
1 /*
2 * NormalDistribution.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.ErrorFunction;
29 import dr.math.MathUtils;
30 import dr.math.UnivariateFunction;
31
32 /**
33 * normal distribution (pdf, cdf, quantile)
34 *
35 * @author Korbinian Strimmer
36 * @version $Id: NormalDistribution.java,v 1.7 2005/05/24 20:26:01 rambaut Exp $
37 */
38 public class NormalDistribution implements Distribution, RandomGenerator {
39 //
40 // Public stuff
41 //
42
43 /**
44 * Constructor
45 */
46 public NormalDistribution(double mean, double sd) {
47 this.m = mean;
48 this.sd = sd;
49 }
50
51 public double getMean() {
52 return m;
53 }
54
55 public void setMean(double value) {
56 m = value;
57 }
58
59 public double getSD() {
60 return sd;
61 }
62
63 public void setSD(double value) {
64 sd = value;
65 }
66
67 public double pdf(double x) {
68 return pdf(x, m, sd);
69 }
70
71 public double logPdf(double x) {
72 return logPdf(x, m, sd);
73 }
74
75 public double cdf(double x) {
76 return cdf(x, m, sd);
77 }
78
79 public double quantile(double y) {
80 return quantile(y, m, sd);
81 }
82
83 public double mean() {
84 return mean(m, sd);
85 }
86
87 public double variance() {
88 return variance(m, sd);
89 }
90
91 public final UnivariateFunction getProbabilityDensityFunction() {
92 return pdfFunction;
93 }
94
95 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
96 public final double evaluate(double x) {
97 return pdf(x);
98 }
99
100 public final double getLowerBound() {
101 return Double.NEGATIVE_INFINITY;
102 }
103
104 public final double getUpperBound() {
105 return Double.POSITIVE_INFINITY;
106 }
107 };
108
109 /**
110 * probability density function
111 *
112 * @param x argument
113 * @param m mean
114 * @param sd standard deviation
115 * @return pdf at x
116 */
117 public static double pdf(double x, double m, double sd) {
118 double a = 1.0 / (Math.sqrt(2.0 * Math.PI) * sd);
119 double b = -(x - m) * (x - m) / (2.0 * sd * sd);
120
121 return a * Math.exp(b);
122 }
123
124 /**
125 * the natural log of the probability density function of the distribution
126 *
127 * @param x argument
128 * @param m mean
129 * @param sd standard deviation
130 * @return log pdf at x
131 */
132 public static double logPdf(double x, double m, double sd) {
133 double a = 1.0 / (Math.sqrt(2.0 * Math.PI) * sd);
134 double b = -(x - m) * (x - m) / (2.0 * sd * sd);
135
136 return Math.log(a) + b;
137 }
138
139 public static double gradLogPdf(double x, double m, double sd) {
140 return (m - x) / (sd * sd);
141 }
142
143 /**
144 * cumulative density function
145 *
146 * @param x argument
147 * @param m mean
148 * @param sd standard deviation
149 * @return cdf at x
150 */
151 public static double cdf(double x, double m, double sd) {
152 return cdf(x, m, sd, false);
153 // double a = (x - m) / (Math.sqrt(2.0) * sd);
154 //
155 // return 0.5 * (1.0 + ErrorFunction.erf(a));
156 }
157
158 /**
159 * quantiles (=inverse cumulative density function)
160 *
161 * @param z argument
162 * @param m mean
163 * @param sd standard deviation
164 * @return icdf at z
165 */
166 public static double quantile(double z, double m, double sd) {
167 return m + Math.sqrt(2.0) * sd * ErrorFunction.inverseErf(2.0 * z - 1.0);
168 }
169
170 /**
171 * mean
172 *
173 * @param m mean
174 * @param sd standard deviation
175 * @return mean
176 */
177 public static double mean(double m, double sd) {
178 return m;
179 }
180
181 /**
182 * variance
183 *
184 * @param m mean
185 * @param sd standard deviation
186 * @return variance
187 */
188 public static double variance(double m, double sd) {
189 return sd * sd;
190 }
191
192
193 /**
194 * A more accurate and faster implementation of the cdf (taken from function pnorm in the R statistical language)
195 * This implementation has discrepancies depending on the programming language and system architecture
196 * In Java, returned values become zero once z reaches -37.5193 exactly on the machine tested
197 * In the other implementation, the returned value 0 at about z = -8
198 * In C, this 0 value is reached approximately z = -37.51938
199 * <p/>
200 * Will later need to be optimised for BEAST
201 *
202 * @param x argument
203 * @param mu mean
204 * @param sigma standard deviation
205 * @param log_p is p logged
206 * @return cdf at x
207 */
208 public static double cdf(double x, double mu, double sigma, boolean log_p) {
209
210 if (Double.isNaN(x) || Double.isNaN(mu) || Double.isNaN(sigma)) {
211 return Double.NaN;
212 }
213 if (Double.isInfinite(x) && mu == x) { /* x-mu is NaN */
214 return Double.NaN;
215 }
216 if (sigma <= 0) {
217 if (sigma < 0) {
218 return Double.NaN;
219 }
220 return (x < mu) ? 0.0 : 1.0;
221 }
222 double p = (x - mu) / sigma;
223 if (Double.isInfinite(p)) {
224 return (x < mu) ? 0.0 : 1.0;
225 }
226 return standardCDF(p, log_p);
227 }
228
229 /**
230 * A more accurate and faster implementation of the cdf (taken from function pnorm in the R statistical language)
231 * This implementation has discrepancies depending on the programming language and system architecture
232 * In Java, returned values become zero once z reaches -37.5193 exactly on the machine tested
233 * In the other implementation, the returned value 0 at about z = -8
234 * In C, this 0 value is reached approximately z = -37.51938
235 * <p/>
236 * Will later need to be optimised for BEAST
237 *
238 * @param x argument
239 * @param log_p is p logged
240 * @return cdf at x
241 */
242 public static double standardCDF(double x, boolean log_p) {
243 boolean i_tail = false;
244 if (Double.isNaN(x)) {
245 return Double.NaN;
246 }
247
248 double xden, xnum, temp, del, eps, xsq, y;
249 int i;
250 double p = x, cp = Double.NaN;
251 boolean lower, upper;
252 eps = DBL_EPSILON * 0.5;
253 lower = !i_tail;
254 upper = i_tail;
255
256 y = Math.abs(x);
257 if (y <= 0.67448975) { /* Normal.quantile(3/4, 1, 0) = 0.67448975 */
258 if (y > eps) {
259 xsq = x * x;
260 xnum = a[4] * xsq;
261 xden = xsq;
262 for (i = 0; i < 3; i++) {
263 xnum = (xnum + a[i]) * xsq;
264 xden = (xden + b[i]) * xsq;
265 }
266 } else {
267 xnum = xden = 0.0;
268 }
269 temp = x * (xnum + a[3]) / (xden + b[3]);
270 if (lower) {
271 p = 0.5 + temp;
272 }
273 if (upper) {
274 cp = 0.5 - temp;
275 }
276 if (log_p) {
277 if (lower) {
278 p = Math.log(p);
279 }
280 if (upper) {
281 cp = Math.log(cp);
282 }
283 }
284 } else if (y <= M_SQRT_32) {
285 /* Evaluate pnorm for 0.67448975 = Normal.quantile(3/4, 1, 0) < |x| <= sqrt(32) ~= 5.657 */
286
287 xnum = c[8] * y;
288 xden = y;
289 for (i = 0; i < 7; i++) {
290 xnum = (xnum + c[i]) * y;
291 xden = (xden + d[i]) * y;
292 }
293 temp = (xnum + c[7]) / (xden + d[7]);
294
295 //do_del(y);
296 //swap_tail;
297 //#define do_del(X) \
298 xsq = ((int) (y * CUTOFF)) * 1.0 / CUTOFF;
299 del = (y - xsq) * (y + xsq);
300 if (log_p) {
301 p = (-xsq * xsq * 0.5) + (-del * 0.5) + Math.log(temp);
302 if ((lower && x > 0.0) || (upper && x <= 0.0)) {
303 cp = Math.log(1.0 - Math.exp(-xsq * xsq * 0.5) * Math.exp(-del * 0.5) * temp);
304 }
305 } else {
306 p = Math.exp(-xsq * xsq * 0.5) * Math.exp(-del * 0.5) * temp;
307 cp = 1.0 - p;
308 }
309 //#define swap_tail \
310 if (x > 0.0) {
311 temp = p;
312 if (lower) {
313 p = cp;
314 }
315 cp = temp;
316 }
317 }
318 /* else |x| > sqrt(32) = 5.657 :
319 * the next two case differentiations were really for lower=T, log=F
320 * Particularly *not* for log_p !
321 * Cody had (-37.5193 < x && x < 8.2924) ; R originally had y < 50
322 * Note that we do want symmetry(0), lower/upper -> hence use y
323 */
324 else if (log_p || (lower && -37.5193 < x && x < 8.2924)
325 || (upper && -8.2924 < x && x < 37.5193)) {
326
327 /* Evaluate pnorm for x in (-37.5, -5.657) union (5.657, 37.5) */
328 xsq = 1.0 / (x * x);
329 xnum = p_[5] * xsq;
330 xden = xsq;
331 for (i = 0; i < 4; i++) {
332 xnum = (xnum + p_[i]) * xsq;
333 xden = (xden + q[i]) * xsq;
334 }
335 temp = xsq * (xnum + p_[4]) / (xden + q[4]);
336 temp = (M_1_SQRT_2PI - temp) / y;
337
338 //do_del(x);
339 xsq = ((int) (x * CUTOFF)) * 1.0 / CUTOFF;
340 del = (x - xsq) * (x + xsq);
341 if (log_p) {
342 p = (-xsq * xsq * 0.5) + (-del * 0.5) + Math.log(temp);
343 if ((lower && x > 0.0) || (upper && x <= 0.0)) {
344 cp = Math.log(1.0 - Math.exp(-xsq * xsq * 0.5) * Math.exp(-del * 0.5) * temp);
345 }
346 } else {
347 p = Math.exp(-xsq * xsq * 0.5) * Math.exp(-del * 0.5) * temp;
348 cp = 1.0 - p;
349 }
350 //swap_tail;
351 if (x > 0.0) {
352 temp = p;
353 if (lower) {
354 p = cp;
355 }
356 cp = temp;
357 }
358 } else { /* no log_p , large x such that probs are 0 or 1 */
359 if (x > 0) {
360 p = 1.0;
361 cp = 0.0;
362 } else {
363 p = 0.0;
364 cp = 1.0;
365 }
366 }
367 return p;
368
369 }
370
371 // Private
372
373 protected double m, sd;
374
375 private static final double[] a = {
376 2.2352520354606839287,
377 161.02823106855587881,
378 1067.6894854603709582,
379 18154.981253343561249,
380 0.065682337918207449113
381 };
382 private static final double[] b = {
383 47.20258190468824187,
384 976.09855173777669322,
385 10260.932208618978205,
386 45507.789335026729956
387 };
388 private static final double[] c = {
389 0.39894151208813466764,
390 8.8831497943883759412,
391 93.506656132177855979,
392 597.27027639480026226,
393 2494.5375852903726711,
394 6848.1904505362823326,
395 11602.651437647350124,
396 9842.7148383839780218,
397 1.0765576773720192317e-8
398 };
399 private static final double[] d = {
400 22.266688044328115691,
401 235.38790178262499861,
402 1519.377599407554805,
403 6485.558298266760755,
404 18615.571640885098091,
405 34900.952721145977266,
406 38912.003286093271411,
407 19685.429676859990727
408 };
409 private static final double[] p_ = {
410 0.21589853405795699,
411 0.1274011611602473639,
412 0.022235277870649807,
413 0.001421619193227893466,
414 2.9112874951168792e-5,
415 0.02307344176494017303
416 };
417 private static final double[] q = {
418 1.28426009614491121,
419 0.468238212480865118,
420 0.0659881378689285515,
421 0.00378239633202758244,
422 7.29751555083966205e-5
423 };
424
425 private static final int CUTOFF = 16; /* Cutoff allowing exact "*" and "/" */
426
427 private static final double M_SQRT_32 = 5.656854249492380195206754896838; /* The square root of 32 */
428 private static final double M_1_SQRT_2PI = 0.398942280401432677939946059934;
429 private static final double DBL_EPSILON = 2.2204460492503131e-016;
430
431 public static double standardTail(double x, boolean isUpper) {
432 if (x < 0.0D) {
433 isUpper = !isUpper;
434 x = -x;
435 }
436 double d1;
437 if ((x <= 8.0D) || ((isUpper) && (x <= 37.0D))) {
438 double d2 = 0.5D * x * x;
439 if (x >= 1.28D) {
440 d1 = 0.398942280385D * Math.exp(-d2) / (x - 3.8052E-08D + 1.00000615302D / (x + 0.000398064794D + 1.98615381364D / (x - 0.151679116635D + 5.29330324926D / (x + 4.8385912808D - 15.150897245099999D / (x + 0.742380924027D + 30.789933034000001D / (x + 3.99019417011D))))));
441 } else {
442 d1 = 0.5D - x * (0.398942280444D - 0.399903438504D * d2 / (d2 + 5.75885480458D - 29.821355780800001D / (d2 + 2.62433121679D + 48.6959930692D / (d2 + 5.92885724438D))));
443 }
444 } else {
445 d1 = 0.0D;
446 }
447 if (!isUpper) {
448 d1 = 1.0D - d1;
449 }
450 return d1;
451 }
452
453 public static double tailCDF(double x, double mu, double sigma) {
454 return standardTail((x - mu) / sigma, true);
455 }
456
457 public static double tailCDF(double x, double mu, double sigma, boolean isUpper) {
458 return standardTail((x - mu) / sigma, isUpper);
459 }
460
461
462 public double tailCDF(double x) {
463 return standardTail((x - this.m) / this.sd, true);
464 }
465
466 static void testTail(double x, double mu, double sigma) {
467 double cdf1 = NormalDistribution.cdf(x, mu, sigma);
468 double tail1 = 1.0 - cdf1;
469 double cdf2 = NormalDistribution.cdf(x, mu, sigma, false);
470 double tail2 = 1.0 - cdf2;
471 double tail3 = NormalDistribution.tailCDF(x, mu, sigma);
472
473 System.out.println(">" + x + " N(" + mu + ", " + sigma + ")");
474 System.out.println("Original CDF: " + tail1);
475 System.out.println(" New CDF: " + tail2);
476 System.out.println(" tailCDF: " + tail3);
477 }
478
479 public static void main(String[] args) {
480 testTail(0.1, 0.0, 1.0);
481 System.out.println();
482 testTail(1, 0.0, 1.0);
483 System.out.println();
484 testTail(5, 0.0, 1.0);
485 System.out.println();
486 testTail(7, 0.0, 1.0);
487 System.out.println();
488 testTail(8, 0.0, 1.0);
489 System.out.println();
490 testTail(8.25, 0.0, 1.0);
491 System.out.println();
492 testTail(10, 0.0, 1.0);
493 System.out.println(NormalDistribution.standardCDF(2.0 / 0.5, true));
494 }
495
496 // RandomGenerator interface
497 public Object nextRandom() {
498 double eps = MathUtils.nextGaussian();
499 eps *= getSD();
500 eps += getMean();
501 return eps;
502 }
503
504 public double logPdf(Object x) {
505 double v = (Double) x;
506 return logPdf(x);
507 }
508 }