Debian Codegraph @

src/dr/evomodel/coalescent/ExponentialProductSufficientStatisticsLikelihood.java

raw symbols SCIP
1 /*
2 * ExponentialProductSufficientStatisticsLikelihood.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 java.util.List;
29
30 import dr.evolution.coalescent.IntervalType;
31 import dr.evolution.tree.Tree;
32 import dr.evomodel.coalescent.OldAbstractCoalescentLikelihood.CoalescentEventType;
33 import dr.evomodel.tree.TreeModel;
34 import dr.inference.model.Likelihood;
35
36 /**
37 * Calculates a product of exponential densities and exponential tail probabilities.
38 *
39 * @author Guy Baele
40 */
41
42 public class ExponentialProductSufficientStatisticsLikelihood extends OldAbstractCoalescentLikelihood {
43
44 //not used at the moment
45 public final static boolean FIXED_TREE = false;
46
47 private TreeModel treeModel;
48 private double[] posteriorMeans;
49
50 protected int fieldLength;
51 protected double[] coalescentIntervals;
52 protected double[] sufficientStatistics;
53
54 //make sure to use in combination with coalescentEventsStatistic
55 public ExponentialProductSufficientStatisticsLikelihood(TreeModel treeModel, double[] posteriorMeans) {
56 super("ExponentialProductSufficientStatisticsLikelihood");
57 this.treeModel = treeModel;
58 this.posteriorMeans = posteriorMeans;
59
60 tree = treeModel;
61 addModel((TreeModel) tree);
62
63 this.fieldLength = posteriorMeans.length;
64 wrapSetupIntervals();
65 coalescentIntervals = new double[fieldLength];
66 sufficientStatistics = new double[fieldLength];
67 }
68
69 protected void wrapSetupIntervals() {
70 setupIntervals();
71 }
72
73 /*protected void setTree(List<Tree> treeList) {
74 if (treeList.size() != 1) {
75 throw new RuntimeException("GMRFSkyrideLikelihood only implemented for one tree");
76 }
77 this.tree = treeList.get(0);
78 this.treesSet = null;
79 if (tree instanceof TreeModel) {
80 addModel((TreeModel) tree);
81 }
82 }*/
83
84 protected void setupSufficientStatistics() {
85 int index = 0;
86
87 double length = 0;
88 double weight = 0;
89 for (int i = 0; i < getIntervalCount(); i++) {
90 //System.err.println(getInterval(i));
91 length += getInterval(i);
92 weight += getInterval(i) * getLineageCount(i) * (getLineageCount(i) - 1);
93 if (getIntervalType(i) == CoalescentEventType.COALESCENT) {
94 coalescentIntervals[index] = length;
95 sufficientStatistics[index] = weight / 2.0;
96 index++;
97 length = 0;
98 weight = 0;
99 }
100 }
101 }
102
103 private void makeIntervalsKnown() {
104 if (!intervalsKnown) {
105 wrapSetupIntervals();
106 intervalsKnown = true;
107 }
108 }
109
110 public double calculateLogLikelihood() {
111 tree = treeModel;
112 makeIntervalsKnown();
113 setupSufficientStatistics();
114
115 // Matrix operations taken from block update sampler to calculate data likelihood and field prior
116
117 double currentLike = 0;
118 double[] currentGamma = posteriorMeans;
119
120 for (int i = 0; i < fieldLength; i++) {
121 //System.err.println(currentGamma[i] + " " + sufficientStatistics[i] + " " + (-currentGamma[i] - sufficientStatistics[i] * Math.exp(-currentGamma[i])));
122 currentLike += -currentGamma[i] - sufficientStatistics[i] * Math.exp(-currentGamma[i]);
123 }
124 //System.err.println("currentLike = " + currentLike + "\n");
125
126 return currentLike;
127 }
128
129 /**
130 * Overridden to always return false.
131 */
132 protected boolean getLikelihoodKnown() {
133 return false;
134 }
135
136 }