Debian Codegraph @

src/dr/evomodel/coalescent/structure/StructuredCoalescentLikelihood.java

raw symbols SCIP
1 /*
2 * StructuredCoalescentLikelihood.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.structure;
27
28 import dr.evolution.coalescent.structure.ColouredTreeIntervals;
29 import dr.evolution.coalescent.structure.StructuredCoalescent;
30 import dr.evolution.coalescent.structure.StructuredIntervalList;
31 import dr.evolution.colouring.ColourChangeMatrix;
32 import dr.evomodel.tree.TreeModel;
33 import dr.inference.model.*;
34 import dr.xml.*;
35
36 import java.util.logging.Logger;
37
38 /**
39 * A likelihood function for a structered coalescent model
40 *
41 * @author Andrew Rambaut
42 * @author Alexei Drummond
43 * @version $Id: StructuredCoalescentLikelihood.java,v 1.20 2006/09/11 09:33:01 gerton Exp $
44 */
45 @Deprecated
46 public class StructuredCoalescentLikelihood extends AbstractModelLikelihood {
47
48 // PUBLIC STUFF
49
50 public static final String STRUCTURED_COALESCENT_LIKELIHOOD = "structuredCoalescentLikelihood";
51 public static final String META_POPULATION_MODEL = "metaPopulationModel";
52 public static final String MIGRATION_MODEL = "migrationModel";
53 public static final String POPULATION = "population";
54
55 public StructuredCoalescentLikelihood(TreeModel treeModel, MetaPopulationModel metaPopulationModel, ColourSamplerModel colourSamplerModel, MigrationModel migrationModel) {
56 this(STRUCTURED_COALESCENT_LIKELIHOOD, treeModel, metaPopulationModel, colourSamplerModel, migrationModel);
57 }
58
59 public StructuredCoalescentLikelihood(String name, TreeModel treeModel, MetaPopulationModel metaPopulationModel, ColourSamplerModel colourSamplerModel, MigrationModel migrationModel) {
60
61 super(name);
62
63 this.treeModel = treeModel;
64 addModel(treeModel);
65
66 this.metaPopulationModel = metaPopulationModel;
67 addModel(metaPopulationModel);
68
69 this.colourSamplerModel = colourSamplerModel;
70 addModel(colourSamplerModel);
71
72 this.migrationModel = migrationModel;
73 addModel(migrationModel);
74
75 addStatistic(migrationWaitingTimesStatistic);
76 addStatistic(coalescentWaitingTimeStatistic);
77 }
78
79 // **************************************************************
80 // ModelListener IMPLEMENTATION
81 // **************************************************************
82
83 protected final void handleModelChangedEvent(Model model, Object object, int index) {
84 if (model == treeModel) {
85 // treeModel has changed so recalculate the intervals
86 } else {
87 // a demographicModel has changed so we don't need to recalculate the intervals
88 }
89
90 likelihoodKnown = false;
91 }
92
93 // **************************************************************
94 // VariableListener IMPLEMENTATION
95 // **************************************************************
96
97 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
98 likelihoodKnown = false;
99 }
100
101 // **************************************************************
102 // Model IMPLEMENTATION
103 // **************************************************************
104
105 /**
106 * Stores the precalculated state: in this case the intervals
107 */
108 protected final void storeState() {
109 storedLogLikelihood = logLikelihood;
110 }
111
112 /**
113 * Restores the precalculated state: that is the intervals of the tree.
114 */
115 protected final void restoreState() {
116 logLikelihood = storedLogLikelihood;
117 }
118
119 protected final void acceptState() {
120 } // nothing to do
121
122 // **************************************************************
123 // Likelihood IMPLEMENTATION
124 // **************************************************************
125
126 public final Model getModel() {
127 return this;
128 }
129
130 public final double getLogLikelihood() {
131 likelihoodKnown = false;
132
133 if (!likelihoodKnown) {
134 logLikelihood = calculateLogLikelihood();
135 likelihoodKnown = true;
136 }
137 return logLikelihood;
138 }
139
140 public final void makeDirty() {
141 likelihoodKnown = false;
142 }
143
144 /**
145 * Calculates the log likelihood of this set of coalescent intervals,
146 * given a demographic model.
147 */
148 public double calculateLogLikelihood() {
149
150 // make intervals
151 StructuredIntervalList list = new ColouredTreeIntervals(treeModel, colourSamplerModel.getTreeColouring());
152
153 ColourChangeMatrix mm = migrationModel.getMigrationMatrix();
154
155 StructuredCoalescent sc = new StructuredCoalescent();
156
157 double logL = sc.calculateLogLikelihood(colourSamplerModel.getTreeColouring(), list, mm, metaPopulationModel);
158
159 return logL;
160 }
161
162 // ****************************************************************
163 // Private and protected stuff
164 // ****************************************************************
165
166 private final Statistic migrationWaitingTimesStatistic = new Statistic.Abstract() {
167
168 public String getStatisticName() {
169 return "migrationWaitingTimes";
170 }
171
172 public int getDimension() {
173 return metaPopulationModel.getPopulationCount();
174 }
175
176 public double getStatisticValue(int dim) {
177 double[] migrationRates = migrationModel.getMigrationRates(0);
178 return 1.0 / migrationRates[dim];
179 }
180
181 };
182
183 private final Statistic coalescentWaitingTimeStatistic = new Statistic.Abstract() {
184
185 public String getStatisticName() {
186 return "coalescentWaitingTime";
187 }
188
189 public int getDimension() {
190 return 1;
191 }
192
193 public double getStatisticValue(int dim) {
194 double[] popSizes = metaPopulationModel.getPopulationSizes(0);
195 int[] counts = colourSamplerModel.getLeafColourCounts();
196
197 double sum = 0.0;
198 for (int i = 0; i < popSizes.length; i++) {
199 sum += ((double) counts[i]) / popSizes[i];
200 }
201
202 return 1.0 / sum;
203 }
204
205 };
206
207 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
208
209 public String getParserName() {
210 return STRUCTURED_COALESCENT_LIKELIHOOD;
211 }
212
213 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
214
215 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
216
217 ColourSamplerModel colourSamplerModel = (ColourSamplerModel) xo.getChild(ColourSamplerModel.class);
218
219 MigrationModel migrationModel = (MigrationModel) xo.getChild(MigrationModel.class);
220
221 MetaPopulationModel metaPopulationModel = (MetaPopulationModel) xo.getChild(MetaPopulationModel.class);
222
223 StructuredCoalescentLikelihood likelihood = new StructuredCoalescentLikelihood(treeModel, metaPopulationModel, colourSamplerModel, migrationModel);
224
225 Logger.getLogger("dr.evomodel").info("Creating structured coalescent tree prior.");
226
227 return likelihood;
228 }
229
230 //************************************************************************
231 // AbstractXMLObjectParser implementation
232 //************************************************************************
233
234 public String getParserDescription() {
235 return "This element represents a likelihood function for transmission.";
236 }
237
238 public Class getReturnType() {
239 return StructuredCoalescentLikelihood.class;
240 }
241
242 public XMLSyntaxRule[] getSyntaxRules() {
243 return rules;
244 }
245
246 private final XMLSyntaxRule[] rules = {
247 new ElementRule(TreeModel.class, "The tree."),
248 new ElementRule(ColourSamplerModel.class, "The colour sampler model."),
249 new ElementRule(MigrationModel.class, "The migration model."),
250 new ElementRule(MetaPopulationModel.class, "The meta-population model."),
251 };
252 };
253
254 private TreeModel treeModel = null;
255 private MetaPopulationModel metaPopulationModel = null;
256 private ColourSamplerModel colourSamplerModel = null;
257 private MigrationModel migrationModel = null;
258
259 private boolean likelihoodKnown = false;
260 private double logLikelihood;
261 private double storedLogLikelihood;
262 }