流行病模型的 Python 代码示例,SEIR模型,直接写代码
时间: 2023-01-31 09:06:54 浏览: 142
seirsplus:SEIRS流行病学模型
好的,下面是一个关于 SEIR 流行病模型的 Python 代码示例:
```python
import numpy as np
class SEIRModel:
def __init__(self, beta, sigma, gamma, mu, initial_conditions):
"""
beta: transmission rate 传染率
sigma: incubation rate 潜伏期
gamma: recovery rate 恢复率
mu: mortality rate 死亡率
initial_conditions: list of 4 elements, representing the initial number of
susceptible, exposed, infected, and recovered individuals
"""
self.beta = beta
self.sigma = sigma
self.gamma = gamma
self.mu = mu
self.S, self.E, self.I, self.R = initial_conditions
def deriv(self, t, y):
"""
Returns the derivative of the SEIR model at time t.
"""
S, E, I, R = y
N = S + E + I + R
dSdt = -self.beta * S * I / N
dEdt = self.beta * S * I / N - self.sigma * E
dIdt = self.sigma * E - self.gamma * I - self.mu * I
dRdt = self.gamma * I
return dSdt, dEdt, dIdt, dRdt
def step(self, t, dt):
"""
Advances the model by dt time units.
"""
y = self.S, self.E, self.I, self.R
dydt = self.deriv(t, y)
S_inc, E_inc, I_inc, R_inc = [x * dt for x in dydt]
self.S += S_inc
self.E += E_inc
self.I += I_inc
self.R += R_inc
# 初始条件
N = 1000 # population size
initial_conditions = [N - 1, 0, 1, 0] # one infected individual
# 参数
beta = 0.25
sigma = 1.0
gamma = 0.1
mu = 0.01
# 创建模型
model = SEIRModel(beta, sigma, gamma, mu, initial_conditions)
# 模拟
t_max = 365 # run for one year
dt = 0.1 # time step
time_points = np.arange(0, t_max, dt)
S_history = []
E_history = []
I_history = []
R_history = []
for t in time_points:
S_history.append(model.S)
E_history.append(model.E)
I_history.append(model.I)
R_history.append(model.R)
model.step(t,
阅读全文