Debian Codegraph @

src/dr/evomodel/arg/coalescent/ARGUniformPrior.java

raw symbols SCIP
1 /*
2 * ARGUniformPrior.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.coalescent;
27
28
29 import dr.evomodel.arg.ARGModel;
30 import dr.xml.*;
31 import org.apache.commons.math.util.MathUtils;
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.logging.Logger;
36
37 public class ARGUniformPrior extends ARGCoalescentLikelihood {
38
39 public static final String ARG_UNIFORM_PRIOR = "argUniformPrior";
40 public static final String INITIAL_CALCULATIONS = "initialCalculations";
41 public static final int INITIAL_DEFAULT = 5;
42
43
44 /**
45 * First index represents <code>(number of taxa - 3)</code>. Second index
46 * represents <code>(number of reassortments)</code>.
47 */
48 public static final double[][] logARGCoalescentCount = {
49 //3 external taxa
50 {1.0986122886681098, 4.276666119016055, 8.265650165580329,
51 12.882968485504067, 18.022948777876554, 23.61061585799083,
52 29.589035299467483, 35.9136593725818, 42.54885778516801,
53 49.465568765313016, 56.63967062533004, 64.05083469593846,
54 71.6817017141028, 79.51727811078219, 87.54448452157624},
55 //4 external taxa
56 {2.8903717578961645, 7.049254841255837, 11.72325121817378,
57 16.888557143580204, 22.489284935076803, 28.475109395164285,
58 34.804239049061785, 41.44233437710982, 48.360992220084476,
59 55.53645069653148, 62.948589014628716, 70.58017373916618,
60 78.41629051730933, 86.44391155847957, 94.65156231759461},
61 //5 external taxa
62 {5.192956850890211, 10.127430784020902, 15.386536070471918,
63 21.031563309246433, 27.041414773420907, 33.38480151281157,
64 40.03192406503731, 46.95658863790172, 54.13620464676437,
65 61.55131458837549, 69.18508096220405, 77.02283620054189,
66 85.0517120025277, 93.26034012092553, 101.63861177667185},
67 //6 external taxa
68 {7.90100705199242, 13.480736877978641, 19.249258325378765,
69 25.32054548939157, 31.698755104859107, 38.36733349428991,
70 45.30602732354885, 52.49523955146093, 59.917151710215464,
71 67.55588200276209, 75.39734855785784, 83.42905687418532,
72 91.63988547749693, 100.01989325102055, 108.56015364817871},
73 //7 external taxa
74 {10.945529489715843, 17.07892753271249, 23.29838718386135,
75 29.753407176990198, 36.467041602426036, 43.43431706668038,
76 50.64269194407989, 58.077999881992824, 65.7264083635128,
77 73.57505740705963, 81.61221669682189, 89.8272615722345,
78 98.21058447998666, 106.75348902699808, 115.44808566836372},
79 //8 external taxa
80 {14.277733999891046, 20.896472983408266, 27.5204956568849,
81 34.32463786712059, 41.34646380715327, 48.590254195514014,
82 56.04978852563933, 63.71543385947847, 71.57673212169905,
83 79.62341044573522, 87.84577046007783, 96.23481579273648,
84 104.78226604090887, 113.48052233027693, 122.32261419599219},
85 //9 external taxa
86 {17.861252938347157, 24.912242385415205, 31.903176098451674,
87 39.02769936297709, 46.334764858303096, 53.836163974100344,
88 61.53092519032464, 69.4132445398445, 77.47555198269822,
89 85.70981250210274, 94.10810078400579, 102.66285174696894,
90 111.36695890554311, 120.21379947792728, 129.1972243144967},
91 //10 external taxa
92 {21.667915428117478, 29.108649135506738, 36.43531279311537,
93 43.855945609774885, 51.42860726365353, 59.171269494486445,
94 67.08736948530374, 75.17436983012065, 83.4271948608094,
95 91.83976176721744, 100.40570480277214, 109.11872661806457,
96 117.97276669219758, 126.96207643311605, 136.08124559689557},
97 //11 external taxa
98 {25.67524861334995, 33.470895149684544, 41.10703544874183,
99 48.802991754828916, 56.62422391351782, 64.59385162827832,
100 72.71905241455046, 81.00010003398279, 89.43409011267886,
101 98.0166551565224, 106.74281045596386, 115.60738869034444,
102 124.6052669677749, 133.73148419256228, 142.98129861277982},
103 //12 external taxa
104 {29.864903355376374, 37.986383730127116, 45.90958634624165,
105 53.86285360119818, 61.91774245806648, 70.10170706136994,
106 78.42512661349667, 86.890712820353, 95.49746436335495,
107 104.24253036074631, 113.12215615381358, 122.13218720871606,
108 131.26834550980237, 140.52638287915215, 149.90216524590267},
109 //13 external taxa
110 {34.22161218206597, 42.644274889635966, 50.83517511899286,
111 59.02998615127507, 67.30534956176908, 75.69240403974487,
112 84.20429128794189, 92.84585184006139, 101.61776251108103,
113 110.51852076251443, 119.54547309663305, 128.69537701990959,
114 137.96471924891867, 147.34989978600277, 156.84733922685047},
115 //14 external taxa
116 {38.732471688582805, 47.43514810013058, 55.87684474986188,
117 64.2992772236583, 72.78337269607047, 81.36342882144557,
118 90.05498796388106, 98.86476107289788, 107.79491286633757,
119 116.84514548111035, 126.01379567653075, 135.29844575074105,
120 144.69627736969085, 154.20428208422834, 163.8193886161766},
121 //15 external taxa
122 {43.38643203874033, 52.35074398686485, 61.028354989069626,
123 69.6660218145462, 78.34831802900398, 87.11227051188368,
124 95.97552137406072, 104.94643415554035, 114.02849935687857,
125 123.22250024459373, 132.52766774855348, 141.9423328912873,
126 151.46431112672838, 161.0911356940919, 170.82020228928107}
127 };
128
129 private ArrayList<Double> argNumber;
130
131 public ARGUniformPrior(ARGModel arg, int max, int initial) {
132 super(ARG_UNIFORM_PRIOR, arg, max);
133
134 addModel(arg);
135
136 argNumber = new ArrayList<Double>(15);
137
138 if (arg.getExternalNodeCount() - 3 < logARGCoalescentCount.length) {
139 Logger.getLogger("dr.evomodel").info("Creating ARGUniformPrior using stored arg counts");
140 for (int i = 0, n = arg.getExternalNodeCount() - 3; i < logARGCoalescentCount[n].length; i++)
141 argNumber.add(logARGCoalescentCount[n][i]);
142 } else {
143 Logger.getLogger("dr.evomodel").info("Creating ARGUniformPrior by calculating arg counts");
144 for (int i = 0, n = arg.getExternalNodeCount(); i < initial; i++) {
145 argNumber.add(logNumberARGS(n, i));
146 }
147 }
148 }
149
150 public double getLogARGNumber(int i) {
151 if (i >= argNumber.size()) {
152 argNumber.add(logNumberARGS(arg.getExternalNodeCount(), i));
153 }
154 return argNumber.get(i);
155 }
156
157 public double getLogLikelihood() {
158 if (likelihoodKnown) {
159 return logLikelihood;
160 }
161
162 likelihoodKnown = true;
163 logLikelihood = calculateLogLikelihood();
164
165
166 if (arg.getReassortmentNodeCount() > maxReassortments)
167 logLikelihood = Double.NEGATIVE_INFINITY;
168 else
169 logLikelihood = calculateLogLikelihood();
170
171 if (!currentARGValid(true)) {
172 logLikelihood = Double.NEGATIVE_INFINITY;
173 }
174
175 return logLikelihood;
176 }
177
178 public double calculateLogLikelihood() {
179
180
181 double treeHeight = arg.getNodeHeight(arg.getRoot());
182 int internalNodes = arg.getInternalNodeCount() - 1;
183
184
185 double logLike = logFactorial(internalNodes) - (double) internalNodes * Math.log(treeHeight)
186 - getLogARGNumber(arg.getReassortmentNodeCount());
187
188 assert !Double.isInfinite(logLike) && !Double.isNaN(logLike);
189
190
191 return logLike;
192 }
193
194 private double logFactorial(int n) {
195 double rValue = 0;
196
197 for (int i = n; i > 0; i--) {
198 rValue += Math.log(i);
199 }
200 return rValue;
201 }
202
203
204 private int numberARGS(int taxa, int argNumber) {
205 int x = taxa;
206 int n = 2 * argNumber + taxa - 1;
207
208 return shurikoRecursion(x, n);
209 }
210
211 private int shurikoRecursion(int x, int n) {
212 int a = 0;
213 if (x == 0) {
214 a = 0;
215 } else if (x == 1) {
216 if (n == 0) {
217 a = 1;
218 } else {
219 a = 0;
220 }
221 } else if (n == 0) {
222 if (x == 1) {
223 a = 1;
224 } else {
225 a = 0;
226 }
227 } else if (x == n + 1) {
228 a = x * (x - 1) / 2 * shurikoRecursion(x - 1, n - 1);
229 } else {
230 a = x * shurikoRecursion(x + 1, n - 1) + x * (x - 1) / 2 * shurikoRecursion(x - 1, n - 1);
231 }
232 return a;
233 }
234
235 public static double logNumberARGS(int start, int reassortments) {
236 Logger.getLogger("dr.evomodel").warning("Calculating ARG count for " + reassortments + " reassortments. This may take awhile");
237
238 if (reassortments == 0) {
239 double a = 0;
240 for (int i = start; i > 2; i--) {
241 a += Math.log(i * (i - 1) / 2.0);
242 }
243 return a;
244 }
245
246 int[] max = new int[start - 3 + reassortments * 2];
247 int[] x = new int[max.length];
248
249 int i = 0;
250 while (i < reassortments) {
251 x[i] = max[i] = 1;
252 i++;
253 }
254 while (i < max.length) {
255 x[i] = max[i] = -1;
256 i++;
257 }
258 double before = 100;
259
260 double approx = 0;
261 while (x[0] != -9 && !stopCombination(x, start)) {
262 if (testCombination(x, start)) {
263 before = approx;
264 int[] y = generateValues(x, start);
265 approx += reduceThenDivide(y, generateValues(max, start));
266 before = approx - before;
267 }
268 nextCombination(x);
269 }
270
271 approx = Math.log(approx);
272
273 int[] y = new int[max.length + 2];
274 for (i = 0; i < max.length; i++)
275 y[i] = max[i];
276 y[y.length - 2] = y[y.length - 1] = -1;
277
278 max = generateValues(y, start);
279
280 for (int k = 0; k < y.length; k++)
281 approx += Math.log(max[k]);
282
283 return approx;
284
285 }
286
287 private static double reduceThenDivide(int[] top, int[] bottom) {
288
289 if (false) {
290 for (int i = 0; i < top.length; i++) {
291 for (int j = 0; j < bottom.length; j++) {
292 int gcd = MathUtils.gcd(top[i], bottom[j]);
293
294 if (gcd > 1) {
295 top[i] = top[i] / gcd;
296 bottom[j] = bottom[j] / gcd;
297 }
298 }
299
300 }
301
302 }
303
304 Arrays.sort(top);
305 Arrays.sort(bottom);
306
307
308 double a = 1;
309 for (int i = 0; i < top.length; i++)
310 a *= (double) top[i] / bottom[i];
311 return a;
312
313 }
314
315 private static int[] generateValues(int[] x, int start) {
316 int[] y = new int[x.length];
317
318 for (int i = 0; i < x.length; i++) {
319 if (x[i] == 1)
320 y[i] = start;
321 else
322 y[i] = start * (start - 1) / 2;
323
324 start += x[i];
325
326 }
327 return y;
328 }
329
330 private static boolean testCombination(int[] x, int start) {
331
332 for (int i = 0; i < x.length; i++) {
333 start += x[i];
334 if (start == 1)
335 return false;
336 }
337 return true;
338
339 }
340
341 private static boolean stopCombination(int[] x, int start) {
342 for (int i = 0; i < x.length; i++) {
343 if (x[i] == -1) {
344 start--;
345 if (start == 1) {
346 return true;
347 }
348 } else {
349 break;
350 }
351 }
352 return false;
353 }
354
355 private static void nextCombination(int[] x) {
356 if (x[x.length - 1] == -1) {
357 int i = x.length - 1;
358
359 while (i > -1) {
360 if (x[i] == 1) {
361 x[i] = -1;
362 x[i + 1] = 1;
363 return;
364 } else
365 i--;
366
367 }
368 } else {
369 int endOnes = 0;
370 int i = x.length - 1;
371 while (x[i] == 1) {
372 endOnes++;
373 i--;
374 }
375 int nextOne = -1;
376 while (i > -1) {
377 if (x[i] == 1) {
378 nextOne = i;
379 break;
380 } else
381 i--;
382
383 }
384 if (nextOne == -1) {
385 x[0] = -9;
386 return;
387 }
388
389 x[nextOne] = -1;
390 x[nextOne + 1] = 1;
391
392 for (i = 0; i < endOnes; i++)
393 x[i + nextOne + 2] = 1;
394
395 i = nextOne + 2 + endOnes;
396
397 while (i < x.length) {
398 x[i] = -1;
399 i++;
400 }
401 }
402 }
403
404
405 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
406
407 public String getParserDescription() {
408 return "A uniform prior for an ARG model";
409 }
410
411 public Class getReturnType() {
412 return ARGUniformPrior.class;
413 }
414
415 public String getParserName() {
416 return ARG_UNIFORM_PRIOR;
417 }
418
419 public XMLSyntaxRule[] getSyntaxRules() {
420 return rules;
421 }
422
423 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
424 new ElementRule(ARGModel.class),
425
426 };
427
428 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
429 ARGModel argModel = (ARGModel) xo.getChild(ARGModel.class);
430
431 int max = Integer.MAX_VALUE;
432 if (xo.hasAttribute(MAX_REASSORTMENTS)) {
433 max = xo.getIntegerAttribute(MAX_REASSORTMENTS);
434 }
435
436 int initial = INITIAL_DEFAULT;
437 if (xo.hasAttribute(INITIAL_CALCULATIONS)) {
438 initial = xo.getIntegerAttribute(INITIAL_CALCULATIONS);
439 }
440
441 return new ARGUniformPrior(argModel, max, initial);
442 }
443
444
445 };
446
447
448 }