Debian Codegraph @

src/dr/evomodel/coalescent/OldAbstractCoalescentLikelihood.java

raw symbols SCIP
1 /*
2 * OldAbstractCoalescentLikelihood.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.Coalescent;
29 import dr.evolution.coalescent.DemographicFunction;
30 import dr.evolution.coalescent.ScaledDemographic;
31 import dr.evolution.tree.NodeRef;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.util.Units;
34 import dr.evomodel.tree.TreeModel;
35 import dr.evomodelxml.coalescent.CoalescentLikelihoodParser;
36 import dr.inference.model.*;
37 import dr.math.Binomial;
38 import dr.util.ComparableDouble;
39 import dr.util.HeapSort;
40
41 import java.util.ArrayList;
42
43 /**
44 * Forms a base class for a number of coalescent likelihood calculators.
45 * <p/>
46 * This was the former 'CoalescentLikelihood' which, as of BEAST v1.4.x was replaced as the
47 * standard coalescent likelihood by 'NewCoalescentLikelihood'. As this class is used as a base
48 * by a number of other classes (i.e., BayesianSkylineLikelihood), I have made this class abstract
49 * and removed the parser.
50 * <p/>
51 * NewCoalescentLikelihood is now CoalesecentLikelihood (it's parser was installed as the default
52 * 'coalescentLikelihood' anyway).
53 *
54 * @author Andrew Rambaut
55 * @author Alexei Drummond
56 * @version $Id: CoalescentLikelihood.java,v 1.43 2006/07/28 11:27:32 rambaut Exp $
57 */
58 public class OldAbstractCoalescentLikelihood extends AbstractModelLikelihood implements Units {
59
60 // PUBLIC STUFF
61
62 //public static final String COALESCENT_LIKELIHOOD = "oldcoalescentLikelihood";
63 // public static final String ANALYTICAL = "analytical";
64 // public static final String MODEL = "model";
65 //
66 // public static final String POPULATION_TREE = "populationTree";
67 // public static final String POPULATION_FACTOR = "factor";
68 protected MultiLociTreeSet treesSet = null;
69
70 public enum CoalescentEventType {
71 /**
72 * Denotes an interval after which a coalescent event is observed
73 * (i.e. the number of lineages is smaller in the next interval)
74 */
75 COALESCENT,
76 /**
77 * Denotes an interval at the end of which a new sample addition is
78 * observed (i.e. the number of lineages is larger in the next interval).
79 */
80 NEW_SAMPLE,
81 /**
82 * Denotes an interval at the end of which nothing is
83 * observed (i.e. the number of lineages is the same in the next interval).
84 */
85 NOTHING
86 }
87
88 public OldAbstractCoalescentLikelihood(Tree tree, DemographicModel demoModel) {
89 this(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, tree, demoModel, true);
90 }
91
92 public OldAbstractCoalescentLikelihood(MultiLociTreeSet treesSet, DemographicModel demoModel) {
93 super(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD);
94 this.demoModel = demoModel;
95 this.tree = null;
96 this.treesSet = treesSet;
97
98 if (demoModel != null) {
99 addModel(demoModel);
100 }
101
102 for (int nt = 0; nt < treesSet.nLoci(); ++nt) {
103 final Tree t = treesSet.getTree(nt);
104 if (t instanceof Model) {
105 addModel((Model) t);
106 }
107 }
108 }
109
110 public OldAbstractCoalescentLikelihood(String name, Tree tree, DemographicModel demoModel, boolean setupIntervals) {
111 super(name);
112
113 this.demoModel = demoModel;
114 this.tree = tree;
115
116 if (tree instanceof TreeModel) {
117 addModel((TreeModel) tree);
118 }
119
120 if (demoModel != null) {
121 addModel(demoModel);
122 }
123
124 if (setupIntervals) setupIntervals();
125
126 addStatistic(new DeltaStatistic());
127 }
128
129 OldAbstractCoalescentLikelihood(String name) {
130 super(name);
131 }
132
133 // **************************************************************
134 // Extendable methods
135 // **************************************************************
136
137 /**
138 * @param tree given tree
139 * @return the node ref of the MRCA of this coalescent prior in the given tree (i.e. root of tree)
140 */
141 public NodeRef getMRCAOfCoalescent(Tree tree) {
142 return tree.getRoot();
143 }
144
145 /**
146 * @param tree given tree
147 * @return an array of noderefs that represent the MRCAs of subtrees to exclude from coalescent prior.
148 * May return null if no subtrees should be excluded.
149 */
150 public NodeRef[] getExcludedMRCAs(Tree tree) {
151 return null;
152 }
153
154 // **************************************************************
155 // ModelListener IMPLEMENTATION
156 // **************************************************************
157
158 protected void handleModelChangedEvent(Model model, Object object, int index) {
159 if (model == tree) {
160 // treeModel has changed so recalculate the intervals
161 intervalsKnown = false;
162 } else {
163 // demoModel has changed so we don't need to recalculate the intervals
164 }
165
166 likelihoodKnown = false;
167 }
168
169 // **************************************************************
170 // VariableListener IMPLEMENTATION
171 // **************************************************************
172
173 // No parameters to respond to
174
175 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
176 }
177
178 // **************************************************************
179 // Model IMPLEMENTATION
180 // **************************************************************
181
182 /**
183 * Stores the precalculated state: in this case the intervals
184 */
185 protected void storeState() {
186 if (tree != null) {
187 System.arraycopy(intervals, 0, storedIntervals, 0, intervals.length);
188 System.arraycopy(lineageCounts, 0, storedLineageCounts, 0, lineageCounts.length);
189 storedIntervalsKnown = intervalsKnown;
190 storedIntervalCount = intervalCount;
191 storedLikelihoodKnown = likelihoodKnown;
192 } else if (treesSet != null) {
193 treesSet.storeTheState();
194 }
195 storedLogLikelihood = logLikelihood;
196 }
197
198 /**
199 * Restores the precalculated state: that is the intervals of the tree.
200 */
201 protected void restoreState() {
202 if (tree != null) {
203 System.arraycopy(storedIntervals, 0, intervals, 0, storedIntervals.length);
204 System.arraycopy(storedLineageCounts, 0, lineageCounts, 0, storedLineageCounts.length);
205 intervalsKnown = storedIntervalsKnown;
206 intervalCount = storedIntervalCount;
207 } else if (treesSet != null) {
208 treesSet.restoreTheState();
209 }
210
211 likelihoodKnown = storedLikelihoodKnown;
212 logLikelihood = storedLogLikelihood;
213
214 if (!intervalsKnown) {
215 likelihoodKnown = false;
216 }
217 }
218
219 protected final void acceptState() {
220 } // nothing to do
221
222 // **************************************************************
223 // Likelihood IMPLEMENTATION
224 // **************************************************************
225
226 public final Model getModel() {
227 return this;
228 }
229
230 public double getLogLikelihood() {
231 if (!likelihoodKnown) {
232 logLikelihood = calculateLogLikelihood();
233 likelihoodKnown = true;
234 }
235 return logLikelihood;
236 }
237
238 public final void makeDirty() {
239 likelihoodKnown = false;
240 intervalsKnown = false;
241 }
242
243 /**
244 * @return the log likelihood of this set of coalescent intervals,
245 * given a demographic model
246 */
247 public double calculateLogLikelihood() {
248
249 if (treesSet != null) {
250 final int nTrees = treesSet.nLoci();
251 final DemographicFunction demogFunction = demoModel.getDemographicFunction();
252 double logLike = 0.0;
253 for (int nt = 0; nt < nTrees; ++nt) {
254 final double popFactor = treesSet.getPopulationFactor(nt);
255 DemographicFunction df = popFactor != 1.0 ?
256 new ScaledDemographic(demogFunction, popFactor) : demogFunction;
257
258 logLike += Coalescent.calculateLogLikelihood(treesSet.getTreeIntervals(nt), df);
259 }
260 return logLike;
261 }
262
263 if (!intervalsKnown) setupIntervals();
264
265 if (demoModel == null) return calculateAnalyticalLogLikelihood();
266
267 double logL = 0.0;
268
269 double currentTime = 0.0;
270
271 DemographicFunction demoFunction = demoModel.getDemographicFunction();
272
273 for (int j = 0; j < intervalCount; j++) {
274
275 logL += calculateIntervalLikelihood(demoFunction, intervals[j], currentTime, lineageCounts[j],
276 getIntervalType(j));
277
278 // insert zero-length coalescent intervals
279 final int diff = getCoalescentEvents(j) - 1;
280 for (int k = 0; k < diff; k++) {
281 logL += calculateIntervalLikelihood(demoFunction, 0.0, currentTime, lineageCounts[j] - k - 1,
282 CoalescentEventType.COALESCENT);
283 }
284
285 currentTime += intervals[j];
286 }
287
288 return logL;
289 }
290
291 private double calculateAnalyticalLogLikelihood() {
292
293 final double lambda = getLambda();
294 final int n = tree.getExternalNodeCount();
295
296 // assumes a 1/theta prior
297 //logLikelihood = Math.log(1.0/Math.pow(lambda,n));
298
299 // assumes a flat prior
300 //double logL = Math.log(1.0/Math.pow(lambda,n-1));
301 //final double logL = - Math.log(Math.pow(lambda,n-1));
302 return -(n - 1) * Math.log(lambda);
303 }
304
305 /**
306 * Returns the likelihood of a given *coalescent* interval
307 */
308 public final double calculateIntervalLikelihood(DemographicFunction demoFunction, double width,
309 double timeOfPrevCoal, int lineageCount) {
310
311 return calculateIntervalLikelihood(demoFunction, width, timeOfPrevCoal, lineageCount,
312 CoalescentEventType.COALESCENT);
313 }
314
315 /**
316 * k - number of lineages
317 * N - population size
318 * kingsman coalescent: interval to next coalescent event x ~ exp(lambda), where lambda = C(k,2) / N
319 * Like(x ; lambda) = lambda * exp(-lambda * x)
320 * so Like(N) = (C(k,2)/N) * exp(- x * C(k,2)/N)
321 * lg(Like(N)) = lg(C(k,2)) - lg(N) -C(k,2) * x/N
322 * <p/>
323 * When N changes over time N = N(t) we have lambda(t) = C(k,2)/N(t) and the likelihood equation is
324 * Like(t) = lambda(t) * exp(- integral_0^t(lambda(x) dx) )
325 * <p/>
326 * lg(Like(t)) = -C(k,2) * integral_0^t(1/N(x) dx) + lg(C(k,2)/N(t))
327 * <p/>
328 * For a sample event, the likelihood is for no event until time t, and is just the first term of the above.
329 *
330 * @param demogFunction the demographic function
331 * @param width the size of the coalescent interval
332 * @param timeOfPrevCoal the time of previous coalescent event (going backwards in time)
333 * @param lineageCount the number of lineages spanning this coalescent interval
334 * @param type the type of coalescent event that this interval is terminated by
335 * @return likelihood of a given interval,coalescent or otherwise
336 */
337 public static double calculateIntervalLikelihood(DemographicFunction demogFunction,
338 double width, double timeOfPrevCoal, int lineageCount,
339 CoalescentEventType type) {
340 final double timeOfThisCoal = width + timeOfPrevCoal;
341
342 final double intervalArea = demogFunction.getIntegral(timeOfPrevCoal, timeOfThisCoal);
343 final double kchoose2 = Binomial.choose2(lineageCount);
344 double like = -kchoose2 * intervalArea;
345
346 switch (type) {
347 case COALESCENT:
348 final double demographic = demogFunction.getLogDemographic(timeOfThisCoal);
349 like += -demographic;
350
351 break;
352 case NEW_SAMPLE:
353 break;
354 }
355
356 return like;
357 }
358
359 /**
360 * @return the exponent of the population size parameter (shape parameter of Gamma distribution) associated to the
361 * likelihood for this interval, coalescent or otherwise
362 */
363 public final double calculateIntervalShapeParameter(DemographicFunction demogFunction,
364 double width, double timeOfPrevCoal, int lineageCount, CoalescentEventType type) {
365 switch (type) {
366 case COALESCENT:
367 return 1.0;
368 case NEW_SAMPLE:
369 return 0.0;
370 }
371 throw new Error("Unknown event found");
372 }
373
374 /**
375 * @return the intensity of coalescences (rate parameter, or inverse scale parameter, of Gamma distribution)
376 * associated to the likelihood for this interval, coalescent or otherwise
377 */
378 public final double calculateIntervalRateParameter(DemographicFunction demogFunction, double width,
379 double timeOfPrevCoal, int lineageCount, CoalescentEventType type) {
380 final double timeOfThisCoal = width + timeOfPrevCoal;
381 final double intervalArea = demogFunction.getIntegral(timeOfPrevCoal, timeOfThisCoal);
382 return Binomial.choose2(lineageCount) * intervalArea;
383 }
384
385
386 /**
387 * @return a factor lambda such that the likelihood can be expressed as
388 * 1/theta^(n-1) * exp(-lambda/theta). This allows theta to be integrated
389 * out analytically. :-)
390 */
391 private double getLambda() {
392 double lambda = 0.0;
393 for (int i = 0; i < getIntervalCount(); i++) {
394 lambda += (intervals[i] * lineageCounts[i]);
395 }
396 lambda /= 2;
397
398 return lambda;
399 }
400
401 /**
402 * Recalculates all the intervals from the tree model.
403 * GL: made public, to give BayesianSkylineGibbsOperator access
404 */
405 public final void setupIntervals() {
406
407 if (intervals == null) {
408 int maxIntervalCount = tree.getNodeCount();
409
410 intervals = new double[maxIntervalCount];
411 lineageCounts = new int[maxIntervalCount];
412 storedIntervals = new double[maxIntervalCount];
413 storedLineageCounts = new int[maxIntervalCount];
414 }
415
416 XTreeIntervals ti = new XTreeIntervals(intervals, lineageCounts);
417 getTreeIntervals(tree, getMRCAOfCoalescent(tree), getExcludedMRCAs(tree), ti);
418 intervalCount = ti.nIntervals;
419
420 intervalsKnown = true;
421 }
422
423
424 /**
425 * Extract coalescent times and tip information into ArrayList times from tree.
426 * Upon return times contain the time of each node in the subtree below top, and at the corrosponding index
427 * of childs is the descendent count for that time.
428 *
429 * @param top the node to start from
430 * @param excludeBelow an optional array of nodes to exclude (corresponding subtrees) from density.
431 * @param tree given tree
432 * @param times array to fill with times
433 * @param childs array to fill with descendents count
434 */
435 private static void collectAllTimes(Tree tree, NodeRef top, NodeRef[] excludeBelow,
436 ArrayList<ComparableDouble> times, ArrayList<Integer> childs) {
437
438 times.add(new ComparableDouble(tree.getNodeHeight(top)));
439 childs.add(tree.getChildCount(top));
440
441 for (int i = 0; i < tree.getChildCount(top); i++) {
442 NodeRef child = tree.getChild(top, i);
443 if (excludeBelow == null) {
444 collectAllTimes(tree, child, excludeBelow, times, childs);
445 } else {
446 // check if this subtree is included in the coalescent density
447 boolean include = true;
448 for (NodeRef anExcludeBelow : excludeBelow) {
449 if (anExcludeBelow.getNumber() == child.getNumber()) {
450 include = false;
451 break;
452 }
453 }
454 if (include)
455 collectAllTimes(tree, child, excludeBelow, times, childs);
456 }
457 }
458 }
459
460 private class XTreeIntervals {
461
462 public XTreeIntervals(double[] intervals, int[] lineageCounts) {
463 this.intervals = intervals;
464 this.lineagesCount = lineageCounts;
465 }
466
467 int nIntervals;
468 final int[] lineagesCount;
469 final double[] intervals;
470
471 }
472
473 private static void getTreeIntervals(Tree tree, NodeRef root, NodeRef[] exclude, XTreeIntervals ti) {
474 double MULTIFURCATION_LIMIT = 1e-9;
475
476 ArrayList<ComparableDouble> times = new ArrayList<ComparableDouble>();
477 ArrayList<Integer> childs = new ArrayList<Integer>();
478 collectAllTimes(tree, root, exclude, times, childs);
479 int[] indices = new int[times.size()];
480
481 HeapSort.sort(times, indices);
482
483 final double[] intervals = ti.intervals;
484 final int[] lineageCounts = ti.lineagesCount;
485
486 // start is the time of the first tip
487 double start = times.get(indices[0]).doubleValue();
488 int numLines = 0;
489 int i = 0;
490 int intervalCount = 0;
491 while (i < times.size()) {
492
493 int lineagesRemoved = 0;
494 int lineagesAdded = 0;
495
496 final double finish = times.get(indices[i]).doubleValue();
497 double next = finish;
498
499 while (Math.abs(next - finish) < MULTIFURCATION_LIMIT) {
500 final int children = childs.get(indices[i]);
501 if (children == 0) {
502 lineagesAdded += 1;
503 } else {
504 lineagesRemoved += (children - 1);
505 }
506 i += 1;
507 if (i == times.size()) break;
508
509 next = times.get(indices[i]).doubleValue();
510 }
511 //System.out.println("time = " + finish + " removed = " + lineagesRemoved + " added = " + lineagesAdded);
512 if (lineagesAdded > 0) {
513
514 if (intervalCount > 0 || ((finish - start) > MULTIFURCATION_LIMIT)) {
515 intervals[intervalCount] = finish - start;
516 lineageCounts[intervalCount] = numLines;
517 intervalCount += 1;
518 }
519
520 start = finish;
521 }
522 // add sample event
523 numLines += lineagesAdded;
524
525 if (lineagesRemoved > 0) {
526
527 intervals[intervalCount] = finish - start;
528 lineageCounts[intervalCount] = numLines;
529 intervalCount += 1;
530 start = finish;
531 }
532 // coalescent event
533 numLines -= lineagesRemoved;
534 }
535
536 ti.nIntervals = intervalCount;
537 }
538
539 /**
540 * @return number of intervals
541 */
542 public final int getIntervalCount() {
543 return intervalCount;
544 }
545
546 /**
547 * Gets an interval.
548 *
549 * @param i index of interval
550 * @return interval length
551 */
552 public final double getInterval(int i) {
553 if (i >= intervalCount) throw new IllegalArgumentException();
554 return intervals[i];
555 }
556
557 /**
558 * Returns the number of uncoalesced lineages within this interval.
559 * Required for s-coalescents, where new lineages are added as
560 * earlier samples are come across.
561 *
562 * @param i lineage index
563 * @return number of uncoalesced lineages within this interval.
564 */
565 public final int getLineageCount(int i) {
566 if (i >= intervalCount) throw new IllegalArgumentException();
567 return lineageCounts[i];
568 }
569
570 /**
571 * @param i interval index
572 * @return the number coalescent events in an interval
573 */
574 public final int getCoalescentEvents(int i) {
575
576 if (i >= intervalCount) throw new IllegalArgumentException();
577 if (i < intervalCount - 1) {
578 return lineageCounts[i] - lineageCounts[i + 1];
579 } else {
580 return lineageCounts[i] - 1;
581 }
582 }
583
584 /**
585 * @param i interval index
586 * @return the type of interval observed.
587 */
588 public final CoalescentEventType getIntervalType(int i) {
589
590 if (i >= intervalCount) throw new IllegalArgumentException();
591 int numEvents = getCoalescentEvents(i);
592
593 if (numEvents > 0) return CoalescentEventType.COALESCENT;
594 else if (numEvents < 0) return CoalescentEventType.NEW_SAMPLE;
595 else return CoalescentEventType.NOTHING;
596 }
597
598 /**
599 * @return total height of the genealogy represented by these
600 * intervals.
601 */
602 public final double getTotalHeight() {
603
604 double height = 0.0;
605 for (int j = 0; j < intervalCount; j++) {
606 height += intervals[j];
607 }
608 return height;
609 }
610
611 /**
612 * @return whether this set of coalescent intervals is fully resolved
613 * (i.e. whether is has exactly one coalescent event in each
614 * subsequent interval)
615 */
616 public final boolean isBinaryCoalescent() {
617 for (int i = 0; i < intervalCount; i++) {
618 if (getCoalescentEvents(i) != 1) return false;
619 }
620
621 return true;
622 }
623
624 /**
625 * @return whether this set of coalescent intervals coalescent only
626 * (i.e. whether is has exactly one or more coalescent event in each
627 * subsequent interval)
628 */
629 public final boolean isCoalescentOnly() {
630 for (int i = 0; i < intervalCount; i++) {
631 if (getCoalescentEvents(i) < 1) return false;
632 }
633 return true;
634 }
635
636 public String toString() {
637 return getId(); // Double.toString(getLogLikelihood());
638 }
639
640 // **************************************************************
641 // Units IMPLEMENTATION
642 // **************************************************************
643
644 /**
645 * Sets the units these coalescent intervals are
646 * measured in.
647 */
648 public final void setUnits(Type u) {
649 demoModel.setUnits(u);
650 }
651
652 /**
653 * Returns the units these coalescent intervals are
654 * measured in.
655 */
656 public final Type getUnits() {
657 return demoModel.getUnits();
658 }
659
660 public final boolean getIntervalsKnown() {
661 return intervalsKnown;
662 }
663
664 // ****************************************************************
665 // Inner classes
666 // ****************************************************************
667
668 public class DeltaStatistic extends Statistic.Abstract {
669
670 public DeltaStatistic() {
671 super("delta");
672 }
673
674 public int getDimension() {
675 return 1;
676 }
677
678 public double getStatisticValue(int i) {
679 throw new RuntimeException("Not implemented");
680 // return IntervalList.Utils.getDelta(intervals);
681 }
682
683 }
684
685 // ****************************************************************
686 // Private and protected stuff
687 // ****************************************************************
688
689 // public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
690 //
691 // public String getParserName() {
692 // return COALESCENT_LIKELIHOOD;
693 // }
694 //
695 // public Object parseXMLObject(XMLObject xo) throws XMLParseException {
696 //
697 // XMLObject cxo = (XMLObject) xo.getChild(MODEL);
698 // DemographicModel demoModel = (DemographicModel) cxo.getChild(DemographicModel.class);
699 //
700 // List<TreeModel> trees = new ArrayList<TreeModel>();
701 // List<Double> popFactors = new ArrayList<Double>();
702 // MultiLociTreeSet treesSet = demoModel instanceof MultiLociTreeSet ? (MultiLociTreeSet)demoModel : null;
703 //
704 // for(int k = 0; k < xo.getChildCount(); ++k) {
705 // final Object child = xo.getChild(k);
706 // if( child instanceof XMLObject ) {
707 // cxo = (XMLObject)child;
708 // if( cxo.getName().equals(POPULATION_TREE) ) {
709 // final TreeModel treeModel = (TreeModel) cxo.getChild(TreeModel.class);
710 // if( treeModel == null ) {
711 // // xml check not done yet?
712 // throw new XMLParseException("Expecting a tree model.");
713 // }
714 // trees.add(treeModel);
715 //
716 // try {
717 // double v = cxo.hasAttribute(POPULATION_FACTOR) ?
718 // cxo.getDoubleAttribute(POPULATION_FACTOR) : 1.0;
719 // popFactors.add(v);
720 // } catch (XMLParseException e) {
721 // throw new XMLParseException(e.getMessage());
722 // }
723 // }
724 // } else if( child instanceof MultiLociTreeSet ) {
725 // treesSet = (MultiLociTreeSet)child;
726 // }
727 // }
728 //
729 // TreeModel treeModel = null;
730 // if( trees.size() == 1 && popFactors.get(0) == 1.0 ) {
731 // treeModel = trees.get(0);
732 // } else if( trees.size() > 1 ) {
733 // treesSet = new MultiLociTreeSet.Default(trees, popFactors);
734 // } else if( !(trees.size() == 0 && treesSet != null) ) {
735 // throw new XMLParseException("error");
736 // }
737 //
738 // if( treeModel != null ) {
739 // return new OldAbstractCoalescentLikelihood(treeModel, demoModel);
740 // }
741 // return new OldAbstractCoalescentLikelihood(treesSet, demoModel);
742 // }
743 //
744 //
745 // //************************************************************************
746 // // AbstractXMLObjectParser implementation
747 // //************************************************************************
748 //
749 // public String getParserDescription() {
750 // return "This element represents the likelihood of the tree given the demographic function.";
751 // }
752 //
753 // public Class getReturnType() {
754 // return Likelihood.class;
755 // }
756 //
757 // public XMLSyntaxRule[] getSyntaxRules() {
758 // return rules;
759 // }
760 //
761 // private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
762 // new ElementRule(MODEL, new XMLSyntaxRule[] {
763 // new ElementRule(DemographicModel.class)
764 // }),
765 // new ElementRule(POPULATION_TREE, new XMLSyntaxRule[] {
766 // AttributeRule.newDoubleRule(POPULATION_FACTOR, true),
767 // new ElementRule(TreeModel.class)
768 // }, 0, Integer.MAX_VALUE),
769 // };
770 // };
771
772 // ****************************************************************
773 // Private and protected stuff
774 // ****************************************************************
775
776 /* public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
777
778 public String getParserName() { return COALESCENT_LIKELIHOOD; }
779
780 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
781
782 DemographicModel demoModel = null;
783 if (xo.hasAttribute(MODEL)) {
784 demoModel = (DemographicModel)xo.getAttribute(MODEL);
785 }
786 TreeModel treeModel = (TreeModel)xo.getAttribute(TREE);
787 return new CoalescentLikelihood(treeModel, demoModel);
788 }
789
790 //************************************************************************
791 // AbstractXMLObjectParser implementation
792 //************************************************************************
793
794 public String getParserDescription() {
795 return "This element represents the likelihood of the tree given the demographic function.";
796 }
797
798 public Class getReturnType() { return Likelihood.class; }
799
800 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
801
802 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
803 new XORRule(
804 new EnumAttributeRule(ANALYTICAL, new String[] { "constant" }),
805 new AttributeRule(MODEL, DemographicModel.class)
806 ),
807 new AttributeRule(TREE, TreeModel.class)
808 };
809 };*/
810
811 /**
812 * The demographic model.
813 */
814 private DemographicModel demoModel = null;
815
816 /**
817 * The tree.
818 */
819 Tree tree = null;
820
821 /**
822 * The widths of the intervals.
823 */
824 double[] intervals;
825 private double[] storedIntervals;
826
827 /**
828 * The number of uncoalesced lineages within a particular interval.
829 */
830 int[] lineageCounts;
831 private int[] storedLineageCounts;
832
833 boolean intervalsKnown = false;
834 private boolean storedIntervalsKnown = false;
835
836 double logLikelihood;
837 private double storedLogLikelihood;
838 boolean likelihoodKnown = false;
839 private boolean storedLikelihoodKnown = false;
840
841 int intervalCount = 0;
842 private int storedIntervalCount = 0;
843 }