Debian Codegraph @

src/dr/math/distributions/PoissonDistribution.java

raw symbols SCIP
1 /*
2 * PoissonDistribution.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.Poisson;
29 import dr.math.UnivariateFunction;
30 import org.apache.commons.math.MathException;
31 import org.apache.commons.math.distribution.PoissonDistributionImpl;
32
33 /**
34 * @author Alexei Drummond
35 * @version $Id$
36 */
37 public class PoissonDistribution implements Distribution {
38
39 org.apache.commons.math.distribution.PoissonDistribution distribution;
40
41 public PoissonDistribution(double mean) {
42 distribution = new org.apache.commons.math.distribution.PoissonDistributionImpl(mean);
43 }
44
45 public double pdf(double x) {
46 return distribution.probability(x);
47 }
48
49 public double logPdf(double x) {
50
51 double pdf = distribution.probability(x);
52 if (pdf == 0 || Double.isNaN(pdf)) { // bad estimate
53 final double mean = mean();
54 return x * Math.log(mean) - Poisson.gammln(x + 1) - mean;
55 }
56 return Math.log(pdf);
57
58 }
59
60 public double cdf(double x) {
61 try {
62 return distribution.cumulativeProbability(x);
63 } catch (MathException e) {
64 throw new RuntimeException(e);
65 }
66 }
67
68 public double quantile(double y) {
69 try {
70 return distribution.inverseCumulativeProbability(y);
71 } catch (MathException e) {
72 throw new RuntimeException(e);
73 }
74 }
75
76 public double mean() {
77 return distribution.getMean();
78 }
79
80 public double variance() {
81 return distribution.getMean();
82 }
83
84 public UnivariateFunction getProbabilityDensityFunction() {
85 throw new RuntimeException();
86 }
87
88 public double truncatedMean(int max) {
89
90 double CDF = 0;
91 double mean = 0;
92 for(int i=0; i<=max; i++) {
93 double p = distribution.probability(i);
94 mean += i*p;
95 CDF += p;
96 }
97 return mean / CDF;
98 }
99
100 public static double pdf(double x, double mean) {
101 PoissonDistributionImpl dist = new PoissonDistributionImpl(mean);
102 return dist.probability(x);
103 }
104
105 public static double logPdf(double x, double mean) {
106 PoissonDistributionImpl dist = new PoissonDistributionImpl(mean);
107 double pdf = dist.probability(x);
108 if (pdf == 0 || Double.isNaN(pdf)) { // bad estimate
109 return x * Math.log(mean) - Poisson.gammln(x + 1) - mean;
110 }
111 return Math.log(pdf);
112
113 }
114
115 public static double cdf(double x, double mean) {
116 try {
117 PoissonDistributionImpl dist = new PoissonDistributionImpl(mean);
118 return dist.cumulativeProbability(x);
119 } catch (MathException e) {
120 throw new RuntimeException(e);
121 }
122 }
123
124 public static double quantile(double y, double mean) {
125 try {
126 PoissonDistributionImpl dist = new PoissonDistributionImpl(mean);
127 return dist.inverseCumulativeProbability(y);
128 } catch (MathException e) {
129 throw new RuntimeException(e);
130 }
131 }
132 }