-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProblem 2
More file actions
145 lines (121 loc) · 4.05 KB
/
Copy pathProblem 2
File metadata and controls
145 lines (121 loc) · 4.05 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
import numpy as np
import random
from scipy.integrate import odeint
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import statistics
# Define starting conditions
N1 = 1000
S1 = 995
I1 = 5
R1 = N1-S1-I1
N2 = 1000
S2 = 1000
I2 = 0
R2 = N2-S2-I2
days = 100
t = 0.0
t_old = 0
time_step = 0.1
N_populations = 2
interaction_matrix = np.array([[1, 0.01], [0.01, 1]])
value_matrix = np.array([[S1, I1, R1], [S2, I2, R2]])
time = np.arange((t + time_step), (days + time_step), time_step)
N_list = [[N1],[N2]]
S_list = [[S1],[S2]]
I_list = [[I1],[I2]]
R_list = [[R1],[R2]]
t_list = [[t],[t]]
N_smooth_list = [[N1],[N2]]
S_smooth_list = [[S1],[S2]]
I_smooth_list = [[I1],[I2]]
R_smooth_list = [[R1],[R2]]
mu = 0.01
beta = [[1.2], [1.2]]
gamma = [[0.8], [0.8]]
# Defining all events, changes in [S, I, R]
E1 = [1, 0, 0] #birth
E2 = [-1, 1, 0] #infection
E3 = [0, -1, 1] #recovery
E4 = [-1, 0, 0] #deathS
E5 = [0, -1, 0] #deathI
E6 = [0, 0 , -1] #deathR
e_list = [E1, E2, E3, E4, E5, E6]
#e_list = [E2, E3]
for interval in time:
#find the most recent numbers and calculate N's of every population
value_matrix = np.array([[S_smooth_list[0][-1], I_smooth_list[0][-1], R_smooth_list[0][-1]], [S_smooth_list[1][-1], I_smooth_list[1][-1], R_smooth_list[1][-1]]])
N_vector = value_matrix @ np.array([[1], [1], [1]])
#calculate beta/N
beta_N = beta / N_vector
first_matrix = np.identity(2) * beta_N
infecteds = np.array([value_matrix[:,1]]).T
susceptebles = np.array([value_matrix[:,0]]).T
recoverds = np.array([value_matrix[:,2]]).T
#calculate lambdas
lapda = first_matrix @ (interaction_matrix @ infecteds)
#Event rates are calculated for all populations
r_b = mu * N_vector.T[0]
r_i = (lapda * susceptebles).T[0]
r_r = (gamma * infecteds).T[0]
r_ds = mu * susceptebles.T[0]
r_di = mu * infecteds.T[0]
r_dr = mu * recoverds.T[0]
r_list = np.array([r_b, r_i, r_r, r_ds, r_di, r_dr]).T
r_t = r_list @ np.array([[1], [1], [1], [1], [1], [1]])
for pop in range(0,N_populations):
#find specific time in that population and numbers
t = t_list[pop][-1]
S = S_list[pop][-1]
I = I_list[pop][-1]
R = I_list[pop][-1]
r_list_pop = r_list[pop]
r_t_pop = r_t[pop]
#if the time falls within this interval event happens
while interval > t:
#calculate the step in time untill next event
dt = -(1/r_t_pop) * np.log(random.uniform(0, 1))
p = r_t_pop * random.uniform(0, 1)
#update the time of the event
t += dt
p0 = 0
i = 0
#find the event that happens
while p0 < p:
p0 += r_list_pop[i]
if p0 > p:
break
i += 1
#find changes in S I and R and recalculate N
S += e_list[i][0]
I += e_list[i][1]
R += e_list[i][2]
N = S + I + R
S_list[pop].append(S)
I_list[pop].append(I)
R_list[pop].append(R)
N_list[pop].append(N)
t_list[pop].append(t)
#after new events within the time interval are calculated update the smooth list of that population
S_smooth_list[pop].append(S)
I_smooth_list[pop].append(I)
R_smooth_list[pop].append(R)
N_smooth_list[pop].append(N)
t = 0.0
t_old = 0
time_step = 0.1
time_plot = np.arange(t, (days + time_step), time_step)
plt.figure(1)
plt.title('For β= '+str(beta)+' and γ = '+str(gamma))
plt.plot(time_plot, S_smooth_list[0], label='S1')
plt.plot(time_plot, S_smooth_list[1], label='S2')
plt.plot(time_plot, I_smooth_list[0], label='I1')
plt.plot(time_plot, I_smooth_list[1], label='I2')
plt.plot(time_plot, R_smooth_list[0], label='R1')
plt.plot(time_plot, R_smooth_list[1], label='R2')
plt.ylim(0, 1200)
plt.xlim(0, 100)
plt.ylabel('Amount of people')
plt.xlabel('Amount of days')
plt.legend()
plt.show()