Debian Codegraph @

src/dr/evomodel/epidemiology/SIRepidemic.java

raw symbols SCIP
1 /*
2 * SIRepidemic.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.*;
29
30 /**
31 * This class models an SIR epidemic.
32 * Uses the new parameterization (23rd February 2012)
33 *
34 * @author Daniel Wilson
35 */
36 public class SIRepidemic extends ODEDemographicFunction {
37
38 public SIRepidemic(Type units) {
39 super(units);
40 // Number of variables in the Runge-Kutta scheme
41 nvar = 2;
42 }
43
44 public void setN0(double value) { N0 = value; RKinit(); }
45 public double getN0() { return N0; }
46
47 public void setGrowthRate(double value) { r = value; RKinit(); }
48 public double getGrowthRate() { return r; }
49 public void setDoublingTime(double value) { setGrowthRate(Math.log(2)/value); }
50 public double getDoublingTime() { return Math.log(2)/r; }
51
52 public void setu0(double value) { u0 = value; RKinit(); }
53 public double getu0() { return u0; }
54
55 public void setR0(double value) { R0 = value; RKinit(); }
56 public double getR0() { return R0; }
57
58 public void setIntegrateIntensity() { integrate_intensity = true; }
59 public void unsetIntegrateIntensity() { integrate_intensity = false; }
60
61 public double getbeta() { return getGrowthRate()/(1.0-1.0/getR0()); }
62 public double getgamma() { return getGrowthRate()/(getR0()-1.0); }
63
64 public double gets0() { return s0; }
65 public double geti0() { return i0; }
66
67 /* Calculate minimum proportion infectious from R0 */
68 public double getsmin(double _R0) {
69 if(_R0<=1.0) return 0.0;
70 return -W.branch0(-_R0*Math.exp(-_R0))/_R0;
71 }
72
73 /* Calculate proportion infectious from proportion susceptible */
74 public double s_to_i(double susceptible) {
75 double i = 1.0-susceptible+Math.log(susceptible)/R0;
76 // Do not allow i to reach boundaries at 0 and 1.
77 if(i<1e-12) i = 1e-12;
78 if(i>1.0-1e-12) i = 1.0-1e-12;
79 return i;
80 }
81
82 /* Convert proportion susceptible to transformed proportion susceptible, where smin is the minimum proportion of susceptibles */
83 public double s_to_u(double susceptible, double smin) {
84 return Math.log(susceptible-smin)-Math.log(1.0-susceptible);
85 }
86
87 /* Convert transformed proportion susceptible to proportion susceptible, where smin is the minimum proportion of susceptibles */
88 public double u_to_s(double u, double smin) {
89 double s = smin+(1.0-smin)/(1.0+Math.exp(-u));
90 // Do not allow s to reach boundaries at smin and 1
91 if(s<smin+1e-12) s = smin+1e-12;
92 if(s>1.0-1e-12) s = 1.0-1e-12;
93 return s;
94 }
95
96 /* Convert transformed proportion susceptible to proportion infectious, where smin is the minimum proportion of susceptibles */
97 public double u_to_i(double u, double smin, double _R0) {
98 double s = smin+(1.0-smin)/(1.0+Math.exp(-u));
99 double i = 1.0-s+Math.log(s)/_R0;
100 // Do not allow i to reach boundaries at 0 and 1.
101 if(i<1e-12) i = 1e-12;
102 if(i>1.0-1e-12) i = 1.0-1e-12;
103 return i;
104 }
105
106 /**
107 * Variable 0 is the integrated intensity function (by definition)
108 * Variable 1 is the transformed proportion of susceptibles
109 */
110 void derivs(double t, double[] y, double[] dydt) {
111 double Lambda = y[0];
112 if(!integrate_intensity) {
113 // Switch off to avoid underflow/overflow problems when wishing to calculate prevalence only
114 dydt[0] = 1.0;
115 } else if(Lambda<LAMBDA_MAX) {
116 double Ne = getDemographicFromPrevalence(y,t);
117 dydt[0] = 1.0/Ne;
118 } else {
119 // When the coalescence intensity is already extremely large, set a ceiling. This will have the effect of inducing
120 // a zero likelihood when the density of coalescence times is evaluated. The interpretation is that all coalescence
121 // events must have already occurred by this point.
122 dydt[0] = 0.0;
123 }
124 double s = u_to_s(y[1],smin);
125 if(s==1.0) {
126 dydt[1] = beta-gamma;
127 } else {
128 if(s<smin+1e-6) s = smin+1e-6;
129 dydt[1] = (beta * s * (1.0-s) + gamma * s * Math.log(s)) * (1.0-smin) / (s-smin) / (1.0-s);
130 }
131 if(Double.isNaN(dydt[0]) | Double.isInfinite(dydt[0]) | Double.isNaN(dydt[1]) | Double.isInfinite(dydt[1])) {
132 System.out.println("t = " + t + ", y = {" + y[0] + ", " + y[1] + "}, s = " + s + ", dydt = {" + dydt[0] + ", " + dydt[1] + "}");
133 derivs(t,y,dydt);
134 }
135 }
136
137 /**
138 * Calculate the effective population size at time t
139 */
140 double getDemographicFromPrevalence(double[] y, double t) {
141 double s = u_to_s(y[1],smin);
142 double i = u_to_i(y[1],smin,getR0());
143 double Ne = N0 * s0 / i0 * i / s;
144 // Do not allow NaN or very small values
145 if(Double.isNaN(Ne) | Ne<1e-12) Ne = 1e-12;
146 return Ne;
147 }
148
149 /**
150 *
151 */
152 public double getDemographic(double t) {
153 if(!valid) return 0.0;
154 double ret = super.getDemographic(t);
155 if(Ynow[0]>=LAMBDA_MAX) return 0.0;
156 return ret;
157 }
158
159 /**
160 *
161 */
162 public double getIntensity(double t) {
163 if(!valid) return Math.log(0.0);
164 double ret = super.getIntensity(t);
165 if(ret>=LAMBDA_MAX) return Math.log(0.0);
166 return ret;
167 }
168
169 /**
170 * Obtain the proportion of susceptibles at time t (compare to super.getDemographic and super.getIntensity)
171 */
172 public double getSusceptibles(double t) {
173 if(!valid) return Math.log(0.0);
174 Evaluate(t);
175 if(RKfail) return Math.log(0.0);
176 if(Ynow[0]>=LAMBDA_MAX) return Math.log(0.0);
177 return u_to_s(Ynow[1],smin);
178 }
179
180 /**
181 * Obtain the transformed proportion of susceptibles at time t (compare to super.getDemographic and super.getIntensity)
182 */
183 public double getTransformedSusceptibles(double t) {
184 if(!valid) return Math.log(0.0);
185 Evaluate(t);
186 if(RKfail) return Math.log(0.0);
187 return Ynow[1];
188 }
189
190 /**
191 * Overload RKinit to make use of "valid"
192 */
193 void RKinit() {
194 super.RKinit();
195 /* Set internal variables */
196 valid = true;
197 smin = getsmin(getR0());
198 s0 = u_to_s(u0,smin);
199 i0 = u_to_i(u0,smin,getR0());
200 beta = getbeta();
201 gamma = getgamma();
202 }
203
204 /**
205 * Initially the integrated intensity function (Y[0]) is zero
206 * and the transformed proportion of susceptibles (Y[1]) is u0
207 */
208 void setInit() {
209 Y[0][0] = 0.0;
210 Y[1][0] = u0;
211 }
212
213 public double getArgument(int n) {
214 switch (n) {
215 case 0:
216 return getN0();
217 case 1:
218 return getGrowthRate();
219 case 2:
220 return getu0();
221 case 3:
222 return getR0();
223 }
224 throw new IllegalArgumentException("Argument " + n + " does not exist");
225 }
226
227 public String getArgumentName(int n) {
228 switch (n) {
229 case 0:
230 return "N0";
231 case 1:
232 return "r";
233 case 2:
234 return "u0";
235 case 3:
236 return "R0";
237 }
238 throw new IllegalArgumentException("Argument " + n + " does not exist");
239 }
240
241 public DemographicFunction getCopy() {
242 SIRepidemic df = new SIRepidemic(getUnits());
243 df.setN0(getN0());
244 df.setGrowthRate(getGrowthRate());
245 df.setu0(getu0());
246 df.setR0(getR0());
247 // Copy RK variables?
248 return df;
249 }
250
251 /**
252 * Returns the inverse function of getIntensity
253 */
254 public double getInverseIntensity(double x) {
255 // Can be implemented using simple linear interpolation
256 throw new RuntimeException("Not implemented!");
257 }
258
259 public double getLowerBound(int n) {
260 return 0.0;
261 }
262
263 public double getUpperBound(int n) {
264 return Double.POSITIVE_INFINITY;
265 }
266
267 public int getNumArguments() {
268 return 4;
269 }
270
271 public void setArgument(int n, double value) {
272 switch (n) {
273 case 0:
274 setN0(value);
275 break;
276 case 1:
277 setGrowthRate(value);
278 break;
279 case 2:
280 setu0(value);
281 break;
282 case 3:
283 setR0(value);
284 break;
285 default:
286 throw new IllegalArgumentException("Argument " + n + " does not exist");
287
288 }
289 }
290
291 // Parameters
292 private boolean valid = false;
293 private double N0 = 0; // Initial effective population size (0<N0)
294 private double r = 0; // Intrinsic growth rate (0<r)
295 private double u0 = 0; // Initial transformed progress of the epidemic (-Inf<u0<Inf)
296 private double R0 = 0; // Basic reproductive number (1<R0)
297 private boolean integrate_intensity = true; // Flag indicating whether the integrated intensity function is calculated
298 // Pre-calculated from parameters
299 private double smin = 0; // Minimum proportion of susceptibles (depends on R0)
300 private double s0 = 0; // Initial progress of the epidemic (smin<s0<1)
301 private double i0 = 0; // Initial proportion infectious
302 private double beta = 0; // Initial proportion infectious
303 private double gamma = 0; // Initial proportion infectious
304 LambertW W; // Needed for calculating smin
305 // Maximum possible value of Lambda
306 private double LAMBDA_MAX = 1000.0;
307 }