| 1 | /* |
| 2 | * CoalescentConstantLikelihood.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 jebl.math.Binomial; |
| 29 | import dr.evolution.coalescent.TreeIntervals; |
| 30 | import dr.evomodel.tree.TreeModel; |
| 31 | import dr.inference.model.Likelihood; |
| 32 | |
| 33 | /** |
| 34 | * Calculates the coalescent constant for a given tree(Model). |
| 35 | * |
| 36 | * @author Guy Baele |
| 37 | */ |
| 38 | public class CoalescentConstantLikelihood extends Likelihood.Abstract { |
| 39 | |
| 40 | public final static boolean COALESCENT_EVENTS_ONLY = false; |
| 41 | |
| 42 | private TreeModel treeModel; |
| 43 | private TreeIntervals intervals; |
| 44 | |
| 45 | // PUBLIC STUFF |
| 46 | public CoalescentConstantLikelihood(TreeModel treeModel) { |
| 47 | |
| 48 | super(treeModel); |
| 49 | this.treeModel = treeModel; |
| 50 | this.intervals = new TreeIntervals(treeModel); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | // ************************************************************** |
| 55 | // Likelihood IMPLEMENTATION |
| 56 | // ************************************************************** |
| 57 | |
| 58 | /** |
| 59 | * Calculates the coalescent constant given the treeModel. |
| 60 | */ |
| 61 | public double calculateLogLikelihood() { |
| 62 | |
| 63 | intervals.setIntervalsUnknown(); |
| 64 | final int nIntervals = intervals.getIntervalCount(); |
| 65 | //System.err.println(treeModel); |
| 66 | //System.err.println("Interval count: " + nIntervals); |
| 67 | double logPDF = 0.0; |
| 68 | for (int i = 0; i < nIntervals; i++) { |
| 69 | //System.err.println("Lineage count " + i + ": " + intervals.getLineageCount(i)); |
| 70 | //System.err.println("Interval time " + i + ": " + intervals.getIntervalTime(i)); |
| 71 | //System.err.println("Coalescent event " + i + ": " + intervals.getCoalescentEvents(i)); |
| 72 | if (COALESCENT_EVENTS_ONLY) { |
| 73 | if (intervals.getCoalescentEvents(i) > 0) { |
| 74 | logPDF += Math.log(Binomial.choose2(intervals.getLineageCount(i))); |
| 75 | } |
| 76 | } else if (intervals.getLineageCount(i) > 2) { |
| 77 | logPDF += Math.log(Binomial.choose2(intervals.getLineageCount(i))); |
| 78 | //System.err.println("PDF: " + Binomial.choose2(intervals.getLineageCount(i))); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | //START TEST CONTEMPORANEOUS |
| 83 | /*double test = 0.0; |
| 84 | for (int i = 5; i > 2; --i) { |
| 85 | test += Math.log(Binomial.choose2(i)); |
| 86 | } |
| 87 | if (test != logPDF) { |
| 88 | System.err.println(test + " " + logPDF); |
| 89 | System.exit(0); |
| 90 | }*/ |
| 91 | //END TEST CONTEMPORANEOUS |
| 92 | |
| 93 | //System.err.println("logPDF = " + (-logPDF) + "\n"); |
| 94 | |
| 95 | return -logPDF; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | } |