我已经为上述模型创建了一些非常基本的实现。然而,尽管图表看起来是正确的,但数字加起来并不是一个常数。那是因为每个隔间中的易感者/感染者/康复者的总和应该加起来为 N(这是总人数),但事实并非如此,由于某种原因,它加起来是一些奇怪的小数,我真的不知道如何解决它,现在看了 3 天。
SI 模型:
import matplotlib.pyplot as plt
N = 1000000
S = N - 1
I = 1
beta = 0.6
sus = [] # infected compartment
inf = [] # susceptible compartment
prob = [] # probability of infection at time t
def infection(S, I, N):
t = 0
while (t < 100):
S = S - beta * ((S * I / N))
I = I + beta * ((S * I) / N)
p = beta * (I / N)
sus.append(S)
inf.append(I)
prob.append(p)
t = t + 1
infection(S, I, N)
figure = plt.figure()
figure.canvas.set_window_title('SI model')
figure.add_subplot(211)
inf_line, =plt.plot(inf, label='I(t)')
sus_line, = plt.plot(sus, label='S(t)')
plt.legend(handles=[inf_line, sus_line])
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) # use scientific notation
ax = figure.add_subplot(212)
prob_line = plt.plot(prob, label='p(t)')
plt.legend(handles=prob_line)
type(ax) # matplotlib.axes._subplots.AxesSubplot
# manipulate
vals = ax.get_yticks()
ax.set_yticklabels(['{:3.2f}%'.format(x*100) for x in vals])
plt.xlabel('T')
plt.ylabel('p')
plt.show()
SIS 模型:
import matplotlib.pylab as plt
N = 1000000
S = N - 1
I = 1
beta = 0.3
gamma = 0.1
sus = \[\]
inf = \[\]
def infection(S, I, N):
for t in range (0, 1000):
S = S - (beta*S*I/N) + gamma * I
I = I + (beta*S*I/N) - gamma * I
sus.append(S)
inf.append(I)
infection(S, I, N)
figure = plt.figure()
figure.canvas.set_window_title('SIS model')
inf_line, =plt.plot(inf, label='I(t)')
sus_line, = plt.plot(sus, label='S(t)')
plt.legend(handles=\[inf_line, sus_line\])
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xlabel('T')
plt.ylabel('N')
plt.show()
SIR 型号:
import matplotlib.pylab as plt
N = 1000000
S = N - 1
I = 1
R = 0
beta = 0.5
mu = 0.1
sus = []
inf = []
rec = []
def infection(S, I, R, N):
for t in range (1, 100):
S = S -(beta * S * I)/N
I = I + ((beta * S * I)/N) - R
R = mu * I
sus.append(S)
inf.append(I)
rec.append(R)
infection(S, I, R, N)
figure = plt.figure()
figure.canvas.set_window_title('SIR model')
inf_line, =plt.plot(inf, label='I(t)')
sus_line, = plt.plot(sus, label='S(t)')
rec_line, = plt.plot(rec, label='R(t)')
plt.legend(handles=[inf_line, sus_line, rec_line])
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xlabel('T')
plt.ylabel('N')
plt.show()
最佳答案
我只会看 SI 模型。
您的两个关键变量是S
和I
。 (您可能已经颠倒了这两个变量的含义,尽管这不会影响我在这里写的内容。)您将它们初始化,使它们的总和为 N
,即常量 1000000
.
您更新行中的两个关键变量
S = S - beta * ((S * I / N))
I = I + beta * ((S * I) / N)
您显然打算将相同的值加到 I
并从 S
中减去相同的值,因此 S
和 I
不变。但是,实际上你先改变了S
,然后用那个新值去改变I
,所以加减的值实际上并不相同,变量的总和也没有保持不变。
您可以使用 Python 在一行中更新多个变量的功能来解决此问题。将这两行替换为
S, I = S - beta * ((S * I / N)), I + beta * ((S * I) / N)
这会在更新变量之前计算两个新值,因此实际上从两个变量中添加和减去相同的值。 (还有其他方法可以获得相同的效果,例如更新值的临时变量,或者一个临时变量来存储要添加和减去的量,但是既然你使用 Python,你也可以使用它的功能。)
当我现在运行程序时,我得到了这些图表:
我想这就是你想要的。
关于python - 正确实现 SI、SIS、SIR 模型(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47366712/