generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
02_tutorial.zombies.py
262 lines (133 loc) · 4.18 KB
/
02_tutorial.zombies.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
#!/usr/bin/env python
# coding: utf-8
# # Introduction to MCMC on Dynamical Systems Using Zombies
# In[1]:
#| hide
#skip
get_ipython().system(' [ -e /content ] && pip install -Uqq pyndamics3 emcee # upgrade pyndamics3 on colab')
# In[2]:
get_ipython().run_line_magic('pylab', 'inline')
# In[3]:
from pyndamics3 import Simulation
# In[4]:
from pyndamics3.mcmc import *
# ## SIR Model
# In[5]:
sim=Simulation()
sim.add("S'=-β*S*I",1,plot=1)
sim.add("I'=β*S*I-ζ*I",.001,plot=1)
sim.add("R'=ζ*I",0,plot=1)
sim.params(β=5,ζ=1)
sim.run(0,10)
# ## SEIR Model
# In[6]:
sim=Simulation()
sim.add("S'=-β*S*I",1,plot=1)
sim.add("E'=β*S*I-ζ*E",0,plot=1)
sim.add("I'=ζ*E-α*I",.001,plot=1)
sim.add("R'=α*I",0,plot=1)
sim.params(α=.3,β=10,ζ=.5)
sim.run(0,10)
# ## SZR Model from Munz et al. (2009)
#
# Notice that no matter what the parameters are changed to, Z (zombies) always win.
# In[7]:
sim=Simulation()
sim.add("S'=Π-β*S*Z-δ*S",500,plot=1) #S (Susceptible)
sim.add("Z'=β*S*Z+ζ*R-α*S*Z",.002,plot=1) #Z (Zombie)
sim.add("R'=δ*S+α*S*Z-ζ*R",1,plot=False) #R (Removed)
sim.params(α=.005,β=.0095,ζ=.05, δ=.01,Π=0) #parameters changed to match the Munz et al. (2009) figures
sim.run(0,30)
# ## SEZR Model based on dynamics observed in 'Night of the Living Dead'
# Movie "data" from Night of the Living Dead
# In[8]:
t=array([0,1,1.5,3,4.5,5,5.75,5.9,10])
zombies=array([1,1,3,8,10,20,28,30,40])
# In[9]:
sim=Simulation()
sim.add("S'=-β*S*Z-δ*S",178.5,plot=1)
sim.add("E'=β*S*Z-ζ*E",0,plot=False)
sim.add("Z'=ζ*E-α*S*Z",1,plot=1)
sim.add("R'=α*S*Z+δ*S",0,plot=False)
sim.params(α=.0342,β=.0445,ζ=4.63, δ=0.0)
sim.add_data(t=t,Z=zombies,plot=1)
sim.run(0,10)
# MCMC parameter estimation for $\alpha$ (rate of zombies being permanently removed), $\beta$ (rate of susceptibles becoming infected), $\zeta$ (the rate of infected into becoming zombies), and $\delta$ (suicide rate among susceptibles)
# In[18]:
model=MCMCModel(sim,
α=Uniform(0,.5),
β=Uniform(0,.5),
ζ=Uniform(0,10),
δ=Uniform(0,.01),
)
# In[19]:
number_of_iterations=500 # use 500 or so for the figures below, but for CI timeout reasons I include only 5
model.run_mcmc(number_of_iterations,repeat=3)
model.plot_chains()
# In[20]:
sim.run(0,10)
# In[22]:
Ro=model.eval('β/α')
# In[23]:
model.plot_distributions(Ro)
# In[24]:
model.plot_many(0,13,'Z')
# In[25]:
model.triangle_plot()
# In[26]:
model.plot_distributions()
# ## SEZR Model based on dynamics observed in 'Shaun of the Dead'
#
# Data from Shaun of the Dead
# In[5]:
t=array([0,3,5,6,8,10,22,22.2,22.5,24,25.5,26,26.5,27.5,27.75,28.5,29,29.5,31.5])
zombies=array([0,1,2,2,3,3,4,6,2,3,5,12,15,25,37,25,65,80,100])
# In[6]:
sim=Simulation()
sim.add("S'=-β*S*Z",508.2,plot=1)
sim.add("E'=β*S*Z-ζ*E",0,plot=0)
sim.add("Z'=ζ*E-α*S*Z",.000347759,plot=1)
sim.add("R'=α*S*Z",0,plot=False)
sim.params(α=2.96e-8,β=0.000808995,ζ=60)
sim.add_data(t=t,Z=zombies,plot=1)
sim.run(0,50)
# In[86]:
model=MCMCModel(sim,
α=Uniform(0,.01),
β=Uniform(0,.01),
ζ=Uniform(0,100),
)
# In[87]:
model.run_mcmc(2*number_of_iterations,repeat=3)
model.plot_chains()
# In[88]:
model.plot_distributions()
# In[90]:
model.plot_many(0,35,'Z')
# In[91]:
model.triangle_plot()
# ## With different priors
# In[5]:
t=array([0,3,5,6,8,10,22,22.2,22.5,24,25.5,26,26.5,27.5,27.75,28.5,29,29.5,31.5])
zombies=array([0,1,2,2,3,3,4,6,2,3,5,12,15,25,37,25,65,80,100])
sim=Simulation()
sim.add("S'=-β*S*Z",508.2,plot=1)
sim.add("E'=β*S*Z-ζ*E",0,plot=0)
sim.add("Z'=ζ*E-α*S*Z",.000347759,plot=1)
sim.add("R'=α*S*Z",0,plot=False)
sim.params(α=2.96e-8,β=0.000808995,ζ=60)
sim.add_data(t=t,Z=zombies,plot=1)
sim.run(0,50)
model=MCMCModel(sim,
α=Uniform(0,.01),
β=Uniform(0,.01),
ζ=Normal(10,10,all_positive=True)
)
# In[6]:
model.run_mcmc(800,repeat=2)
model.plot_chains()
# In[9]:
model.plot_many(0,35,'Z')
# In[8]:
model.plot_distributions()
# In[ ]: