Debian Codegraph @

src/dr/evomodel/tree/UniformNodeHeightPrior.java

raw symbols SCIP
1 /*
2 * UniformNodeHeightPrior.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.tree;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodelxml.tree.UniformNodeHeightPriorParser;
31 import dr.inference.model.AbstractModelLikelihood;
32 import dr.inference.model.Model;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Variable;
35 import dr.math.LogTricks;
36 import dr.math.MathUtils;
37 import dr.math.Polynomial;
38
39 import java.util.*;
40 import java.util.logging.Logger;
41
42 //import org.jscience.mathematics.number.Rational;
43
44 /**
45 * Two priors for the tree that are relatively non-informative on the internal node heights given the root height.
46 * The first further assumes that the root height is truncated uniform, see Nicholls, G. & R.D. Gray (2004) for details.
47 * The second allows any marginal specification over the root height given that it is larger than the oldest
48 * sampling time (Bloomquist and Suchard, unpublished).
49 *
50 * @author Alexei Drummond
51 * @author Andrew Rambaut
52 * @author Erik Bloomquist
53 * @author Marc Suchard
54 * @version $Id: UniformRootPrior.java,v 1.10 2005/05/24 20:25:58 rambaut Exp $
55 */
56 public class UniformNodeHeightPrior extends AbstractModelLikelihood {
57
58 // PUBLIC STUFF
59 public static final int MAX_ANALYTIC_TIPS = 60; // TODO Determine this value!
60 public static final int DEFAULT_MC_SAMPLE = 100000;
61
62 private static final double tolerance = 1E-6;
63
64 private int k = 0;
65 private double logFactorialK;
66
67 private double maxRootHeight;
68
69 private boolean isNicholls;
70 private boolean useAnalytic;
71 private boolean useMarginal;
72 private boolean leadingTerm;
73 private int mcSampleSize;
74
75 Set<Double> tipDates = new TreeSet<Double>();
76 List<Double> reversedTipDateList = new ArrayList<Double>();
77 Map<Double, Integer> intervals = new TreeMap<Double, Integer>();
78
79 public UniformNodeHeightPrior(Tree tree, boolean useAnalytic, boolean marginal, boolean leadingTerm) {
80 this(UniformNodeHeightPriorParser.UNIFORM_NODE_HEIGHT_PRIOR, tree, useAnalytic, DEFAULT_MC_SAMPLE, marginal, leadingTerm);
81 }
82
83 public UniformNodeHeightPrior(Tree tree, boolean useAnalytic, int mcSampleSize) {
84 this(UniformNodeHeightPriorParser.UNIFORM_NODE_HEIGHT_PRIOR,tree,useAnalytic,mcSampleSize, false, false);
85 }
86
87 private UniformNodeHeightPrior(String name, Tree tree, boolean useAnalytic, int mcSampleSize,
88 boolean marginal, boolean leadingTerm) {
89
90 super(name);
91
92 this.tree = tree;
93 this.isNicholls = false;
94 this.useAnalytic = useAnalytic;
95 this.useMarginal = marginal;
96 this.mcSampleSize = mcSampleSize;
97 this.leadingTerm = leadingTerm;
98
99 if (tree instanceof TreeModel) {
100 addModel((TreeModel) tree);
101 }
102
103 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
104 double h = tree.getNodeHeight(tree.getExternalNode(i));
105 tipDates.add(h);
106 }
107
108 if (tipDates.size() == 1 || leadingTerm) {
109 // the tips are contemporaneous so these are constant...
110 k = tree.getInternalNodeCount() - 1;
111 Logger.getLogger("dr.evomodel").info("Uniform Node Height Prior, Intervals = " + (k + 1));
112
113 logFactorialK = logFactorial(k);
114 } else {
115 reversedTipDateList.addAll(tipDates);
116 Collections.reverse(reversedTipDateList);
117
118 // Prune out intervals smaller in length than tolerance
119 double intervalStart = tree.getNodeHeight(tree.getRoot());
120 List<Double> pruneDates = new ArrayList<Double>();
121
122 for (Double intervalEnd : reversedTipDateList) {
123 if (intervalStart - intervalEnd < tolerance) {
124 pruneDates.add(intervalStart);
125 }
126 intervalStart = intervalEnd;
127 }
128
129 for (Double date : pruneDates)
130 reversedTipDateList.remove(date);
131
132 if (!useAnalytic) {
133 logLikelihoods = new double[mcSampleSize];
134 drawNodeHeights = new double[tree.getNodeCount()][mcSampleSize];
135 minNodeHeights = new double[tree.getNodeCount()];
136 }
137 }
138
139 // Leading coefficient on tree polynomial is X = (# internal nodes)!
140 // To keep X > 10E-40, should use log-space polynomials for more than ~30 tips
141 if (tree.getExternalNodeCount() < 30) {
142 polynomialType = Polynomial.Type.DOUBLE; // Much faster
143 } else if (tree.getExternalNodeCount() < 45){
144 polynomialType = Polynomial.Type.LOG_DOUBLE;
145 } else {
146 // polynomialType = Polynomial.Type.APDOUBLE;
147 polynomialType = Polynomial.Type.LOG_DOUBLE;
148 }
149 Logger.getLogger("dr.evomodel").info("Using "+polynomialType+" polynomials!");
150
151 }
152
153 public UniformNodeHeightPrior(Tree tree, double maxRootHeight) {
154 this(UniformNodeHeightPriorParser.UNIFORM_NODE_HEIGHT_PRIOR, tree, maxRootHeight);
155 }
156
157 private UniformNodeHeightPrior(String name, Tree tree, double maxRootHeight) {
158
159 super(name);
160
161 this.tree = tree;
162 this.maxRootHeight = maxRootHeight;
163 isNicholls = true;
164 if (tree instanceof TreeModel) {
165 addModel((TreeModel) tree);
166 }
167 }
168
169 UniformNodeHeightPrior(String name) {
170 super(name);
171 }
172
173 // **************************************************************
174 // Extendable methods
175 // **************************************************************
176
177 // **************************************************************
178 // ModelListener IMPLEMENTATION
179 // **************************************************************
180
181 protected final void handleModelChangedEvent(Model model, Object object, int index) {
182
183 likelihoodKnown = false;
184 treePolynomialKnown = false;
185 return;
186
187 // Only set treePolynomialKnown = false when a topology change occurs
188 // Only set likelihoodKnown = false when a topology change occurs or the rootHeight is changed
189
190 // if (model == tree) {
191 // if (object instanceof TreeModel.TreeChangedEvent) {
192 // TreeModel.TreeChangedEvent event = (TreeModel.TreeChangedEvent) object;
193 // if (event.isHeightChanged()) {
194 // if (event.getNode() == tree.getRoot()) {
195 // likelihoodKnown = false;
196 // return;
197 // } // else
198 // return;
199 // }
200 // if (event.isNodeParameterChanged())
201 // return;
202 // // All others are probably tree structure changes
203 // likelihoodKnown = false;
204 // treePolynomialKnown = false;
205 // return;
206 // }
207 // // TODO Why are not all node height changes invoking TreeChangedEvents?
208 // if (object instanceof Parameter.Default) {
209 // Parameter parameter = (Parameter) object;
210 // if (tree.getNodeHeight(tree.getRoot()) == parameter.getParameterValue(index)) {
211 // likelihoodKnown = false;
212 // treePolynomialKnown = false;
213 // return;
214 // }
215 // return;
216 // }
217 // }
218 //
219 // throw new RuntimeException("Unexpected event!");
220 }
221
222 // **************************************************************
223 // VariableListener IMPLEMENTATION
224 // **************************************************************
225
226 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
227 }
228
229 // **************************************************************
230 // Model IMPLEMENTATION
231 // **************************************************************
232
233 /**
234 * Stores the precalculated state: in this case the intervals
235 */
236 protected final void storeState() {
237 storedLikelihoodKnown = likelihoodKnown;
238 storedLogLikelihood = logLikelihood;
239 // storedTreePolynomialKnown = treePolynomialKnown;
240 // if (treePolynomial != null)
241 // storedTreePolynomial = treePolynomial.copy(); // TODO Swap pointers
242 }
243
244 /**
245 * Restores the precalculated state: that is the intervals of the tree.
246 */
247 protected final void restoreState() {
248 likelihoodKnown = storedLikelihoodKnown;
249 logLikelihood = storedLogLikelihood;
250 // treePolynomialKnown = storedTreePolynomialKnown;
251 // treePolynomial = storedTreePolynomial;
252 }
253
254 protected final void acceptState() {
255 } // nothing to do
256
257 // **************************************************************
258 // Likelihood IMPLEMENTATION
259 // **************************************************************
260
261 public final Model getModel() {
262 return this;
263 }
264
265 public double getLogLikelihood() {
266
267 // return calculateLogLikelihood();
268
269 if (!likelihoodKnown) {
270 logLikelihood = calculateLogLikelihood();
271 likelihoodKnown = true;
272 }
273 return logLikelihood;
274 }
275
276 public final void makeDirty() {
277 likelihoodKnown = false;
278 treePolynomialKnown = false;
279 }
280
281 public double calculateLogLikelihood() {
282
283 double rootHeight = tree.getNodeHeight(tree.getRoot());
284
285 if (isNicholls) {
286 int nodeCount = tree.getExternalNodeCount();
287
288 if (rootHeight < 0 || rootHeight > (0.999 * maxRootHeight)) return Double.NEGATIVE_INFINITY;
289
290 // from Nicholls, G. & R.D. Gray (2004)
291 return rootHeight * (2 - nodeCount) - Math.log(maxRootHeight - rootHeight);
292
293 } else {
294 // the Bloomquist & Suchard variant
295 // Let the sampling times and rootHeight specify the boundaries between a fixed number of intervals.
296 // Internal node heights are equally likely to fall in any of these intervals and uniformly distributed
297 // in an interval before sorting (i.e. the intercoalescent times in an interval form a scaled Dirchelet(1,1,\ldots,1)
298 // This is a conditional density on the rootHeight, so it is possible to specify a marginal distribution
299 // on the rootHeight given it is greater than the oldest sampling time.
300 double logLike;
301
302 if (k > 0) { // Also valid for leading-term approximation
303 // the tips are contemporaneous
304 logLike = logFactorialK - (double) k * Math.log(rootHeight);
305
306 // double cutoff = 62;
307 // int count = 0;
308 // for (int i = 0; i < tree.getNodeCount(); i++) {
309 // if (tree.getNodeHeight(tree.getNode(i)) > cutoff) {
310 // count++;
311 //// if (tree.isExternal(tree.getNode(i))) {
312 //// System.err.println("Problem");
313 //// System.exit(-1);
314 //// }
315 // }
316 // }
317 // count -= 1; // ignore root
318 //// System.err.println("c = " + count);
319 // logLike = logFactorial(count) - (double) count * Math.log(rootHeight - cutoff);
320 // logLike = logFactorial(k - count) - (double) (k - count) * Math.log(cutoff);
321
322 } else {
323 // TODO Rewrite description above to discuss this new prior
324 if (useAnalytic) {
325
326 // long startTime1 = System.nanoTime();
327 if (useMarginal) {
328
329 if (!treePolynomialKnown) {
330 // treePolynomial = recursivelyComputePolynomial(tree, tree.getRoot(), polynomialType).getPolynomial();
331 treePolynomials = constructRootPolyonmials(tree,polynomialType); // Each polynomial is of lower degree
332 treePolynomialKnown = true;
333 }
334
335 // logLike = -treePolynomial.logEvaluate(rootHeight);
336 logLike = -treePolynomials[0].logEvaluate(rootHeight) - treePolynomials[1].logEvaluate(rootHeight);
337
338 if (Double.isNaN(logLike)) {
339 // Try using Horner's method
340 // logLike = -treePolynomial.logEvaluateHorner(rootHeight); // TODO this could be causing the problem!!
341 logLike = -treePolynomials[0].logEvaluateHorner(rootHeight) - treePolynomials[1].logEvaluateHorner(rootHeight);
342 if (Double.isNaN(logLike)) {
343 logLike = Double.NEGATIVE_INFINITY;
344 }
345 }
346 } else {
347
348 tmpLogLikelihood = 0;
349 recursivelyComputeDensity(tree, tree.getRoot(), 0);
350 logLike = tmpLogLikelihood;
351
352 }
353
354 // long stopTime1 = System.nanoTime();
355
356
357 } else {
358
359 // long startTime2 = System.nanoTime();
360
361 // Copy over current root height
362 final double[] drawRootHeight = drawNodeHeights[tree.getRoot().getNumber()];
363 Arrays.fill(drawRootHeight,rootHeight); // TODO Only update when rootHeight changes
364
365 // Determine min heights for each node in tree
366 recursivelyFindNodeMinHeights(tree,tree.getRoot()); // TODO Only update when topology changes
367
368 // Simulate from prior
369 Arrays.fill(logLikelihoods,0.0);
370 recursivelyComputeMCIntegral(tree, tree.getRoot(), tree.getRoot().getNumber()); // TODO Only update when topology or rootHeight changes
371
372 // Take average
373 logLike = -LogTricks.logSum(logLikelihoods) + Math.log(mcSampleSize);
374
375 // long stopTime2 = System.nanoTime();
376
377 }
378 }
379
380 assert !Double.isInfinite(logLike) && !Double.isNaN(logLike);
381 return logLike;
382 }
383 }
384
385 // Map<Double,Integer> boxCounts;
386 //
387 // private double recursivelyComputeMarcDensity(Tree tree, NodeRef node, double rootHeight) {
388 // if (tree.isExternal(node))
389 // return tree.getNodeHeight(node);
390 //
391 //// double thisHeight = tree.getNodeHeight(node);
392 //// double thisHeight = rootHeight;
393 // double heightChild1 = recursivelyComputeMarcDensity(tree, tree.getChild(node, 0), rootHeight);
394 // double heightChild2 = recursivelyComputeMarcDensity(tree, tree.getChild(node, 1), rootHeight);
395 // double minHeight = (heightChild1 > heightChild2) ? heightChild1 : heightChild2;
396 //
397 // if (!tree.isRoot(node)) {
398 // double diff = rootHeight - minHeight;
399 // if (diff <= 0)
400 // tmpLogLikelihood = Double.NEGATIVE_INFINITY;
401 // else
402 // tmpLogLikelihood -= Math.log(diff);
403 //
404 // Integer count = boxCounts.get(minHeight);
405 // if (count == null) {
406 // boxCounts.put(minHeight,1);
407 //// System.err.println("new height: "+minHeight);
408 // } else {
409 // boxCounts.put(minHeight,count+1);
410 //// System.err.println("old height: "+minHeight);
411 // }
412 // // TODO Could do the logFactorial right here
413 // } else {
414 // // Do nothing
415 // }
416 // return minHeight;
417 // }
418
419
420 private double recursivelyComputeDensity(Tree tree, NodeRef node, double parentHeight) {
421 if (tree.isExternal(node))
422 return tree.getNodeHeight(node);
423
424 double thisHeight = tree.getNodeHeight(node);
425 double heightChild1 = recursivelyComputeDensity(tree, tree.getChild(node, 0), thisHeight);
426 double heightChild2 = recursivelyComputeDensity(tree, tree.getChild(node, 1), thisHeight);
427 double minHeight = (heightChild1 > heightChild2) ? heightChild1 : heightChild2;
428
429 if (!tree.isRoot(node)) {
430 double diff = parentHeight - minHeight;
431 if (diff <= 0)
432 tmpLogLikelihood = Double.NEGATIVE_INFINITY;
433 else
434 tmpLogLikelihood -= Math.log(diff);
435 // tmpLogLikelihood -= Math.log(parentHeight-minHeight);
436 } else {
437 // Do nothing
438 }
439 return minHeight;
440 }
441
442 private double recursivelyFindNodeMinHeights(Tree tree, NodeRef node) {
443
444 double minHeight;
445
446 if (tree.isExternal(node))
447 minHeight = tree.getNodeHeight(node);
448 else {
449 double minHeightChild0 = recursivelyFindNodeMinHeights(tree, tree.getChild(node,0));
450 double minHeightChild1 = recursivelyFindNodeMinHeights(tree, tree.getChild(node,1));
451 minHeight = (minHeightChild0 > minHeightChild1) ? minHeightChild0 : minHeightChild1;
452 }
453
454 minNodeHeights[node.getNumber()] = minHeight;
455 return minHeight;
456 }
457
458 private void recursivelyComputeMCIntegral(Tree tree, NodeRef node, int parentNodeNumber) {
459
460 if (tree.isExternal(node))
461 return;
462
463 final int nodeNumber = node.getNumber();
464
465 if (!tree.isRoot(node)) {
466
467 final double[] drawParentHeight = drawNodeHeights[parentNodeNumber];
468 final double[] drawThisNodeHeight = drawNodeHeights[nodeNumber];
469 final double minHeight = minNodeHeights[nodeNumber];
470
471 final boolean twoChild = (tree.isExternal(tree.getChild(node,0)) && tree.isExternal(tree.getChild(node,1)));
472
473 for(int i=0; i<mcSampleSize; i++) {
474
475 final double diff = drawParentHeight[i] - minHeight;
476 if (diff <= 0) {
477 logLikelihoods[i] = Double.NEGATIVE_INFINITY;
478 break;
479 }
480
481 if (!twoChild)
482 drawThisNodeHeight[i] = MathUtils.nextDouble() * diff + minHeight;
483
484 logLikelihoods[i] += Math.log(diff);
485 }
486 }
487
488 recursivelyComputeMCIntegral(tree, tree.getChild(node,0), nodeNumber);
489 recursivelyComputeMCIntegral(tree, tree.getChild(node,1), nodeNumber);
490
491 }
492
493 private static final double INV_PRECISION = 10;
494
495 private static double round(double x) {
496 return Math.round(x * INV_PRECISION) / INV_PRECISION;
497 }
498
499 private Polynomial[] constructRootPolyonmials(Tree tree, Polynomial.Type type) {
500 NodeRef root = tree.getRoot();
501 return new Polynomial[] {
502 recursivelyComputePolynomial(tree,tree.getChild(root,0),type).getPolynomial(),
503 recursivelyComputePolynomial(tree,tree.getChild(root,1),type).getPolynomial()
504 };
505 }
506
507 private TipLabeledPolynomial recursivelyComputePolynomial(Tree tree, NodeRef node, Polynomial.Type type) {
508
509 if (tree.isExternal(node)) {
510 double[] value = new double[]{1.0};
511 double height = round(tree.getNodeHeight(node)); // Should help in numerical stability
512 return new TipLabeledPolynomial(value, height, type, true);
513 }
514
515 TipLabeledPolynomial childPolynomial1 = recursivelyComputePolynomial(tree, tree.getChild(node, 0), type);
516 TipLabeledPolynomial childPolynomial2 = recursivelyComputePolynomial(tree, tree.getChild(node, 1), type);
517 // TODO The partialPolynomial below *should* be cached in an efficient reuse scheme (at least for arbitrary precision)
518 TipLabeledPolynomial polynomial = childPolynomial1.multiply(childPolynomial2);
519 // See AbstractTreeLikelihood for an example of how to flag cached polynomials for re-evaluation
520 if (!tree.isRoot(node)) {
521 polynomial = polynomial.integrateWithLowerBound(polynomial.label);
522 }
523
524 return polynomial;
525 }
526
527 // private void test() {
528 //
529 // double[] value = new double[]{2, 0, 2};
530 // Polynomial a = new Polynomial.Double(value);
531 // Polynomial a2 = a.multiply(a);
532 // System.err.println("a :" + a);
533 // System.err.println("a*a: " + a2);
534 // System.err.println("eval :" + a2.evaluate(2));
535 // Polynomial intA = a.integrate();
536 // System.err.println("intA: " + intA);
537 // Polynomial intA2 = a.integrateWithLowerBound(2.0);
538 // System.err.println("intA2: " + intA2);
539 // System.err.println("");
540 //
541 // Polynomial b = new Polynomial.APDouble(value);
542 // System.err.println("b : " + b);
543 // Polynomial b2 = b.multiply(b);
544 // System.err.println("b2 : " + b2);
545 // System.err.println("eval : " + b2.evaluate(2));
546 // Polynomial intB = b.integrate();
547 // System.err.println("intB: " + intB);
548 // Polynomial intB2 = b.integrateWithLowerBound(2.0);
549 // System.err.println("intB2: " + intB2);
550 // System.err.println("");
551 //
552 // Polynomial c = new Polynomial.LogDouble(value);
553 // System.err.println("c : " + c);
554 // Polynomial c2 = c.multiply(c);
555 // System.err.println("c2 : " + c2);
556 // System.err.println("eval : " + c2.evaluate(2));
557 // Polynomial intC = c.integrate();
558 // System.err.println("intC: " + intC);
559 // Polynomial intC2 = c.integrateWithLowerBound(2.0);
560 // System.err.println("intC2: " + intC2);
561 // System.exit(-1);
562 // }
563
564 class TipLabeledPolynomial extends Polynomial.Abstract {
565
566 TipLabeledPolynomial(double[] coefficients, double label, Polynomial.Type type, boolean isTip) {
567 switch (type) {
568 case DOUBLE:
569 polynomial = new Polynomial.Double(coefficients);
570 break;
571 case LOG_DOUBLE:
572 polynomial = new Polynomial.LogDouble(coefficients);
573 break;
574 case BIG_DOUBLE:
575 polynomial = new Polynomial.BigDouble(coefficients);
576 break;
577 // case APDOUBLE: polynomial = new Polynomial.APDouble(coefficients);
578 // break;
579 // case RATIONAL: polynomial = new Polynomial.RationalDouble(coefficients);
580 // break;
581 // case MARCRATIONAL: polynomial = new Polynomial.MarcRational(coefficients);
582 // break;
583
584 default:
585 throw new RuntimeException("Unknown polynomial type");
586 }
587 this.label = label;
588 this.isTip = isTip;
589 }
590
591 TipLabeledPolynomial(Polynomial polynomial, double label, boolean isTip) {
592 this.polynomial = polynomial;
593 this.label = label;
594 this.isTip = isTip;
595 }
596
597 public TipLabeledPolynomial copy() {
598 Polynomial copyPolynomial = polynomial.copy();
599 return new TipLabeledPolynomial(copyPolynomial, this.label, this.isTip);
600 }
601
602 public Polynomial getPolynomial() {
603 return polynomial;
604 }
605
606 public TipLabeledPolynomial multiply(TipLabeledPolynomial b) {
607 double maxLabel = Math.max(label, b.label);
608 return new TipLabeledPolynomial(polynomial.multiply(b), maxLabel, false);
609 }
610
611 public int getDegree() {
612 return polynomial.getDegree();
613 }
614
615 public Polynomial multiply(Polynomial b) {
616 return polynomial.multiply(b);
617 }
618
619 public Polynomial integrate() {
620 return polynomial.integrate();
621 }
622
623 public void expand(double x) {
624 polynomial.expand(x);
625 }
626
627 public double evaluate(double x) {
628 return polynomial.evaluate(x);
629 }
630
631 public double logEvaluate(double x) {
632 return polynomial.logEvaluate(x);
633 }
634
635 public double logEvaluateHorner(double x) {
636 return polynomial.logEvaluateHorner(x);
637 }
638
639 public void setCoefficient(int n, double x) {
640 polynomial.setCoefficient(n, x);
641 }
642
643 public TipLabeledPolynomial integrateWithLowerBound(double bound) {
644 return new TipLabeledPolynomial(polynomial.integrateWithLowerBound(bound), label, isTip);
645 }
646
647 public double getCoefficient(int n) {
648 return polynomial.getCoefficient(n);
649 }
650
651 public String toString() {
652 return polynomial.toString() + " {" + label + "}";
653 }
654
655 public String getCoefficientString(int n) {
656 return polynomial.getCoefficientString(n);
657 }
658
659 private double label;
660 private Polynomial polynomial;
661 private boolean isTip;
662 }
663
664 private double logFactorial(int n) {
665 if (n == 0 || n == 1) {
666 return 0;
667 }
668
669 double rValue = 0;
670
671 for (int i = n; i > 1; i--) {
672 rValue += Math.log(i);
673 }
674 return rValue;
675 }
676
677 // **************************************************************
678 // XMLElement IMPLEMENTATION
679 // **************************************************************
680
681 public org.w3c.dom.Element createElement(org.w3c.dom.Document d) {
682 throw new RuntimeException("createElement not implemented");
683 }
684
685 // ****************************************************************
686 // Private and protected stuff
687 // ****************************************************************
688
689 /**
690 * The tree.
691 */
692 Tree tree = null;
693
694 double logLikelihood;
695 private double storedLogLikelihood;
696 boolean likelihoodKnown = false;
697 private boolean storedLikelihoodKnown = false;
698 private boolean treePolynomialKnown = false;
699 private boolean storedTreePolynomialKnown = false;
700 private Polynomial treePolynomial;
701 private Polynomial[] treePolynomials;
702 private Polynomial storedTreePolynomial;
703
704 private double tmpLogLikelihood;
705 // private Iterator<Polynomial.Type> typeIterator = EnumSet.allOf(Polynomial.Type.class).iterator();
706 // private Polynomial.Type polynomialType = typeIterator.next();
707 private Polynomial.Type polynomialType;
708
709 private double[] logLikelihoods;
710 private double[][] drawNodeHeights;
711 private double[] minNodeHeights;
712
713 }