forked from benmaier/COVID19CaseNumberModel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIRX.py
305 lines (220 loc) · 7.5 KB
/
SIRX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import numpy as np
from scipy.integrate import ode
from lmfit import minimize, Parameters
class SIRXConfirmedModel:
def __init__(self):
pass
# set equation of motion for SIRX dynaics
def dxdt(self,t,y,eta,rho,kappa,kappa0):
S = y[0]
I = y[1]
X = y[2]
H = y[3]
dy = np.zeros(4)
dy[0] = -eta*S*I - kappa0*S
dy[1] = +eta*S*I - rho*I - kappa*I - kappa0*I
dy[2] = +kappa*I + kappa0*I
dy[3] = +kappa0*S
return dy
def SIRX(self,t, y0, eta, rho, kappa,kappa0, N, I0_factor):
X0 = y0 / N
I0 = X0 * I0_factor
S0 = 1-X0-I0
y0 = np.array([S0, I0, X0, 0.0])
t0 = t[0]
t = t[1:]
r = ode(self.dxdt)
# Runge-Kutta with step size control
r.set_integrator('dopri5')
# set initial values
r.set_initial_value(y0,t0)
# set transmission rate and recovery rate
r.set_f_params(eta,rho,kappa,kappa0)
result = np.zeros((4,len(t)+1))
result[:,0] = y0
# loop through all demanded time points
for it, t_ in enumerate(t):
# get result of ODE integration
y = r.integrate(t_)
# write result to result vector
result[:,it+1] = y
return result
def residual(self,params, x, data):
eta = params['eta']
rho = params['rho']
kappa = params['kappa']
kappa0 = params['kappa0']
I0_factor = params['I0_factor']
#N = 10**params['log10N']
N = params['N']
result = self.SIRX(x, data[0], eta, rho, kappa, kappa0, N, I0_factor)
X = result[2,:]
residual = X*N - data
return residual
def fit(self,t, data,maxfev=100000,params=None,N=None,Nmax=None,method='leastsq'):
R0 = 6.2
rho = 1/8
eta = R0*rho
if params is None:
params = Parameters()
params.add('eta',value=eta,vary=False)
params.add('rho',value=rho, vary=False)
params.add('kappa',value=rho,min=0)
params.add('kappa0',value=rho/2,min=0)
params.add('I0_factor',value=10,min=0.001,vary=True)
varyN = N is None
if varyN:
N = 1e7
if Nmax is None:
Nmax=115000000
params.add('N',value=N,min=1000,max=Nmax,vary=varyN)
if method=='Nelder':
out = minimize(self.residual, params, args=(t, data, ),
method=method,
)
else:
out = minimize(self.residual, params, args=(t, data, ),
maxfev=maxfev,
method=method,
)
return out
class SIRXShutdownModel:
def __init__(self):
pass
# set equation of motion for SIRX dynaics
def dxdt(self,t,y,eta,rho,kappa,kappa0):
S = y[0]
I = y[1]
Q = y[2]
H = y[3]
dy = np.zeros(4)
dy[0] = -eta*S*I - kappa0*S
dy[1] = +eta*S*I - rho*I - kappa0*I
dy[2] = +kappa0*I
dy[3] = +kappa0*S
return dy
def SIRX(self,t,y0, eta, rho, kappa, kappa0, N, I0_factor):
Q0 = y0 / N
I0 = Q0 * I0_factor
S0 = 1-Q0-I0
y0 = np.array([S0, I0, Q0, 0.0])
t0 = t[0]
t = t[1:]
r = ode(self.dxdt)
# Runge-Kutta with step size control
r.set_integrator('dopri5')
# set initial values
r.set_initial_value(y0,t0)
# set transmission rate and recovery rate
r.set_f_params(eta,rho,kappa,kappa0)
result = np.zeros((4,len(t)+1))
result[:,0] = y0
# loop through all demanded time points
for it, t_ in enumerate(t):
# get result of ODE integration
y = r.integrate(t_)
# write result to result vector
result[:,it+1] = y
return result
def residual(self,params, x, data):
eta = params['eta']
rho = params['rho']
kappa = params['kappa']
kappa0 = params['kappa0']
I0_factor = params['I0_factor']
#N = 10**params['log10N']
N = params['N']
result = self.SIRX(x, data[0], eta, rho, kappa, kappa0, N, I0_factor)
Q = result[2,:]
residual = Q*N - data
return residual
def fit(self,t, data,maxfev=1000,params=None,N=None,Nmax=None):
if params is None:
params = Parameters()
R0 = 6.2
rho = 1/8
eta = R0*rho
params.add('eta',value=eta,vary=False)
params.add('rho',value=rho, vary=False)
params.add('kappa0',value=rho,vary=True,min=0)
params.add('kappa',value=0,vary=False,min=0)
params.add('I0_factor', value=10,min=1)
varyN = N is None
if varyN:
N = 1e7
if Nmax is None and varyN:
Nmax=115000000
else:
Nmax=None
params.add('N',value=N,min=100000,max=Nmax,vary=varyN)
out = minimize(self.residual, params, args=(t, data, ),maxfev=maxfev)
return out
class SIRXQuarantineModel:
def __init__(self):
pass
# set equation of motion for SIRX dynaics
def dxdt(self,t,y,eta,rho,kappa,kappa0):
S = y[0]
I = y[1]
X = y[2]
H = y[3]
dy = np.zeros(4)
dy[0] = -eta*S*I
dy[1] = +eta*S*I - rho*I - kappa*I
dy[2] = +kappa*I
dy[3] = 0
return dy
def SIRX(self,t, y0, eta, rho, kappa,kappa0, N, I0_factor):
X0 = y0 / N
I0 = X0 * I0_factor
S0 = 1-X0-I0
y0 = np.array([S0, I0, X0, 0.0])
t0 = t[0]
t = t[1:]
r = ode(self.dxdt)
# Runge-Kutta with step size control
r.set_integrator('dopri5')
# set initial values
r.set_initial_value(y0,t0)
# set transmission rate and recovery rate
r.set_f_params(eta,rho,kappa,kappa0)
result = np.zeros((4,len(t)+1))
result[:,0] = y0
# loop through all demanded time points
for it, t_ in enumerate(t):
# get result of ODE integration
y = r.integrate(t_)
# write result to result vector
result[:,it+1] = y
return result
def residual(self,params, x, data):
eta = params['eta']
rho = params['rho']
kappa = params['kappa']
kappa0 = params['kappa0']
I0_factor = params['I0_factor']
#N = 10**params['log10N']
N = params['N']
result = self.SIRX(x, data[0], eta, rho, kappa, kappa0, N, I0_factor)
X = result[2,:]
residual = X*N - data
return residual
def fit(self,t, data,maxfev=100000,params=None,N=None,Nmax=None):
if params is None:
params = Parameters()
R0 = 6.2
rho = 1/8
eta = R0*rho
params.add('eta',value=eta,vary=False)
params.add('rho',value=rho, vary=False)
params.add('kappa',value=0,min=0)
params.add('kappa0',value=rho,min=0,vary=False)
params.add('I0_factor', value=300,min=0.001)
varyN = N is None
if varyN:
N = 10000
if Nmax is None:
Nmax=1000000000
params.add('N',value=N,min=10,max=Nmax,vary=varyN)
out = minimize(self.residual, params, args=(t, data, ),maxfev=maxfev)
return out