Debian Codegraph @

src/dr/evomodel/speciation/ModelAveragingSpeciationLikelihood.java

raw symbols SCIP
1 /*
2 * ModelAveragingSpeciationLikelihood.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.speciation;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.util.Units;
30 import dr.evomodelxml.speciation.SpeciationLikelihoodParser;
31 import dr.inference.model.*;
32
33 import java.util.List;
34
35 /**
36 * @author Alexei Drummond
37 */
38 public class ModelAveragingSpeciationLikelihood extends AbstractModelLikelihood implements Units {
39
40 // PUBLIC STUFF
41 /**
42 * @param trees the tree
43 * @param speciationModels the model of speciation
44 * @param id a unique identifier for this likelihood
45 */
46 public ModelAveragingSpeciationLikelihood(List<Tree> trees, List<MaskableSpeciationModel> speciationModels,
47 Variable<Integer> indexVariable, Variable<Double> maxIndexVariable, String id) {
48 this(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD, trees, speciationModels, indexVariable, maxIndexVariable);
49 setId(id);
50 }
51
52 public ModelAveragingSpeciationLikelihood(String name, List<Tree> trees, List<MaskableSpeciationModel> speciationModels,
53 Variable<Integer> indexVariable, Variable<Double> maxIndexVariable) {
54
55 super(name);
56
57 this.trees = trees;
58 this.speciationModels = speciationModels;
59
60 if (trees.size() != speciationModels.size()) {
61 throw new IllegalArgumentException("The number of trees and the number of speciation models should be equal.");
62 }
63
64 for (Tree tree : trees) {
65 if (tree instanceof Model) {
66 addModel((Model) tree);
67 }
68 }
69 for (SpeciationModel speciationModel : speciationModels) {
70 if (speciationModel != null) {
71 addModel(speciationModel);
72 }
73 }
74
75 if ( (indexVariable.getSize() + 1) != trees.size()) { // integer index parameter size = real size - 1
76 throw new IllegalArgumentException("Index parameter must be same size as the number of trees.");
77 }
78 this.indexVariable = indexVariable;
79 for (int i = 0; i < indexVariable.getSize(); i++) {
80 indexVariable.setValue(i, i+1); // if starts all 0, the top value (i+1) of index will be missing
81 }
82 indexVariable.addBounds(new Bounds.Staircase(indexVariable));
83 addVariable(indexVariable);
84
85 for (int i = 0; i < maxIndexVariable.getSize(); i++) {
86 maxIndexVariable.setValue(i, 0.0);
87 }
88 this.maxIndexVariable = maxIndexVariable;
89 addVariable(maxIndexVariable);
90 }
91
92 // **************************************************************
93 // ModelListener IMPLEMENTATION
94 // **************************************************************
95
96 protected final void handleModelChangedEvent(Model model, Object object, int index) {
97 likelihoodKnown = false;
98 }
99
100 // **************************************************************
101 // VariableListener IMPLEMENTATION
102 // **************************************************************
103
104 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
105 likelihoodKnown = false;
106 } // No parameters to respond to
107
108 // **************************************************************
109 // Model IMPLEMENTATION
110 // **************************************************************
111
112 /**
113 * Stores the precalculated state: likelihood
114 */
115 protected final void storeState() {
116 storedLikelihoodKnown = likelihoodKnown;
117 storedLogLikelihood = logLikelihood;
118 }
119
120 /**
121 * Restores the precalculated state: computed likelihood
122 */
123 protected final void restoreState() {
124 likelihoodKnown = storedLikelihoodKnown;
125 logLikelihood = storedLogLikelihood;
126 }
127
128 protected final void acceptState() {
129 } // nothing to do
130
131 // **************************************************************
132 // Likelihood IMPLEMENTATION
133 // **************************************************************
134
135 public final Model getModel() {
136 return this;
137 }
138
139 public final double getLogLikelihood() {
140 if (!likelihoodKnown) {
141 logLikelihood = calculateLogLikelihood();
142 likelihoodKnown = true;
143 }
144 return logLikelihood;
145 }
146
147 public final void makeDirty() {
148 likelihoodKnown = false;
149 }
150
151 /**
152 * Calculates the log likelihood of this set of coalescent intervals,
153 * given a demographic model.
154 *
155 * @return the log likelihood
156 */
157 private double calculateLogLikelihood() {
158
159 double logL = 0;
160
161 // Rule: index k cannot be appeared unless k-1 appeared
162 if (!isValidate(indexVariable.getValues())) {
163 // output("illegal index variable", indexVariable);
164 return Double.NEGATIVE_INFINITY;
165 }
166
167 for (int i = 0; i < trees.size(); i++) {
168 MaskableSpeciationModel model = speciationModels.get(i);
169 if (i > 0) {
170 SpeciationModel mask = speciationModels.get(indexVariable.getValue(i-1)); // integer index parameter size = real size - 1
171 if (model != mask) {
172 model.mask(mask);
173 } else {
174 model.unmask();
175 }
176 }
177 logL += model.calculateTreeLogLikelihood(trees.get(i));
178 }
179
180 Double maxI = (double) (int) getMaxIndex(indexVariable.getValues());
181 maxIndexVariable.setValue(0, maxI);
182
183 return logL;
184 }
185
186 private boolean isValidate(Integer[] pattern) {
187 // Rule: index k cannot be appeared unless k-1 appeared before it appears
188 int[] indexFreq = new int[pattern.length];
189 for (int i = 0; i < pattern.length; i++) {
190 if (pattern[i] > 0) // not validate 0
191 indexFreq[pattern[i] - 1] += 1; // integer index parameter size = real size - 1
192
193 if (i > 0 && (pattern[i] - pattern[i - 1] > 1)) {
194 for (int f = 0; f < i; f++) {
195 if (indexFreq[f] < 1) return false;
196 }
197 }
198 }
199
200 return true;
201 }
202
203 private int getMaxIndex(Integer[] pattern) {
204 int max = 0;
205
206 for (int p : pattern) {
207 if (p > max) {
208 max = p;
209 }
210 }
211
212 return max;
213 }
214
215 private void output(String message, Variable<Integer> indexVariable) {
216 System.out.print(message + ": ");
217 for (int i = 0; i < indexVariable.getSize(); i++) {
218 System.out.print(indexVariable.getValue(i) + "\t");
219 }
220 System.out.println();
221 }
222
223 // **************************************************************
224 // Loggable IMPLEMENTATION
225 // **************************************************************
226
227 /**
228 * @return the log columns.
229 */
230 public final dr.inference.loggers.LogColumn[] getColumns() {
231
232 String columnName = getId();
233 if (columnName == null) columnName = getModelName() + ".likelihood";
234
235 return new dr.inference.loggers.LogColumn[]{
236 new LikelihoodColumn(columnName)
237 };
238 }
239
240 private final class LikelihoodColumn extends dr.inference.loggers.NumberColumn {
241 public LikelihoodColumn(String label) {
242 super(label);
243 }
244
245 public double getDoubleValue() {
246 return getLogLikelihood();
247 }
248 }
249
250 // **************************************************************
251 // Units IMPLEMENTATION
252 // **************************************************************
253
254 /**
255 * Sets the units these coalescent intervals are
256 * measured in.
257 */
258 public final void setUnits(Type u) {
259 for (SpeciationModel speciationModel : speciationModels) {
260 speciationModel.setUnits(u);
261 }
262 }
263
264 /**
265 * Returns the units these coalescent intervals are
266 * measured in.
267 */
268 public final Type getUnits() {
269 return speciationModels.get(0).getUnits();
270 }
271
272 // ****************************************************************
273 // Private and protected stuff
274 // ****************************************************************
275
276 /**
277 * The speciation models.
278 */
279 List<MaskableSpeciationModel> speciationModels = null;
280
281 /**
282 * The trees.
283 */
284 List<Tree> trees = null;
285
286 Variable<Integer> indexVariable = null; // integer index parameter size = real size - 1
287 Variable<Double> maxIndexVariable = null;
288
289 private double logLikelihood;
290 private double storedLogLikelihood;
291 private boolean likelihoodKnown = false;
292 private boolean storedLikelihoodKnown = false;
293 }