Debian Codegraph @

src/dr/evomodel/coalescent/ExponentialSkythingLikelihood.java

raw symbols SCIP
1 /*
2 * ExponentialSkythingLikelihood.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.ExponentialBSPGrowth;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.tree.TreeModel;
31 import dr.evomodelxml.coalescent.BayesianSkylineLikelihoodParser;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Statistic;
34 import dr.math.MathUtils;
35
36 import java.util.Date;
37
38 /**
39 * A likelihood function for a sky**** function where exponential growth or decay parameters are what are piecewise
40 * constant
41 *
42 * @version $Id: BayesianSkylineLikelihood.java,v 1.5 2006/03/06 11:26:49 rambaut Exp $
43 *
44 * @author Alexei Drummond
45 */
46 public class ExponentialSkythingLikelihood extends OldAbstractCoalescentLikelihood {
47
48 // PUBLIC STUFF
49
50 public ExponentialSkythingLikelihood(Tree tree,
51 Parameter slopeParameter,
52 Parameter startingPopSize,
53 int type) {
54 super(BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD);
55
56 groupSizeParameter = new Parameter.Default(slopeParameter.getDimension(), 1);
57 popSizeParameter = new Parameter.Default(slopeParameter.getDimension()-1);
58
59 this.slopeParameter = slopeParameter;
60 this.startingPopSize = startingPopSize;
61 int events = tree.getExternalNodeCount() - 1;
62 int paramDim = slopeParameter.getDimension();
63 this.type = type;
64
65 if (paramDim != events) {
66 throw new IllegalArgumentException("There are more groups than coalescent nodes in the tree.");
67 }
68
69 this.tree = tree;
70 if (tree instanceof TreeModel) {
71 addModel((TreeModel)tree);
72 }
73 addVariable(slopeParameter);
74
75 setupIntervals();
76
77 addStatistic(new GroupHeightStatistic());
78 }
79
80 // **************************************************************
81 // Likelihood IMPLEMENTATION
82 // **************************************************************
83
84 /**
85 * Calculates the log likelihood of this set of coalescent intervals,
86 * given a demographic model.
87 */
88 public double getLogLikelihood() {
89
90 setupIntervals();
91
92 double logL = 0.0;
93
94 double currentTime = 0.0;
95
96 int groupIndex=0;
97 int[] groupSizes = getGroupSizes();
98 double[] groupEnds = getGroupHeights();
99
100 int subIndex = 0;
101
102
103 ExponentialBSPGrowth eg = new ExponentialBSPGrowth(Type.YEARS);
104
105 double startGroupPopSize = 0;
106 double endGroupPopSize;
107
108 for (int j = intervalCount - 1; j >=0; j--) {
109 if(j==intervalCount - 1){
110 endGroupPopSize = startingPopSize.getParameterValue(0);
111 } else {
112 endGroupPopSize = startGroupPopSize;
113 }
114 double startTime = currentTime;
115 double endTime = currentTime + intervals[j];
116 startGroupPopSize = endGroupPopSize*Math.exp(slopeParameter.getParameterValue(j)*(endTime - startTime));
117
118
119 eg.setupN1(endGroupPopSize, slopeParameter.getParameterValue(j), endTime - startTime);
120
121 if (getIntervalType(j) == CoalescentEventType.COALESCENT) {
122 subIndex += 1;
123 if (subIndex >= groupSizes[groupIndex]) {
124 groupIndex += 1;
125 subIndex = 0;
126 }
127 }
128
129 logL += calculateIntervalLikelihood(eg, intervals[j], currentTime, lineageCounts[j], getIntervalType(j));
130
131 // insert zero-length coalescent intervals
132 int diff = getCoalescentEvents(j)-1;
133 for (int k = 0; k < diff; k++) {
134 eg.setup(startGroupPopSize, startGroupPopSize, endTime - startTime);
135 logL += calculateIntervalLikelihood(eg, 0.0, currentTime, lineageCounts[j]-k-1,
136 CoalescentEventType.COALESCENT);
137 subIndex += 1;
138 if (subIndex >= groupSizes[groupIndex]) {
139 groupIndex += 1;
140 subIndex = 0;
141 }
142 }
143
144 currentTime += intervals[j];
145 }
146
147 return logL;
148 }
149
150 /**
151 * @return the pop size for the given time. If linear model is being used then this pop size is
152 * interpolated between the two pop sizes at either end of the grouped interval.
153 */
154 public final double getPopSize(int groupIndex, double midTime, double[] groupHeights) {
155
156 return popSizeParameter.getParameterValue(groupIndex);
157
158 }
159
160 /* GAL: made public to give BayesianSkylineGibbsOperator access */
161 public final int[] getGroupSizes() {
162 if (groupSizeParameter.getParameterValue(0) < 2.0) {
163 throw new IllegalArgumentException("For linear model first group size must be >= 2.");
164 }
165
166 int[] groupSizes = new int[groupSizeParameter.getDimension()];
167
168 for (int i = 0; i < groupSizes.length; i++) {
169 double g = groupSizeParameter.getParameterValue(i);
170 if (g != Math.round(g)) {
171 throw new RuntimeException("Group size " + i + " should be integer but found:" + g);
172 }
173 groupSizes[i] = (int)Math.round(g);
174 }
175 return groupSizes;
176 }
177
178 private int getGroupCount() {
179 return groupSizeParameter.getDimension();
180 }
181
182 private int getGroupSize(int groupIndex) {
183 double g = groupSizeParameter.getParameterValue(groupIndex);
184 if (g != Math.round(g)) {
185 throw new RuntimeException("Group size " + groupIndex + " should be integer but found:" + g);
186 }
187 return (int)Math.round(g);
188 }
189
190 /* GAL: made public to give BayesianSkylineGibbsOperator access */
191 public final double[] getGroupHeights() {
192 double[] groupEnds = new double[getGroupCount()];
193
194 double timeEnd = 0.0;
195 int groupIndex = 0;
196 int subIndex = 0;
197 for (int i = 0; i < intervalCount; i++) {
198
199 timeEnd += intervals[i];
200
201 if (getIntervalType(i) == CoalescentEventType.COALESCENT) {
202 subIndex += 1;
203 if (subIndex >= getGroupSize(groupIndex)) {
204 groupEnds[groupIndex] = timeEnd;
205 groupIndex += 1;
206 subIndex = 0;
207 }
208 }
209 }
210 groupEnds[getGroupCount()-1] = timeEnd;
211
212 return groupEnds;
213 }
214
215 private double getGroupHeight(int groupIndex) {
216 return getGroupHeights()[groupIndex];
217 }
218
219 final public int getType() {
220 return type;
221 }
222
223 final public Parameter getPopSizeParameter() {
224 return popSizeParameter;
225 }
226
227 final public Parameter getGroupSizeParameter() {
228 return groupSizeParameter;
229 }
230
231 // ****************************************************************
232 // Implementing Demographic Reconstructor
233 // ****************************************************************
234
235 public String getTitle() {
236 final String title = "Bayesian Skything (exponential)\n" +
237 "Generated " + (new Date()).toString() + " [seed=" + MathUtils.getSeed() + "]";
238 return title;
239 }
240
241 // ****************************************************************
242 // Inner classes
243 // ****************************************************************
244
245 public class GroupHeightStatistic extends Statistic.Abstract {
246
247 public GroupHeightStatistic() {
248 super("groupHeight");
249 }
250
251 public int getDimension() { return getGroupCount(); }
252
253 public double getStatisticValue(int i) {
254 return getGroupHeight(i);
255 }
256
257 }
258
259 // ****************************************************************
260 // Private and protected stuff
261 // ****************************************************************
262
263 /** The demographic model. */
264 private final Parameter slopeParameter;
265
266 private final Parameter popSizeParameter;
267
268 private final Parameter groupSizeParameter;
269
270 private final Parameter startingPopSize;
271
272 private final int type;
273
274 }