Debian Codegraph @

src/dr/evomodel/speciation/SpeciationLikelihood.java

raw symbols SCIP
1 /*
2 * SpeciationLikelihood.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.Taxon;
30 import dr.evolution.util.Units;
31 import dr.evomodelxml.speciation.SpeciationLikelihoodParser;
32 import dr.inference.model.AbstractModelLikelihood;
33 import dr.inference.model.Model;
34 import dr.inference.model.Parameter;
35 import dr.inference.model.Variable;
36
37 import java.util.Set;
38
39 /**
40 * A likelihood function for speciation processes. Takes a tree and a speciation model.
41 * <p/>
42 * Parts of this class were derived from C++ code provided by Oliver Pybus.
43 *
44 * @author Andrew Rambaut
45 * @author Alexei Drummond
46 * @version $Id: SpeciationLikelihood.java,v 1.10 2005/05/18 09:51:11 rambaut Exp $
47 */
48 public class SpeciationLikelihood extends AbstractModelLikelihood implements Units {
49
50 // PUBLIC STUFF
51 /**
52 * @param tree the tree
53 * @param speciationModel the model of speciation
54 * @param id a unique identifier for this likelihood
55 * @param exclude taxa to exclude from this model
56 */
57 public SpeciationLikelihood(Tree tree, SpeciationModel speciationModel, Set<Taxon> exclude, String id) {
58 this(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD, tree, speciationModel, exclude);
59 setId(id);
60 }
61
62 public SpeciationLikelihood(Tree tree, SpeciationModel speciationModel, String id) {
63 this(tree, speciationModel, null, id);
64 }
65
66 public SpeciationLikelihood(String name, Tree tree, SpeciationModel speciationModel, Set<Taxon> exclude) {
67
68 super(name);
69
70 this.tree = tree;
71 this.speciationModel = speciationModel;
72 this.exclude = exclude;
73
74 if (tree instanceof Model) {
75 addModel((Model) tree);
76 }
77 if (speciationModel != null) {
78 addModel(speciationModel);
79 }
80 }
81
82 public SpeciationLikelihood(Tree tree, SpeciationModel specModel, String id, CalibrationPoints calib) {
83 this(tree, specModel, id);
84 this.calibration = calib;
85 }
86
87 // **************************************************************
88 // ModelListener IMPLEMENTATION
89 // **************************************************************
90
91 protected final void handleModelChangedEvent(Model model, Object object, int index) {
92 likelihoodKnown = false;
93 }
94
95 // **************************************************************
96 // VariableListener IMPLEMENTATION
97 // **************************************************************
98
99 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
100 } // No parameters to respond to
101
102 // **************************************************************
103 // Model IMPLEMENTATION
104 // **************************************************************
105
106 /**
107 * Stores the precalculated state: likelihood
108 */
109 protected final void storeState() {
110 storedLikelihoodKnown = likelihoodKnown;
111 storedLogLikelihood = logLikelihood;
112 }
113
114 /**
115 * Restores the precalculated state: computed likelihood
116 */
117 protected final void restoreState() {
118 likelihoodKnown = storedLikelihoodKnown;
119 logLikelihood = storedLogLikelihood;
120 }
121
122 protected final void acceptState() {
123 } // nothing to do
124
125 // **************************************************************
126 // Likelihood IMPLEMENTATION
127 // **************************************************************
128
129 public final Model getModel() {
130 return this;
131 }
132
133 public final double getLogLikelihood() {
134 if (!likelihoodKnown) {
135 logLikelihood = calculateLogLikelihood();
136 likelihoodKnown = true;
137 }
138 return logLikelihood;
139 }
140
141 public final void makeDirty() {
142 likelihoodKnown = false;
143 }
144
145 /**
146 * Calculates the log likelihood of this set of coalescent intervals,
147 * given a demographic model.
148 *
149 * @return the log likelihood
150 */
151 private double calculateLogLikelihood() {
152 if (exclude != null) {
153 return speciationModel.calculateTreeLogLikelihood(tree, exclude);
154 }
155
156 if ( calibration != null ) {
157 return speciationModel.calculateTreeLogLikelihood(tree, calibration);
158 }
159
160 return speciationModel.calculateTreeLogLikelihood(tree);
161 }
162
163 // **************************************************************
164 // Loggable IMPLEMENTATION
165 // **************************************************************
166
167 /**
168 * @return the log columns.
169 */
170 public final dr.inference.loggers.LogColumn[] getColumns() {
171
172 String columnName = getId();
173 if (columnName == null) columnName = getModelName() + ".likelihood";
174
175 return new dr.inference.loggers.LogColumn[]{
176 new LikelihoodColumn(columnName)
177 };
178 }
179
180 private final class LikelihoodColumn extends dr.inference.loggers.NumberColumn {
181 public LikelihoodColumn(String label) {
182 super(label);
183 }
184
185 public double getDoubleValue() {
186 return getLogLikelihood();
187 }
188 }
189
190 // **************************************************************
191 // Units IMPLEMENTATION
192 // **************************************************************
193
194 /**
195 * Sets the units these coalescent intervals are
196 * measured in.
197 */
198 public final void setUnits(Type u) {
199 speciationModel.setUnits(u);
200 }
201
202 /**
203 * Returns the units these coalescent intervals are
204 * measured in.
205 */
206 public final Type getUnits() {
207 return speciationModel.getUnits();
208 }
209
210 @Override
211 public String prettyName() {
212 String s = speciationModel.getClass().getName();
213 String[] parts = s.split("\\.");
214 s = parts[parts.length - 1];
215 if( speciationModel.getId() != null ) {
216 s = s + '/' + speciationModel.getId();
217 }
218 s = s + '(' + tree.getId() + ')';
219 return s;
220 }
221 // ****************************************************************
222 // Private and protected stuff
223 // ****************************************************************
224
225 /**
226 * The speciation model.
227 */
228 SpeciationModel speciationModel = null;
229
230 /**
231 * The tree.
232 */
233 Tree tree = null;
234 private final Set<Taxon> exclude;
235
236 private CalibrationPoints calibration;
237
238 private double logLikelihood;
239 private double storedLogLikelihood;
240 private boolean likelihoodKnown = false;
241 private boolean storedLikelihoodKnown = false;
242 }