/* * OldAbstractCoalescentLikelihood.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.evomodel.coalescent; import dr.evolution.coalescent.Coalescent; import dr.evolution.coalescent.DemographicFunction; import dr.evolution.coalescent.ScaledDemographic; import dr.evolution.tree.NodeRef; import dr.evolution.tree.Tree; import dr.evolution.util.Units; import dr.evomodel.tree.TreeModel; import dr.evomodelxml.coalescent.CoalescentLikelihoodParser; import dr.inference.model.*; import dr.math.Binomial; import dr.util.ComparableDouble; import dr.util.HeapSort; import java.util.ArrayList; /** * Forms a base class for a number of coalescent likelihood calculators. *
* This was the former 'CoalescentLikelihood' which, as of BEAST v1.4.x was replaced as the * standard coalescent likelihood by 'NewCoalescentLikelihood'. As this class is used as a base * by a number of other classes (i.e., BayesianSkylineLikelihood), I have made this class abstract * and removed the parser. * * NewCoalescentLikelihood is now CoalesecentLikelihood (it's parser was installed as the default * 'coalescentLikelihood' anyway). * * @author Andrew Rambaut * @author Alexei Drummond * @version $Id: CoalescentLikelihood.java,v 1.43 2006/07/28 11:27:32 rambaut Exp $ */ public class OldAbstractCoalescentLikelihood extends AbstractModelLikelihood implements Units { // PUBLIC STUFF //public static final String COALESCENT_LIKELIHOOD = "oldcoalescentLikelihood"; // public static final String ANALYTICAL = "analytical"; // public static final String MODEL = "model"; // // public static final String POPULATION_TREE = "populationTree"; // public static final String POPULATION_FACTOR = "factor"; protected MultiLociTreeSet treesSet = null; public enum CoalescentEventType { /** * Denotes an interval after which a coalescent event is observed * (i.e. the number of lineages is smaller in the next interval) */ COALESCENT, /** * Denotes an interval at the end of which a new sample addition is * observed (i.e. the number of lineages is larger in the next interval). */ NEW_SAMPLE, /** * Denotes an interval at the end of which nothing is * observed (i.e. the number of lineages is the same in the next interval). */ NOTHING } public OldAbstractCoalescentLikelihood(Tree tree, DemographicModel demoModel) { this(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, tree, demoModel, true); } public OldAbstractCoalescentLikelihood(MultiLociTreeSet treesSet, DemographicModel demoModel) { super(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD); this.demoModel = demoModel; this.tree = null; this.treesSet = treesSet; if (demoModel != null) { addModel(demoModel); } for (int nt = 0; nt < treesSet.nLoci(); ++nt) { final Tree t = treesSet.getTree(nt); if (t instanceof Model) { addModel((Model) t); } } } public OldAbstractCoalescentLikelihood(String name, Tree tree, DemographicModel demoModel, boolean setupIntervals) { super(name); this.demoModel = demoModel; this.tree = tree; if (tree instanceof TreeModel) { addModel((TreeModel) tree); } if (demoModel != null) { addModel(demoModel); } if (setupIntervals) setupIntervals(); addStatistic(new DeltaStatistic()); } OldAbstractCoalescentLikelihood(String name) { super(name); } // ************************************************************** // Extendable methods // ************************************************************** /** * @param tree given tree * @return the node ref of the MRCA of this coalescent prior in the given tree (i.e. root of tree) */ public NodeRef getMRCAOfCoalescent(Tree tree) { return tree.getRoot(); } /** * @param tree given tree * @return an array of noderefs that represent the MRCAs of subtrees to exclude from coalescent prior. * May return null if no subtrees should be excluded. */ public NodeRef[] getExcludedMRCAs(Tree tree) { return null; } // ************************************************************** // ModelListener IMPLEMENTATION // ************************************************************** protected void handleModelChangedEvent(Model model, Object object, int index) { if (model == tree) { // treeModel has changed so recalculate the intervals intervalsKnown = false; } else { // demoModel has changed so we don't need to recalculate the intervals } likelihoodKnown = false; } // ************************************************************** // VariableListener IMPLEMENTATION // ************************************************************** // No parameters to respond to protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { } // ************************************************************** // Model IMPLEMENTATION // ************************************************************** /** * Stores the precalculated state: in this case the intervals */ protected void storeState() { if (tree != null) { System.arraycopy(intervals, 0, storedIntervals, 0, intervals.length); System.arraycopy(lineageCounts, 0, storedLineageCounts, 0, lineageCounts.length); storedIntervalsKnown = intervalsKnown; storedIntervalCount = intervalCount; storedLikelihoodKnown = likelihoodKnown; } else if (treesSet != null) { treesSet.storeTheState(); } storedLogLikelihood = logLikelihood; } /** * Restores the precalculated state: that is the intervals of the tree. */ protected void restoreState() { if (tree != null) { System.arraycopy(storedIntervals, 0, intervals, 0, storedIntervals.length); System.arraycopy(storedLineageCounts, 0, lineageCounts, 0, storedLineageCounts.length); intervalsKnown = storedIntervalsKnown; intervalCount = storedIntervalCount; } else if (treesSet != null) { treesSet.restoreTheState(); } likelihoodKnown = storedLikelihoodKnown; logLikelihood = storedLogLikelihood; if (!intervalsKnown) { likelihoodKnown = false; } } protected final void acceptState() { } // nothing to do // ************************************************************** // Likelihood IMPLEMENTATION // ************************************************************** public final Model getModel() { return this; } public double getLogLikelihood() { if (!likelihoodKnown) { logLikelihood = calculateLogLikelihood(); likelihoodKnown = true; } return logLikelihood; } public final void makeDirty() { likelihoodKnown = false; intervalsKnown = false; } /** * @return the log likelihood of this set of coalescent intervals, * given a demographic model */ public double calculateLogLikelihood() { if (treesSet != null) { final int nTrees = treesSet.nLoci(); final DemographicFunction demogFunction = demoModel.getDemographicFunction(); double logLike = 0.0; for (int nt = 0; nt < nTrees; ++nt) { final double popFactor = treesSet.getPopulationFactor(nt); DemographicFunction df = popFactor != 1.0 ? new ScaledDemographic(demogFunction, popFactor) : demogFunction; logLike += Coalescent.calculateLogLikelihood(treesSet.getTreeIntervals(nt), df); } return logLike; } if (!intervalsKnown) setupIntervals(); if (demoModel == null) return calculateAnalyticalLogLikelihood(); double logL = 0.0; double currentTime = 0.0; DemographicFunction demoFunction = demoModel.getDemographicFunction(); for (int j = 0; j < intervalCount; j++) { logL += calculateIntervalLikelihood(demoFunction, intervals[j], currentTime, lineageCounts[j], getIntervalType(j)); // insert zero-length coalescent intervals final int diff = getCoalescentEvents(j) - 1; for (int k = 0; k < diff; k++) { logL += calculateIntervalLikelihood(demoFunction, 0.0, currentTime, lineageCounts[j] - k - 1, CoalescentEventType.COALESCENT); } currentTime += intervals[j]; } return logL; } private double calculateAnalyticalLogLikelihood() { final double lambda = getLambda(); final int n = tree.getExternalNodeCount(); // assumes a 1/theta prior //logLikelihood = Math.log(1.0/Math.pow(lambda,n)); // assumes a flat prior //double logL = Math.log(1.0/Math.pow(lambda,n-1)); //final double logL = - Math.log(Math.pow(lambda,n-1)); return -(n - 1) * Math.log(lambda); } /** * Returns the likelihood of a given *coalescent* interval */ public final double calculateIntervalLikelihood(DemographicFunction demoFunction, double width, double timeOfPrevCoal, int lineageCount) { return calculateIntervalLikelihood(demoFunction, width, timeOfPrevCoal, lineageCount, CoalescentEventType.COALESCENT); } /** * k - number of lineages * N - population size * kingsman coalescent: interval to next coalescent event x ~ exp(lambda), where lambda = C(k,2) / N * Like(x ; lambda) = lambda * exp(-lambda * x) * so Like(N) = (C(k,2)/N) * exp(- x * C(k,2)/N) * lg(Like(N)) = lg(C(k,2)) - lg(N) -C(k,2) * x/N * * When N changes over time N = N(t) we have lambda(t) = C(k,2)/N(t) and the likelihood equation is * Like(t) = lambda(t) * exp(- integral_0^t(lambda(x) dx) ) * * lg(Like(t)) = -C(k,2) * integral_0^t(1/N(x) dx) + lg(C(k,2)/N(t)) * * For a sample event, the likelihood is for no event until time t, and is just the first term of the above. * * @param demogFunction the demographic function * @param width the size of the coalescent interval * @param timeOfPrevCoal the time of previous coalescent event (going backwards in time) * @param lineageCount the number of lineages spanning this coalescent interval * @param type the type of coalescent event that this interval is terminated by * @return likelihood of a given interval,coalescent or otherwise */ public static double calculateIntervalLikelihood(DemographicFunction demogFunction, double width, double timeOfPrevCoal, int lineageCount, CoalescentEventType type) { final double timeOfThisCoal = width + timeOfPrevCoal; final double intervalArea = demogFunction.getIntegral(timeOfPrevCoal, timeOfThisCoal); final double kchoose2 = Binomial.choose2(lineageCount); double like = -kchoose2 * intervalArea; switch (type) { case COALESCENT: final double demographic = demogFunction.getLogDemographic(timeOfThisCoal); like += -demographic; break; case NEW_SAMPLE: break; } return like; } /** * @return the exponent of the population size parameter (shape parameter of Gamma distribution) associated to the * likelihood for this interval, coalescent or otherwise */ public final double calculateIntervalShapeParameter(DemographicFunction demogFunction, double width, double timeOfPrevCoal, int lineageCount, CoalescentEventType type) { switch (type) { case COALESCENT: return 1.0; case NEW_SAMPLE: return 0.0; } throw new Error("Unknown event found"); } /** * @return the intensity of coalescences (rate parameter, or inverse scale parameter, of Gamma distribution) * associated to the likelihood for this interval, coalescent or otherwise */ public final double calculateIntervalRateParameter(DemographicFunction demogFunction, double width, double timeOfPrevCoal, int lineageCount, CoalescentEventType type) { final double timeOfThisCoal = width + timeOfPrevCoal; final double intervalArea = demogFunction.getIntegral(timeOfPrevCoal, timeOfThisCoal); return Binomial.choose2(lineageCount) * intervalArea; } /** * @return a factor lambda such that the likelihood can be expressed as * 1/theta^(n-1) * exp(-lambda/theta). This allows theta to be integrated * out analytically. :-) */ private double getLambda() { double lambda = 0.0; for (int i = 0; i < getIntervalCount(); i++) { lambda += (intervals[i] * lineageCounts[i]); } lambda /= 2; return lambda; } /** * Recalculates all the intervals from the tree model. * GL: made public, to give BayesianSkylineGibbsOperator access */ public final void setupIntervals() { if (intervals == null) { int maxIntervalCount = tree.getNodeCount(); intervals = new double[maxIntervalCount]; lineageCounts = new int[maxIntervalCount]; storedIntervals = new double[maxIntervalCount]; storedLineageCounts = new int[maxIntervalCount]; } XTreeIntervals ti = new XTreeIntervals(intervals, lineageCounts); getTreeIntervals(tree, getMRCAOfCoalescent(tree), getExcludedMRCAs(tree), ti); intervalCount = ti.nIntervals; intervalsKnown = true; } /** * Extract coalescent times and tip information into ArrayList times from tree. * Upon return times contain the time of each node in the subtree below top, and at the corrosponding index * of childs is the descendent count for that time. * * @param top the node to start from * @param excludeBelow an optional array of nodes to exclude (corresponding subtrees) from density. * @param tree given tree * @param times array to fill with times * @param childs array to fill with descendents count */ private static void collectAllTimes(Tree tree, NodeRef top, NodeRef[] excludeBelow, ArrayList