Debian Codegraph @

src/dr/evomodel/antigenic/AntigenicDriftPrior.java

raw symbols SCIP
1 /*
2 * AntigenicDriftPrior.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 public class AntigenicDriftPrior extends AbstractModelLikelihood implements Citable {
44
45 public final static String ANTIGENIC_DRIFT_PRIOR = "antigenicDriftPrior";
46
47 public AntigenicDriftPrior(
48 MatrixParameter locationsParameter,
49 Parameter offsetsParameter,
50 Parameter regressionSlopeParameter,
51 Parameter regressionPrecisionParameter
52 ) {
53
54 super(ANTIGENIC_DRIFT_PRIOR);
55
56 this.locationsParameter = locationsParameter;
57 addVariable(this.locationsParameter);
58
59 this.offsetsParameter = offsetsParameter;
60 addVariable(this.offsetsParameter);
61
62 dimension = locationsParameter.getParameter(0).getDimension();
63 count = locationsParameter.getParameterCount();
64
65 this.regressionSlopeParameter = regressionSlopeParameter;
66 addVariable(regressionSlopeParameter);
67 regressionSlopeParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));
68
69 this.regressionPrecisionParameter = regressionPrecisionParameter;
70 addVariable(regressionPrecisionParameter);
71 regressionPrecisionParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));
72
73 likelihoodKnown = false;
74
75 }
76
77
78 @Override
79 protected void handleModelChangedEvent(Model model, Object object, int index) {
80 }
81
82 @Override
83 protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) {
84 if (variable == locationsParameter || variable == offsetsParameter
85 || variable == regressionSlopeParameter || variable == regressionPrecisionParameter) {
86 likelihoodKnown = false;
87 }
88 }
89
90 @Override
91 protected void storeState() {
92 storedLogLikelihood = logLikelihood;
93 }
94
95 @Override
96 protected void restoreState() {
97 logLikelihood = storedLogLikelihood;
98 likelihoodKnown = false;
99 }
100
101 @Override
102 protected void acceptState() {
103 }
104
105 @Override
106 public Model getModel() {
107 return this;
108 }
109
110 @Override
111 public double getLogLikelihood() {
112 if (!likelihoodKnown) {
113 logLikelihood = computeLogLikelihood();
114 }
115 return logLikelihood;
116 }
117
118 private double computeLogLikelihood() {
119
120 double precision = regressionPrecisionParameter.getParameterValue(0);
121 double logLikelihood = (0.5 * Math.log(precision) * count) - (0.5 * precision * sumOfSquaredResiduals());
122 likelihoodKnown = true;
123 return logLikelihood;
124
125 }
126
127 // go through each location and compute sum of squared residuals from regression line
128 protected double sumOfSquaredResiduals() {
129
130 double ssr = 0.0;
131
132 for (int i = 0; i < count; i++) {
133
134 Parameter loc = locationsParameter.getParameter(i);
135 double offset = offsetsParameter.getParameterValue(i);
136 double beta = regressionSlopeParameter.getParameterValue(0);
137 double x = loc.getParameterValue(0);
138
139 double y = offset * beta;
140
141 ssr += (x - y) * (x - y);
142
143 for (int j = 1; j < dimension; j++) {
144 x = loc.getParameterValue(j);
145 ssr += x * x;
146 }
147
148 }
149
150 return ssr;
151 }
152
153 @Override
154 public void makeDirty() {
155 likelihoodKnown = false;
156 }
157
158 private final int dimension;
159 private final int count;
160 private final Parameter offsetsParameter;
161 private final MatrixParameter locationsParameter;
162 private final Parameter regressionSlopeParameter;
163 private final Parameter regressionPrecisionParameter;
164
165 private double logLikelihood = 0.0;
166 private double storedLogLikelihood = 0.0;
167 private boolean likelihoodKnown = false;
168
169 // **************************************************************
170 // XMLObjectParser
171 // **************************************************************
172
173 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
174
175 public final static String LOCATIONS = "locations";
176 public final static String OFFSETS = "offsets";
177 public final static String REGRESSION_SLOPE = "regressionSlope";
178 public final static String REGRESSION_PRECISION = "regressionPrecision";
179
180 public String getParserName() {
181 return ANTIGENIC_DRIFT_PRIOR;
182 }
183
184 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
185
186 MatrixParameter locationsParameter = (MatrixParameter) xo.getElementFirstChild(LOCATIONS);
187 Parameter offsetsParameter = (Parameter) xo.getElementFirstChild(OFFSETS);
188 Parameter regressionSlopeParameter = (Parameter) xo.getElementFirstChild(REGRESSION_SLOPE);
189 Parameter regressionPrecisionParameter = (Parameter) xo.getElementFirstChild(REGRESSION_PRECISION);
190
191 AntigenicDriftPrior AGDP = new AntigenicDriftPrior(
192 locationsParameter,
193 offsetsParameter,
194 regressionSlopeParameter,
195 regressionPrecisionParameter);
196
197 return AGDP;
198 }
199
200 //************************************************************************
201 // AbstractXMLObjectParser implementation
202 //************************************************************************
203
204 public String getParserDescription() {
205 return "Provides the likelihood of a vector of coordinates in some multidimensional 'antigenic' space based on an expected relationship with time.";
206 }
207
208 public XMLSyntaxRule[] getSyntaxRules() {
209 return rules;
210 }
211
212 private final XMLSyntaxRule[] rules = {
213 new ElementRule(LOCATIONS, MatrixParameter.class),
214 new ElementRule(OFFSETS, Parameter.class),
215 new ElementRule(REGRESSION_SLOPE, Parameter.class),
216 new ElementRule(REGRESSION_PRECISION, Parameter.class)
217 };
218
219 public Class getReturnType() {
220 return AntigenicDriftPrior.class;
221 }
222 };
223
224 @Override
225 public Citation.Category getCategory() {
226 return Citation.Category.TRAIT_MODELS;
227 }
228
229 @Override
230 public String getDescription() {
231 return "Bayesian Antigenic Cartography framework";
232 }
233
234 public List<Citation> getCitations() {
235 return Arrays.asList(CommonCitations.BEDFORD_2015_INTEGRATING);
236 }
237 }