-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_simul_policy.py
More file actions
471 lines (370 loc) · 17.3 KB
/
class_simul_policy.py
File metadata and controls
471 lines (370 loc) · 17.3 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 19 18:28:27 2020
"""
from class_averageStats import averageStats
import numpy as np
import pickle,gzip
from class_simul import spSAYDR_behav
import matplotlib.pyplot as plt
class simul_policy(spSAYDR_behav) :
''' class simulating a lockdown
shutr: shut down shutr[1]% of people i if fraction in state I >shutr[0]
'''
def __init__(self, p_shutr, p_openr,
shutOnce = False,
*args,**kwargs,) :
spSAYDR_behav.__init__(self,*args,**kwargs)
self.p_shutr = p_shutr #this is to avoid hitting exactly the threshold
self.p_openr = p_openr
self.shutOnce = shutOnce
self.reopened = False
# draws shutdown-specific ids to lock down the same workers / firms
self.shutid = np.arange(self.q_popsize)
np.random.shuffle(self.shutid)
# define vectors of people shut down by govt
self.shut = np.zeros(self.q_popsize,dtype=bool)
self.outside = np.ones(self.q_days) # fraction of people outside
self.shutdownMode = False
def shutdown(self,day) :
# detect people shut down by government
self.shut = np.zeros(self.q_popsize,dtype=bool)
#### WARNING -2 vs -1
fY = self.prstates[day,self.I]+self.prstates[day,self.Y]
fYprev = self.prstates[day-2,self.I]+self.prstates[day-2,self.Y]
# shut either when allowed multiple times, or when never reopened
if (self.shutOnce == False or self.reopened == False) :
# if going over the shutdown threshold from below: shutdown
if (fY>=self.p_shutr[0]) & (fYprev<self.p_shutr[0]) & (self.shutdownMode==False):
self.shutdownMode=True
# if going over the reopen threshold from above: reopen
if (fY<self.p_openr[0]) and (fYprev>=self.p_openr[0]):
if self.shutdownMode==True:
self.shutdownMode = False
self.reopened = True
## now define masks of who is staying home
# shut down (p_shutr[1]%) of city if asymptomatics > p_shutr[0]%
if self.shutdownMode :
if self.p_shutr[1]==1 :
self.shut[:] = True
else:
lockedDown = self.p_shutr[1]*self.q_popsize
self.shut = (self.shutid<lockedDown)
if self.q_printOption>=2:
print('\t\t Shutdownmodes: ',self.shutdownMode)
return
def aDayInTheLife(self,day):
# save initial positions and figure out who is locked in
self.savepos = np.copy(self.pos)
self.shutdown(day)
# move locked in out of the box
self.pos[self.shut,1] = self.q_citysize*(2*(self.id[self.shut]+1))
# figure out who is scared and save their positions
self.scaredycats(day)
# move scared out of the box and far from each other
self.pos[self.scared,1] = self.q_citysize*(2*(self.id[self.scared]+1))
# compute who is outside that day for later reference
self.outside[day] = 1 - np.sum(self.shut | self.scared )/self.q_popsize
# generate infections, deaths and recoveries
self.infections(day)
self.symptoms(day)
self.deaths(day)
self.recoveries(day) ,
# return people to their position before moving them arund
self.pos = self.savepos
# move people around
self.pos = self.movepeople(self.pos)
self.lastday = day
class simul_policyByTime(simul_policy) :
# shut down at a specific time period defined by p_shutr[0]
def shutdown(self,day) :
# detect people shut down by government
self.shut = np.zeros(self.q_popsize,dtype=bool)
if day>=self.p_shutr[0] :
self.shutdownMode = True
if day>=self.p_openr[0]:
self.shutdownMode = False
## now define masks of who is staying home
# shut down (p_shutr[1]%) of city if asymptomatics > p_shutr[0]%
if self.shutdownMode :
if self.p_shutr[1]==1 :
self.shut[:] = True
else:
lockedDown = self.p_shutr[1]*self.q_popsize
self.shut = (self.shutid<lockedDown)
if self.q_printOption>=2:
print('\t\t Shutdownmodes: ',self.shutdownMode)
return
def simulatePool(kwargs) :
''' function that simulates the model once, to be used in conjunction
with multiprocessing.Pool to get multiple replications quickly
'''
m = simul_policy(**kwargs)
m.simulateOnce()
m.summaryStats(m.lastday,nondef=kwargs,savefile='output/results.txt',clear='a')
return m
def policyTimePool(kwargs) :
''' function that simulates the model once, to be used in conjunction
with multiprocessing.Pool to get multiple replications quickly
'''
m = simul_policyByTime(**kwargs)
m.simulateOnce(stoprule=0)
m.summaryStats(m.lastday,nondef=kwargs,savefile='output/results.txt',clear='a')
return m
#%% simulate 10-5 lockdown policy from base model
if __name__ == "__main__":
from multiprocessing import Pool
benchkwargs = {"q_seed" : 2443,
"p_probc" : [[0,0.038130388,0.038130388,0,0]], #prob of contagion by type and state
"q_popsize" : 25600,
"p_infradius" : 0.013016,
"p_avgstep" : 0.033993284,
"p_stdstep" : 0,
"q_printOption" : 0.6,
'q_days' : 500,
'p_cluster' : 'cluster',
'p_shutr' : [0.10,0.30],
'p_openr' : [0.05,1],
}
# a = simul_policy(**benchkwargs)
# a.simulateOnce()
kwargs = benchkwargs.copy()
nboots = 4
nprocs = 4
iterlist = []
# change seeds
for bootn in range(nboots):
newkw = kwargs.copy()
newkw['q_seed'] = kwargs['q_seed'] + bootn
iterlist.append((newkw))
# spawn nboots processes, nprocs at once
pool = Pool(processes=nprocs)
allRandmodels = pool.map(simulatePool,(iterlist))
pool.close()
pool.join()
list(map(lambda x: delattr(x,'pos'),allRandmodels))
file = gzip.open('output/basePolicy-10-5-30pc.pickle.gz','wb')
pickle.dump(allRandmodels,file)
#%% Figures 10-5 base model 30pc
import pandas as pd
from class_SIRmodel import SIRmodel
# benchmark
file = open('output/zbasesim.pickle','rb')
bench = pickle.load(file)
file.close()
dfb = pd.DataFrame()
logsb = pd.DataFrame()
for idx,m in enumerate(bench) :
dfb[idx] = np.sum(m.nstates[:,1:3],axis=1)
dfb[str(idx)+'L1'] = dfb[idx].shift()
logsb[str(idx)] = np.log(dfb[idx]) - np.log(dfb[str(idx)+'L1'])
file = gzip.open('output/basePolicy-10-5-30pc.pickle.gz','rb')
pol = pickle.load(file)
file.close()
dfpol = pd.DataFrame()
logpol = pd.DataFrame()
for idx,m in enumerate(pol) :
dfpol[idx] = np.sum(m.nstates[:,1:3],axis=1)
dfpol[str(idx)+'L1'] = dfpol[idx].shift()
logpol[str(idx)] = np.log(dfpol[idx]) - np.log(dfpol[str(idx)+'L1'])
aa = SIRmodel(.03813*13.5,0.05,q_popsize=25600,q_init=30)
dfSIR = pd.DataFrame(aa.SIR,index=np.arange(aa.day+1),columns=['S','I','R'])
dfSIR['ILag'] = dfSIR['I'].shift()
dfSIR['ld'] = np.log(dfSIR['I']) - np.log(dfSIR['ILag'])
dfSIRld = np.array(dfSIR['ld'])
bb = SIRmodel(.03813*13.5,0.05,q_popsize=25600,
q_init=30,
behModel = {'type':'Lockdown','p_shutr':[0.1,.3],'p_openr':[0.05,1],'p_shutOnce': False}
)
dfSIRp = pd.DataFrame(bb.SIR,index=np.arange(bb.day+1),columns=['S','I','R'])
dfSIRp['ILag'] = dfSIRp['I'].shift()
dfSIRp['ld'] = np.log(dfSIRp['I']) - np.log(dfSIRp['ILag'])
dfSIRpld = np.array(dfSIRp['ld'])
bp = averageStats(bench)
bpol = averageStats(pol)
days = 180
#first plot: compare SIR with benchmark
fig,(ax,ax2,ax3) = plt.subplots(1,3,figsize=(15,5))
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.set_xlabel('Days ')
ax.plot(np.arange(days),logsb.mean(axis=1)[:days],color='olive',label ='Spatial-SIR')
ax.plot(np.arange(days),logpol.mean(axis=1)[:days],':',linewidth=1.9, color='darkorange',label ='Spatial-SIR with lockdown')
ax.plot(np.arange(days),dfSIRld[:days],'--',color='olive',label ='SIR')
ax.plot(np.arange(days),dfSIRpld[:days],'-.',color='darkOrange',label ='SIR with lockdown')
ax.legend(title='Infection growth rates')
ax2.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.set_xlabel('Days ')
ax2.plot(np.arange(days),bp.prinf[:days],color='olive',label ='Spatial-SIR')
ax2.plot(np.arange(days),bpol.prinf[:days],':',linewidth=1.9, color='darkorange',label ='Spatial-SIR with lockdown')
ax2.plot(np.arange(days),aa.SIR[:days,1]/25600,'--',color='olive',label ='SIR')
ax2.plot(np.arange(days),bb.SIR[:days,1]/25600,'-.',color='darkOrange',label ='SIR with lockdown')
# ax2.axhline(pol[0].p_shutr[0],color='olive',ls='dotted')
# ax2.axhline(pol[0].p_openr[0],color='olive',ls='-.')
days=230
bp.prtinf = np.append(bp.prtinf,[0]*300)
bp.prtinf[140:] = bp.prtinf[140]
aa.SIR = np.append(aa.SIR,[aa.SIR[aa.day,:]]*300,axis=0)
ax3.spines['right'].set_visible(False)
ax3.spines['top'].set_visible(False)
ax3.set_xlabel('Days ')
ax3.plot(np.arange(days),bp.prtinf[:days],color='olive',label ='Spatial-SIR')
ax3.plot(np.arange(days),bpol.prtinf[:days],':',linewidth=1.9, color='darkorange',label ='Spatial-SIR with lockdown')
ax3.plot(np.arange(days),1-aa.SIR[:days,0]/25600,'--',color='olive',label ='SIR')
ax3.plot(np.arange(days),1-bb.SIR[:days,0]/25600,'-.',color='darkOrange',label ='SIR with lockdown')
ax3.legend(title='Total cases',loc='center right')
ax2.legend(title='Active cases')
fig.tight_layout()
plt.savefig('../Covid BisinMoro/write/images/base_policy-10-5-30pc-comp.png')
#%% Compute 10-5-50pc
benchkwargs = {"q_seed" : 2443,
"p_probc" : [[0,0.038130388,0.038130388,0,0]], #prob of contagion by type and state
"q_popsize" : 25600,
"p_infradius" : 0.013016,
"p_avgstep" : 0.033993284,
"p_stdstep" : 0,
"q_printOption" : 0.6,
'q_days' : 500,
'p_cluster' : 'cluster',
'p_shutr' : [0.10,0.50],
'p_openr' : [0.05,1],
}
# a = simul_policy(**benchkwargs)
# a.simulateOnce()
kwargs = benchkwargs.copy()
nboots = 4
nprocs = 4
iterlist = []
# change seeds
for bootn in range(nboots):
newkw = kwargs.copy()
newkw['q_seed'] = kwargs['q_seed'] + bootn
iterlist.append((newkw))
# spawn nboots processes, nprocs at once
pool = Pool(processes=nprocs)
allRandmodels = pool.map(simulatePool,(iterlist))
pool.close()
pool.join()
list(map(lambda x: delattr(x,'pos'),allRandmodels))
file = gzip.open('output/basePolicy-10-5-50pc.pickle.gz','wb')
pickle.dump(allRandmodels,file)
#%% Figures 10-5 base model 30pc
import pandas as pd
from class_SIRmodel import SIRmodel
# benchmark
file = open('output/zbasesim.pickle','rb')
bench = pickle.load(file)
file.close()
dfb = pd.DataFrame()
logsb = pd.DataFrame()
for idx,m in enumerate(bench) :
dfb[idx] = np.sum(m.nstates[:,1:3],axis=1)
dfb[str(idx)+'L1'] = dfb[idx].shift()
logsb[str(idx)] = np.log(dfb[idx]) - np.log(dfb[str(idx)+'L1'])
file = gzip.open('output/basePolicy-10-5-50pc.pickle.gz','rb')
pol = pickle.load(file)
file.close()
dfpol = pd.DataFrame()
logpol = pd.DataFrame()
for idx,m in enumerate(pol) :
dfpol[idx] = np.sum(m.nstates[:,1:3],axis=1)
dfpol[str(idx)+'L1'] = dfpol[idx].shift()
logpol[str(idx)] = np.log(dfpol[idx]) - np.log(dfpol[str(idx)+'L1'])
aa = SIRmodel(.03813*13.5,0.05,q_popsize=25600,q_init=30)
dfSIR = pd.DataFrame(aa.SIR,index=np.arange(aa.day+1),columns=['S','I','R'])
dfSIR['ILag'] = dfSIR['I'].shift()
dfSIR['ld'] = np.log(dfSIR['I']) - np.log(dfSIR['ILag'])
dfSIRld = np.array(dfSIR['ld'])
bb = SIRmodel(.03813*13.5,0.05,q_popsize=25600,
q_init=30,
behModel = {'type':'Lockdown','p_shutr':[0.1,.5],'p_openr':[0.05,1],'p_shutOnce': False}
)
dfSIRp = pd.DataFrame(bb.SIR,index=np.arange(bb.day+1),columns=['S','I','R'])
dfSIRp['ILag'] = dfSIRp['I'].shift()
dfSIRp['ld'] = np.log(dfSIRp['I']) - np.log(dfSIRp['ILag'])
dfSIRpld = np.array(dfSIRp['ld'])
bp = averageStats(bench)
bpol = averageStats(pol)
days = 180
#first plot: compare SIR with benchmark
fig,(ax,ax2,ax3) = plt.subplots(1,3,figsize=(15,5))
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.set_xlabel('Days ')
ax.plot(np.arange(days),logsb.mean(axis=1)[:days],color='olive',label ='Spatial-SIR')
ax.plot(np.arange(days),logpol.mean(axis=1)[:days],':',linewidth=1.9, color='darkorange',label ='Spatial-SIR with lockdown')
ax.plot(np.arange(days),dfSIRld[:days],'--',color='olive',label ='SIR')
ax.plot(np.arange(days),dfSIRpld[:days],'-.',color='darkOrange',label ='SIR with lockdown')
ax.legend(title='Infection growth rates')
ax2.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.set_xlabel('Days ')
ax2.plot(np.arange(days),bp.prinf[:days],color='olive',label ='Spatial-SIR')
ax2.plot(np.arange(days),bpol.prinf[:days],':',linewidth=1.9, color='darkorange',label ='Spatial-SIR with lockdown')
ax2.plot(np.arange(days),aa.SIR[:days,1]/25600,'--',color='olive',label ='SIR')
ax2.plot(np.arange(days),bb.SIR[:days,1]/25600,'-.',color='darkOrange',label ='SIR with lockdown')
# ax2.axhline(pol[0].p_shutr[0],color='black',ls='dotted',linewidth=1)
# ax2.axhline(pol[0].p_openr[0],color='black',ls='-.',linewidth=.8)
days=230
bp.prtinf = np.append(bp.prtinf,[0]*300)
bp.prtinf[140:] = bp.prtinf[140]
aa.SIR = np.append(aa.SIR,[aa.SIR[aa.day,:]]*300,axis=0)
ax3.spines['right'].set_visible(False)
ax3.spines['top'].set_visible(False)
ax3.set_xlabel('Days ')
ax3.plot(np.arange(days),bp.prtinf[:days],color='olive',label ='Spatial-SIR')
ax3.plot(np.arange(days),bpol.prtinf[:days],':',linewidth=1.9, color='darkorange',label ='Spatial-SIR with lockdown')
ax3.plot(np.arange(days),1-aa.SIR[:days,0]/25600,'--',color='olive',label ='SIR')
ax3.plot(np.arange(days),1-bb.SIR[:days,0]/25600,'-.',color='darkOrange',label ='SIR with lockdown')
ax3.legend(title='Total cases', loc='center right')
ax2.legend(title='Active cases')
fig.tight_layout()
plt.savefig('../Covid BisinMoro/write/images/base_policy-10-5-50pc-comp.png')
#%% another figure option
# benchmark
file = open('output/zbasesim.pickle','rb')
bench = pickle.load(file)
file.close()
file = gzip.open('output/basePolicy-10-5-50pc.pickle.gz','rb')
pol = pickle.load(file)
file.close()
file = gzip.open('output/basepolicy-10-5-30pc.pickle.gz','rb')
pol30 = pickle.load(file)
file.close()
bp = averageStats(bench)
bpol = averageStats(pol)
bpol30 = averageStats(pol30)
aa = SIRmodel(.03813*13.5,0.05,q_popsize=25600,q_init=30)
bb = SIRmodel(.03813*13.5,0.05,q_popsize=25600,
q_init=30,
behModel = {'type':'Lockdown','p_shutr':[0.1,.3],'p_openr':[0.05,1],'p_shutOnce': False}
)
cc = SIRmodel(.03813*13.5,0.05,q_popsize=25600,
q_init=30,
behModel = {'type':'Lockdown','p_shutr':[0.1,.5],'p_openr':[0.05,1],'p_shutOnce': False}
)
#first plot: compare SIR with benchmark
days=250
fig,(ax,ax2) = plt.subplots(1,2,figsize=(10,5))
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.set_xlabel('Days ')
ax.set_ylabel('Percent infected')
ax.plot(np.arange(days),bp.prinf[:days],color='olive',label ='Benchmark')
ax.plot(np.arange(days),bpol30.prinf[:days],':',linewidth=1.9, color='saddlebrown',label ='30% lockdown')
ax.plot(np.arange(days),bpol.prinf[:days],'--', color='darkorange',label ='50% lockdown')
ax.legend(title='Spatial-SIR, Active cases')
days=130
ax2.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.set_xlabel('Days ')
ax2.plot(np.arange(days),aa.SIR[:days,1]/25600,'',color='olive',label ='SIR')
ax2.plot(np.arange(days),bb.SIR[:days,1]/25600,':',linewidth=1.9,color='saddlebrown',label ='SIR, 30% lockdown')
ax2.plot(np.arange(days),cc.SIR[:days,1]/25600,'--',color='darkorange',label ='SIR, 50% lockdown')
# ax2.axhline(pol[0].p_shutr[0],color='black',ls='dotted',linewidth=1)
# ax2.axhline(pol[0].p_openr[0],color='black',ls='-.',linewidth=.8)
ax2.legend(title='SIR model, Active cases')
fig.tight_layout()
plt.savefig('../Covid BisinMoro/write/images/base_policy-10-5-active-.png')