Debian Codegraph @

src/dr/math/EmpiricalBayesPoissonSmoother.java

raw symbols SCIP
1 /*
2 * EmpiricalBayesPoissonSmoother.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 import dr.math.distributions.GammaDistribution;
29 import dr.stats.DiscreteStatistics;
30
31 /**
32 * @author Marc A. Suchard
33 * @author Vladimir Minin
34 * @author Philippe Lemey
35 */
36
37 public class EmpiricalBayesPoissonSmoother {
38
39 /**
40 * Provides an empirical Bayes estimate of counts under a Poisson sampling density with a Gamma prior
41 * on the unknown intensity. The marginal distribution of the data is then a Negative-Binomial.
42 *
43 * @param in vector of Poisson counts
44 * @return smoothed count estimates
45 */
46 public static double[] smooth(double[] in) {
47 final int length = in.length;
48 double[] out = new double[length];
49 double[] gammaStats = getNegBin(in);
50 double alpha = gammaStats[0];
51 double beta = gammaStats[1]; // As defined on wiki page (scale)
52 double mean = gammaStats[2];
53
54 if (beta == 0) {
55 for (int i = 0; i < length; i++) {
56 out[i] = mean;
57 }
58 } else {
59 for (int i = 0; i < length; i++) {
60 out[i] = (in[i] + alpha) / (1 + 1 / beta);
61 }
62 }
63 return out;
64 }
65
66 public static double[] smoothWithSample(double[] in) {
67 final int length = in.length;
68 double[] out = new double[length];
69 double[] gammaStats = getNegBin(in);
70 double alpha = gammaStats[0];
71 double beta = gammaStats[1]; // As defined on wiki page (scale)
72 double mean = gammaStats[2];
73
74 if (beta == 0) {
75 for (int i = 0; i < length; i++) {
76 out[i] = mean;
77 }
78 } else {
79 for (int i = 0; i < length; i++) {
80 double shape = in[i] + alpha;
81 double scale = 1 / (1 + 1 / beta);
82 out[i] = GammaDistribution.nextGamma(shape, scale);
83 }
84 }
85 return out;
86 }
87
88 public static double[] smoothOld(double[] in) {
89 final int length = in.length;
90 double[] out = new double[length];
91 double[] gammaStats = getNegBin(in);
92 for (int i = 0; i < length; i++) {
93 out[i] = (in[i] + gammaStats[0]) / (1 + 1 / gammaStats[1]);
94 }
95 return out;
96 }
97
98 // Method of moments estimators following Martiz 1969
99
100 private static double[] getNegBin(double[] array) {
101 double mean = DiscreteStatistics.mean(array);
102 double variance = DiscreteStatistics.variance(array, mean);
103 double returnArray0 = (1 - (mean / variance));
104 double returnArray1 = (mean * ((1 - returnArray0) / returnArray0));
105
106 double shape = returnArray1;
107 double scale = (returnArray0 / (1 - returnArray0));
108
109 if (variance <= mean) {
110 shape = 0.0;
111 scale = 0.0;
112 }
113
114 // // Check against Martiz 1969 (beta = shape, alpha = rate in the 1969 paper)
115 // double matrizBeta = mean * mean / (variance - mean);
116 // double matrizAlphaInv = mean / matrizBeta; // scale
117 // System.err.println("mb = " + matrizBeta + " shape = " + shape);
118 // System.err.println("ma = " + matrizAlphaInv + " scale = " + scale);
119
120 return new double[]{shape, scale, mean};
121 }
122 }