传染病模型拟合五个代码
时间: 2023-06-12 16:04:55 浏览: 89
代码 传染病的蔓延模型代码
以下是五个传染病模型拟合的代码示例:
1. SI模型拟合代码:
```python
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import minimize
def SI_model(init_vals, t, beta, N):
S, I = init_vals
dSdt = -beta * S * I / N
dIdt = beta * S * I / N
return dSdt, dIdt
def loss_fn_SI(params, t, data):
N = 100000 # 总人口
init_vals = 0.99*N, 0.01*N # 初始值
beta = params[0]
sol = odeint(SI_model, init_vals, t, args=(beta, N))
return np.mean((sol[:, 1] - data)**2)
data = np.array([2, 5, 10, 20, 50, 100, 200, 400, 800, 1600, 3200]) # 感染人数
t = np.arange(len(data)) # 时间
res = minimize(loss_fn_SI, [0.001], args=(t, data), method='L-BFGS-B', bounds=[(0, 1)]) # 拟合
beta = res.x[0] # 拟合参数
print('beta: ', beta)
```
2. SIR模型拟合代码:
```python
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import minimize
def SIR_model(init_vals, t, beta, gamma, N):
S, I, R = init_vals
dSdt = -beta * S * I / N
dIdt = beta * S * I / N - gamma * I
dRdt = gamma * I
return dSdt, dIdt, dRdt
def loss_fn_SIR(params, t, data):
N = 100000 # 总人口
init_vals = 0.99*N, 0.01*N, 0 # 初始值
beta, gamma = params
sol = odeint(SIR_model, init_vals, t, args=(beta, gamma, N))
return np.mean((sol[:, 1] - data)**2)
data = np.array([2, 5, 10, 20, 50, 100, 200, 400, 800, 1600, 3200]) # 感染人数
t = np.arange(len(data)) # 时间
res = minimize(loss_fn_SIR, [0.001, 0.001], args=(t, data), method='L-BFGS-B', bounds=[(0, 1), (0, 1)]) # 拟合
beta, gamma = res.x # 拟合参数
print('beta: ', beta)
print('gamma: ', gamma)
```
3. SEIR模型拟合代码:
```python
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import minimize
def SEIR_model(init_vals, t, beta, sigma, gamma, N):
S, E, I, R = init_vals
dSdt = -beta * S * I / N
dEdt = beta * S * I / N - sigma * E
dIdt = sigma * E - gamma * I
dRdt = gamma * I
return dSdt, dEdt, dIdt, dRdt
def loss_fn_SEIR(params, t, data):
N = 100000 # 总人口
init_vals = 0.99*N, 0.01*N, 0, 0 # 初始值
beta, sigma, gamma = params
sol = odeint(SEIR_model, init_vals, t, args=(beta, sigma, gamma, N))
return np.mean((sol[:, 2] - data)**2)
data = np.array([2, 5, 10, 20, 50, 100, 200, 400, 800, 1600, 3200]) # 感染人数
t = np.arange(len(data)) # 时间
res = minimize(loss_fn_SEIR, [0.001, 0.001, 0.001], args=(t, data), method='L-BFGS-B', bounds=[(0, 1), (0, 1), (0, 1)]) # 拟合
beta, sigma, gamma = res.x # 拟合参数
print('beta: ', beta)
print('sigma: ', sigma)
print('gamma: ', gamma)
```
4. SIS模型拟合代码:
```python
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import minimize
def SIS_model(init_vals, t, beta, gamma, N):
S, I = init_vals
dSdt = -beta * S * I / N + gamma * I
dIdt = beta * S * I / N - gamma * I
return dSdt, dIdt
def loss_fn_SIS(params, t, data):
N = 100000 # 总人口
init_vals = 0.99*N, 0.01*N # 初始值
beta, gamma = params
sol = odeint(SIS_model, init_vals, t, args=(beta, gamma, N))
return np.mean((sol[:, 1] - data)**2)
data = np.array([2, 5, 10, 20, 50, 100, 200, 400, 800, 1600, 3200]) # 感染人数
t = np.arange(len(data)) # 时间
res = minimize(loss_fn_SIS, [0.001, 0.001], args=(t, data), method='L-BFGS-B', bounds=[(0, 1), (0, 1)]) # 拟合
beta, gamma = res.x # 拟合参数
print('beta: ', beta)
print('gamma: ', gamma)
```
5. SEIRS模型拟合代码:
```python
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import minimize
def SEIRS_model(init_vals, t, beta, sigma, gamma, xi, N):
S, E, I, R, D = init_vals
dSdt = -beta * S * I / N
dEdt = beta * S * I / N - sigma * E
dIdt = sigma * E - (1 - xi) * gamma * I - xi * D
dRdt = (1 - xi) * gamma * I
dDdt = xi * gamma * I
return dSdt, dEdt, dIdt, dRdt, dDdt
def loss_fn_SEIRS(params, t, data):
N = 100000 # 总人口
init_vals = 0.99*N, 0.01*N, 0, 0, 0 # 初始值
beta, sigma, gamma, xi = params
sol = odeint(SEIRS_model, init_vals, t, args=(beta, sigma, gamma, xi, N))
return np.mean((sol[:, 2] - data)**2)
data = np.array([2, 5, 10, 20, 50, 100, 200, 400, 800, 1600, 3200]) # 感染人数
t = np.arange(len(data)) # 时间
res = minimize(loss_fn_SEIRS, [0.001, 0.001, 0.001, 0.001], args=(t, data), method='L-BFGS-B', bounds=[(0, 1), (0, 1), (0, 1), (0, 1)]) # 拟合
beta, sigma, gamma, xi = res.x # 拟合参数
print('beta: ', beta)
print('sigma: ', sigma)
print('gamma: ', gamma)
print('xi: ', xi)
```
上述代码中,分别使用了SI、SIR、SEIR、SIS和SEIRS五种常见的传染病模型进行拟合,并输出了拟合参数。实际应用中,可以根据不同的疾病特性和数据情况选择合适的模型进行拟合。
阅读全文