Debian Codegraph @

src/dr/evomodel/continuous/TraitLikelihood.java

raw symbols SCIP
1 /*
2 * TraitLikelihood.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.continuous;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evomodel.tree.TreeModel;
30 import dr.inference.model.AbstractModelLikelihood;
31 import dr.inference.model.Model;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34 import dr.xml.*;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38 import java.util.ArrayList;
39
40 /**
41 * A class that returns the log likelihood of a set of data (statistics)
42 * being distributed according to the given parametric distribution.
43 *
44 * @author Alexei Drummond
45 * @version $Id: TraitLikelihood.java,v 1.7 2004/11/25 12:19:56 rambaut Exp $
46 */
47
48 public class TraitLikelihood extends AbstractModelLikelihood {
49
50 public static final String TRAIT_LIKELIHOOD = "traitLikelihood";
51 public static final String TRAIT_NAME = "traitName";
52 public static final String JEFFERYS_PRIOR = "jefferysPrior";
53 public static final String MODEL = "model";
54 public static final String TREE = "tree";
55
56 public TraitLikelihood(TreeModel treeModel, DiffusionModel diffusionModel, String traitName, boolean jeffreysPrior) {
57
58 super(TRAIT_LIKELIHOOD);
59
60 this.treeModel = treeModel;
61 this.diffusionModel = diffusionModel;
62 this.jeffreysPrior = jeffreysPrior;
63 addModel(treeModel);
64 addModel(diffusionModel);
65
66 this.traitName = traitName;
67 }
68
69 // **************************************************************
70 // ModelListener IMPLEMENTATION
71 // **************************************************************
72
73 protected void handleModelChangedEvent(Model model, Object object, int index) {
74 likelihoodKnown = false;
75 }
76
77 // **************************************************************
78 // VariableListener IMPLEMENTATION
79 // **************************************************************
80
81 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
82 }
83
84 // **************************************************************
85 // Model IMPLEMENTATION
86 // **************************************************************
87
88 /**
89 * Stores the precalculated state: in this case the intervals
90 */
91 protected void storeState() {
92 storedLikelihoodKnown = likelihoodKnown;
93 storedLogLikelihood = logLikelihood;
94 }
95
96 /**
97 * Restores the precalculated state: that is the intervals of the tree.
98 */
99 protected void restoreState() {
100 likelihoodKnown = storedLikelihoodKnown;
101 logLikelihood = storedLogLikelihood;
102 }
103
104 protected void acceptState() {
105 } // nothing to do
106
107 // **************************************************************
108 // Likelihood IMPLEMENTATION
109 // **************************************************************
110
111 public Model getModel() {
112 return this;
113 }
114
115 public final double getLogLikelihood() {
116 if (!likelihoodKnown) {
117 logLikelihood = calculateLogLikelihood();
118 likelihoodKnown = true;
119 }
120 return logLikelihood;
121 }
122
123 public void makeDirty() {
124 likelihoodKnown = false;
125 }
126
127 /**
128 * Calculate the log likelihood of the current state.
129 *
130 * @return the log likelihood.
131 */
132 public double calculateLogLikelihood() {
133
134 double logLikelihood = traitLogLikelihood(treeModel.getRoot());
135 if (logLikelihood > maxLogLikelihood) {
136 maxLogLikelihood = logLikelihood;
137 }
138 return logLikelihood;
139 }
140
141 public double getMaxLogLikelihood() {
142 return maxLogLikelihood;
143 }
144
145 private double traitLogLikelihood(NodeRef node) {
146
147 double logL = 0.0;
148 if (!treeModel.isRoot(node)) {
149 NodeRef parent = treeModel.getParent(node);
150 Double parentTrait = (Double) treeModel.getNodeAttribute(parent, traitName);
151 Double childTrait = (Double) treeModel.getNodeAttribute(node, traitName);
152
153 double time = treeModel.getNodeHeight(parent) - treeModel.getNodeHeight(node);
154 logL = diffusionModel.getLogLikelihood(parentTrait, childTrait, time);
155 }
156 int childCount = treeModel.getChildCount(node);
157 for (int i = 0; i < childCount; i++) {
158 logL += traitLogLikelihood(treeModel.getChild(node, i));
159 }
160
161 if (jeffreysPrior) {
162 logL += Math.log(1 / diffusionModel.getD());
163 }
164
165 return logL;
166 }
167
168 // **************************************************************
169 // XMLElement IMPLEMENTATION
170 // **************************************************************
171
172 public Element createElement(Document d) {
173 throw new RuntimeException("Not implemented yet!");
174 }
175
176 // **************************************************************
177 // XMLObjectParser
178 // **************************************************************
179
180 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
181
182 public String getParserName() {
183 return TRAIT_LIKELIHOOD;
184 }
185
186 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
187
188 DiffusionModel diffusionModel = (DiffusionModel) xo.getChild(DiffusionModel.class);
189 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
190
191 String traitName = xo.getStringAttribute(TRAIT_NAME);
192 boolean jeffreysPrior = xo.getBooleanAttribute(JEFFERYS_PRIOR);
193
194 return new TraitLikelihood(treeModel, diffusionModel, traitName, jeffreysPrior);
195 }
196
197 //************************************************************************
198 // AbstractXMLObjectParser implementation
199 //************************************************************************
200
201 public String getParserDescription() {
202 return "Provides the likelihood of a continuous trait evolving on a tree by a " +
203 "given diffusion model.";
204 }
205
206 public XMLSyntaxRule[] getSyntaxRules() {
207 return rules;
208 }
209
210 private final XMLSyntaxRule[] rules = {
211 new StringAttributeRule(TRAIT_NAME, "The name of the trait for which a likelihood should be calculated"),
212 AttributeRule.newBooleanRule(JEFFERYS_PRIOR),
213 new ElementRule(DiffusionModel.class),
214 new ElementRule(TreeModel.class)
215 };
216
217 public Class getReturnType() {
218 return TraitLikelihood.class;
219 }
220 };
221
222 TreeModel treeModel = null;
223 DiffusionModel diffusionModel = null;
224 String traitName = null;
225 private boolean jeffreysPrior = false;
226
227 ArrayList dataList = new ArrayList();
228
229 private double logLikelihood;
230 private double maxLogLikelihood = Double.NEGATIVE_INFINITY;
231 private double storedLogLikelihood;
232 private boolean likelihoodKnown = false;
233 private boolean storedLikelihoodKnown = false;
234
235 }
236