Debian Codegraph @

src/dr/evomodel/arg/PoissonPartitionLikelihood.java

raw symbols SCIP
1 /*
2 * PoissonPartitionLikelihood.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.evomodel.arg;
27
28
29 import dr.inference.model.Model;
30 import dr.inference.model.Variable;
31 import dr.inference.model.Variable.ChangeType;
32 import dr.inferencexml.distribution.PriorParsers;
33 import dr.math.MathUtils;
34 import dr.math.Poisson;
35 import dr.math.distributions.PoissonDistribution;
36 import dr.xml.*;
37 import jebl.math.Binomial;
38
39 public class PoissonPartitionLikelihood extends ARGPartitionLikelihood {
40
41 public static final String POISSON_PARTITION_LIKELIHOOD = "poissonPartitionLikelihood";
42
43 PoissonDistribution pd;
44 double mean;
45
46 public PoissonPartitionLikelihood(String id, ARGModel arg, double mean) {
47 super(id, arg);
48
49 pd = new PoissonDistribution(mean);
50 this.mean = mean;
51 }
52
53 public double getLogLikelihood(double[] partition) {
54 if ((getNumberOfPartitionsMinusOne() + 1) % 2 == 0) {
55 return getEvenLogLikelihood(partition);
56 }
57
58 return getOddLogLikelihood(partition);
59
60 }
61
62 private double getEvenLogLikelihood(double[] partition) {
63 int numberOfZeros = 0;
64 int numberOfOnes = 0;
65
66 for (double d : partition) {
67 assert d == 0.0 || d == 1.0;
68
69 if (d == 0.0)
70 numberOfZeros++;
71 else
72 numberOfOnes++;
73 }
74
75 double poissonValue = (double) Math.min(numberOfZeros, numberOfOnes);
76
77 double logLike = pd.logPdf(poissonValue);
78
79 if (poissonValue < partition.length / 2) {
80 return logLike - Math.log(Binomial.choose(partition.length, poissonValue));
81 } else {
82 return logLike - Math.log(Binomial.choose(partition.length - 1, poissonValue));
83 }
84 }
85
86 private double getOddLogLikelihood(double[] partition) {
87 int numberOfZeros = 0;
88 int numberOfOnes = 0;
89
90 for (double d : partition) {
91 assert d == 0.0 || d == 1.0;
92
93 if (d == 0.0)
94 numberOfZeros++;
95 else
96 numberOfOnes++;
97
98 }
99
100 double poissonValue = (double) Math.min(numberOfZeros, numberOfOnes);
101
102 return pd.logPdf(poissonValue) -
103 Math.log(Binomial.choose(partition.length, poissonValue));
104 }
105
106 public double[] generatePartition() {
107 int lengthDividedByTwo = (getNumberOfPartitionsMinusOne() + 1) / 2;
108
109 int value = 0;
110
111 while (value < 1 || value > lengthDividedByTwo) {
112 value = Poisson.nextPoisson(mean);
113 }
114
115 int[] x = new int[getNumberOfPartitionsMinusOne() + 1];
116
117 for (int i = 0; i < value; i++) {
118 x[i] = 1;
119 }
120
121 MathUtils.permute(x);
122
123 if (x[0] == 1) {
124 for (int i = 0; i < x.length; i++) {
125 if (x[i] == 1) {
126 x[i] = 0;
127 } else {
128 x[i] = 1;
129 }
130 }
131 }
132
133 double[] rValue = new double[x.length];
134
135 for (int i = 0; i < rValue.length; i++) {
136 rValue[i] = x[i];
137 }
138
139 return rValue;
140 }
141
142
143 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
144
145 public String getParserDescription() {
146 return null;
147 }
148
149 public Class getReturnType() {
150 return PoissonPartitionLikelihood.class;
151 }
152
153 public XMLSyntaxRule[] getSyntaxRules() {
154 return new XMLSyntaxRule[]{
155 AttributeRule.newDoubleRule(PriorParsers.MEAN, false),
156 new ElementRule(ARGModel.class, false),
157 };
158 }
159
160 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
161 String id = "";
162 if (xo.hasId())
163 id = xo.getId();
164
165 double mean = xo.getDoubleAttribute(PriorParsers.MEAN);
166 ARGModel arg = (ARGModel) xo.getChild(ARGModel.class);
167
168 if (arg.isRecombinationPartitionType()) {
169 throw new XMLParseException(ARGModel.TREE_MODEL + " must be of type " + ARGModel.REASSORTMENT_PARTITION);
170 }
171
172 return new PoissonPartitionLikelihood(id, arg, mean);
173 }
174
175 public String getParserName() {
176 return POISSON_PARTITION_LIKELIHOOD;
177 }
178
179 };
180
181 protected void acceptState() {
182 //nothing to do
183 }
184
185 @Override
186 protected void handleModelChangedEvent(Model model, Object object, int index) {
187 //has no submodels
188 }
189
190 @Override
191 protected void handleVariableChangedEvent(Variable variable, int index,
192 ChangeType type) {
193 //has no parameters
194 }
195
196 @Override
197 protected void restoreState() {
198 //nothing to restore
199 }
200
201 @Override
202 protected void storeState() {
203 // nothing to store
204 }
205
206
207 }