Debian Codegraph @

src/dr/evomodel/antigenic/AntigenicSplitPrior.java

raw symbols SCIP
1 /*
2 * AntigenicSplitPrior.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.antigenic;
27
28 import dr.inference.model.*;
29 import dr.util.Author;
30 import dr.util.Citable;
31 import dr.util.Citation;
32 import dr.util.CommonCitations;
33 import dr.xml.*;
34
35 import java.util.*;
36
37 /**
38 * @author Andrew Rambaut
39 * @author Trevor Bedford
40 * @author Marc Suchard
41 * @version $Id$
42 */
43 @Deprecated // drift prior incorporated into antigenicLikelihood
44 public class AntigenicSplitPrior extends AbstractModelLikelihood implements Citable {
45
46 public final static String ANTIGENIC_SPLIT_PRIOR = "antigenicSplitPrior";
47
48 public AntigenicSplitPrior(
49 MatrixParameter locationsParameter,
50 Parameter datesParameter,
51 Parameter regressionSlopeParameter,
52 Parameter regressionPrecisionParameter,
53 Parameter splitTimeParameter,
54 Parameter splitAngleParameter,
55 Parameter splitAssignmentParameter,
56 List<String> topBranchList,
57 List<String> bottomBranchList
58 ) {
59
60 super(ANTIGENIC_SPLIT_PRIOR);
61
62 this.locationsParameter = locationsParameter;
63 addVariable(this.locationsParameter);
64
65 this.datesParameter = datesParameter;
66 addVariable(this.datesParameter);
67
68 dimension = locationsParameter.getParameter(0).getDimension();
69 count = locationsParameter.getParameterCount();
70
71 earliestDate = datesParameter.getParameterValue(0);
72 double latestDate = datesParameter.getParameterValue(0);
73 for (int i=0; i<count; i++) {
74 double date = datesParameter.getParameterValue(i);
75 if (earliestDate > date) {
76 earliestDate = date;
77 }
78 if (latestDate < date) {
79 latestDate = date;
80 }
81 }
82 double timeSpan = latestDate - earliestDate;
83
84 this.regressionSlopeParameter = regressionSlopeParameter;
85 addVariable(regressionSlopeParameter);
86 regressionSlopeParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));
87
88 this.regressionPrecisionParameter = regressionPrecisionParameter;
89 addVariable(regressionPrecisionParameter);
90 regressionPrecisionParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));
91
92 this.splitTimeParameter = splitTimeParameter;
93 addVariable(splitTimeParameter);
94 splitTimeParameter.addBounds(new Parameter.DefaultBounds(50.0, 20.0, 1));
95
96 this.splitAngleParameter = splitAngleParameter;
97 addVariable(splitAngleParameter);
98 splitAngleParameter.addBounds(new Parameter.DefaultBounds(0.5*Math.PI, 0.01, 1));
99
100 this.splitAssignmentParameter = splitAssignmentParameter;
101 addVariable(splitAssignmentParameter);
102 splitAssignmentParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
103
104 String[] labelArray = new String[count];
105 splitAssignmentParameter.setDimension(count);
106
107 topBranchIndices = new int[topBranchList.size()];
108 bottomBranchIndices = new int[bottomBranchList.size()];
109
110 for (int i = 0; i < count; i++) {
111 String iName = datesParameter.getDimensionName(i);
112 labelArray[i] = iName;
113 splitAssignmentParameter.setParameterValueQuietly(i, 0.0);
114
115 // top branch is 1
116 for (int j=0; j < topBranchList.size(); j++) {
117 String jName = topBranchList.get(j);
118 if (jName.equals(iName)) {
119 topBranchIndices[j] = i;
120 splitAssignmentParameter.setParameterValueQuietly(i, 1.0);
121 }
122 }
123
124 // bottom branch is 0
125 for (int j=0; j < bottomBranchList.size(); j++) {
126 String jName = bottomBranchList.get(j);
127 if (jName.equals(iName)) {
128 bottomBranchIndices[j] = i;
129 splitAssignmentParameter.setParameterValueQuietly(i, 0.0);
130 }
131 }
132
133 }
134
135 splitAssignmentParameter.setDimensionNames(labelArray);
136
137 likelihoodKnown = false;
138
139 }
140
141
142 @Override
143 protected void handleModelChangedEvent(Model model, Object object, int index) {
144 }
145
146 @Override
147 protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) {
148 if (variable == locationsParameter || variable == datesParameter
149 || variable == regressionSlopeParameter || variable == regressionPrecisionParameter
150 || variable == splitTimeParameter || variable == splitAngleParameter
151 || variable == splitAssignmentParameter) {
152 likelihoodKnown = false;
153 }
154 }
155
156 @Override
157 protected void storeState() {
158 storedLogLikelihood = logLikelihood;
159 }
160
161 @Override
162 protected void restoreState() {
163 logLikelihood = storedLogLikelihood;
164 likelihoodKnown = false;
165 }
166
167 @Override
168 protected void acceptState() {
169 }
170
171 @Override
172 public Model getModel() {
173 return this;
174 }
175
176 @Override
177 public double getLogLikelihood() {
178 if (!likelihoodKnown) {
179 logLikelihood = computeLogLikelihood();
180 }
181 return logLikelihood;
182 }
183
184 private double computeLogLikelihood() {
185
186 double logLikelihood = 0.0;
187
188 double precision = regressionPrecisionParameter.getParameterValue(0);
189 logLikelihood += (0.5 * Math.log(precision) * count) - (0.5 * precision * sumOfSquaredResiduals());
190 logLikelihood += assignmentLikelihood();
191
192 likelihoodKnown = true;
193 return logLikelihood;
194
195 }
196
197 // set likelihood to negative infinity is an assignment is broken
198 protected double assignmentLikelihood() {
199
200 double logLikelihood = 0.0;
201
202 // these must be 1
203 for (int index : topBranchIndices) {
204 int assignment = (int) splitAssignmentParameter.getParameterValue(index);
205 if (assignment == 0) {
206 // logLikelihood = Double.NEGATIVE_INFINITY;
207 logLikelihood -= 1000;
208 }
209 }
210
211 // these must be 0
212 for (int index : bottomBranchIndices) {
213 int assignment = (int) splitAssignmentParameter.getParameterValue(index);
214 if (assignment == 1) {
215 // logLikelihood = Double.NEGATIVE_INFINITY;
216 logLikelihood -= 1000;
217 }
218 }
219
220 return logLikelihood;
221
222 }
223
224 // go through each location and compute sum of squared residuals from regression line
225 protected double sumOfSquaredResiduals() {
226
227 double ssr = 0.0;
228
229 for (int i=0; i < count; i++) {
230
231 Parameter loc = locationsParameter.getParameter(i);
232
233 double x = loc.getParameterValue(0);
234 double y = expectedAG1(i);
235 ssr += (x - y) * (x - y);
236
237 if (dimension > 1) {
238 x = loc.getParameterValue(1);
239 y = expectedAG2(i);
240 ssr += (x - y) * (x - y);
241 }
242
243 for (int j=2; j < dimension; j++) {
244 x = loc.getParameterValue(j);
245 ssr += x*x;
246 }
247
248 }
249
250 return ssr;
251 }
252
253 // given a location index, calculate the expected AG1 value
254 protected double expectedAG1(int index) {
255
256 double date = datesParameter.getParameterValue(index);
257
258 double ag1 = 0;
259 double time = date - earliestDate;
260 double splitTime = splitTimeParameter.getParameterValue(0);
261 double splitAngle = splitAngleParameter.getParameterValue(0);
262 double beta = regressionSlopeParameter.getParameterValue(0);
263
264 if (time <= splitTime) {
265 ag1 = beta * time;
266 }
267 else {
268 ag1 = (beta * splitTime) + (beta * (time-splitTime) * Math.cos(splitAngle));
269 }
270
271 return ag1;
272
273 }
274
275 // given a location index, calculate the expected AG2 value of top branch
276 protected double expectedAG2(int index) {
277
278 double date = datesParameter.getParameterValue(index);
279 int assignment = (int) splitAssignmentParameter.getParameterValue(index);
280
281 double ag2 = 0;
282 double time = date - earliestDate;
283 double splitTime = splitTimeParameter.getParameterValue(0);
284 double splitAngle = splitAngleParameter.getParameterValue(0);
285 double beta = regressionSlopeParameter.getParameterValue(0);
286
287 if (time <= splitTime) {
288 ag2 = 0;
289 }
290 else {
291 ag2 = beta * (time-splitTime) * Math.sin(splitAngle);
292 }
293
294 if (assignment == 1) {
295 ag2 = -1*ag2;
296 }
297
298 return ag2;
299
300 }
301
302 @Override
303 public void makeDirty() {
304 likelihoodKnown = false;
305 }
306
307 private final int dimension;
308 private final int count;
309 private final Parameter datesParameter;
310 private final MatrixParameter locationsParameter;
311 private final Parameter regressionSlopeParameter;
312 private final Parameter regressionPrecisionParameter;
313 private final Parameter splitTimeParameter;
314 private final Parameter splitAngleParameter;
315 private final Parameter splitAssignmentParameter;
316 private final int[] topBranchIndices;
317 private final int[] bottomBranchIndices;
318
319 private double earliestDate;
320 private double logLikelihood = 0.0;
321 private double storedLogLikelihood = 0.0;
322 private boolean likelihoodKnown = false;
323
324 // **************************************************************
325 // XMLObjectParser
326 // **************************************************************
327
328 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
329
330 public final static String LOCATIONS = "locations";
331 public final static String DATES = "dates";
332 public final static String REGRESSIONSLOPE = "regressionSlope";
333 public final static String REGRESSIONPRECISION = "regressionPrecision";
334 public final static String SPLITTIME = "splitTime";
335 public final static String SPLITANGLE = "splitAngle";
336 public final static String SPLITASSIGNMENT = "splitAssignment";
337 public static final String TOPBRANCH = "topBranch";
338 public static final String BOTTOMBRANCH = "bottomBranch";
339
340 public String getParserName() {
341 return ANTIGENIC_SPLIT_PRIOR;
342 }
343
344 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
345
346 MatrixParameter locationsParameter = (MatrixParameter) xo.getElementFirstChild(LOCATIONS);
347 Parameter datesParameter = (Parameter) xo.getElementFirstChild(DATES);
348 Parameter regressionSlopeParameter = (Parameter) xo.getElementFirstChild(REGRESSIONSLOPE);
349 Parameter regressionPrecisionParameter = (Parameter) xo.getElementFirstChild(REGRESSIONPRECISION);
350 Parameter splitTimeParameter = (Parameter) xo.getElementFirstChild(SPLITTIME);
351 Parameter splitAngleParameter = (Parameter) xo.getElementFirstChild(SPLITANGLE);
352 Parameter splitAssignmentParameter = (Parameter) xo.getElementFirstChild(SPLITASSIGNMENT); List<String> virusLocationStatisticList = null;
353
354 List<String> topBranchList = null;
355 String[] topBranch = xo.getStringArrayAttribute(TOPBRANCH);
356 if (topBranch != null) {
357 topBranchList = Arrays.asList(topBranch);
358 }
359
360 List<String> bottomBranchList = null;
361 String[] bottomBranch = xo.getStringArrayAttribute(BOTTOMBRANCH);
362 if (bottomBranch != null) {
363 bottomBranchList = Arrays.asList(bottomBranch);
364 }
365
366 AntigenicSplitPrior AGDP = new AntigenicSplitPrior(
367 locationsParameter,
368 datesParameter,
369 regressionSlopeParameter,
370 regressionPrecisionParameter,
371 splitTimeParameter,
372 splitAngleParameter,
373 splitAssignmentParameter,
374 topBranchList,
375 bottomBranchList);
376
377 // Logger.getLogger("dr.evomodel").info("Using EvolutionaryCartography model. Please cite:\n" + Utils.getCitationString(AGL));
378
379 return AGDP;
380 }
381
382 //************************************************************************
383 // AbstractXMLObjectParser implementation
384 //************************************************************************
385
386 public String getParserDescription() {
387 return "Provides the likelihood of a vector of coordinates in some multidimensional 'antigenic' space based on an expected relationship with time.";
388 }
389
390 public XMLSyntaxRule[] getSyntaxRules() {
391 return rules;
392 }
393
394 private final XMLSyntaxRule[] rules = {
395 new ElementRule(LOCATIONS, MatrixParameter.class),
396 new ElementRule(DATES, Parameter.class),
397 new ElementRule(REGRESSIONSLOPE, Parameter.class),
398 new ElementRule(REGRESSIONPRECISION, Parameter.class),
399 new ElementRule(SPLITTIME, Parameter.class),
400 new ElementRule(SPLITANGLE, Parameter.class),
401 new ElementRule(SPLITASSIGNMENT, Parameter.class),
402 AttributeRule.newStringArrayRule(TOPBRANCH, true, "A list of virus names to assign to the top branch."),
403 AttributeRule.newStringArrayRule(BOTTOMBRANCH, true, "A list of virus names to assign to the bottom branch.")
404 };
405
406 public Class getReturnType() {
407 return ContinuousAntigenicTraitLikelihood.class;
408 }
409 };
410
411 @Override
412 public Citation.Category getCategory() {
413 return Citation.Category.TRAIT_MODELS;
414 }
415
416 @Override
417 public String getDescription() {
418 return "Bayesian Antigenic Cartography framework";
419 }
420
421 public List<Citation> getCitations() {
422 return Arrays.asList(CommonCitations.BEDFORD_2015_INTEGRATING);
423 }
424 }