Debian Codegraph @

src/dr/evomodel/speciation/RandomLocalYuleModel.java

raw symbols SCIP
1 /*
2 * RandomLocalYuleModel.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.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeTrait;
31 import dr.evolution.tree.TreeTraitProvider;
32 import dr.evomodel.tree.TreeModel;
33 import dr.evomodel.tree.randomlocalmodel.RandomLocalTreeVariable;
34 import dr.evomodelxml.speciation.RandomLocalYuleModelParser;
35 import dr.inference.model.Parameter;
36
37 import java.text.NumberFormat;
38 import java.util.Locale;
39 import java.util.logging.Logger;
40
41 /**
42 * This class contains methods that describe a Yule speciation model whose rate of birth changes
43 * at different points in the tree.
44 *
45 * @author Alexei Drummond
46 */
47 public class RandomLocalYuleModel extends UltrametricSpeciationModel implements TreeTraitProvider, RandomLocalTreeVariable {
48 private boolean calculateAllBirthRates = false;
49
50 public RandomLocalYuleModel(Parameter birthRates, Parameter indicators, Parameter meanRate,
51 boolean ratesAsMultipliers, Type units, int dp) {
52
53 super(RandomLocalYuleModelParser.YULE_MODEL, units);
54
55 addVariable(birthRates);
56 birthRates.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, birthRates.getDimension()));
57
58 for (int i = 0; i < indicators.getDimension(); i++) {
59 indicators.setParameterValueQuietly(i, 0.0);
60 }
61
62 addVariable(indicators);
63
64 this.meanRate = meanRate;
65 addVariable(meanRate);
66
67 birthRatesAreMultipliers = ratesAsMultipliers;
68
69 format.setMaximumFractionDigits(dp);
70
71 birthRatesName = birthRates.getParameterName();
72 Logger.getLogger("dr.evomodel").info(" birth rates parameter is named '" + birthRatesName + "'");
73 indicatorsName = indicators.getParameterName();
74 Logger.getLogger("dr.evomodel").info(" indicator parameter is named '" + indicatorsName + "'");
75
76 this.birthRates = new double[birthRates.getDimension() + 1];
77
78 treeTraits.addTrait(new TreeTrait.I() {
79 public String getTraitName() {
80 return "I";
81 }
82
83 public Intent getIntent() {
84 return Intent.NODE;
85 }
86
87 public Integer getTrait(Tree tree, NodeRef node) {
88 return (isVariableSelected(tree, node) ? 1 : 0);
89 }
90
91 });
92
93 treeTraits.addTrait(new TreeTrait.D() {
94 public String getTraitName() {
95 return "b";
96 }
97
98 public Intent getIntent() {
99 return Intent.NODE;
100 }
101
102 public Double getTrait(Tree tree, NodeRef node) {
103 return RandomLocalYuleModel.this.birthRates[node.getNumber()];
104 }
105
106 });
107 }
108
109 public final double getVariable(Tree tree, NodeRef node) {
110 return ((TreeModel)tree).getNodeTrait(node, birthRatesName);
111 }
112
113 public final boolean isVariableSelected(Tree tree, NodeRef node) {
114 return ((TreeModel)tree).getNodeTrait(node, indicatorsName) > 0.5;
115 }
116
117 //
118 // functions that define a speciation model
119 //
120 public final double logTreeProbability(int taxonCount) {
121
122 // calculate all nodes birth rates
123 calculateAllBirthRates = true;
124
125 return 0.0;
126
127 }
128
129 //
130 // functions that define a speciation model
131 //
132 public final double logNodeProbability(Tree tree, NodeRef node) {
133
134 if (calculateAllBirthRates) {
135 calculateBirthRates((TreeModel) tree, tree.getRoot(), 0.0);
136 calculateAllBirthRates = false;
137 }
138
139 if (tree.isRoot(node)) {
140 return 0.0;
141 } else {
142
143 double lambda = birthRates[node.getNumber()];
144 double branchLength = tree.getNodeHeight(tree.getParent(node)) - tree.getNodeHeight(node);
145 double logP = -lambda * branchLength;
146
147 if (tree.isExternal(node)) logP += Math.log(lambda);
148
149 return logP;
150 }
151 }
152
153 private void calculateBirthRates(TreeModel tree, NodeRef node, double rate) {
154
155 int nodeNumber = node.getNumber();
156
157 if (tree.isRoot(node)) {
158 rate = meanRate.getParameterValue(0);
159 } else {
160 if (isVariableSelected(tree, node)) {
161 if (birthRatesAreMultipliers) {
162 rate *= getVariable(tree, node);
163 } else {
164 rate = getVariable(tree, node);
165 }
166 }
167 }
168 birthRates[nodeNumber] = rate;
169
170 int childCount = tree.getChildCount(node);
171 for (int i = 0; i < childCount; i++) {
172 calculateBirthRates(tree, tree.getChild(node, i), rate);
173 }
174 }
175
176 // /**
177 // * @param tree the tree
178 // * @param node the node to retrieve the birth rate of
179 // * @return the birth rate of the given node;
180 // */
181 // private double getBirthRate(TreeModel tree, NodeRef node) {
182 //
183 // double birthRate;
184 // if (!tree.isRoot(node)) {
185 //
186 // double parentRate = getBirthRate(tree, tree.getParent(node));
187 // if (isVariableSelected(tree, node)) {
188 // birthRate = getVariable(tree, node);
189 // if (birthRatesAreMultipliers) {
190 // birthRate *= parentRate;
191 // } else {
192 // throw new RuntimeException("Rates must be multipliers in current implementation! " +
193 // "Otherwise root rate might be ignored");
194 // }
195 // } else {
196 // birthRate = parentRate;
197 // }
198 // } else {
199 // birthRate = meanRate.getParameterValue(0);
200 // }
201 // return birthRate;
202 // }
203
204 protected TreeTraitProvider.Helper treeTraits = new Helper();
205
206 public TreeTrait[] getTreeTraits() {
207 return treeTraits.getTreeTraits();
208 }
209
210 public TreeTrait getTreeTrait(String key) {
211 return treeTraits.getTreeTrait(key);
212 }
213
214 public boolean includeExternalNodesInLikelihoodCalculation() {
215 return true;
216 }
217
218 // **************************************************************
219 // XMLElement IMPLEMENTATION
220 // **************************************************************
221
222 public org.w3c.dom.Element createElement(org.w3c.dom.Document d) {
223 throw new RuntimeException("createElement not implemented");
224 }
225
226 private double[] birthRates;
227
228 private String birthRatesName = "birthRates";
229 private String indicatorsName = "birthRateIndicator";
230
231 private Parameter meanRate;
232 private boolean birthRatesAreMultipliers = false;
233 private NumberFormat format = NumberFormat.getNumberInstance(Locale.ENGLISH);
234 }