Debian Codegraph @

src/dr/evomodel/speciation/BirthDeathSerialSkylineModel.java

raw symbols SCIP
1 /*
2 * BirthDeathSerialSkylineModel.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.io.Importer;
29 import dr.evolution.io.NewickImporter;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.util.Taxon;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34
35 import java.io.IOException;
36 import java.util.Arrays;
37 import java.util.Set;
38
39 /**
40 * Beginning of tree prior for birth-death + serial sampling + extant sample proportion. More Tanja magic...
41 * <p/>
42 * log:
43 * 25 Mar 2011, Denise: added int i (index) for the Variables that change over time such as the methods p0(..), q(..); fixed some formulas (old versions commented out)
44 * unclear marked with todo
45 *
46 * @author Alexei Drummond
47 */
48 public class BirthDeathSerialSkylineModel extends SpeciationModel {
49
50 // times
51 Variable<Double> times;
52
53 // birth rate
54 Variable<Double> lambda;
55
56 // death rate
57 Variable<Double> mu;
58
59 // serial sampling rate
60 Variable<Double> psi;
61
62 // extant sampling proportion
63 Variable<Double> p;
64
65 Variable<Double> origin;
66
67 //boolean death rate is relative?
68 boolean relativeDeath = false;
69
70 // the number of intervals;
71 int size = 1;
72
73 double t_root;
74 double x0;
75 protected double[] p0_iMinus1;
76 protected double[] Ai;
77 protected double[] Bi;
78
79 protected boolean birthChanges = true;
80 protected boolean deathChanges = true;
81 protected boolean samplingChanges = true;
82
83 protected boolean timesStartFromOrigin = true;
84 protected double[] timesFromTips;
85
86 public BirthDeathSerialSkylineModel(
87 Variable<Double> times,
88 Variable<Double> lambda,
89 Variable<Double> mu,
90 Variable<Double> psi,
91 Variable<Double> p,
92 Variable<Double> origin,
93 boolean relativeDeath,
94 boolean sampledIndividualsRemainInfectious,
95 boolean timesStartFromOrigin,
96 Type units) {
97
98 this("birthDeathSerialSamplingModel", times, lambda, mu, psi, p, origin, relativeDeath,
99 sampledIndividualsRemainInfectious, timesStartFromOrigin, units);
100 }
101
102 public BirthDeathSerialSkylineModel(
103 String modelName,
104 Variable<Double> times,
105 Variable<Double> lambda,
106 Variable<Double> mu,
107 Variable<Double> psi,
108 Variable<Double> p,
109 Variable<Double> origin,
110 boolean relativeDeath,
111 boolean sampledIndividualsRemainInfectious,
112 boolean timesStartFromOrigin,
113 Type units) {
114
115 super(modelName, units);
116
117 this.size = times.getSize();
118
119 if (lambda.getSize() != 1 && lambda.getSize() != size)
120 throw new RuntimeException("Length of Lambda parameter should be one or equal to the size of time parameter (size = " + size + ")");
121
122 if (mu.getSize() != 1 && mu.getSize() != size)
123 throw new RuntimeException("Length of mu parameter should be one or equal to the size of time parameter (size = " + size + ")");
124
125 this.timesStartFromOrigin = timesStartFromOrigin;
126
127 this.times = times;
128 addVariable(times);
129 times.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, times.getSize()));
130
131 this.lambda = lambda;
132 addVariable(lambda);
133 lambda.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, lambda.getSize()));
134
135 this.mu = mu;
136 addVariable(mu);
137 mu.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, mu.getSize()));
138
139 this.p = p;
140 addVariable(p);
141 p.addBounds(new Parameter.DefaultBounds(1.0, 0.0, p.getSize()));
142
143 this.origin = origin;
144 addVariable(origin);
145 p.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, origin.getSize()));
146
147 this.psi = psi;
148 addVariable(psi);
149 psi.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, psi.getSize()));
150
151
152 this.relativeDeath = relativeDeath;
153 }
154
155 /**
156 * @param time the time
157 * @param tree the tree
158 * @return the number of lineages that exist at the given time in the given tree.
159 */
160 public int lineageCountAtTime(double time, Tree tree) {
161
162 int count = 1;
163 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
164 if (tree.getNodeHeight(tree.getInternalNode(i)) > time) count += 1;
165 }
166 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
167 if (tree.getNodeHeight(tree.getExternalNode(i)) > time) count -= 1;
168 }
169 return count;
170 }
171
172 public double Ai(double b, double g, double psi) {
173
174 return Math.sqrt((b - g - psi) * (b - g - psi) + 4.0 * b * psi);
175 }
176
177 public double Bi(double b, double g, double psi, double A, double p0) {
178
179 return (-((1.0 - 2.0 * p0) * b + g + psi) / A);
180 }
181
182 public double p0(int index, double t, double ti) {
183
184 return p0(birth(birthChanges ? index : 0), death(deathChanges ? index : 0), psi(samplingChanges ? index : 0), Ai[index], Bi[index], t, ti);
185 }
186
187 public double p0(double b, double g, double psi, double A, double B, double t, double ti) {
188
189 return ((b + g + psi - A * ((Math.exp(A * (t - ti)) * (1.0 - B) - (1.0 + B))) / (Math.exp(A * (t - ti)) * (1.0 - B) + (1.0 + B))) / (2.0 * b));
190 }
191
192 public double g(int index, double t, double ti) {
193
194 double oneMinusBiSq = (1.0 - Bi[index]) * (1.0 - Bi[index]);
195 double onePlusBiSq = (1.0 + Bi[index]) * (1.0 + Bi[index]);
196
197 return 4.0 / (2.0 * (1.0 - Bi[index] * Bi[index]) + Math.exp(Ai[index] * (t - ti)) *
198 oneMinusBiSq + Math.exp(-Ai[index] * (t - ti)) * onePlusBiSq);
199 }
200
201 /**
202 * Returns the time at which epoch i begins. If
203 *
204 * @param i index of the epoch
205 * @return the time at which this epoch begins
206 */
207 public double t(int i) {
208 return timesFromTips[i];
209 }
210
211 public double birth(int i) {
212 return lambda.getValue(i);
213 }
214
215 public double death(int i) {
216 return relativeDeath ? mu.getValue(i) * birth(i) : mu.getValue(i);
217 }
218
219 public double psi(int i) {
220 return psi.getValue(i);
221 }
222
223 public double p() {
224 return p.getValue(0);
225 }
226
227 /**
228 * @param t
229 * @return the birth parameter for the given time
230 */
231 public double lambda(double t) {
232
233 return lambda.getValue(index(t));
234
235 }
236
237 /**
238 * @param t
239 * @return the mutation parameter for the given time
240 */
241 public double mu(double t) {
242
243 return mu.getValue(index(t));
244 }
245
246 public int index(double t) {
247
248 int epoch = Arrays.binarySearch(timesFromTips, t);
249
250 if (epoch < 0) {
251 epoch = -epoch - 1;
252 }
253 return Math.max(epoch - 1, 0);
254 }
255
256 /* calculate and store Ai, Bi and p0_iMinus1 */
257 public void preCalculation(Tree tree) {
258
259 t_root = tree.getNodeHeight(tree.getRoot());
260 x0 = t_root + origin.getValue(0);
261
262 // set up timesFromTips array
263 if (timesFromTips == null) {
264 timesFromTips = new double[times.getSize()];
265 }
266 if (timesStartFromOrigin) {
267 timesFromTips[0] = 0;
268 for (int i = 1; i < timesFromTips.length; i++) {
269 timesFromTips[i] = Math.max(0, x0 - times.getValue(timesFromTips.length - i));
270 }
271
272 } else {
273 for (int i = 0; i < timesFromTips.length; i++) {
274 timesFromTips[i] = times.getValue(i);
275 }
276 }
277
278 Ai = new double[size];
279 Bi = new double[size];
280 p0_iMinus1 = new double[size];
281
282 for (int i = 0; i < size; i++) {
283 Ai[i] = Ai(birth(birthChanges ? i : 0), death(deathChanges ? i : 0), psi(samplingChanges ? i : 0));
284
285 //System.out.println("Ai[" + i + "]=" + Ai[i]);
286 }
287
288 Bi[0] = Bi(birth(0), death(0), psi(0), Ai[0], 1);
289 //System.out.println("Bi[0]=" + Bi[0]);
290 for (int i = 1; i < size; i++) {
291 p0_iMinus1[i - 1] = p0(birth(birthChanges ? (i - 1) : 0), death(deathChanges ? (i - 1) : 0), psi(samplingChanges ? (i - 1) : 0), Ai[i - 1], Bi[i - 1], t(i), t(i - 1));
292 Bi[i] = Bi(birth(birthChanges ? i : 0), death(deathChanges ? i : 0), psi(samplingChanges ? i : 0), Ai[i], p0_iMinus1[i - 1]);
293
294 //System.out.println("Bi[" + i + "]=" + Bi[i]);
295 }
296 }
297
298 /**
299 * Generic likelihood calculation
300 *
301 * @param tree the tree to calculate likelihood of
302 * @return log-likelihood of density
303 */
304 public final double calculateTreeLogLikelihood(Tree tree) {
305
306 // number of lineages at each time ti
307 int[] n = new int[size];
308 int nTips = tree.getExternalNodeCount();
309 preCalculation(tree);
310
311 int index = size - 1; // x0 must be in last interval
312
313 double t = t(index);
314 double g = g(index, x0, t);
315
316 double logP = Math.log(g);
317
318 // first product term in f[T]
319 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
320
321 double x = tree.getNodeHeight(tree.getInternalNode(i));
322 index = index(x);
323
324 double contrib = Math.log(birth(birthChanges ? index : 0) * g(index, x, t(index)));
325
326 logP += contrib;
327
328 //System.out.println("internalNode.logP=" + contrib);
329
330 t = t(index);
331 g = g(index, x, t);
332
333
334 //System.out.println("logP+=" + (Math.log(birth(birthChanges ? index : 0) * g)) + " t= " + t + " g=" + g);
335 }
336
337 // middle product term in f[T]
338 for (int i = 0; i < nTips; i++) {
339
340 double y = tree.getNodeHeight(tree.getExternalNode(i));
341 index = index(y);
342
343 double contrib = Math.log(psi(samplingChanges ? index : 0)) - Math.log(g(index, y, t(index)));
344 ;
345 logP += contrib;
346
347 //System.out.println("externalNode.logP=" + contrib);
348
349 }
350
351 // last product term in f[T], factorizing from 1 to m
352 for (int j = 0; j < size - 1; j++) {
353
354 double contrib = 0;
355
356 double time = t(j + 1);
357 n[j] = lineageCountAtTime(time, tree);
358 if (n[j] > 0) {
359 contrib += n[j] * Math.log(g(j, time, t(j)));
360 //System.out.println("n[" + j + "]" + n[j] + " time=" + time + " t(" + j + ")=" + t(j));
361 }
362
363 logP += contrib;
364 //System.out.println("last term=" + contrib);
365 }
366 return logP;
367 }
368
369 public double calculateTreeLogLikelihood(Tree tree, Set<Taxon> exclude) {
370 if (exclude.size() == 0) return calculateTreeLogLikelihood(tree);
371 throw new RuntimeException("Not implemented!");
372 }
373
374 public static void main(String[] args) throws IOException, Importer.ImportException {
375
376 // test framework
377
378 Variable<Double> times = new Variable.D(1, 10);
379
380 Variable<Double> mu = new Variable.D(1, 10);
381 for (int i = 0; i < mu.getSize(); i++) {
382 times.setValue(i, (i + 1) * 2.0);
383 mu.setValue(i, i + 1.0);
384 }
385
386 Variable<Double> lambda = new Variable.D(1, 10);
387 Variable<Double> psi = new Variable.D(0.5, 1);
388 Variable<Double> p = new Variable.D(0.5, 1);
389 Variable<Double> origin = new Variable.D(0.5, 1);
390 boolean relativeDeath = false;
391 boolean sampledIndividualsRemainInfectious = false;
392 boolean timesStartFromOrigin = false;
393
394 BirthDeathSerialSkylineModel model =
395 new BirthDeathSerialSkylineModel(times, lambda, mu, psi, p, origin, relativeDeath,
396 sampledIndividualsRemainInfectious, timesStartFromOrigin, Type.SUBSTITUTIONS);
397
398 NewickImporter importer = new NewickImporter("((A:6,B:5):4,(C:3,D:2):1);");
399 Tree tree = importer.importNextTree();
400
401 model.calculateTreeLogLikelihood(tree);
402
403 for (int i = 0; i < times.getSize(); i += 1) {
404 System.out.println("mu at time " + i + " is " + model.mu(i));
405 System.out.println("p0 at time " + i + " is " + model.p0(0, i, i));
406 }
407
408 }
409 }