| 1 | /* |
| 2 | * ExponentialProductLikelihood.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.evomodel.tree.TreeModel; |
| 29 | import dr.inference.model.Likelihood; |
| 30 | |
| 31 | /** |
| 32 | * Calculates a product of exponential densities and exponential tail probabilities. |
| 33 | * |
| 34 | * @author Guy Baele |
| 35 | */ |
| 36 | |
| 37 | public class ExponentialProductLikelihood extends Likelihood.Abstract { |
| 38 | |
| 39 | private TreeModel treeModel; |
| 40 | private double logPopSize; |
| 41 | |
| 42 | //make sure to provide a log(popSize) |
| 43 | public ExponentialProductLikelihood(TreeModel treeModel, double logPopSize) { |
| 44 | super(treeModel); |
| 45 | this.treeModel = treeModel; |
| 46 | this.logPopSize = logPopSize; |
| 47 | } |
| 48 | |
| 49 | public double calculateLogLikelihood() { |
| 50 | |
| 51 | //System.err.println(treeModel); |
| 52 | |
| 53 | double logPDF = 0.0; |
| 54 | |
| 55 | //System.err.println("log(popSize) = " + this.popSize); |
| 56 | |
| 57 | CoalescentTreeIntervalStatistic ctis = new CoalescentTreeIntervalStatistic(treeModel); |
| 58 | |
| 59 | for (int i = 0; i < ctis.getDimension(); i++) { |
| 60 | |
| 61 | int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2; |
| 62 | double branchLength = ctis.getStatisticValue(i); |
| 63 | |
| 64 | //System.err.println("combinations = " + combinations); |
| 65 | //System.err.println("branchLength = " + branchLength); |
| 66 | |
| 67 | //System.err.println(ctis.getLineageCount(i)); |
| 68 | |
| 69 | //single-lineage intervals are not counted |
| 70 | if (ctis.getLineageCount(i) != 1) { |
| 71 | |
| 72 | //System.err.println(i + " -> lineage count: " + ctis.getLineageCount(i)); |
| 73 | |
| 74 | if (i == ctis.getDimension()-1) { |
| 75 | //coalescent event at root: exponential density |
| 76 | //System.err.print("coalescent event at root: "); |
| 77 | double logContribution = -logPopSize - combinations*branchLength*Math.exp(-logPopSize); |
| 78 | logPDF += logContribution; |
| 79 | //System.err.println(logContribution); |
| 80 | } else if (ctis.getLineageCount(i) > ctis.getLineageCount(i+1)) { |
| 81 | //coalescent event: exponential density |
| 82 | //System.err.print("coalescent event (not at root): "); |
| 83 | double logContribution = -logPopSize - combinations*branchLength*Math.exp(-logPopSize); |
| 84 | logPDF += logContribution; |
| 85 | //System.err.println(logContribution); |
| 86 | } else { |
| 87 | //sampling event: exponential tail probability |
| 88 | //System.err.print("sampling event: "); |
| 89 | double logContribution = -combinations*branchLength*Math.exp(-logPopSize); |
| 90 | logPDF += logContribution; |
| 91 | //System.err.println(logContribution); |
| 92 | } |
| 93 | |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | //System.err.println("expoLike = " + logPDF + "\n"); |
| 99 | |
| 100 | return logPDF; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Overridden to always return false. |
| 106 | */ |
| 107 | protected boolean getLikelihoodKnown() { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | } |