python實現SI、SIS、SIR、SIRS、SEIR、SEIRS模型


在家閒着無事,搜了一些關於傳染病模型的知識,在此做個總結。

SI

最簡單的SI模型首先把人羣分爲2種,一種是易感者(Susceptibles),易感者是健康的人羣,用S表示其人數,另外一種是感染者(The Infected),人數用 I來表示。
假設:
1、在疾病傳播期間總人數N不變,N=S+I
2、每個病人每天接觸人數爲定值

import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt

# N爲人羣總數
N = 10000
# β爲傳染率係數
beta = 0.25
# gamma爲恢復率係數
gamma = 0
# I_0爲感染者的初始人數
I_0 = 1
# S_0爲易感者的初始人數
S_0 = N - I_0
# 感染者每天接觸人數
P = 1
# T爲傳播時間
T = 150

# INI爲初始狀態下的數組
INI = (S_0,I_0)


def funcSI(inivalue,_):
    Y = np.zeros(2)
    X = inivalue
    # 易感個體變化
    Y[0] = - (P * beta * X[0] * X[1]) / N + gamma * X[1]
    # 感染個體變化
    Y[1] = (P * beta * X[0] * X[1]) / N - gamma * X[1]
    return Y

T_range = np.arange(0,T + 1)

RES = spi.odeint(funcSI,INI,T_range)


plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'red',label = 'Infection',marker = '.')
plt.title('SI Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()

在這裏插入圖片描述

SIS

在SI模型基礎上加入康復的概率

import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt

# N爲人羣總數
N = 10000
# β爲傳染率係數
beta = 0.25
# gamma爲恢復率係數
gamma = 0.05
# I_0爲感染者的初始人數
I_0 = 1
# S_0爲易感者的初始人數
S_0 = N - I_0
# T爲傳播時間
T = 150

# INI爲初始狀態下的數組
INI = (S_0,I_0)


def funcSIS(inivalue,_):
    Y = np.zeros(2)
    X = inivalue
    # 易感個體變化
    Y[0] = - (beta * X[0]) / N * X[1] + gamma * X[1]
    # 感染個體變化
    Y[1] = (beta * X[0] * X[1]) / N - gamma * X[1]
    return Y

T_range = np.arange(0,T + 1)

RES = spi.odeint(funcSIS,INI,T_range)


plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'red',label = 'Infection',marker = '.')
plt.title('SIS Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()

在這裏插入圖片描述

SIR

SIR是三個單詞首字母的縮寫,其中S是Susceptible的縮寫,表示易感者;I是Infective的縮寫,表示感染者;R是Removal的縮寫,表示移除者。這個模型本身是在研究這三者的關係。在病毒最開始的時候,所有人都是易感者,也就是所有人都有可能中病毒;當一部分人在接觸到病毒以後中病毒了,變成了感染者;感染者會接受各種治療,最後變成了移除者。
在這裏插入圖片描述
該模型有兩個假設條件
1.一段時間內總人數N是不變的,也就是不考慮新生以及自然死亡的人數
2.從S到I的變化速度α、從I到R的變化速度β也是保持不變的
3.移除者不再被感染

import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt

# N爲人羣總數
N = 10000
# β爲傳染率係數
beta = 0.25
# gamma爲恢復率係數
gamma = 0.05
# I_0爲感染者的初始人數
I_0 = 1
# R_0爲治癒者的初始人數
R_0 = 0
# S_0爲易感者的初始人數
S_0 = N - I_0 - R_0
# T爲傳播時間
T = 150

# INI爲初始狀態下的數組
INI = (S_0,I_0,R_0)


def funcSIR(inivalue,_):
    Y = np.zeros(3)
    X = inivalue
    # 易感個體變化
    Y[0] = - (beta * X[0] * X[1]) / N
    # 感染個體變化
    Y[1] = (beta * X[0] * X[1]) / N - gamma * X[1]
    # 治癒個體變化
    Y[2] = gamma * X[1]
    return Y

T_range = np.arange(0,T + 1)

RES = spi.odeint(funcSIR,INI,T_range)


plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'red',label = 'Infection',marker = '.')
plt.plot(RES[:,2],color = 'green',label = 'Recovery',marker = '.')
plt.title('SIR Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()

在這裏插入圖片描述

SIRS

與SIR不同在於,康復者的免疫力是暫時的,康復者會轉化爲易感者

import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt

# N爲人羣總數
N = 10000
# β爲傳染率係數
beta = 0.25
# gamma爲恢復率係數
gamma = 0.05
# Ts爲抗體持續時間
Ts = 7
# I_0爲感染者的初始人數
I_0 = 1
# R_0爲治癒者的初始人數
R_0 = 0
# S_0爲易感者的初始人數
S_0 = N - I_0 - R_0
# T爲傳播時間
T = 150

# INI爲初始狀態下的數組
INI = (S_0,I_0,R_0)


def funcSIRS(inivalue,_):
    Y = np.zeros(3)
    X = inivalue
    # 易感個體變化
    Y[0] = - (beta * X[0] * X[1]) / N + X[2] / Ts
    # 感染個體變化
    Y[1] = (beta * X[0] * X[1]) / N - gamma * X[1]
    # 治癒個體變化
    Y[2] = gamma * X[1] - X[2] / Ts
    return Y

T_range = np.arange(0,T + 1)

RES = spi.odeint(funcSIRS,INI,T_range)


plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'red',label = 'Infection',marker = '.')
plt.plot(RES[:,2],color = 'green',label = 'Recovery',marker = '.')
plt.title('SIRS Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()

在這裏插入圖片描述

SEIR(相比較貼合新冠狀病毒)

在其他模型的基礎上,加入傳染病潛伏期的存在,更貼合這次的新冠狀病毒

import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt

# N爲人羣總數
N = 10000
# β爲傳染率係數
beta = 0.6
# gamma爲恢復率係數
gamma = 0.1
# Te爲疾病潛伏期
Te = 14
# I_0爲感染者的初始人數
I_0 = 1
# E_0爲潛伏者的初始人數
E_0 = 0
# R_0爲治癒者的初始人數
R_0 = 0
# S_0爲易感者的初始人數
S_0 = N - I_0 - E_0 - R_0
# T爲傳播時間
T = 150

# INI爲初始狀態下的數組
INI = (S_0,E_0,I_0,R_0)


def funcSEIR(inivalue,_):
    Y = np.zeros(4)
    X = inivalue
    # 易感個體變化
    Y[0] = - (beta * X[0] * X[2]) / N
    # 潛伏個體變化(每日有一部分轉爲感染者)
    Y[1] = (beta * X[0] * X[2]) / N - X[1] / Te
    # 感染個體變化
    Y[2] = X[1] / Te - gamma * X[2]
    # 治癒個體變化
    Y[3] = gamma * X[2]
    return Y

T_range = np.arange(0,T + 1)

RES = spi.odeint(funcSEIR,INI,T_range)


plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'orange',label = 'Exposed',marker = '.')
plt.plot(RES[:,2],color = 'red',label = 'Infection',marker = '.')
plt.plot(RES[:,3],color = 'green',label = 'Recovery',marker = '.')

