-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStochastic SIR Model 1.py
210 lines (185 loc) · 9.3 KB
/
Stochastic SIR Model 1.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
# -*- coding: utf-8 -*-
"""
@authors: Kaixin and Suzan
Stochastic Model 1
"""
import numpy as np
import matplotlib.pyplot as plt
import probability
from matplotlib import mpl
def main():
"""
Simulate the spread of disease n times, for a time period of population.time.
"""
# run through the different combinations of probabilities.
probCatch_vector=[0.1,0.5,0.9]
probBeSusceptible_vector=[0.1,0.5,0.9]
probSus_vector=[0.1,0.5,0.9]
probInf_vector=[0.1,0.5,0.9]
for k in range(2,3):
for m in range(1,3):
for c in range(0,3):
for s in range(0,3):
array_S_total = []
array_I_total = []
array_R_total = []
# simulate 100 times
rep = 1
while rep <= 100:
population = Population()
population.probCatch = probCatch_vector[c]
population.probBeSusceptible =probBeSusceptible_vector[s]
population.probSusceptible=probSus_vector[k]
population.probInfected=probInf_vector[m]
population.initial_values()
# simulate the spread of disease over time
t = 0
while t <= population.time:
population.show()
population.spread_of_disease()
population.array_T.append(t)
t = t + 1
array_S_total.append(population.array_S)
array_I_total.append(population.array_I)
array_R_total.append(population.array_R)
rep = rep + 1
array_means_S = np.mean(array_S_total, axis = 0)
array_means_I = np.mean(array_I_total, axis = 0)
array_means_R = np.mean(array_R_total, axis = 0)
array_sd_S = np.std(array_S_total, axis = 0)
array_sd_I = np.std(array_I_total, axis = 0)
array_sd_R = np.std(array_R_total, axis = 0)
name='Project2_probSus_'+str(probSus_vector[k])+'probInf_'+str(probInf_vector[m])+'probCatch__'+str(probCatch_vector[c])+'probBeSusceptible_'+str(probBeSusceptible_vector[s])
'''
np.savetxt('array_means_S'+name, array_means_S)
np.savetxt('array_means_I'+name, array_means_I)
np.savetxt('array_means_R'+name, array_means_R)
np.savetxt('array_S_total'+name, array_S_total)
np.savetxt('array_I_total'+name, array_I_total)
np.savetxt('array_R_total'+name, array_R_total)
'''
# plot the means of the simulation with error bars that represent the 95% confidence interval
plt.gca().set_color_cycle(['green', 'blue', 'red'])
plt.plot(population.array_T, array_means_S, label='S')
plt.plot(population.array_T, array_means_I, label='I')
plt.plot(population.array_T, array_means_R, label='R')
plt.errorbar(population.array_T, array_means_S, yerr = 2*array_sd_S)
plt.errorbar(population.array_T, array_means_I, yerr = 2*array_sd_I)
plt.errorbar(population.array_T, array_means_R, yerr = 2*array_sd_R)
plt.xlabel('Days')
plt.ylabel('Population')
plt.title('Stochastic model 1 \n probSus:'+str(probSus_vector[k])+'; probInf:'+str(probInf_vector[m])+';probCatch:'+str(probCatch_vector[c])+';probBeSusceptible:'+str(probBeSusceptible_vector[s]))
plt.legend(fontsize=8)
plt.savefig(name+'.png', dpi=1200)
# plot the 20-day-period graph
plt.clf()
plt.gca().set_color_cycle(['green', 'blue', 'red'])
plt.plot(population.array_T,array_means_S, label='S')
plt.plot(population.array_T, array_means_I, label='I')
plt.plot(population.array_T, array_means_R, label='R')
plt.xlim(0,20)
plt.errorbar(population.array_T, array_means_S, yerr = 2*array_sd_S)
plt.errorbar(population.array_T, array_means_I, yerr = 2*array_sd_I)
plt.errorbar(population.array_T, array_means_R, yerr = 2*array_sd_R)
plt.xlabel('Days')
plt.ylabel('Population')
plt.title('Stochastic model 1 (20 days) \n probSus:'+str(probSus_vector[k])+'; probInf:'+str(probInf_vector[m])+';probCatch:'+str(probCatch_vector[c])+';probBeSusceptible:'+str(probBeSusceptible_vector[s]))
plt.legend(fontsize=8)
plt.savefig('shorter_period_'+name+'.png', dpi=1200)
class Population():
def __init__(self):
"""
Initial creation of the population where the dimensions are determined.
"""
self.height = 50
self.width = 50
self.matrix = np.zeros((self.height,self.width))
self.probSusceptible = 0.1
self.probInfected = 0.1
self.probCatch = 0.5
self.probBeSusceptible = 0.5
self.susceptible = [0]
self.infected = [1,2]
self.immune = [3,4,5,6,7]
self.time = 100
self.array_S = []
self.array_I = []
self.array_R = []
self.array_T = []
self.probCatch = 0.9
self.probBeSusceptible = 0.5
def initial_values(self):
"""
Set the initial grid.
"""
self.value=[0,1,2,3,4,5,6,7]
self.prob=[self.probSusceptible, (1-self.probSusceptible)*self.probInfected+self.probSusceptible, 1]
self.matrix=[]
for i in range(self.height):
self.matrix.append([])
for j in range(self.width):
self.matrix[i].append(probability.initial(self.value,self.prob))
def show(self):
"""
Print grid to console and store the values for S, I and R per time step in arrays.
"""
self.image = self.matrix
self.cmap = mpl.colors.ListedColormap(['green', 'midnightblue', 'blue', 'darkred', 'red', 'tomato', 'salmon', 'lightsalmon'])
self.bounds = [-1, 1, 2, 3, 4, 5, 6, 7, 8]
self.norm = mpl.colors.BoundaryNorm(self.bounds, self.cmap.N)
grid = plt.imshow(self.image, interpolation = 'nearest', cmap = self.cmap, norm = self.norm)
cbar = plt.colorbar(grid, cmap=self.cmap, norm=self.norm, ticks=self.bounds, boundaries=self.bounds)
labels = ['S', 'I day 1', 'I day 2', 'R day 1', 'R day 2', 'R day 3', 'R day 4', 'R day 5']
cbar.set_ticklabels(labels)
plt.show()
plt.clf()
self.count_S = 0
self.count_I = 0
self.count_R = 0
for i in range(self.height):
for j in range(self.width):
if self.matrix[i][j] in self.susceptible:
self.count_S = self.count_S + 1
if self.matrix[i][j] in self.infected:
self.count_I = self.count_I + 1
if self.matrix[i][j] in self.immune:
self.count_R = self.count_R + 1
self.array_S.append(self.count_S)
self.array_I.append(self.count_I)
self.array_R.append(self.count_R)
def spread_of_disease(self):
"""
Examine state of the neighbors of each cell, and update new matrix for t+1.
"""
self.value1=[0,1]
self.prob1=[self.probCatch,1]
self.value2=[7,0]
self.prob2=[self.probBeSusceptible,1]
self.new_matrix = np.zeros((self.height, self.width))
for i in range(self.height):
for j in range(self.width):
self.neighbors = []
try:
self.neighbors.append(self.matrix[i+1][j])
except IndexError:
pass
if i-1 >= 0:
self.neighbors.append(self.matrix[i-1][j])
try:
self.neighbors.append(self.matrix[i][j+1])
except IndexError:
pass
if j-1 >= 0:
self.neighbors.append(self.matrix[i][j-1])
if self.matrix[i][j] == 0:
for k in range(len(self.neighbors)):
if self.neighbors[k] in self.infected:
self.new_matrix[i][j] = probability.ProbCatchF(self.value1,self.prob1)
break
elif self.matrix[i][j] > 0 and self.matrix[i][j] < 7:
self.new_matrix[i][j] = self.matrix[i][j] + 1
else:
self.new_matrix[i][j] = probability.ProBeSusceptibleF(self.value2,self.prob2)
self.matrix = self.new_matrix
if __name__ == "__main__":
main()