-
Notifications
You must be signed in to change notification settings - Fork 0
/
original_epidemics_model.py
385 lines (312 loc) · 14.1 KB
/
original_epidemics_model.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import torch
import numpy as np
from scipy import stats
from functools import partial
alpha_f = (0.7**2)*((1-0.7)/(0.17**2) - (1-0.7))
beta_f = alpha_f*(1/0.7 - 1)
def prior_sir():
"""
Implements batch sampling from a stationary prior over the parameters
of the non-stationary SIR model.
"""
t1 = np.random.normal(loc=8, scale=3)
t2 = np.random.normal(loc=15, scale=1)
t3 = np.random.normal(loc=22, scale=1)
t4 = np.random.normal(loc=66, scale=1)
delta_t1 = np.random.lognormal(mean=np.log(3), sigma=0.3)
delta_t2 = np.random.lognormal(mean=np.log(3), sigma=0.3)
delta_t3 = np.random.lognormal(mean=np.log(3), sigma=0.3)
delta_t4 = np.random.lognormal(mean=np.log(3), sigma=0.3)
lambd0 = np.random.lognormal(mean=np.log(1.2), sigma=0.5)
lambd1 = np.random.lognormal(mean=np.log(0.6), sigma=0.5)
lambd2 = np.random.lognormal(mean=np.log(0.3), sigma=0.5)
lambd3 = np.random.lognormal(mean=np.log(0.1), sigma=0.5)
lambd4 = np.random.lognormal(mean=np.log(0.1), sigma=0.5)
mu = np.random.lognormal(mean=np.log(1/8), sigma=0.2)
f_i = np.random.beta(a=alpha_f, b=beta_f)
phi_i = stats.vonmises(kappa=0.01).rvs()
f_r = np.random.beta(a=alpha_f, b=beta_f)
phi_r = stats.vonmises(kappa=0.01).rvs()
f_d = np.random.beta(a=alpha_f, b=beta_f)
phi_d = stats.vonmises(kappa=0.01).rvs()
D_i = np.random.lognormal(mean=np.log(8), sigma=0.2)
D_r = np.random.lognormal(mean=np.log(8), sigma=0.2)
D_d = np.random.lognormal(mean=np.log(8), sigma=0.2)
E0 = np.random.gamma(shape=2, scale=30)
scale_I = np.random.gamma(shape=1, scale=5)
scale_R = np.random.gamma(shape=1, scale=5)
scale_D = np.random.gamma(shape=1, scale=5)
return [t1, t2, t3, t4, delta_t1, delta_t2, delta_t3, delta_t4,
lambd0, lambd1, lambd2, lambd3, lambd4, mu,
f_i, phi_i, f_r, phi_r, f_d, phi_d,
D_i, D_r, D_d, E0, scale_I, scale_R, scale_D]
def prior_secir():
"""
Implements batch sampling from a stationary prior over the parameters
of the non-stationary SIR model.
"""
alpha = np.random.uniform(low=0.005, high=0.9)
beta = np.random.lognormal(mean=np.log(0.25), sigma=0.3)
gamma = np.random.lognormal(mean=np.log(1/6.5), sigma=0.5)
eta = np.random.lognormal(mean=np.log(1/3.2), sigma=0.3)
theta = np.random.uniform(low=1/14, high=1/3)
delta = np.random.uniform(low=0.01, high=0.3)
d = np.random.uniform(low=1/14, high=1/3)
return [alpha, beta, gamma, eta, theta, delta, d]
def calc_lambda_array(sim_lag, lambd0, lambd1, lambd2, lambd3, lambd4,
t1, t2, t3, t4, delta_t1, delta_t2, delta_t3, delta_t4, T):
"""Computes the array of time-varying contact rates/transimission probabilities."""
# Array of initial lambdas
lambd0_arr = np.array([lambd0] * (t1+sim_lag))
# Compute lambd1 array
if delta_t1 == 1:
lambd1_arr = np.array([lambd1] * (t2-t1))
else:
lambd1_arr = np.linspace(lambd0, lambd1, delta_t1)
lambd1_arr = np.append(lambd1_arr, [lambd1] * (t2-t1-delta_t1))
# Compute lambd2 array
if delta_t2 == 1:
lambd2_arr = np.array([lambd2] * (t3-t2))
else:
lambd2_arr = np.linspace(lambd1, lambd2, delta_t2)
lambd2_arr = np.append(lambd2_arr, [lambd2] * (t3-t2-delta_t2))
# Compute lambd3 array
if delta_t3 == 1:
lambd3_arr = np.array([lambd3] * (t4-t3))
else:
lambd3_arr = np.linspace(lambd3, lambd4, delta_t3)
lambd3_arr = np.append(lambd3_arr, [lambd3] * (t4-t3-delta_t3))
# Compute lambd4 array
if delta_t4 == 1:
lambd4_arr = np.array([lambd4] * (T-t4))
else:
lambd4_arr = np.linspace(lambd3, lambd4, delta_t4)
lambd4_arr = np.append(lambd4_arr, [lambd4] * (T-t4-delta_t4))
return np.r_[lambd0_arr, lambd1_arr, lambd2_arr, lambd3_arr, lambd4_arr]
def non_stationary_SEICR(params_sir, params_secir, N, T, sim_diff=16, observation_model=True):
"""
Performs a forward simulation from the stationary SIR model.
"""
# Extract parameters
t1, t2, t3, t4, delta_t1, delta_t2, delta_t3, delta_t4, lambd0, lambd1, lambd2, lambd3, lambd4, mu, f_i, phi_i, f_r, phi_r, f_d, phi_d, delay_i, delay_r, delay_d, E0, scale_I, scale_R, scale_D = params_sir
alpha, beta, gamma, eta, theta, delta, d = params_secir
# Round integer parameters
t1, t2, t3, t4 = int(round(t1)), int(round(t2)), int(round(t3)), int(round(t4))
delta_t1, delta_t2, delta_t3, delta_t4 = int(round(delta_t1)), int(round(delta_t2)), int(round(delta_t3)), int(round(delta_t4))
E0 = max(1, np.round(E0))
delay_i = int(round(delay_i))
delay_r = int(round(delay_r))
delay_d = int(round(delay_d))
# Impose constraints
assert sim_diff > delay_i
assert sim_diff > delay_r
assert sim_diff > delay_d
assert t1 > 0 and t2 > 0 and t3 > 0 and t4 > 0
assert t1 < t2 < t3 < t4
assert delta_t1 > 0 and delta_t2 > 0 and delta_t3 > 0 and delta_t4 > 0
assert t2 - t1 >= delta_t1 and t3 - t2 >= delta_t2 and t4-t3 >= delta_t3 and T-t4 >= delta_t4
# Calculate lambda arrays
# Lambda0 is the initial contact rate which will be consecutively
# reduced via the government measures
sim_lag = sim_diff - 1
lambd_arr = calc_lambda_array(sim_lag, lambd0, lambd1, lambd2, lambd3, lambd4,
t1, t2, t3, t4, delta_t1, delta_t2, delta_t3, delta_t4, T)
# Initial conditions
S, E, C, I, R, D = [N-E0], [E0], [0], [0], [0], [0]
# Containers
I_news = []
R_news = []
D_news = []
# Reported new cases
I_data = np.zeros(T)
R_data = np.zeros(T)
D_data = np.zeros(T)
fs_i = np.zeros(T)
fs_r = np.zeros(T)
fs_d = np.zeros(T)
# Simulate T-1 tiemsteps
for t in range(T+sim_lag):
# Calculate new exposed cases
E_new = lambd_arr[t] * ((C[t] + beta*I[t])/N)*S[t]
# Remove exposed from susceptible
S_t = S[t] - E_new
# Calculate current exposed by adding new exposed and
# subtracting the exposed becoming carriers.
E_t = E[t] + E_new - gamma*E[t]
# Calculate current carriers by adding the new exposed and subtracting
# those who will develop symptoms and become detected and those who
# will go through the disease asymptomatically.
C_t = C[t] + gamma*E[t] - (1-alpha)*eta*C[t] - alpha*theta*C[t]
# Calculate current infected by adding the symptomatic carriers and
# subtracting the dead and recovered. The newly infected are just the
# carriers who get detected.
I_t = I[t] + (1-alpha)*eta*C[t] - (1-delta)*mu*I[t] - delta*d*I[t]
I_new = (1-alpha)*eta*C[t]
# Calculate current recovered by adding the symptomatic and asymptomatic
# recovered. The newly recovered are only the detected recovered
R_t = R[t] + alpha*theta*C[t] + (1-delta)*mu*I[t]
R_new = (1-delta)*mu*I[t]
# Calculate the current dead
D_t = D[t] + delta*d*I[t]
D_new = delta*d*I[t]
# Ensure some numerical onstraints
S_t = np.clip(S_t, 0, N)
E_t = np.clip(E_t, 0, N)
C_t = np.clip(C_t, 0, N)
I_t = np.clip(I_t, 0, N)
R_t = np.clip(R_t, 0, N)
D_t = np.clip(D_t, 0, N)
# Keep track of process over time
S.append(S_t)
E.append(E_t)
C.append(C_t)
I.append(I_t)
R.append(R_t)
D.append(D_t)
I_news.append(I_new)
R_news.append(R_new)
D_news.append(D_new)
# From here, start adding new cases with delay D
# Note, we assume the same delay
if t >= sim_lag:
# Compute lags and add to data arrays
fs_i[t-sim_lag] = (1-f_i)*(1 - np.abs( np.sin( (np.pi/7) * (t-sim_lag) - 0.5*phi_i)) )
fs_r[t-sim_lag] = (1-f_r)*(1 - np.abs( np.sin( (np.pi/7) * (t-sim_lag) - 0.5*phi_r)) )
fs_d[t-sim_lag] = (1-f_d)*(1 - np.abs( np.sin( (np.pi/7) * (t-sim_lag) - 0.5*phi_d)) )
I_data[t-sim_lag] = I_news[t-delay_i]
R_data[t-sim_lag] = R_news[t-delay_r]
D_data[t-sim_lag] = D_news[t-delay_d]
# Compute weekly modulation
I_data = (1-fs_i) * I_data
R_data = (1-fs_r) * R_data
D_data = (1-fs_d) * D_data
# Add noise
I_data = stats.t(df=4, loc=I_data, scale=np.sqrt(I_data)*scale_I).rvs()
R_data = stats.t(df=4, loc=R_data, scale=np.sqrt(R_data)*scale_R).rvs()
D_data = stats.t(df=4, loc=D_data, scale=np.sqrt(D_data)*scale_D).rvs()
if observation_model:
return np.stack((I_data, R_data, D_data)).T
return np.stack((S, E, I, C, R, D)).T
def SECIR_asymptomatic(params_sir, params_secir, N, T, sim_diff=16):
"""
Performs a forward simulation from the stationary SIR model.
"""
# Extract parameters
t1, t2, t3, t4, delta_t1, delta_t2, delta_t3, delta_t4, lambd0, lambd1, lambd2, lambd3, lambd4, mu, f_i, phi_i, f_r, phi_r, f_d, phi_d, delay_i, delay_r, delay_d, E0, scale_I, scale_R, scale_D = params_sir
alpha, beta, gamma, eta, theta, delta, d = params_secir
# Round integer parameters
t1, t2, t3, t4 = int(round(t1)), int(round(t2)), int(round(t3)), int(round(t4))
delta_t1, delta_t2, delta_t3, delta_t4 = int(round(delta_t1)), int(round(delta_t2)), int(round(delta_t3)), int(round(delta_t4))
E0 = max(1, np.round(E0))
delay_i = int(round(delay_i))
delay_r = int(round(delay_r))
delay_d = int(round(delay_d))
# Impose constraints
assert sim_diff > delay_i
assert sim_diff > delay_r
assert sim_diff > delay_d
assert t1 > 0 and t2 > 0 and t3 > 0 and t4 > 0
assert t1 < t2 < t3 < t4
assert delta_t1 > 0 and delta_t2 > 0 and delta_t3 > 0 and delta_t4 > 0
assert t2 - t1 >= delta_t1 and t3 - t2 >= delta_t2 and t4-t3 >= delta_t3 and T-t4 >= delta_t4
# Calculate lambda arrays
# Lambda0 is the initial contact rate which will be consecutively
# reduced via the government measures
sim_lag = sim_diff - 1
lambd_arr = calc_lambda_array(sim_lag, lambd0, lambd1, lambd2, lambd3, lambd4,
t1, t2, t3, t4, delta_t1, delta_t2, delta_t3, delta_t4, T)
# Initial conditions
S, E, C, I, R, D = [N-E0], [E0], [0], [0], [0], [0]
# Containers
A_data = [0]
S_data = [0]
# Simulate T-1 tiemsteps
for t in range(T+sim_lag):
# Calculate new exposed cases
E_new = lambd_arr[t] * ((C[t] + beta*I[t])/N)*S[t]
# Remove exposed from susceptible
S_t = S[t] - E_new
# Calculate current exposed by adding new exposed and
# subtracting the exposed becoming carriers.
E_t = E[t] + E_new - gamma*E[t]
# Calculate current carriers by adding the new exposed and subtracting
# those who will develop symptoms and become detected and those who
# will go through the disease asymptomatically.
C_t = C[t] + gamma*E[t] - (1-alpha)*eta*C[t] - alpha*theta*C[t]
# Calculate current infected by adding the symptomatic carriers and
# subtracting the dead and recovered. The newly infected are just the
# carriers who get detected.
I_t = I[t] + (1-alpha)*eta*C[t] - (1-delta)*mu*I[t] - delta*d*I[t]
I_new = (1-alpha)*eta*C[t]
# Calculate current recovered by adding the symptomatic and asymptomatic
# recovered. The newly recovered are only the detected recovered
R_t = R[t] + alpha*theta*C[t] + (1-delta)*mu*I[t]
R_new = (1-delta)*mu*I[t]
# Calculate the current dead
D_t = D[t] + delta*d*I[t]
D_new = delta*d*I[t]
# Ensure some numerical onstraints
S_t = np.clip(S_t, 0, N)
E_t = np.clip(E_t, 0, N)
C_t = np.clip(C_t, 0, N)
I_t = np.clip(I_t, 0, N)
R_t = np.clip(R_t, 0, N)
D_t = np.clip(D_t, 0, N)
# Keep track of process over time
S.append(S_t)
E.append(E_t)
C.append(C_t)
I.append(I_t)
R.append(R_t)
D.append(D_t)
# Add the new saymptomatic cases
A_data.append(alpha*theta*C[t])
S_data.append((1-alpha)*eta*C[t])
return np.array(A_data), np.array(S_data)
def data_generator(batch_size, mean_g, std_g, T=None, N=None, T_min=10, T_max=90, sim_diff=21,
N_min=10000, N_max=70000000, to_tensor=True, seed=None):
"""
Runs the forward model 'batch_size' times by first sampling fromt the prior
theta ~ p(theta) and running x ~ p(x|theta).
----------
Arguments:
batch_size : int -- the number of samples to draw from the prior
simulator : callable -- the data simulator
to_tensor : boolean -- converts theta and x to tensors if True
----------
Output:
theta : tf.Tensor or np.ndarray of shape (batch_size, theta_dim) - the data gen parameters
x : tf.Tensor of np.ndarray of shape (batch_size, n_obs, x_dim) - the generated data
"""
if seed is not None:
np.random.seed(seed)
# Variable-size t
if T is None:
T = np.random.randint(T_min, T_max+1)
# Variable size N
if N is None:
N = np.random.randint(N_min, N_max)
# Generate data
# x is a np.ndarray of shape (batch_size, n_obs, x_dim)
x = []
theta = []
for i in range(batch_size):
# Reject meaningless simulaitons
x_i = None
while x_i is None:
try:
theta1 = prior_sir()
theta2 = prior_secir()
x_i = non_stationary_SEICR(theta1, theta2, N, T, sim_diff=sim_diff)
x_i = (x_i - mean_g) / std_g
except:
pass
# Simulate SECIR
x.append(x_i)
theta.append(theta1 + theta2)
x = np.array(x)
# Convert to tensor, if specified
if to_tensor:
theta = torch.from_numpy(np.array(theta)).float()
x = torch.from_numpy(x).float()
return {'theta': theta, 'x': x}