-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimulation.py
More file actions
289 lines (208 loc) · 10.6 KB
/
simulation.py
File metadata and controls
289 lines (208 loc) · 10.6 KB
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
# mcandrew
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.interpolate import interp1d
import scienceplots
class compartment_forecast_with_GP(object):
# Initialize the forecasting framework
def __init__(self
, N=None # Total population
, y=None # Observed incident cases (with missing values)
, X=None # Covariate matrix for GP kernel (e.g. time or other predictors)
, times=None # Array of time points
, start=None, end=None # Start and end time (used if times is None)
, infectious_period=None): # Fixed infectious period (used to derive gamma)
self.N = N
self.times = times
self.infectious_period = infectious_period
# Set time boundaries
if times is not None:
self.start = min(times)
self.end = max(times)
else:
self.start, self.end = start, end
self.y = y
# Find first missing value in y to determine training length
if y is not None:
self.nobs = np.min(np.argwhere(np.isnan(y)))
else:
self.nobs = None
self.X = X
# Simulate epidemic trajectories with a stochastic SIR model
def simulation(self, I0=None, repo=None, dt=1./7):
import numpy as np
N = self.N
infectious_period = self.infectious_period
start, end = self.start, self.end
gamma = 1. / infectious_period
S0, I0, R0, i0 = N - I0, I0, 0, I0
y = [(S0, I0, R0, i0)]
# Time grid for simulation
times = np.linspace(start, end, (end - start) * int(1. / dt))
for t in times:
S, I, R, i = y[-1]
beta = repo * gamma
# Simulate infections and recoveries using Poisson noise
infection = np.random.poisson(dt * (beta * S * I / N))
recover = np.random.poisson(dt * (gamma * I))
# Update compartments (clipped to [0, N])
S = np.clip(S - infection, 0, N)
I = np.clip(I + infection - recover, 0, N)
R = np.clip(R + recover, 0, N)
i += infection
y.append((S, I, R, i))
S, I, R, i = zip(*y)
i = np.diff(i) # Daily incident cases
return times, i, y
# Fit model to control scenario using NumPyro and GP residuals
def control_fit(self, dt=1./7):
import jax
import jax.numpy as jnp
import numpyro
import numpyro.distributions as dist
from numpyro.infer import MCMC, NUTS
from diffrax import diffeqsolve, ODETerm, Heun, SaveAt
def model(y=None, times=None, N=None, forecast = False):
# Define SIR ODE system
def f(t, y, args):
S, I, R, i = y
repo, gamma, N = args
beta = repo * gamma
dS = -beta * S * I / N
dI = beta * S * I / N - gamma * I
dR = gamma * I
di = beta * S * I / N
return jnp.array([dS, dI, dR, di])
# Sample initial infectious proportion and scale to population
I0 = numpyro.sample("I0", dist.Beta(1, 1))
I0 = N * I0
infectious_period = self.infectious_period
gamma = 1. / infectious_period
# Sample reproduction rate (scaled by 5)
repo = numpyro.sample("repo", dist.Beta(1, 1))
repo = 5 * repo
# Solve ODE forward in time
saves = SaveAt(ts=jnp.arange(-1., self.end + 1, 1))
term = ODETerm(f)
solver = Heun()
y0 = jnp.array([N - I0, I0, 0, I0])
solution = diffeqsolve(term, solver, t0=-1, t1=self.end + 1, dt0=dt, y0=y0, saveat=saves, args=(repo, gamma, N))
times = solution.ts[1:]
cinc = solution.ys[:, -1] # cumulative incidence
inc = jnp.diff(cinc) # incidence
inc = numpyro.deterministic("inc", inc)
#--setup residual vector
nobs = self.nobs
#resid_center = jnp.nanmean(y)
resid = (y.reshape(-1,) - inc )[:nobs]
# Define RBF kernel (optional for multiple covariates)
def rbf_kernel_ard(X1, X2, amplitude, lengthscales):
X1_scaled = X1 / lengthscales
X2_scaled = X2 / lengthscales
dists = jnp.sum((X1_scaled[:, None, :] - X2_scaled[None, :, :])**2, axis=-1)
return amplitude**2 * jnp.exp(-0.5 * dists)
def random_walk_kernel(X, X2=None, variance=1.0):
if X2 is None:
X2 = X
return variance * jnp.minimum(X, X2.T)
noise = numpyro.sample("noise", dist.HalfCauchy(1.))
ncols = X.shape[-1]
rw_var = numpyro.sample("rw_var", dist.HalfCauchy(1.))
K1 = random_walk_kernel(X[:, 0].reshape(-1, 1), variance=rw_var)
# Optionally add RBF kernel if extra features exist
if ncols > 1:
amp = numpyro.sample("amp", dist.Beta(1., 1.))
leng = numpyro.sample("leng", dist.HalfCauchy(1.))
K2 = rbf_kernel_ard(X[:, 1:], X[:, 1:], amp, leng)
K = K1 + K2
else:
K = K1
# Compute submatrices for GP residual conditioning
KOO = numpyro.deterministic("KOO", K[:nobs, :nobs] + noise * jnp.eye(nobs))
KTT = numpyro.deterministic("KTT", K[nobs:, nobs:] )
KOT = numpyro.deterministic("KOT", K[:nobs, nobs:] )
# Poisson observation model on residual-corrected prediction
training_resid = numpyro.sample("training_resid", dist.MultivariateNormal(0, covariance_matrix = KOO))
numpyro.sample("likelihood",
dist.Poisson(inc[:nobs] + training_resid),
obs=y[:nobs])
# training_resid = numpyro.sample("likelihood",
# dist.MultivariateNormal( inc[:nobs] , covariance_matrix = KOO) ,
# obs=y[:nobs])
# Compute conditional GP mean and covariance
if forecast:
L = jnp.linalg.cholesky(KOO + 1e-5 * jnp.eye(nobs))
alpha = jax.scipy.linalg.solve_triangular(L , resid, lower=True)
alpha = jax.scipy.linalg.solve_triangular(L.T, alpha, lower=False)
mean = KOT.T @ alpha
v = jax.scipy.linalg.solve_triangular(L, KOT, lower=True)
cov = KTT - v.T @ v
fitted_resid = numpyro.sample("fitted_resid", dist.MultivariateNormal(mean, covariance_matrix=cov))
final_resid = jnp.concatenate([resid[:nobs], fitted_resid])
yhat_mean = numpyro.deterministic("yhat_mean", inc + final_resid)
yhat_obs = numpyro.sample("yhat_obs", dist.Poisson(jnp.clip( inc + final_resid,10**-5,jnp.inf) ) )
yhat = numpyro.deterministic("yhat", jnp.concatenate( [yhat_mean[:nobs], yhat_obs[nobs:]]))
print(len(yhat))
#yhat = numpyro.sample("yhat", dist.Normal( inc + final_resid, jnp.clip(inc + final_resid,10**-5,jnp.inf) ) )
# Run MCMC with NUTS sampler
mcmc = MCMC(NUTS(model, max_tree_depth=3), num_warmup=5000, num_samples=5000)
mcmc.run(jax.random.PRNGKey(1), y=jnp.array(self.y), times=jnp.array(self.times), N=self.N)
mcmc.print_summary()
samples = mcmc.get_samples()
#incs = samples["yhat"]
# Generate posterior predictive samples using previously drawn MCMC samples
from numpyro.infer import Predictive
# Define model as used in control_fit (reusing trace)
predictive = Predictive(model
,posterior_samples=samples
,return_sites=["yhat"])
preds = predictive(jax.random.PRNGKey(2)
,y = jnp.array(self.y)
,times = jnp.array(self.times)
,N = self.N
,forecast = True)
yhats = preds["yhat"]
self.samples = samples
return times, yhats, samples
if __name__ == "__main__":
np.random.seed(1010)
#--this simulation is at a daily temporal scale
framework = compartment_forecast_with_GP(N = 1000
, start = 0
, end = 32
, infectious_period = 2)
times, infections, all_states = framework.simulation(I0=5,repo=2)
#--aggregating up to week temporal scale
weeks = np.arange(0,32)
weekly_infections = infections.reshape(32,-1).sum(-1)
#--lets further assume we know only the first 10 time units
full_weekly_infections = weekly_infections
weekly_infections = np.array([ float(x) for x in weekly_infections])
weekly_infections[10:] = np.nan
#--time paraemters
start,end = min(weeks), max(weeks)+1
#--Control model only uses X = time in the kernel
X = np.arange( 1,end+1 ).reshape(-1,1)
#--model fit for control
framework = compartment_forecast_with_GP(N = 1000
, times = weeks
, y = weekly_infections
, X = X
, infectious_period = 2)
times,infections,samples = framework.control_fit()
colors = sns.color_palette("tab10",2)
plt.style.use("science")
fig, ax = plt.subplots()
ax.scatter(weeks, full_weekly_infections,s=8, color="black")
ax.set_xlabel("MMWR week", fontsize=8)
ax.set_ylabel("Incident cases", fontsize=8)
ax.axvline(9,color="black",ls="--")
lower1,lower2,lower3,middle,upper3,upper2,upper1 = np.percentile(infections,[2.5, 10, 25,50,75,90,97.5],axis=0)
ax.fill_between(weeks,lower1,upper1,alpha=0.2 ,color=colors[0])
ax.fill_between(weeks,lower2,upper2,alpha=0.2 ,color=colors[0])
ax.fill_between(weeks,lower2,upper2,alpha=0.2 ,color=colors[0])
ax.plot( weeks,middle ,lw=1.5, color=colors[0])
plt.show()