Debian Codegraph @

src/dr/inference/regression/SelfControlledCaseSeries.java

raw symbols SCIP
1 /*
2 * SelfControlledCaseSeries.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.inference.regression;
27
28 import dr.inference.model.AbstractModelLikelihood;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.xml.*;
33
34 import java.util.*;
35
36 /**
37 * @author Marc Suchard
38 * @author Trevor Shaddox
39 */
40 public class SelfControlledCaseSeries extends AbstractModelLikelihood {
41
42 public static final String SCCS_NAME = "selfControlledCaseSeries";
43 public static final String FILE_NAME = "fileName";
44 public static final String BETA = "beta";
45 public static final String PRECISION = "precision";
46
47 public SelfControlledCaseSeries(String name, String fileName, Parameter beta, Parameter precision) {
48 super(name);
49
50 regressionInterface = RegressionJNIWrapper.loadLibrary();
51
52 // Load data and find mode
53 instance = regressionInterface.loadData(fileName);
54 regressionInterface.setPriorType(instance, RegressionJNIWrapper.NORMAL_PRIOR);
55
56 this.precision = precision;
57 setPrecision();
58 precisionChanged = true;
59
60 // Set beta to mode
61 final int dim = regressionInterface.getBetaSize(instance);
62 if (dim != beta.getDimension()) {
63 beta.setDimension(dim);
64 }
65
66 // Start beta at mode (given precision)
67 this.beta = beta;
68 double[] mode = getMode();
69 for (int i = 0; i < beta.getDimension(); ++i) {
70 beta.setParameterValue(i, mode[i]);
71 }
72 logSCCSLikelihood = regressionInterface.getLogLikelihood(instance);
73 logSCCSPrior = regressionInterface.getLogPrior(instance);
74 betaChanged = false; // Internal state is at mode
75
76 addVariable(beta);
77 addVariable(precision);
78 }
79
80 private void setPrecision() {
81 regressionInterface.setHyperprior(instance, 1.0 / precision.getParameterValue(0));
82 }
83
84 public double[] getMode() {
85 if (precisionChanged) {
86 setPrecision();
87 mode = null;
88 }
89 if (mode == null) {
90 regressionInterface.findMode(instance);
91 mode = new double[beta.getDimension()];
92 for (int i = 0; i < beta.getDimension(); ++i) {
93 mode[i] = regressionInterface.getBeta(instance, i);
94 }
95 betaChanged = true; // Internal beta-state is at mode, not betaParameter
96 // betaFlag.clear();
97 newMode = true;
98 // System.err.println("A");
99 if (DEBUG_MODE) {
100 System.err.println("Recomputed mode!");
101 }
102 }
103 double[] rtn = new double[mode.length];
104 System.arraycopy(mode, 0, rtn, 0, mode.length);
105 return rtn;
106 }
107
108 @Override
109 protected void handleModelChangedEvent(Model model, Object object, int index) {
110 // Do nothing
111 }
112
113 /**
114 * This method is called whenever a parameter is changed.
115 * <p/>
116 * It is strongly recommended that the model component sets a "dirty" flag and does no
117 * further calculations. Recalculation is typically done when the model component is asked for
118 * some information that requires them. This mechanism is 'lazy' so that this method
119 * can be safely called multiple times with minimal computational cost.
120 */
121 @Override
122 protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) {
123 if (variable == beta) {
124 betaChanged = true;
125 if (type == Variable.ChangeType.ALL_VALUES_CHANGED) {
126 betaFlag.clear();
127 } else {
128 betaFlag.add(index);
129 }
130 } else if (variable == precision) {
131 precisionChanged = true;
132 } else {
133 throw new IllegalArgumentException("Unknown variable in SCCS");
134 }
135 }
136
137 /**
138 * Additional state information, outside of the sub-model is stored by this call.
139 */
140 @Override
141 protected void storeState() {
142 storedLogSCCSLikelihood = logSCCSLikelihood;
143 storedLogSCCSPrior = logSCCSPrior;
144
145 storedBetaChanged = betaChanged;
146 storedPrecisionChanged = precisionChanged;
147
148 storedPrecision = precision.getParameterValue(0);
149 }
150
151 /**
152 * After this call the model is guaranteed to have returned its extra state information to
153 * the values coinciding with the last storeState call.
154 * Sub-models are handled automatically and do not need to be considered in this method.
155 */
156 @Override
157 protected void restoreState() {
158 logSCCSLikelihood = storedLogSCCSLikelihood;
159 logSCCSPrior = storedLogSCCSPrior;
160
161 betaChanged = storedBetaChanged;
162 precisionChanged = storedPrecisionChanged;
163 }
164
165 /**
166 * This call specifies that the current state is accept. Most models will not need to do anything.
167 * Sub-models are handled automatically and do not need to be considered in this method.
168 */
169 @Override
170 protected void acceptState() {
171 if (storedPrecision != precision.getParameterValue(0)) {
172 mode = null; // Accepted new precision state; mode has moved
173 }
174 }
175
176 /**
177 * Get the model.
178 *
179 * @return the model.
180 */
181 public Model getModel() {
182 return this;
183 }
184
185 /**
186 * Get the log likelihood.
187 *
188 * @return the log likelihood.
189 */
190 public double getLogLikelihood() {
191 return calculateLogLikelihood();
192 }
193
194 private double calculateLogLikelihood() {
195
196 if (betaChanged) {
197 if (betaFlag.isEmpty() || newMode) {
198 regressionInterface.setBeta(instance, beta.getParameterValues());
199 newMode = false;
200 } else {
201 while (!betaFlag.isEmpty()) {
202 final int index = betaFlag.remove();
203 regressionInterface.setBeta(instance, index, beta.getParameterValue(index));
204 }
205 }
206 }
207
208 if (precisionChanged) {
209 setPrecision();
210 }
211
212 if (betaChanged) {
213 logSCCSLikelihood = regressionInterface.getLogLikelihood(instance);
214 }
215
216 if (betaChanged || precisionChanged) {
217 logSCCSPrior = regressionInterface.getLogPrior(instance);
218 }
219
220 betaChanged = false;
221 precisionChanged = false;
222
223 double logLike = logSCCSLikelihood + logSCCSPrior;
224
225 if (DEBUG_LAZY) {
226 double checkLike = regressionInterface.getLogLikelihood(instance);
227 double checkPrior = regressionInterface.getLogPrior(instance);
228
229 double check = checkLike + checkPrior;
230 if (check != logLike) {
231 System.err.println("Error in internal state in calculateLogLikelihood()");
232 System.err.println(checkLike + " " + logSCCSLikelihood + " d: " + (checkLike - logSCCSLikelihood));
233 System.err.println(checkPrior + " " + logSCCSPrior + " d: " + (checkPrior - logSCCSPrior));
234 System.err.println(betaChanged + " " + precisionChanged);
235 System.exit(-1);
236 }
237 }
238
239 return logLike;
240 }
241
242 /**
243 * Forces a complete recalculation of the likelihood next time getLikelihood is called
244 */
245 public void makeDirty() {
246 betaChanged = true;
247 newMode = true;
248 precisionChanged = true;
249 regressionInterface.makeDirty(instance);
250 }
251
252 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
253
254 public String getParserName() {
255 return SCCS_NAME;
256 }
257
258 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
259
260 String fileName = xo.getStringAttribute(FILE_NAME);
261 Parameter beta = (Parameter) xo.getElementFirstChild(BETA);
262 Parameter precision = (Parameter) xo.getElementFirstChild(PRECISION);
263
264 return new SelfControlledCaseSeries(xo.getId(), fileName, beta, precision);
265 }
266
267 public String getParserDescription() {
268 return "Self-controlled case series design.";
269 }
270
271 public Class getReturnType() {
272 return SelfControlledCaseSeries.class;
273 }
274
275 public XMLSyntaxRule[] getSyntaxRules() {
276 return rules;
277 }
278
279 private final XMLSyntaxRule[] rules = {
280 AttributeRule.newStringRule(FILE_NAME),
281 new ElementRule(BETA, Parameter.class),
282 new ElementRule(PRECISION, Parameter.class),
283 };
284 };
285
286 private final RegressionJNIWrapper regressionInterface;
287 private final int instance;
288 private final Parameter beta;
289 private final Parameter precision;
290
291 private double logSCCSLikelihood;
292 private double logSCCSPrior;
293 private double storedLogSCCSLikelihood;
294 private double storedLogSCCSPrior;
295
296 private boolean betaChanged;
297 private boolean precisionChanged;
298 private boolean storedBetaChanged;
299 private boolean storedPrecisionChanged;
300
301 // private boolean[] betaFlag;
302 // private boolean betaFlagAll;
303 private Queue<Integer> betaFlag = new LinkedList<Integer>();
304 private boolean newMode = false;
305
306 private double[] mode = null;
307 private double storedPrecision;
308
309 private static final boolean DEBUG_MODE = false;
310 private static final boolean DEBUG_LAZY = false;
311
312 }