Debian Codegraph @

src/dr/math/Poisson.java

raw symbols SCIP
1 /*
2 * Poisson.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;
27
28
29 /**
30 * @author Alexei Drummond
31 *
32 * @version $Id: Poisson.java,v 1.4 2005/04/28 16:51:44 rambaut Exp $
33 */
34 public class Poisson {
35
36 private Poisson() {
37 }
38
39 // oldm is a flag for whether xm has been changed since last call.
40 private static double oldm = -1.0;
41 private static double sq, alxm, g;
42
43 // used by gammaln
44 private static final double[] cof = { 76.18009172947146, -86.50532032941677,
45 24.01409824083091, -1.231739572450155,
46 0.1208650973866179e-2, -0.5395239384953e-5};
47
48 /**
49 * Returns an integer value that is a
50 * random deviate drawn from a Poisson distribution of mean xm.
51 */
52 public static int nextPoisson(double xm) {
53
54 double em; // expected mean
55 double t, y;
56
57 if (xm < 12.0) { // use direct method
58 if (xm != oldm) {
59 oldm = xm;
60 g = Math.exp(-xm); // if xm is new compute the exponential
61 }
62 em = -1.0;
63 t = 1.0;
64 do {
65 ++em;
66 t *= MathUtils.nextDouble();
67 } while (t > g);
68 } else {
69 if (xm != oldm) {
70 oldm = xm;
71 sq = Math.sqrt(2.0 * xm);
72 alxm = Math.log(xm);
73 g = xm * alxm - gammln(xm + 1.0);
74 // The function gammln is the natural log of the gamma function
75 }
76 do {
77 do {
78 y = Math.tan(Math.PI * MathUtils.nextDouble());
79 em = sq * y + xm; // em is y shifted and scaled
80 } while (em < 0.0); // reject if in realm of zero probability
81 em = Math.floor(em);
82 t = 0.9 *(1.0 + y*y) * Math.exp(em*alxm-gammln(em + 1.0)-g);
83 // The ratio of the desired distribution to the comparison function;
84 // we accept or reject by comparing it to another uniform deviate.
85 // The factor 0.9 is chosen so that t never exceeds 1.
86 } while (MathUtils.nextDouble() > t);
87 }
88 return (int)em;
89 }
90
91 /**
92 * Returns the value of log gamma(xx) for xx > 0.
93 */
94 public static double gammln(double xx) {
95 double x, y, tmp, ser;
96
97 int j;
98
99 y = x = xx;
100 tmp = x + 5.5;
101 tmp -= (x + 0.5) * Math.log(tmp);
102 ser = 1.000000000190015;
103 for (j = 0; j <= 5; j++) {
104 ser += cof[j] / ++y;
105 }
106 return -tmp + Math.log(2.5066282746310005 * ser / x);
107 }
108 }