Debian Codegraph @

src/dr/evomodel/coalescent/BNPRSamplingLikelihood.java

raw symbols SCIP
1 package dr.evomodel.coalescent;
2
3 import dr.evolution.coalescent.DemographicFunction;
4 import dr.evolution.io.NewickImporter;
5 import dr.evolution.tree.NodeRef;
6 import dr.evolution.tree.Tree;
7 import dr.evolution.util.Units;
8 import dr.evomodel.tree.TreeModel;
9 import dr.evomodelxml.coalescent.BNPRSamplingLikelihoodParser;
10 import dr.inference.model.*;
11 import dr.util.Author;
12 import dr.util.Citable;
13 import dr.util.Citation;
14
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.List;
18
19 /**
20 * Created by Mike Karcher on 3/9/17.
21 */
22 public class BNPRSamplingLikelihood extends AbstractModelLikelihood implements Citable {
23 private boolean likelihoodKnown = false;
24 private boolean storedLikelihoodKnown = false;
25 private double logLikelihood;
26 private double storedLogLikelihood;
27
28 private boolean samplingTimesKnown = false;
29 private boolean storedSamplingTimesKnown = false;
30 private double[] samplingTimes;
31 private double[] storedSamplingTimes;
32 private double[] logPopSizes;
33 private double[] storedLogPopSizes;
34
35 private double[] epochWidths = null;
36 private double[] midpoints = null;
37
38 private Tree tree;
39 private int numSamples;
40 private Parameter betas;
41 private DemographicModel population;
42 private MatrixParameter covariates;
43 private MatrixParameter powerCovariates;
44 private Parameter powerBetas;
45
46 private BNPRSamplingLikelihood(Tree tree, Parameter betas, DemographicModel population, double[] epochWidths,
47 MatrixParameter covariates) {
48 this(tree, betas, population, epochWidths, covariates, null, null);
49 }
50
51 public BNPRSamplingLikelihood(Tree tree, Parameter betas, DemographicModel population, double[] epochWidths,
52 MatrixParameter covariates, MatrixParameter powerCovariates, Parameter powerBetas) {
53 super(BNPRSamplingLikelihoodParser.SAMPLING_LIKELIHOOD);
54
55 this.tree = tree;
56 this.betas = betas;
57 this.population = population;
58 this.covariates = covariates;
59 this.powerCovariates = powerCovariates;
60 this.powerBetas = powerBetas;
61
62 this.likelihoodKnown = false;
63 this.samplingTimesKnown = false;
64
65 this.numSamples = tree.getExternalNodeCount();
66 samplingTimes = new double[numSamples];
67 storedSamplingTimes = new double[numSamples];
68 logPopSizes = new double[numSamples];
69 storedLogPopSizes = new double[numSamples];
70
71 if (epochWidths != null) {
72 setEpochs(epochWidths);
73 }
74
75 if (tree instanceof TreeModel) {
76 addModel((TreeModel) tree);
77 }
78
79 addModel(population);
80
81 if (this.betas != null) {
82 addVariable(this.betas);
83 }
84
85 if (this.powerBetas != null) {
86 addVariable(this.powerBetas);
87 }
88
89 if (this.covariates != null) {
90 addVariable(this.covariates);
91 }
92
93 if (this.powerCovariates != null) {
94 addVariable(this.powerCovariates);
95 }
96
97 setupSamplingTimes();
98 }
99
100 private void setEpochs(double[] epochWidths) {
101 this.epochWidths = epochWidths;
102
103 if (epochWidths == null) {
104 this.midpoints = null;
105 } else {
106 this.midpoints = new double[epochWidths.length + 1];
107 double start = 0;
108 for (int i = 0; i < epochWidths.length; i++) {
109 this.midpoints[i] = start + epochWidths[i] / 2.0;
110 start += epochWidths[i];
111 }
112 this.midpoints[epochWidths.length] = start + epochWidths[epochWidths.length - 1] / 2.0;
113 }
114 }
115
116 private void setupSamplingTimes() {
117
118 for (int i = 0; i < numSamples; i++) {
119 NodeRef node = tree.getExternalNode(i);
120 samplingTimes[i] = tree.getNodeHeight(node);
121 }
122
123 Arrays.sort(samplingTimes);
124
125 samplingTimesKnown = true;
126 }
127
128 @Override
129 public Model getModel() {
130 return this;
131 }
132
133 @Override
134 public double getLogLikelihood() {
135 if (!samplingTimesKnown) {
136 setupSamplingTimes();
137 }
138
139 if (!likelihoodKnown) {
140 logLikelihood = calculateLogLikelihood();
141 likelihoodKnown = true;
142 }
143
144 return logLikelihood;
145 }
146
147 public double calculateLogLikelihood() {
148 double minSample = samplingTimes[0];
149 double maxSample = samplingTimes[numSamples-1];
150
151 double beta0 = betas.getParameterValue(0);
152 double beta1 = betas.getParameterValue(1);
153
154 double logLik = numSamples * beta0; // Start with beta0 part of likelihood.
155
156 DemographicFunction f = population.getDemographicFunction();
157
158 // Gather log-population sizes to facilitate likelihood calculation.
159 // Separated to make cut-paste into a new setupLogPopSizes() function easy.
160 for (int i = 0; i < numSamples; i++) {
161 logPopSizes[i] = f.getLogDemographic(samplingTimes[i]);
162 }
163
164 double eventLogLik = 0.0;
165 // Calculate the event component of the inhomogeneous Poisson process log-likelihood
166 for (int i = 0; i < numSamples; i++) {
167 eventLogLik += beta1 * logPopSizes[i];
168
169 if (this.powerCovariates != null) {
170 for (int j = 0; j < powerBetas.getDimension(); j++) {
171 eventLogLik += powerBetas.getParameterValue(j) * evaluatePiecewise(this.epochWidths, powerCovariates.getColumnValues(j), samplingTimes[i]) * logPopSizes[i];
172 }
173 }
174
175 if (this.covariates != null) {
176 for (int j = 2; j < betas.getDimension(); j++) {
177 eventLogLik += betas.getParameterValue(j) * evaluatePiecewise(this.epochWidths, covariates.getColumnValues(j-2), samplingTimes[i]);
178 }
179 }
180 }
181
182 logLik += eventLogLik;
183
184 double integralLogLik = 0.0;
185
186 double[] fBeta = new double[this.midpoints.length];
187 double fBetaLog;
188 for (int i = 0; i < fBeta.length; i++) {
189 fBetaLog = beta1 * f.getLogDemographic(this.midpoints[i]);
190
191 if (this.powerCovariates != null) {
192 for (int j = 0; j < powerBetas.getDimension(); j++) {
193 fBetaLog += powerBetas.getParameterValue(j) * this.powerCovariates.getParameterValue(i, j) * f.getLogDemographic(this.midpoints[i]);
194 }
195 }
196
197 if (this.covariates != null) {
198 for (int j = 2; j < betas.getDimension(); j++) {
199 fBetaLog += betas.getParameterValue(j) * this.covariates.getParameterValue(i, j - 2);
200 }
201 }
202
203 fBeta[i] = Math.exp(fBetaLog);
204 }
205 integralLogLik -= Math.exp(beta0) * integratePiecewise(this.epochWidths, fBeta, minSample, maxSample);
206
207 logLik += integralLogLik;
208
209 return logLik;
210 }
211
212 private static double evaluatePiecewise(double[] widths, double[] heights, double t) {
213 double[] widths2 = new double[widths.length + 1];
214 System.arraycopy(widths, 0, widths2, 0, widths.length);
215 widths2[widths.length] = Double.POSITIVE_INFINITY;
216
217 int i = 0;
218 while (t > widths2[i]) {
219 t -= widths2[i];
220 i++;
221 }
222
223 return heights[i];
224 }
225
226 private static double integratePiecewise(double[] widths, double[] heights, double start, double end) {
227 double[] widths2 = new double[widths.length + 1];
228 System.arraycopy(widths, 0, widths2, 0, widths.length);
229 widths2[widths.length] = Double.POSITIVE_INFINITY;
230
231 double result = 0.0;
232 int i = 0;
233 while (start > widths2[i]) {
234 start -= widths2[i];
235 end -= widths2[i];
236 i++;
237 }
238
239 if (end < widths2[i]) {
240 result = (end - start) * heights[i];
241 } else {
242 result += (widths2[i] - start) * heights[i];
243 end -= widths2[i];
244 i++;
245
246 while (end > widths2[i]) {
247 result += widths2[i] * heights[i];
248 end -= widths2[i];
249 i++;
250 }
251
252 if (end > 0.0) {
253 result += end * heights[i];
254 }
255 }
256
257 return result;
258 }
259
260 @Override
261 public void makeDirty() {
262 likelihoodKnown = false;
263 samplingTimesKnown = false;
264 }
265
266 @Override
267 protected void handleModelChangedEvent(Model model, Object object, int index) {
268 makeDirty();
269 }
270
271 @Override
272 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
273 makeDirty();
274 }
275
276 @Override
277 protected void storeState() {
278 // copy the sampling times into the storedSamplingTimes
279 System.arraycopy(samplingTimes, 0, storedSamplingTimes, 0, samplingTimes.length);
280
281 storedSamplingTimesKnown = samplingTimesKnown;
282 storedLikelihoodKnown = likelihoodKnown;
283 storedLogLikelihood = logLikelihood;
284
285 System.arraycopy(logPopSizes, 0, storedLogPopSizes, 0, logPopSizes.length);
286 }
287
288 @Override
289 protected void restoreState() {
290 // copy the sampling times into the storedSamplingTimes
291 System.arraycopy(storedSamplingTimes, 0, samplingTimes, 0, storedSamplingTimes.length);
292
293 samplingTimesKnown = storedSamplingTimesKnown;
294 likelihoodKnown = storedLikelihoodKnown;
295 logLikelihood = storedLogLikelihood;
296
297 System.arraycopy(storedLogPopSizes, 0, logPopSizes, 0, storedLogPopSizes.length);
298 }
299
300 @Override
301 protected void acceptState() {
302 // nothing to do
303 }
304
305 public String toString() {
306 return Double.toString(logLikelihood);
307 }
308
309 @Override
310 public Citation.Category getCategory() {
311 return Citation.Category.PRIOR_MODELS;
312 }
313
314 @Override
315 public String getDescription() {
316 return "Bayesian non-parametric preferential sampling";
317 }
318
319 @Override
320 public List<Citation> getCitations() {
321 return Collections.singletonList(CITATION);
322 }
323
324 public static Citation CITATION = new Citation(
325 new Author[]{
326 new Author("MD", "Karcher"),
327 new Author("MA", "Suchard"),
328 new Author("G", "Dudas"),
329 new Author( "T", "Bedford"),
330 new Author("VN","Minin")
331 },
332 // "Estimating effective population size changes from preferentially sampled genetic sequences",
333 Citation.Status.IN_PREPARATION
334 );
335
336 public static void main(String[] args) throws Exception {
337
338 NewickImporter importer = new NewickImporter("(((t93_0:0.1823217653,t94_0:0.08702282874):0.853414946,t95_0:0.5591826007):16.46923208,((t96_0:4.730080227,(t97_0:3.316833,((t98_0:1.117259784,((t84_0:0.02918861288,((((t77_0:0.6269405877,t78_0:0.1883673111):0.1035487414,(((((t58_0:0.366605532,(t59_0:0.2902223473,t60_0:0.09330035051):0.05814249819):0.2687774288,(t61_0:0.03909732613,((((t31_0:0.4925976152,(t1_0:1.543243834,(t2_0:0.3786992111,t3_0:0.286843186):0.9369069124):1.84806174):1.388380979,(((((t4_0:1.57060332,t5_0:1.565578151):0.9340116213,(t6_0:0.6129297388,t7_0:0.5252059248):1.883951667):1.298791879,(t8_0:3.070277733,((t9_0:2.351705269,t10_0:2.318654989):0.03364809509,t11_0:1.986711821):0.6207718581):0.558736892):0.1439273897,((((t12_0:0.3565815494,t13_0:0.2420866743):1.027508236,t14_0:1.128497322):0.1217011969,t15_0:1.211013892):0.5793893699,((t16_0:0.4746827902,t17_0:0.4609704827):1.049116131,t18_0:1.497407856):0.1530705075):1.000179818):0.0008681768441,(t19_0:2.204632212,((t20_0:1.857557731,(t21_0:1.005514944,t22_0:0.9291487706):0.6230458901):0.1109842512,t23_0:1.582286036):0.1992671825):0.4433270097):0.3343580536):0.6454071651,(((t24_0:0.815171935,t25_0:0.6918964743):0.2147852872,(t26_0:0.1228387289,t27_0:0.05280374254):0.5882964168):2.025084596,(t28_0:0.2705161616,t29_0:0.2642467551):2.230464942):0.1103642803):6.832809449,(((t30_0:0.9906878227,t32_0:0.1856034526):1.311909106,t33_0:0.3458898346):6.842038256,t34_0:4.353598435):0.2887890018):0.414867716):0.05823058913):0.3120716399,(t35_0:1.623697863,((t36_0:1.029267152,t37_0:0.9940333973):0.2630810985,t38_0:1.077299678):0.2899006266):2.324485801):1.915577302,(t39_0:1.748484212,t40_0:1.720294263):3.606542332):0.6429878862,(t41_0:5.650860772,((t42_0:0.8033637796,t43_0:0.3493332566):3.763664014,(t44_0:0.8778820702,(t45_0:0.5483027215,t46_0:0.4562980629):0.1491010534):3.197472308):0.8000617359):0.2857177615):0.4150148533):0.7376635867,(((t47_0:0.3446780656,t48_0:0.3130649187):3.810586451,(((((t49_0:0.9994210908,t50_0:0.8953272412):0.5705160932,(t51_0:0.7994882815,t52_0:0.7164389879):0.6268954443):0.34752079,t53_0:1.598405188):0.2494469601,(t54_0:1.508633144,t55_0:1.473482952):0.05756852234):0.6596007127,(t56_0:1.076711525,t57_0:1.070680706):1.008030326):1.241809834):0.7439478609,((t62_0:0.1842275742,t63_0:0.1339199725):1.294985145,(t64_0:1.040418148,t65_0:0.7556259274):0.3408415617):1.72091551):0.5986199428):2.461710024,(((t66_0:3.108881856,((t67_0:2.000699305,((t68_0:1.382538601,t69_0:1.246844479):0.249533307,t70_0:1.366941817):0.176205828):0.5708242903,(t71_0:0.9490874836,t72_0:0.7691535035):0.8078785817):0.475066817):0.06683807466,((t73_0:0.7617022066,t74_0:0.7482687036):0.1047558487,t75_0:0.7677691921):0.8692021164):0.1592569402,t76_0:1.722131386):2.262393917):2.572913635):2.595279242,(t79_0:3.600210891,(t80_0:0.7070684518,t81_0:0.3253173205):2.684283986):2.510617438):0.5734740762):1.936674132,t82_0:6.475760804):0.2353584188):1.216419756):0.9696225718,(((t83_0:0.4830428417,t85_0:0.214507183):4.223119782,(((t86_0:1.283242236,t87_0:1.278786271):1.710456,t88_0:2.295501437):0.6485241096,(t89_0:1.619761915,t90_0:1.575819491):1.230444387):0.7762895827):1.668960635,(t91_0:4.403654576,t92_0:4.367902389):0.7940511151):1.277021867):11.16032274)");
339 Tree tree = importer.importNextTree();
340 Parameter betas = new Parameter.Default(new double[] {2.430198, 1.121209, 18.963120});
341 Parameter N0 = new Parameter.Default(new double[] {0.957477459020635, 2.43878447702397, 1.16178606305887, 2.47803465457009, 3.59425875085121, 5.99484118979476, 8.45281123199342, 5.32731462686442, 2.14530385537217, 1.32838970512892, 1.57682364008761, 2.06977561883114, 0.975318782259623, 0.729019262672561, 0.528122712339147, 1.73826551939609, 1.59115234812228, 0.991767268014354, 1.19544144384689, 4.2431612093277, 9.68837537548171, 21.7809958450516, 72.9542238498992, 67.4709298097791, 17.5930802539968, 17.9641115502897, 20.2230985692042, 18.5751760066478, 15.316828425915, 53.4144639187011, 20.6589204576208, 11.4695515805863, 25.1358546005526, 41.1222703999958, 36.0749583093113, 43.5582733013315, 191.509377345225, 60.2774566116462, 13.6461065289052, 29.0150510257721, 11.8060344563918, 14.0771159483723, 17.2536924114547, 15.3833722037801, 46.48760509832, 41.881067569086, 27.6941667792898, 55.4922963887114, 253.986328605972, 305.304267851333, 243.190770701089, 144.095172595153, 172.765640994598, 174.182113284746, 454.771916114004, 514.94845166669, 266.355413273477, 2039.89326970745, 972.728248184194, 305.635176732534, 227.134976673311, 143.297859235514, 74.5162610231541, 45.3481762741632, 32.1527697118794, 7.6456190280646, 12.1418530975389, 9.48239531687115, 7.37481430050462, 4.49432342829254, 14.6077380251641, 13.5858667415758, 11.2001057413539, 4.12650576804321, 3.83168028086946, 5.56705828329053, 7.86450990980968, 5.6493737041167, 4.76307833501312, 6.45317992085796, 9.78615502142029, 13.8294443012538, 22.5127519861243, 28.9164141530354, 85.3528130029434, 34.8658623017608, 47.1574370857575, 40.2205642235078, 57.50963654004, 65.3719036601996, 44.8775886163862, 17.6951867195527, 9.07043638586854, 18.663530289391, 15.6125065593161, 2.12354416497279, 4.96867966440677, 3.63537271117703, 2.09626570198215, 1.03533797751651});
342 double[] epochLengths = new double[] {0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317, 0.404760913356317};
343 Parameter covariate1 = new Parameter.Default(new double[]{-0.00505951141695401, -0.015178534250862, -0.0252975570847698, -0.0354165799186778, -0.0455356027525857, -0.0556546255864937, -0.0657736484204016, -0.0758926712543096, -0.0860116940882175, -0.0961307169221254, -0.106249739756033, -0.116368762589941, -0.126487785423849, -0.136606808257757, -0.146725831091665, -0.156844853925573, -0.166963876759481, -0.177082899593389, -0.187201922427297, -0.197320945261205, -0.207439968095113, -0.217558990929021, -0.227678013762929, -0.237797036596836, -0.247916059430744, -0.258035082264652, -0.26815410509856, -0.278273127932468, -0.288392150766376, -0.298511173600284, -0.308630196434192, -0.3187492192681, -0.328868242102008, -0.338987264935916, -0.349106287769824, -0.359225310603732, -0.36934433343764, -0.379463356271548, -0.389582379105455, -0.399701401939363, -0.409820424773271, -0.419939447607179, -0.430058470441087, -0.440177493274995, -0.450296516108903, -0.460415538942811, -0.470534561776719, -0.480653584610627, -0.490772607444535, -0.500891630278443, -0.511010653112351, -0.521129675946259, -0.531248698780167, -0.541367721614075, -0.551486744447982, -0.56160576728189, -0.571724790115798, -0.581843812949706, -0.591962835783614, -0.602081858617522, -0.61220088145143, -0.622319904285338, -0.632438927119246, -0.642557949953154, -0.652676972787062, -0.66279599562097, -0.672915018454878, -0.683034041288786, -0.693153064122694, -0.703272086956601, -0.713391109790509, -0.723510132624417, -0.733629155458325, -0.743748178292233, -0.753867201126141, -0.763986223960049, -0.774105246793957, -0.784224269627865, -0.794343292461773, -0.804462315295681, -0.814581338129589, -0.824700360963497, -0.834819383797405, -0.844938406631313, -0.85505742946522, -0.865176452299128, -0.875295475133036, -0.885414497966944, -0.895533520800852, -0.90565254363476, -0.915771566468668, -0.925890589302576, -0.936009612136484, -0.946128634970392, -0.9562476578043, -0.966366680638208, -0.976485703472116, -0.986604726306024, -0.996723749139932, -1.00684277197384});
344 MatrixParameter covariates = new MatrixParameter("Covariates", new Parameter[]{covariate1});
345
346 DemographicModel Ne = new PiecewisePopulationModel("Ne(t)", N0, epochLengths, false, Units.Type.DAYS);
347
348 BNPRSamplingLikelihood samplingLikelihood = new BNPRSamplingLikelihood(tree, betas, Ne, epochLengths, covariates);
349
350 System.out.println("Sampling likelihood: " + samplingLikelihood.getLogLikelihood());
351 }
352 }