Debian Codegraph @

src/dr/evomodel/epidemiology/SIRModel.java

raw symbols SCIP
1 /*
2 * SIRModel.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.epidemiology;
27
28 import dr.evolution.coalescent.DemographicFunction;
29 import dr.evomodel.coalescent.DemographicModel;
30 import dr.inference.loggers.LogColumn;
31 import dr.inference.loggers.NumberColumn;
32 import dr.inference.model.*;
33 import dr.math.distributions.NormalDistribution;
34
35 import java.util.Arrays;
36 import java.util.HashSet;
37 import java.util.Set;
38
39
40 /**
41 * This class gives an SIR trajectory and hands off a rate of coalescence at a given point in time.
42 * SIR trajectories are simulated deterministically
43 * Transmission is frequency-dependent
44 *
45 * transmissionRateParameter is the number of contacts an infected individual makes per time unit
46 * recoveryRateParameter is the number of recovery events an infected individual makes per time unit
47 * susceptibleParameter is the number of susceptible hosts at present day (t=0)
48 * infectedParameter is the number of infected hosts at present day (t=0)
49 * recoveredParameter is the number of recovered hosts at present day (t=0)
50 *
51 * @author Trevor Bedford
52 * @author Andrew Rambaut
53 */
54 public class SIRModel extends DemographicModel implements Likelihood {
55
56 //
57 // Public stuff
58 //
59 /**
60 * Construct demographic model with default settings
61 */
62 public SIRModel(Parameter reproductiveNumberParameter,
63 Parameter recoveryRateParameter,
64 Parameter hostPopulationSizeParameter,
65 Parameter proportionsParameter,
66 Type units) {
67
68 this(SIRModelParser.SIR_MODEL, reproductiveNumberParameter, recoveryRateParameter,
69 hostPopulationSizeParameter, proportionsParameter, units);
70 }
71
72 /**
73 * Construct demographic model with default settings
74 */
75 public SIRModel(String name,
76 Parameter reproductiveNumberParameter,
77 Parameter recoveryRateParameter,
78 Parameter hostPopulationSizeParameter,
79 Parameter proportionsParameter,
80 Type units) {
81
82 super(name);
83
84 this.reproductiveNumberParameter = reproductiveNumberParameter;
85 addVariable(reproductiveNumberParameter);
86 reproductiveNumberParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 1.0, 1));
87
88 this.recoveryRateParameter = recoveryRateParameter;
89 addVariable(recoveryRateParameter);
90 recoveryRateParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));
91
92 this.hostPopulationSizeParameter = hostPopulationSizeParameter;
93 addVariable(hostPopulationSizeParameter);
94 hostPopulationSizeParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1));
95
96 this.proportionsParameter = proportionsParameter;
97 addVariable(proportionsParameter);
98
99 demographicFunction = new SIRDemographicFunction(units);
100 setUnits(units);
101
102 addStatistic(new TimeseriesStatistic("susceptibles"));
103 addStatistic(new TimeseriesStatistic("infecteds"));
104 addStatistic(new TimeseriesStatistic("recovereds"));
105 addStatistic(new TimeseriesStatistic("effectivePopulationSize"));
106
107 }
108
109 @Override
110 protected void handleVariableChangedEvent(final Variable variable, final int index, final Variable.ChangeType type) {
111 demographicFunction.reset();
112 fireModelChanged();
113 likelihoodKnown = false;
114 }
115
116 @Override
117 protected void storeState() {
118 storedLogLikelihood = logLikelihood;
119 demographicFunction.store();
120 }
121
122 @Override
123 protected void restoreState() {
124 logLikelihood = storedLogLikelihood;
125 likelihoodKnown = true;
126 demographicFunction.restore();
127 }
128
129 // return S(t)
130 public double getSusceptibles(final double t) {
131 return demographicFunction.getSusceptibles(t);
132 }
133
134 // return I(t)
135 public double getInfecteds(final double t) {
136 return demographicFunction.getInfecteds(t);
137 }
138
139 // return R(t)
140 public double getRecovereds(final double t) {
141 return demographicFunction.getRecovereds(t);
142 }
143
144 // return R(t)
145 public double getEffectivePopulationSize(final double t) {
146 return demographicFunction.getDemographic(t);
147 }
148
149 /* Likelihood methods */
150
151 public String prettyName() {
152 return Likelihood.Abstract.getPrettyName(this);
153 }
154
155 public final double getLogLikelihood() {
156 if (!getLikelihoodKnown()) {
157 logLikelihood = calculateLogLikelihood();
158 likelihoodKnown = true;
159 }
160 return logLikelihood;
161 }
162
163 protected boolean getLikelihoodKnown() {
164 return likelihoodKnown;
165 }
166
167 public boolean evaluateEarly() {
168 return false;
169 }
170
171 @Override
172 public Set<Likelihood> getLikelihoodSet() {
173 return new HashSet<Likelihood>(Arrays.asList(this));
174 }
175
176 public boolean isUsed() {
177 return isUsed;
178 }
179
180 public void setUsed() {
181 isUsed = true;
182 }
183
184 public Model getModel() {
185 return this;
186 }
187
188 public void makeDirty() {
189 likelihoodKnown = false;
190 }
191
192 public LogColumn[] getColumns() {
193 return new LogColumn[]{
194 new LikelihoodColumn(getId())
195 };
196 }
197
198 protected class LikelihoodColumn extends NumberColumn {
199 public LikelihoodColumn(String label) {
200 super(label);
201 }
202
203 public double getDoubleValue() {
204 return getLogLikelihood();
205 }
206 }
207
208 public double calculateLogLikelihood() {
209 double r = demographicFunction.getRecovereds(endTime);
210 return NormalDistribution.logPdf(r, 0, 100);
211 }
212
213 private boolean isUsed = false;
214 private boolean likelihoodKnown = false;
215 private double logLikelihood;
216 private double storedLogLikelihood;
217
218 private double stepSize = 0.01;
219 private double endTime = 5;
220
221 public DemographicFunction getDemographicFunction() {
222 return demographicFunction;
223 }
224
225 Parameter reproductiveNumberParameter = null;
226 Parameter recoveryRateParameter = null;
227 Parameter hostPopulationSizeParameter = null;
228 Parameter proportionsParameter = null;
229
230 SIRDemographicFunction demographicFunction = null;
231
232 class SIRDemographicFunction extends DemographicFunction.Abstract {
233
234 DynamicalSystem syst = new DynamicalSystem(0, 0.01);
235
236 public SIRDemographicFunction(Type units) {
237
238 super(units);
239
240 double hostPop = hostPopulationSizeParameter.getParameterValue(0);
241 double initialS = hostPop * proportionsParameter.getParameterValue(0);
242 double initialI = hostPop * proportionsParameter.getParameterValue(1);
243 double initialR = hostPop * proportionsParameter.getParameterValue(2);
244
245 syst.addVariable("susceptibles", initialS);
246 syst.addVariable("infecteds", initialI);
247 syst.addVariable("recovereds", initialR);
248 syst.addVariable("total", hostPop);
249 syst.addForce("contact", reproductiveNumberParameter.getParameterValue(0) * recoveryRateParameter.getParameterValue(0),
250 new String[]{"infecteds","susceptibles"}, new String[]{"total"}, "susceptibles", "infecteds");
251 syst.addForce("recovery", recoveryRateParameter.getParameterValue(0), new String[]{"infecteds"},
252 new String[]{}, "infecteds", "recovereds");
253
254 }
255
256 public void reset() {
257
258 double hostPop = hostPopulationSizeParameter.getParameterValue(0);
259 double initialS = hostPop * proportionsParameter.getParameterValue(0);
260 double initialI = hostPop * proportionsParameter.getParameterValue(1);
261 double initialR = hostPop * proportionsParameter.getParameterValue(2);
262
263 syst.resetVar("susceptibles", initialS);
264 syst.resetVar("infecteds", initialI);
265 syst.resetVar("recovereds", initialR);
266 syst.resetVar("total", hostPop);
267 syst.resetForce("contact", reproductiveNumberParameter.getParameterValue(0) * recoveryRateParameter.getParameterValue(0));
268 syst.resetForce("recovery", recoveryRateParameter.getParameterValue(0));
269 syst.resetTime();
270 }
271
272 public void store () {
273 syst.store();
274 }
275
276 public void restore () {
277 syst.restore();
278 }
279
280 // return N(t)
281 public double getDemographic(final double t) {
282
283 double beta = reproductiveNumberParameter.getParameterValue(0) * recoveryRateParameter.getParameterValue(0);
284 double numer = getInfecteds(t) * hostPopulationSizeParameter.getParameterValue(0);
285 double denom = 2.0 * beta * getSusceptibles(t);
286 double ne = numer / denom;
287
288 if (ne < 0.001) {
289 ne = 0.001;
290 }
291
292 return ne;
293
294 }
295
296 // return log N(t)
297 public double getLogDemographic(final double t) {
298 return Math.log(getDemographic(t));
299 }
300
301 // return S(t)
302 public double getSusceptibles(final double t) {
303 return syst.getValue("susceptibles", t);
304 }
305
306 // return I(t)
307 public double getInfecteds(final double t) {
308 return syst.getValue("infecteds", t);
309 }
310
311 // return R(t)
312 public double getRecovereds(final double t) {
313 return syst.getValue("recovereds", t);
314 }
315
316 // return t/N(t)
317 public double getIntensity(final double t) {
318 return 1.0;
319 }
320
321 // return x*N(t)
322 public double getInverseIntensity(final double x) {
323 return 1.0;
324 }
325
326 // return integral of 1/N(t)
327 public double getIntegral(final double start, final double finish) {
328
329 double neAvg = 0.5 * (getDemographic(start) + getDemographic(finish));
330 double integral = (finish-start)*(1.0 / neAvg);
331 return integral ;
332
333 }
334
335 // ignore the rest:
336 public int getNumArguments() {
337 return 0;
338 }
339
340 public String getArgumentName(final int n) {
341 return null;
342 }
343
344 public double getArgument(final int n) {
345 return 0;
346 }
347
348 public void setArgument(final int n, final double value) {
349 }
350
351 public double getLowerBound(final int n) {
352 return 0;
353 }
354
355 public double getUpperBound(final int n) {
356 return 0;
357 }
358
359 public DemographicFunction getCopy() {
360 return null;
361 }
362
363 public double getThreshold() {
364 return 0;
365 }
366
367 }
368
369 public class TimeseriesStatistic extends Statistic.Abstract {
370
371 public TimeseriesStatistic(String name) {
372 super(name);
373 }
374
375 @Override
376 public String getDimensionName(final int i) {
377 double t = (double) i * stepSize;
378 return Double.toString(t);
379 }
380
381 public int getDimension() {
382 return (int) (endTime / stepSize);
383 }
384
385 public double getStatisticValue(final int i) {
386 double t = (double) i * stepSize;
387 if (getStatisticName().equals("susceptibles")) {
388 return getSusceptibles(t);
389 }
390 else if (getStatisticName().equals("infecteds")) {
391 return getInfecteds(t);
392 }
393 else if (getStatisticName().equals("recovereds")) {
394 return getRecovereds(t);
395 }
396 else if (getStatisticName().equals("effectivePopulationSize")) {
397 return getEffectivePopulationSize(t);
398 }
399 else {
400 return 0.0;
401 }
402 }
403
404 }
405
406 }