-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsir_data.py
61 lines (48 loc) · 1.22 KB
/
sir_data.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
"""
Make some data
"""
# Load packages
import numpy as np
import sciris as sc
import pylab as pl
sc.options(dpi=150)
# Define our parameters
beta = 0.5
gamma = 0.2
npts = 100
I0 = 3
N = 1000
dt = 1
for randomize in [True, False]:
seed = 1
noise = 1.0
np.random.seed(seed)
# Make arrays where we will store the estimates
x = np.arange(npts)
S = np.zeros(npts)
I = np.zeros(npts)
R = np.zeros(npts)
# Initial conditions
S[0] = N - I0
I[0] = I0
# Simulate the model over time
for t in x[:-1]:
infections = beta * S[t] * I[t]/N * dt
if randomize:
infections = np.random.poisson(infections) # Randomise it
recoveries = gamma * I[t] * dt
S[t + 1] = S[t] - infections
I[t + 1] = I[t] + infections - recoveries
R[t + 1] = R[t] + recoveries
# Plot the model estimate of the number of infections alongside the data
time = x * dt
if randomize:
pl.scatter(time, I, label='Data')
else:
pl.plot(time, I, label='Model', c='k')
pl.legend()
pl.show()
# Save data
if randomize:
df = sc.dataframe(dict(day=time, cases=I))
df.to_csv('flu_cases.csv')