Debian Codegraph @

src/dr/oldevomodel/clock/RateEvolutionLikelihood.java

raw symbols SCIP
1 /*
2 * RateEvolutionLikelihood.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.oldevomodel.clock;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.branchratemodel.AbstractBranchRateModel;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodel.tree.TreeParameterModel;
33 import dr.inference.model.Model;
34 import dr.inference.model.Parameter;
35 import dr.inference.model.Variable;
36
37 import java.util.logging.Logger;
38
39 /**
40 * Abstract superclass of likelihoods of rate evolution through time.
41 *
42 * @author Alexei Drummond
43 * @author Andrew Rambaut
44 * @author Michael Defoin Platel
45 */
46 public abstract class RateEvolutionLikelihood extends AbstractBranchRateModel {
47
48 public static final String RATES = "rates";
49 public static final String EPISODIC = "episodic";
50 public static final String LOGSPACE = "logspace";
51
52 public static final String ROOTRATE = "rootRate";
53
54 public RateEvolutionLikelihood(String name, TreeModel treeModel, Parameter ratesParameter, Parameter rootRateParameter, boolean isEpisodic) {
55
56 super(name);
57
58 this.treeModel = treeModel;
59 addModel(treeModel);
60
61 this.ratesParameter = new TreeParameterModel(treeModel, ratesParameter, false);
62 Parameter.DefaultBounds bound = new Parameter.DefaultBounds(Double.MAX_VALUE, 0, ratesParameter.getDimension());
63 ratesParameter.addBounds(bound);
64
65 addModel(this.ratesParameter);
66
67 this.rootRateParameter = rootRateParameter;
68 rootRateParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0, 1));
69 addVariable(rootRateParameter);
70
71 if (rootRateParameter.getDimension() != 1) {
72 throw new IllegalArgumentException("The root rate parameter must be of dimension 1");
73 }
74
75 this.isEpisodic = isEpisodic;
76
77 Logger.getLogger("dr.evomodel").info("AutoCorrelated Relaxed Clock: " + name + (isEpisodic ? " (episodic)." : "."));
78
79 }
80
81 // **************************************************************
82 // Model IMPLEMENTATION
83 // **************************************************************
84
85 public final void handleModelChangedEvent(Model model, Object object, int index) {
86 likelihoodKnown = false;
87 if (model == ratesParameter) {
88 fireModelChanged(this, index);
89 }
90 }
91
92 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
93 likelihoodKnown = false;
94 }
95
96 protected void storeState() {
97 }
98
99 protected void restoreState() {
100 likelihoodKnown = false;
101 }
102
103 protected void acceptState() {
104 }
105
106 // **************************************************************
107 // Likelihood IMPLEMENTATION
108 // **************************************************************
109
110 /**
111 * Get the model.
112 *
113 * @return the model.
114 */
115 public Model getModel() {
116 return this;
117 }
118
119 public final double getLogLikelihood() {
120 if (!getLikelihoodKnown()) {
121 logLikelihood = calculateLogLikelihood();
122 likelihoodKnown = true;
123 }
124 return logLikelihood;
125 }
126
127 public void makeDirty() {
128 likelihoodKnown = false;
129 }
130
131 /**
132 * Called to decide if the likelihood must be calculated. Can be overridden
133 * (for example, to always return false).
134 *
135 * @return the likelihood.
136 */
137 protected boolean getLikelihoodKnown() {
138 return likelihoodKnown;
139 }
140
141 /**
142 * Get the log likelihood of the rate changes in this tree.
143 *
144 * @return the log likelihood.
145 */
146 private double calculateLogLikelihood() {
147 NodeRef root = treeModel.getRoot();
148 NodeRef node1 = treeModel.getChild(root, 0);
149 NodeRef node2 = treeModel.getChild(root, 1);
150
151 return calculateLogLikelihood(root, node1) + calculateLogLikelihood(root, node2);
152 }
153
154 /**
155 * Recursively calculate the log likelihood of the rate changes in the given tree.
156 *
157 * @return the partial log likelihood of the rate changes below the given node plus the
158 * branch directly above.
159 */
160 private double calculateLogLikelihood(NodeRef parent, NodeRef node) {
161
162 double logL, length;
163 length = treeModel.getBranchLength(node);
164
165 logL = branchRateChangeLogLikelihood(getBranchRate(treeModel, parent), getBranchRate(treeModel, node),
166 length);
167
168 //System.out.print(parent.getNumber() + " " + getBranchRate(treeModel, parent)+ " " + node.getNumber() + " " + getBranchRate(treeModel, node) + " " + treeModel.getBranchLength(node) + " " + logL + ", ");
169
170 for (int i = 0; i < treeModel.getChildCount(node); i++) {
171 logL += calculateLogLikelihood(node, treeModel.getChild(node, i));
172 }
173 return logL;
174 }
175
176 public String toString() {
177 return Double.toString(getLogLikelihood());
178 }
179
180 abstract double branchRateSample(double parentRate, double time);
181
182 public void sampleRate(NodeRef node) {
183
184 final NodeRef parent = treeModel.getParent(node);
185 final double length = treeModel.getBranchLength(node);
186 final double rate = branchRateSample(getBranchRate(treeModel, parent), length);
187
188 treeModel.setNodeRate(node, rate);
189
190 }
191
192 public double getBranchRate(Tree tree, NodeRef node) {
193
194 if (tree.isRoot(node)) return rootRateParameter.getParameterValue(0);
195 return ratesParameter.getNodeValue(tree, node);
196 }
197
198 public boolean isEpisodic() {
199 return isEpisodic;
200 }
201
202 /**
203 * @return the log likelihood of the rate change from the parent to the given node.
204 */
205 abstract double branchRateChangeLogLikelihood(double parentRate, double childRate, double time);
206
207 // **************************************************************
208 // Private members
209 // **************************************************************
210
211 private double logLikelihood;
212 private boolean likelihoodKnown = false;
213
214 private final TreeModel treeModel;
215 private final TreeParameterModel ratesParameter;
216 protected final Parameter rootRateParameter;
217 private final boolean isEpisodic;
218
219 }