-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtmc_sir.py
79 lines (67 loc) · 1.78 KB
/
dtmc_sir.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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def Prob_dist(time, population):
t = time
d_dt = 0.01
beta = d_dt
b = d_dt / 4
gamma = d_dt / 4
N = population
Trans = np.zeros(((N+1) , (N+1)))
Z = np.zeros(((time+1), (N+1)))
v = np.array(np.arange(0,N+1,1.0))
Z[0][2] = 1.0
bt = np.array([N - x for x in v]) / N
bt = np.array(map(lambda x,y: x * y * beta, v, bt))
dt = (b + gamma) * v
for i in range(1,N) :
Trans[i][i] = 1 - bt[i] - dt[i]
Trans[i][i+1] = dt[i + 1]
Trans[i+1][i] = bt[i]
Trans[0][0] = 1
Trans[0][1] = dt[1]
Trans[N][N] = 1 - dt[N]
for j in range(t) :
y = np.matmul(Trans, Z[j])
Z[j + 1] = np.transpose(y)
i_s = range(N+1)
t_s = range(t+1)
hf = plt.figure()
ha = hf.add_subplot(111, projection='3d')
X, Y = np.meshgrid(i_s, t_s)
ha.plot_wireframe(X, Y, Z)
ha.set_xlabel('# infected')
ha.set_ylabel('Time')
ha.set_zlabel('Probability')
hf.suptitle('P[I(t)] for N = %d' % N)
plt.show()
return Trans
def simulate(prob, iterations, interval):
times = np.array(range(interval + 1))
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
for i in range(iterations):
infecteds = np.zeros(len(times))
i_t = 2
for t in range(interval + 1):
if i_t:
p_ij = np.array([prob[i_t][i_t -1], prob[i_t][i_t], prob[i_t][i_t + 1]])
i_t += np.random.choice(range(-1,2), p = (p_ij / np.sum(p_ij)))
infecteds[t] = i_t
else:
break
plt.plot(times, infecteds, colors[i])
plt.xlabel('Time')
plt.ylabel('Number Infected')
def deterministic(I_t):
a = ((0.25 / 100) * (100 - I_t)) - 0.5
return np.exp(a * I_t) + 2
f = np.vectorize(deterministic)
x = np.array(np.arange(0, interval + 0.1, 0.1))
y = f(x)
plt.plot(x,y, 'k')
plt.show()
def main():
P = Prob_dist(2000, 100)
simulate(P, 4, 2000)
main()