plt.title('SEIR Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()

在這裏插入圖片描述

SEIRS

同時有潛伏期且免疫暫時的條件

import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt

# N爲人羣總數
N = 10000
# β爲傳染率係數
beta = 0.6
# gamma爲恢復率係數
gamma = 0.1
# Ts爲抗體持續時間
Ts = 7
# Te爲疾病潛伏期
Te = 14
# I_0爲感染者的初始人數
I_0 = 1
# E_0爲潛伏者的初始人數
E_0 = 0
# R_0爲治癒者的初始人數
R_0 = 0
# S_0爲易感者的初始人數
S_0 = N - I_0 - E_0 - R_0
# T爲傳播時間
T = 150

# INI爲初始狀態下的數組
INI = (S_0,E_0,I_0,R_0)


def funcSEIRS(inivalue,_):
    Y = np.zeros(4)
    X = inivalue
    # 易感個體變化
    Y[0] = - (beta * X[0] * X[2]) / N + X[3] / Ts
    # 潛伏個體變化
    Y[1] = (beta * X[0] * X[2]) / N - X[1] / Te
    # 感染個體變化
    Y[2] = X[1] / Te - gamma * X[2]
    # 治癒個體變化
    Y[3] = gamma * X[2] - X[3] / Ts
    return Y

T_range = np.arange(0,T + 1)

RES = spi.odeint(funcSEIRS,INI,T_range)


plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'orange',label = 'Exposed',marker = '.')
plt.plot(RES[:,2],color = 'red',label = 'Infection',marker = '.')
plt.plot(RES[:,3],color = 'green',label = 'Recovery',marker = '.')

plt.title('SEIRS Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章