Debian Codegraph @

src/dr/evomodel/coalescent/ExponentialProductPosteriorMeansLikelihood.java

raw symbols SCIP
1 /*
2 * ExponentialProductPosteriorMeansLikelihood.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.coalescent;
27
28 import dr.evomodel.tree.TreeModel;
29 import dr.inference.model.Likelihood;
30
31 /**
32 * Calculates a product of exponential densities and exponential tail probabilities.
33 *
34 * @author Guy Baele
35 */
36
37 public class ExponentialProductPosteriorMeansLikelihood extends Likelihood.Abstract {
38
39 //not used at the moment
40 public final static boolean FIXED_TREE = false;
41 public static final boolean DEBUG = false;
42
43 private TreeModel treeModel;
44 private double[] posteriorMeans;
45
46 //make sure to use in combination with coalescentEventsStatistic
47 public ExponentialProductPosteriorMeansLikelihood(TreeModel treeModel, double[] posteriorMeans) {
48 super(treeModel);
49 this.treeModel = treeModel;
50 this.posteriorMeans = posteriorMeans;
51 }
52
53 public double calculateLogLikelihood() {
54
55 //System.err.println(treeModel);
56
57 double logPDF = 0.0;
58
59 //System.err.println("log(popSize) = " + this.popSize);
60
61 CoalescentTreeIntervalStatistic ctis = new CoalescentTreeIntervalStatistic(treeModel);
62 int coalescentIntervalCounter = 0;
63
64 if (DEBUG) {
65 System.err.println("ExponentialProductPosteriorMeansLikelihood dimension: " + ctis.getDimension());
66 }
67
68 for (int i = 0; i < ctis.getDimension(); i++) {
69
70 int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2;
71 double branchLength = ctis.getStatisticValue(i);
72
73 //System.err.println("combinations = " + combinations);
74 //System.err.println("branchLength = " + branchLength);
75
76 //System.err.println(ctis.getLineageCount(i));
77
78 //single-lineage intervals are not counted
79 if (ctis.getLineageCount(i) != 1) {
80
81 //System.err.println(i + " -> lineage count: " + ctis.getLineageCount(i));
82
83 if (i == ctis.getDimension()-1) {
84 //coalescent event at root: exponential density
85 //System.err.print("coalescent event at root: ");
86 double logContribution = -posteriorMeans[coalescentIntervalCounter] - combinations*branchLength*Math.exp(-posteriorMeans[coalescentIntervalCounter]);
87 if (DEBUG) {
88 System.err.println(i + ": " + logContribution);
89 }
90 logPDF += logContribution;
91 //System.err.println(logContribution);
92 } else if (ctis.getLineageCount(i) > ctis.getLineageCount(i+1)) {
93 //coalescent event: exponential density
94 //System.err.print("coalescent event (not at root): ");
95 double logContribution = -posteriorMeans[coalescentIntervalCounter] - combinations*branchLength*Math.exp(-posteriorMeans[coalescentIntervalCounter]);
96 if (DEBUG) {
97 System.err.println(i + ": " + logContribution);
98 }
99 logPDF += logContribution;
100 //System.err.println(logContribution);
101 coalescentIntervalCounter++;
102 } else {
103 //sampling event: exponential tail probability
104 //System.err.print("sampling event: ");
105 double logContribution = -combinations*branchLength*Math.exp(-posteriorMeans[coalescentIntervalCounter]);
106 if (DEBUG) {
107 System.err.println(i + ": " + logContribution);
108 }
109 logPDF += logContribution;
110 //System.err.println(logContribution);
111 }
112
113 }
114
115 }
116
117 //System.err.println("expoLike = " + logPDF + "\n");
118
119 return logPDF;
120
121 }
122
123 /**
124 * Overridden to always return false.
125 */
126 protected boolean getLikelihoodKnown() {
127 return false;
128 }
129
130 }