Debian Codegraph @

src/dr/evomodel/coalescent/GaussianProcessMultilocusSkytrackLikelihood.java

raw symbols SCIP
1 /*
2 * GaussianProcessMultilocusSkytrackLikelihood.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.coalescent;
27
28 //import dr.evolution.coalescent.IntervalType;
29 import dr.evolution.coalescent.TreeIntervals;
30 import dr.evolution.tree.Tree;
31 import dr.evomodel.tree.TreeModel;
32 //import dr.evomodelxml.coalescent.GMRFSkyrideLikelihoodParser;
33 import dr.evomodelxml.coalescent.GaussianProcessSkytrackLikelihoodParser;
34 import dr.inference.model.Model;
35 import dr.inference.model.Parameter;
36 //import no.uib.cipr.matrix.DenseVector;
37 //import no.uib.cipr.matrix.SymmTridiagMatrix;
38
39 import java.util.ArrayList;
40 //import java.util.Arrays;
41 import java.util.List;
42
43 /**
44 * @author Julia Palacios
45 * @author Marc A. Suchard
46 * @author Vladimir Minin
47 * @author Michael Karcher
48 */
49
50 public class GaussianProcessMultilocusSkytrackLikelihood extends GaussianProcessSkytrackLikelihood implements MultiLociTreeSet {
51
52
53 // private double cutOff;
54 // private int numGridPoints;
55 // protected int oldFieldLength;
56 // number of coalescent events which occur in an interval with constant population size
57 // protected double[] numCoalEvents;
58 // protected double[] storedNumCoalEvents;
59 // protected double[] gridPoints;
60 // protected double theLastTime;
61 // protected double diagonalValue;
62 // sortedPoints[i][0] is the time of the i-th grid point or sampling or coalescent event
63 // sortedPoints[i][1] is 0 if the i-th point is a grid point, 1 if it's a sampling point, and 2 if it's a coalescent point
64 // sortedPoints[i][2] is the number of lineages present in the interval starting at time sortedPoints[i][0]
65
66 // protected Parameter phiParameter;
67 // protected SymmTridiagMatrix precMatrix;
68 // protected SymmTridiagMatrix storedPrecMatrix;
69
70 public GaussianProcessMultilocusSkytrackLikelihood(List<Tree> treeList,
71 Parameter precParameter,
72 boolean rescalebyRootHeight,
73 Parameter lambda_bound,
74 Parameter lambda_parameter,
75 Parameter popParameter,
76 Parameter alpha_parameter,
77 Parameter beta_parameter,
78 Parameter change_points,
79 Parameter GPtype,
80 Parameter GPcounts,
81 Parameter coalfactor,
82 Parameter CoalCounts,
83 Parameter numPoints,
84 Parameter Tmrca) {
85
86 super(GaussianProcessSkytrackLikelihoodParser.SKYTRACK_LIKELIHOOD);
87
88
89 this.popSizeParameter = popParameter;
90 this.Tmrca = Tmrca;
91 this.changePoints=change_points;
92 this.numPoints=numPoints;
93 this.precisionParameter = precParameter;
94 this.lambdaParameter = lambda_parameter;
95 this.betaParameter = beta_parameter;
96 this.alphaParameter=alpha_parameter;
97 this.rescaleByRootHeight=rescalebyRootHeight;
98 this.lambda_boundParameter=lambda_bound;
99 this.GPcounts=GPcounts;
100 this.GPtype=GPtype;
101 this.coalfactor=coalfactor;
102 this.CoalCounts=CoalCounts;
103
104 // System.out.println("numGridPoints: " + numGridPoints);
105 // setupGridPoints();
106
107 addVariable(popSizeParameter);
108 addVariable(precisionParameter);
109 addVariable(changePoints);
110 addVariable(numPoints);
111 addVariable(GPcounts);
112 addVariable(GPtype);
113 addVariable(coalfactor);
114 addVariable(lambda_boundParameter);
115 addVariable(CoalCounts);
116
117
118 setTree(treeList);
119
120 numintervals=getIntervalCount(); //Not sure, check. I want the number of intervals defined by either
121 // sampling times or coalescent times of all trees
122 System.err.println("by getIntervalCount, I get:"+getIntervalCount());
123
124 numcoalpoints = getCorrectFieldLength();
125 System.err.println("by getCorrectField"+getCorrectFieldLength());
126
127 GPcoalfactor = new double[numintervals];
128 backupIntervals=new double[numintervals];
129 GPCoalInterval=new double[numcoalpoints];
130 storedGPCoalInterval=new double[numcoalpoints];
131 CoalPosIndicator= new int[numcoalpoints];
132 storedCoalPosIndicator=new int[numcoalpoints];
133 CoalTime=new double[numcoalpoints];
134 storedCoalTime=new double[numcoalpoints];
135 storedGPcoalfactor = new double[numintervals];
136 GPcounts.setDimension(numintervals);
137 CoalCounts.setDimension(numcoalpoints);
138 GPtype.setDimension(numcoalpoints);
139 numPoints.setParameterValue(0,numcoalpoints);
140 popSizeParameter.setDimension(numcoalpoints);
141 changePoints.setDimension(numcoalpoints);
142 coalfactor.setDimension(numcoalpoints);
143
144 initializationReport();
145
146 // wrapSetupIntervals();
147
148 // coalescentIntervals = new double[oldFieldLength];
149 // storedCoalescentIntervals = new double[oldFieldLength];
150 // sufficientStatistics = new double[fieldLength];
151 // storedSufficientStatistics = new double[fieldLength];
152 // numCoalEvents = new double[fieldLength];
153 // storedNumCoalEvents = new double[fieldLength];
154
155 // setupGPWeights();
156 setupSufficientStatistics();
157
158 // addStatistic(new DeltaStatistic());
159
160
161
162 /* Force all entries in groupSizeParameter = 1 for compatibility with Tracer */
163
164 }
165
166
167
168 // OK
169 protected void setTree(List<Tree> treeList) {
170 treesSet = this;
171 this.treeList = treeList;
172 makeTreeIntervalList(treeList, true);
173 numTrees = treeList.size();
174 }
175 // OK
176 private void makeTreeIntervalList(List<Tree> treeList, boolean add) {
177 if (intervalsList == null) {
178 intervalsList = new ArrayList<TreeIntervals>();
179 } else {
180 intervalsList.clear();
181 }
182 for (Tree tree : treeList) {
183 numIntervals+=tree.getExternalNodeCount();
184 intervalsList.add(new TreeIntervals(tree));
185 if (add && tree instanceof TreeModel) {
186 addModel((TreeModel) tree);
187 }
188 }
189 }
190
191 protected int getCorrectFieldLength() {
192
193 return numIntervals-treeList.size();
194 // TODO add correction when not all samples are gathered at the same time
195 }
196 //
197 // protected int getCorrectOldFieldLength() {
198 // int tips = 0;
199 // for (Tree tree : treeList) {
200 // tips += tree.getExternalNodeCount();
201 // }
202 // return tips - treeList.size();
203 // }
204
205 protected void handleModelChangedEvent(Model model, Object object, int index) {
206
207 if (model instanceof TreeModel) {
208 TreeModel treeModel = (TreeModel) model;
209 int tn = treeList.indexOf(treeModel);
210 if (tn >= 0) {
211 // intervalsList.get(tn).setIntervalsUnknown(); // TODO Why is this slower (?) than remaking whole list?
212 makeTreeIntervalList(treeList, false);
213 intervalsKnown = false;
214 likelihoodKnown = false;
215 } else {
216 throw new RuntimeException("Unknown tree modified in GMRFMultilocusSkyrideLikelihood");
217 }
218 } else {
219 throw new RuntimeException("Unknown object modified in GMRFMultilocusSkyrideLikelihood");
220 }
221 }
222
223 public void initializationReport() {
224 System.out.println("Creating a GP based estimation of effective population size trajectories for multiple loci:");
225 System.out.println("\tIf you publish results using this model, please reference: ");
226 System.out.println("\t\tPalacios, Minin and Suchard (XXXX)");
227 }
228 //
229 // public void wrapSetupIntervals() {
230 // // Do nothing
231 // }
232
233 int numTrees;
234 int numIntervals;
235
236 //
237 // protected void setupGridPoints() {
238 // if (gridPoints == null) {
239 // gridPoints = new double[numGridPoints];
240 // } else {
241 // Arrays.fill(gridPoints, 0);
242 // }
243 //
244 // for (int pt = 0; pt < numGridPoints; pt++) {
245 // gridPoints[pt] = (pt + 1) * (cutOff / numGridPoints);
246 // }
247 // }
248
249 protected void setupSufficientStatistics() {
250
251 // //numCoalEvents = new double[fieldLength];
252 // //sufficientStatistics = new double[fieldLength];
253 //
254 // Arrays.fill(numCoalEvents, 0);
255 // Arrays.fill(sufficientStatistics, 0);
256 //
257 // //index of smallest grid point greater than at least one sampling/coalescent time in current tree
258 // int minGridIndex;
259 // //index of greatest grid point less than at least one sampling/coalescent time in current tree
260 // int maxGridIndex;
261 //
262 // int numLineages;
263 //
264 // int currentGridIndex;
265 // int currentTimeIndex;
266 //
267 // double currentTime;
268 // double nextTime;
269 //
270 // //time of last coalescent event in tree
271 // double lastCoalescentTime;
272 //
273 // for (int i = 0; i < numTrees; i++) {
274 // currentTimeIndex = 0;
275 // currentTime = intervalsList.get(i).getIntervalTime(currentTimeIndex);
276 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
277 // while (nextTime <= currentTime) {
278 // currentTimeIndex++;
279 // currentTime = intervalsList.get(i).getIntervalTime(currentTimeIndex);
280 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
281 // }
282 //
283 //
284 // numLineages = intervalsList.get(i).getLineageCount(currentTimeIndex + 1);
285 // minGridIndex = 0;
286 // while (gridPoints[minGridIndex] <= currentTime) {
287 // minGridIndex++;
288 // }
289 // currentGridIndex = minGridIndex;
290 //
291 // lastCoalescentTime = currentTime + intervalsList.get(i).getTotalDuration();
292 //
293 // theLastTime = lastCoalescentTime;
294 //
295 // maxGridIndex = numGridPoints - 1;
296 // while ((maxGridIndex >= 0) && (gridPoints[maxGridIndex] >= lastCoalescentTime)) {
297 // maxGridIndex = maxGridIndex - 1;
298 // }
299 //
300 // if (maxGridIndex >= 0) {
301 //
302 // //from likelihood of interval between first sampling time and gridPoints[minGridIndex]
303 //
304 // while (nextTime < gridPoints[currentGridIndex]) {
305 //
306 // //check to see if interval ends with coalescent event
307 // if (intervalsList.get(i).getCoalescentEvents(currentTimeIndex + 1) > 0) {
308 //
309 // numCoalEvents[currentGridIndex]++;
310 // }
311 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (nextTime - currentTime) * numLineages * (numLineages - 1) * 0.5;
312 // currentTime = nextTime;
313 // currentTimeIndex++;
314 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
315 //
316 // while (nextTime <= currentTime) {
317 // currentTimeIndex++;
318 // currentTime = intervalsList.get(i).getIntervalTime(currentTimeIndex);
319 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
320 // }
321 //
322 // numLineages = intervalsList.get(i).getLineageCount(currentTimeIndex + 1);
323 //
324 // }
325 //
326 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (gridPoints[currentGridIndex] - currentTime) * numLineages * (numLineages - 1) * 0.5;
327 //
328 // currentGridIndex++;
329 //
330 //
331 // //from likelihood of intervals between gridPoints[minGridIndex] and gridPoints[maxGridIndex]
332 //
333 // while (currentGridIndex <= maxGridIndex) {
334 // if (nextTime >= gridPoints[currentGridIndex]) {
335 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (gridPoints[currentGridIndex] - gridPoints[currentGridIndex - 1]) * numLineages * (numLineages - 1) * 0.5;
336 //
337 // currentGridIndex++;
338 // } else {
339 //
340 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (nextTime - gridPoints[currentGridIndex - 1]) * numLineages * (numLineages - 1) * 0.5;
341 //
342 // //check to see if interval ends with coalescent event
343 // if (intervalsList.get(i).getCoalescentEvents(currentTimeIndex + 1) > 0) {
344 // numCoalEvents[currentGridIndex]++;
345 // }
346 // currentTime = nextTime;
347 // currentTimeIndex++;
348 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
349 // while (nextTime <= currentTime) {
350 // currentTimeIndex++;
351 // currentTime = intervalsList.get(i).getIntervalTime(currentTimeIndex);
352 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
353 // }
354 //
355 // numLineages = intervalsList.get(i).getLineageCount(currentTimeIndex + 1);
356 //
357 // while (nextTime < gridPoints[currentGridIndex]) {
358 // //check to see if interval is coalescent interval or sampling interval
359 // if (intervalsList.get(i).getCoalescentEvents(currentTimeIndex + 1) > 0) {
360 // numCoalEvents[currentGridIndex]++;
361 // }
362 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (nextTime - currentTime) * numLineages * (numLineages - 1) * 0.5;
363 //
364 // currentTime = nextTime;
365 // currentTimeIndex++;
366 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
367 // while (nextTime <= currentTime) {
368 // currentTimeIndex++;
369 // currentTime = intervalsList.get(i).getIntervalTime(currentTimeIndex);
370 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
371 // }
372 //
373 // numLineages = intervalsList.get(i).getLineageCount(currentTimeIndex + 1);
374 //
375 // }
376 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (gridPoints[currentGridIndex] - currentTime) * numLineages * (numLineages - 1) * 0.5;
377 //
378 // currentGridIndex++;
379 // }
380 // }
381 //
382 // //from likelihood of interval between gridPoints[maxGridIndex] and lastCoalescentTime
383 //
384 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (nextTime - gridPoints[currentGridIndex - 1]) * numLineages * (numLineages - 1) * 0.5;
385 //
386 // //check to see if interval ends with coalescent event
387 // if (intervalsList.get(i).getCoalescentEvents(currentTimeIndex + 1) > 0) {
388 // numCoalEvents[currentGridIndex]++;
389 // }
390 //
391 // currentTime = nextTime;
392 // currentTimeIndex++;
393 //
394 // while ((currentTimeIndex + 1) < intervalsList.get(i).getIntervalCount()) {
395 // // currentTime = nextTime;
396 // // currentTimeIndex++;
397 //
398 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
399 // while (nextTime <= currentTime) {
400 // currentTimeIndex++;
401 // currentTime = intervalsList.get(i).getIntervalTime(currentTimeIndex);
402 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
403 // }
404 //
405 // numLineages = intervalsList.get(i).getLineageCount(currentTimeIndex + 1);
406 //
407 // //check to see if interval is coalescent interval or sampling interval
408 //
409 //
410 // if (intervalsList.get(i).getCoalescentEvents(currentTimeIndex + 1) > 0) {
411 // numCoalEvents[currentGridIndex]++;
412 // }
413 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (nextTime - currentTime) * numLineages * (numLineages - 1) * 0.5;
414 // currentTime = nextTime;
415 // currentTimeIndex++;
416 //
417 // }
418 // } else {
419 // while ((currentTimeIndex + 1) < intervalsList.get(i).getIntervalCount()) {
420 // //check to see if interval is coalescent interval or sampling interval
421 // if (intervalsList.get(i).getCoalescentEvents(currentTimeIndex + 1) > 0) {
422 // numCoalEvents[currentGridIndex]++;
423 // }
424 // sufficientStatistics[currentGridIndex] = sufficientStatistics[currentGridIndex] + (nextTime - currentTime) * numLineages * (numLineages - 1) * 0.5;
425 //
426 // currentTime = nextTime;
427 // currentTimeIndex++;
428 // if ((currentTimeIndex + 1) < intervalsList.get(i).getIntervalCount()) {
429 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
430 //
431 // while (nextTime <= currentTime) {
432 // currentTimeIndex++;
433 // currentTime = intervalsList.get(i).getIntervalTime(currentTimeIndex);
434 // nextTime = intervalsList.get(i).getIntervalTime(currentTimeIndex + 1);
435 // }
436 //
437 // numLineages = intervalsList.get(i).getLineageCount(currentTimeIndex + 1);
438 //
439 // }
440 //
441 // }
442 //
443 //
444 // }
445 // }
446
447 }
448 //
449 // public double[] getNumCoalEvents() {
450 // return numCoalEvents;
451 // }
452
453 protected double calculateLogCoalescentLikelihood() {
454
455 // if (!intervalsKnown) {
456 // // intervalsKnown -> false when handleModelChanged event occurs in super.
457 // wrapSetupIntervals();
458 // setupSufficientStatistics();
459 // intervalsKnown = true;
460 // }
461
462 // Matrix operations taken from block update sampler to calculate data likelihood and field prior
463
464 double currentLike = 0;
465 // double[] currentGamma = popSizeParameter.getParameterValues();
466 //
467 // for (int i = 0; i < fieldLength; i++) {
468 // currentLike += -numCoalEvents[i] * currentGamma[i] - sufficientStatistics[i] * Math.exp(-currentGamma[i]);
469 // }
470
471 return currentLike;
472 }
473
474
475 protected double calculateLogFieldLikelihood() {
476
477 // if (!intervalsKnown) {
478 // //intervalsKnown -> false when handleModelChanged event occurs in super.
479 // wrapSetupIntervals();
480 // setupSufficientStatistics();
481 // intervalsKnown = true;
482 // }
483
484 double currentLike = 0;
485 // DenseVector diagonal1 = new DenseVector(fieldLength);
486 // DenseVector currentGamma = new DenseVector(popSizeParameter.getParameterValues());
487 //
488 // SymmTridiagMatrix currentQ = getScaledWeightMatrix(precisionParameter.getParameterValue(0), lambdaParameter.getParameterValue(0));
489 // currentQ.mult(currentGamma, diagonal1);
490 //
491 // // currentLike += 0.5 * logGeneralizedDeterminant(currentQ) - 0.5 * currentGamma.dot(diagonal1);
492 //
493 // currentLike += 0.5 * (fieldLength - 1) * Math.log(precisionParameter.getParameterValue(0)) - 0.5 * currentGamma.dot(diagonal1);
494 // if (lambdaParameter.getParameterValue(0) == 1) {
495 // currentLike -= (fieldLength - 1) / 2.0 * LOG_TWO_TIMES_PI;
496 // } else {
497 // currentLike -= fieldLength / 2.0 * LOG_TWO_TIMES_PI;
498 // }
499
500 return currentLike;
501 }
502
503 public double getLogLikelihood() {
504 if (!likelihoodKnown) {
505 logLikelihood = calculateLogCoalescentLikelihood();
506 // logFieldLikelihood = calculateLogFieldLikelihood();
507 likelihoodKnown = true;
508 }
509
510 return logLikelihood;
511 // return logLikelihood + logFieldLikelihood;
512 }
513
514 // protected void setupGPWeights() {
515 //
516 // //setupSufficientStatistics();
517 //
518 // //Set up the weight Matrix
519 // double[] offdiag = new double[fieldLength - 1];
520 // double[] diag = new double[fieldLength];
521 //
522 // diagonalValue = 2;
523 // //First set up the offdiagonal entries;
524 //
525 // for (int i = 0; i < fieldLength - 1; i++) {
526 // offdiag[i] = -1;
527 // }
528 //
529 // //Then set up the diagonal entries;
530 // for (int i = 1; i < fieldLength - 1; i++) {
531 // // diag[i] = -(offdiag[i] + offdiag[i - 1]);
532 // diag[i] = diagonalValue;
533 // }
534 // //Take care of the endpoints
535 // //diag[0] = -offdiag[0];
536 // //diag[fieldLength - 1] = -offdiag[fieldLength - 2];
537 // diag[0] = diagonalValue - 1.0;
538 // diag[fieldLength - 1] = diagonalValue - 1.0;
539 //
540 //
541 // weightMatrix = new SymmTridiagMatrix(diag, offdiag);
542 //
543 // }
544
545 protected double getFieldScalar() {
546 return 1.0;
547 }
548
549
550 private List<Tree> treeList;
551 private List<TreeIntervals> intervalsList;
552
553 public int nLoci() {
554 return treeList.size();
555 }
556
557 public Tree getTree(int nt) {
558 return treeList.get(nt);
559 }
560
561 public TreeIntervals getTreeIntervals(int nt) {
562 return intervalsList.get(nt);
563 }
564
565 public double getPopulationFactor(int nt) {
566 return 1.0;
567 }
568
569 public void storeTheState() {
570 for (TreeIntervals intervals : intervalsList) {
571 intervals.storeState();
572 }
573 }
574
575 public void restoreTheState() {
576 for (TreeIntervals intervals : intervalsList) {
577 intervals.restoreState();
578 }
579 }
580
581 // protected void storeState() {
582 // // System.arraycopy(numCoalEvents, 0, storedNumCoalEvents, 0, numCoalEvents.length);
583 // super.storeState();
584 // System.arraycopy(numCoalEvents, 0, storedNumCoalEvents, 0, numCoalEvents.length);
585 // // storedPrecMatrix = precMatrix.copy();
586 // }
587
588
589 // protected void restoreState() {
590 // super.restoreState();
591 //
592 // // Swap pointers
593 // double[] tmp = numCoalEvents;
594 // numCoalEvents = storedNumCoalEvents;
595 // storedNumCoalEvents = tmp;
596 // }
597
598 // public int getCoalescentIntervalLineageCount(int i) {
599 // return 0; //To change body of implemented methods use File | Settings | File Templates.
600 // }
601 //
602 // public IntervalType getCoalescentIntervalType(int i) {
603 // return null; //To change body of implemented methods use File | Settings | File Templates.
604 // }
605 }
606