Debian Codegraph @

src/dr/evomodel/speciation/BirthDeathGernhard08Model.java

raw symbols SCIP
1 /*
2 * BirthDeathGernhard08Model.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.evomodelxml.speciation.BirthDeathModelParser;
31 import dr.inference.model.Parameter;
32 import dr.util.Author;
33 import dr.util.Citable;
34 import dr.util.Citation;
35 import dr.util.CommonCitations;
36
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40
41 import static org.apache.commons.math.special.Gamma.logGamma;
42
43 /**
44 * Birth Death model based on Gernhard 2008 "The conditioned reconstructed process"
45 * Journal of Theoretical Biology Volume 253, Issue 4, 21 August 2008, Pages 769-778
46 * doi:10.1016/j.jtbi.2008.04.005 (http://dx.doi.org/10.1016/j.jtbi.2008.04.005)
47 * <p/>
48 * This derivation conditions directly on fixed N taxa.
49 * <p/>
50 * The inference is directly on b-d (strictly positive) and d/b (constrained in [0,1))
51 * <p/>
52 * Vefified using simulated trees generated by Klass tree sample. (http://www.klaashartmann.com/treesample/)
53 * <p/>
54 * Sampling proportion not verified via simulation. Proportion set by default to 1, an assignment which makes the expressions
55 * identical to the expressions before the change.
56 *
57 * @author Joseph Heled
58 * Date: 24/02/2008
59 */
60 public class BirthDeathGernhard08Model extends UltrametricSpeciationModel implements Citable {
61
62 public enum TreeType {
63 UNSCALED, // no coefficient
64 TIMESONLY, // n!
65 ORIENTED, // n
66 LABELED, // 2^(n-1)/(n-1)! (conditional on root: 2^(n-1)/n!(n-1) )
67 }
68
69 public static final String BIRTH_DEATH_MODEL = BirthDeathModelParser.BIRTH_DEATH_MODEL;
70
71 /*
72 * mu/lambda
73 *
74 * null means default (0), or pure birth (Yule)
75 */
76 private Parameter relativeDeathRateParameter;
77
78 /**
79 * lambda - mu
80 */
81 private Parameter birthDiffRateParameter;
82
83 private Parameter sampleProbability;
84
85 private TreeType type;
86
87 private boolean conditionalOnRoot;
88
89 /**
90 * rho *
91 */
92
93 public BirthDeathGernhard08Model(Parameter birthDiffRateParameter,
94 Parameter relativeDeathRateParameter,
95 Parameter sampleProbability,
96 TreeType type,
97 Type units) {
98
99 this(BIRTH_DEATH_MODEL, birthDiffRateParameter, relativeDeathRateParameter, sampleProbability, type, units, false);
100 }
101
102 public BirthDeathGernhard08Model(String modelName,
103 Parameter birthDiffRateParameter,
104 Parameter relativeDeathRateParameter,
105 Parameter sampleProbability,
106 TreeType type,
107 Type units, boolean conditionalOnRoot) {
108
109 super(modelName, units);
110
111 this.birthDiffRateParameter = birthDiffRateParameter;
112 addVariable(birthDiffRateParameter);
113 birthDiffRateParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
114
115 this.relativeDeathRateParameter = relativeDeathRateParameter;
116 if( relativeDeathRateParameter != null ) {
117 addVariable(relativeDeathRateParameter);
118 relativeDeathRateParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
119 }
120
121 this.sampleProbability = sampleProbability;
122 if (sampleProbability != null) {
123 addVariable(sampleProbability);
124 sampleProbability.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
125 }
126
127 this.conditionalOnRoot = conditionalOnRoot;
128 if ( conditionalOnRoot && sampleProbability != null) {
129 throw new IllegalArgumentException("Not supported: birth death prior conditional on root with sampling probability.");
130 }
131
132 this.type = type;
133 }
134
135 @Override
136 public boolean isYule() {
137 // Yule only
138 return (relativeDeathRateParameter == null && sampleProbability == null && !conditionalOnRoot);
139 }
140
141 @Override
142 public double getMarginal(Tree tree, CalibrationPoints calibration) {
143 // Yule only
144 return calibration.getCorrection(tree, getR());
145 }
146
147 public double getR() {
148 return birthDiffRateParameter.getParameterValue(0);
149 }
150
151 public double getA() {
152 return relativeDeathRateParameter != null ? relativeDeathRateParameter.getParameterValue(0) : 0;
153 }
154
155 public double getRho() {
156 return sampleProbability != null ? sampleProbability.getParameterValue(0) : 1.0;
157 }
158
159 private double logCoeff(int taxonCount) {
160 switch( type ) {
161 case UNSCALED: break;
162 case TIMESONLY: return logGamma(taxonCount + 1);
163 case ORIENTED: return Math.log(taxonCount);
164 case LABELED: {
165 final double two2nm1 = (taxonCount - 1) * Math.log(2.0);
166 if( ! conditionalOnRoot ) {
167 return two2nm1 - logGamma(taxonCount);
168 } else {
169 return two2nm1 - Math.log(taxonCount-1) - logGamma(taxonCount+1);
170 }
171 }
172 }
173 return 0.0;
174 }
175
176 public double logTreeProbability(int taxonCount) {
177 double c1 = logCoeff(taxonCount);
178 if( ! conditionalOnRoot ) {
179 c1 += (taxonCount - 1) * Math.log(getR() * getRho()) + taxonCount * Math.log(1 - getA());
180 }
181 return c1;
182 }
183
184 public double logNodeProbability(Tree tree, NodeRef node) {
185 final double height = tree.getNodeHeight(node);
186 final double r = getR();
187 final double mrh = -r * height;
188 final double a = getA();
189
190 if( ! conditionalOnRoot ) {
191 final double rho = getRho();
192 final double z = Math.log(rho + ((1 - rho) - a) * Math.exp(mrh));
193 double l = -2 * z + mrh;
194
195 if( tree.getRoot() == node ) {
196 l += mrh - z;
197 }
198 return l;
199 } else {
200 double l;
201 if( tree.getRoot() != node ) {
202 final double z = Math.log(1 - a * Math.exp(mrh));
203 l = -2 * z + mrh;
204 } else {
205 // Root dependent coefficient from each internal node
206 final double ca = 1 - a;
207 final double emrh = Math.exp(-mrh);
208 if( emrh != 1.0 ) {
209 l = (tree.getTaxonCount() - 2) * Math.log(r * ca * (1 + ca /(emrh - 1)));
210 } else { // use exp(x)-1 = x for x near 0
211 l = (tree.getTaxonCount() - 2) * Math.log(ca * (r + ca/height));
212 }
213 }
214 return l;
215 }
216 }
217
218 public boolean includeExternalNodesInLikelihoodCalculation() {
219 return false;
220 }
221
222 @Override
223 public Citation.Category getCategory() {
224 return Citation.Category.TREE_PRIORS;
225 }
226
227 @Override
228 public String getDescription() {
229 return "Gernhard 2008 Birth Death Tree Model";
230 }
231
232 @Override
233 public List<Citation> getCitations() {
234 return Collections.singletonList(new Citation(
235 new Author[]{
236 new Author("T", "Gernhard"),
237 },
238 "The conditioned reconstructed process",
239 2008,
240 "Journal of Theoretical Biology",
241 253,
242 769, 778,
243 "10.1016/j.jtbi.2008.04.005"
244 ));
245 }
246 }
247