Debian Codegraph @

src/dr/evomodel/coalescent/SkylineLikelihood.java

raw symbols SCIP
1 /*
2 * SkylineLikelihood.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.ConstantPopulation;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.util.Units;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodelxml.coalescent.CoalescentLikelihoodParser;
33 import dr.inference.model.Likelihood;
34 import dr.inference.model.Parameter;
35 import dr.util.Author;
36 import dr.util.Citable;
37 import dr.util.Citation;
38 import dr.xml.*;
39
40 import java.util.Collections;
41 import java.util.List;
42
43 /**
44 * A likelihood function for the coalescent. Takes a tree and a exponential markov model.
45 * *
46 * @version $Id: SkylineLikelihood.java,v 1.3 2004/10/01 22:40:04 alexei Exp $
47 *
48 * @author Alexei Drummond
49 */
50 public class SkylineLikelihood extends OldAbstractCoalescentLikelihood implements Citable {
51
52 // PUBLIC STUFF
53
54 public static final String SKYLINE_LIKELIHOOD = "skyLineLikelihood";
55 public static final String POPULATION_SIZES = "populationSizes";
56
57 public SkylineLikelihood(Tree tree, Parameter popSizeParameter) {
58 super(SKYLINE_LIKELIHOOD);
59
60 this.popSizeParameter = popSizeParameter;
61 int tips = tree.getExternalNodeCount();
62 int params = popSizeParameter.getDimension();
63 if (tips - params != 1) {
64 throw new IllegalArgumentException("Number of tips (" + tips + ") must be one greater than number of pop sizes (" + params + ")");
65 }
66
67 this.tree = tree;
68 if (tree instanceof TreeModel) {
69 addModel((TreeModel)tree);
70 }
71 addVariable(popSizeParameter);
72 setupIntervals();
73
74 addStatistic(new DeltaStatistic());
75 }
76
77 // **************************************************************
78 // Likelihood IMPLEMENTATION
79 // **************************************************************
80
81 /**
82 * Calculates the log likelihood of this set of coalescent intervals,
83 * given a demographic model.
84 */
85 public double calculateLogLikelihood() {
86
87 if (!intervalsKnown) setupIntervals();
88
89 double logL = 0.0;
90
91 double currentTime = 0.0;
92
93 int popIndex=0;
94
95 ConstantPopulation cp = new ConstantPopulation(Units.Type.YEARS);
96
97 for (int j = 0; j < intervalCount; j++) {
98
99 cp.setN0(popSizeParameter.getParameterValue(popIndex));
100 if (getIntervalType(j) == CoalescentEventType.COALESCENT) {
101 popIndex += 1;
102 }
103
104 logL += calculateIntervalLikelihood(cp, intervals[j], currentTime, lineageCounts[j], getIntervalType(j));
105
106 // insert zero-length coalescent intervals
107 int diff = getCoalescentEvents(j)-1;
108 for (int k = 0; k < diff; k++) {
109 cp.setN0(popSizeParameter.getParameterValue(popIndex));
110 logL += calculateIntervalLikelihood(cp, 0.0, currentTime, lineageCounts[j]-k-1,
111 CoalescentEventType.COALESCENT);
112 popIndex += 1;
113 }
114
115 currentTime += intervals[j];
116
117
118 }
119
120 return logL;
121 }
122
123 // ****************************************************************
124 // Private and protected stuff
125 // ****************************************************************
126
127 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
128
129 public String getParserName() { return SKYLINE_LIKELIHOOD; }
130
131 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
132
133 XMLObject cxo = (XMLObject)xo.getChild(POPULATION_SIZES);
134 Parameter param = (Parameter)cxo.getChild(Parameter.class);
135
136 cxo = (XMLObject)xo.getChild(CoalescentLikelihoodParser.POPULATION_TREE);
137 TreeModel treeModel = (TreeModel)cxo.getChild(TreeModel.class);
138
139 return new SkylineLikelihood(treeModel, param);
140 }
141
142 //************************************************************************
143 // AbstractXMLObjectParser implementation
144 //************************************************************************
145
146 public String getParserDescription() {
147 return "This element represents the likelihood of the tree given the population size vector.";
148 }
149
150 public Class getReturnType() { return Likelihood.class; }
151
152 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
153
154 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
155 new ElementRule(POPULATION_SIZES, new XMLSyntaxRule[] {
156 new ElementRule(Parameter.class)
157 }),
158 new ElementRule(CoalescentLikelihoodParser.POPULATION_TREE, new XMLSyntaxRule[] {
159 new ElementRule(TreeModel.class)
160 }),
161 };
162 };
163
164 /** The demographic model. */
165 Parameter popSizeParameter = null;
166
167 @Override
168 public Citation.Category getCategory() {
169 return Citation.Category.TREE_PRIORS;
170 }
171
172 @Override
173 public String getDescription() {
174 return "Bayesian Skyline Coalescent";
175 }
176
177 @Override
178 public List<Citation> getCitations() {
179 return Collections.singletonList(CITATION);
180 }
181
182 public static Citation CITATION = new Citation(
183 new Author[]{
184 new Author("AJ", "Drummond"),
185 new Author("A", "Rambaut"),
186 new Author("B", "Shapiro"),
187 new Author("OG", "Pybus")
188 },
189 "Bayesian coalescent inference of past population dynamics from molecular sequences",
190 2005,
191 "Mol Biol Evol",
192 22, 1185, 1192
193 );
194 }