基本的传染病模型:SI、SIS、SIR及其Python代码实现
本文主要参考博客:http://chengjunwang.com/en/2013/08/learn-basic-epidemic-models-with-python/。该博客有一些笔误,并且有些地方表述不准确,推荐大家阅读Albert-Laszlo Barabasi写得书Network Science,大家可以在如下网站直接阅读传染病模型这一章:http://barabasi.com/networksciencebook/chapter/10#contact-networks。Barabasi是一位复杂网络科学领域非常厉害的学者,大家也可以在他的官网上查看作者的一些相关工作。
下面我就直接把SIS模型和SIR模型的代码放上来一起学习一下。我的Python版本是3.6.1,使用的IDE是Anaconda3。Anaconda3这个IDE我个人推荐使用,用起来很方便,而且提供了Jupyther Notebook这个很好的交互工具,大家可以尝试一下,可在官网下载:https://www.continuum.io/downloads/。
在Barabasi写得书中,有两个Hypothesis:1,Compartmentalization; 2, Homogenous Mixing。具体看教材。
默认条件:1, closed population; 2, no births; 3, no deaths; 4, no migrations.
1. SI model
# -*- coding: utf-8 -*- import scipy.integrate as spi
import numpy as np
import pylab as pl beta=1.4247
"""the likelihood that the disease will be transmitted from an infected to a susceptible
individual in a unit time is β"""
gamma=0
#gamma is the recovery rate and in SI model, gamma equals zero
I0=1e-6
#I0 is the initial fraction of infected individuals
ND=70
#ND is the total time step
TS=1.0
INPUT = (1.0-I0, I0) def diff_eqs(INP,t):
'''The main set of equations'''
Y=np.zeros((2))
V = INP
Y[0] = - beta * V[0] * V[1] + gamma * V[1]
Y[1] = beta * V[0] * V[1] - gamma * V[1]
return Y # For odeint t_start = 0.0; t_end = ND; t_inc = TS
t_range = np.arange(t_start, t_end+t_inc, t_inc)
RES = spi.odeint(diff_eqs,INPUT,t_range)
"""RES is the result of fraction of susceptibles and infectious individuals at each time step respectively"""
print(RES) #Ploting
pl.plot(RES[:,0], '-bs', label='Susceptibles')
pl.plot(RES[:,1], '-ro', label='Infectious')
pl.legend(loc=0)
pl.title('SI epidemic without births or deaths')
pl.xlabel('Time')
pl.ylabel('Susceptibles and Infectious')
pl.savefig('2.5-SI-high.png', dpi=900) # This does increase the resolution.
pl.show()
结果如下图所示

在早期,受感染个体的比例呈指数增长, 最终这个封闭群体中的每个人都会被感染,大概在t=16时,群体中所有个体都被感染了。
2. SIS model
# -*- coding: utf-8 -*- import scipy.integrate as spi
import numpy as np
import pylab as pl beta=1.4247
gamma=0.14286
I0=1e-6
ND=70
TS=1.0
INPUT = (1.0-I0, I0) def diff_eqs(INP,t):
'''The main set of equations'''
Y=np.zeros((2))
V = INP
Y[0] = - beta * V[0] * V[1] + gamma * V[1]
Y[1] = beta * V[0] * V[1] - gamma * V[1]
return Y # For odeint t_start = 0.0; t_end = ND; t_inc = TS
t_range = np.arange(t_start, t_end+t_inc, t_inc)
RES = spi.odeint(diff_eqs,INPUT,t_range) print(RES) #Ploting
pl.plot(RES[:,0], '-bs', label='Susceptibles')
pl.plot(RES[:,1], '-ro', label='Infectious')
pl.legend(loc=0)
pl.title('SIS epidemic without births or deaths')
pl.xlabel('Time')
pl.ylabel('Susceptibles and Infectious')
pl.savefig('2.5-SIS-high.png', dpi=900) # This does increase the resolution.
pl.show()
运行之后得到结果如下图:

由于个体被感染后可以恢复,所以在一个大的时间步,上图是t=17,系统达到一个稳态,其中感染个体的比例是恒定的。因此,在稳定状态下,只有有限部分的个体被感染,此时并不意味着感染消失了,而是此时在任意一个时间点,被感染的个体数量和恢复的个体数量达到一个动态平衡,双方比例保持不变。请注意,对于较大的恢复率gamma,感染个体的数量呈指数下降,最终疾病消失,即此时康复的速度高于感染的速度,故根据恢复率gamma的大小,最终可能有两种可能的结果。
3. SIR model
# -*- coding: utf-8 -*- import scipy.integrate as spi
import numpy as np
import pylab as pl beta=1.4247
gamma=0.14286
TS=1.0
ND=70.0
S0=1-1e-6
I0=1e-6
INPUT = (S0, I0, 0.0) def diff_eqs(INP,t):
'''The main set of equations'''
Y=np.zeros((3))
V = INP
Y[0] = - beta * V[0] * V[1]
Y[1] = beta * V[0] * V[1] - gamma * V[1]
Y[2] = gamma * V[1]
return Y # For odeint t_start = 0.0; t_end = ND; t_inc = TS
t_range = np.arange(t_start, t_end+t_inc, t_inc)
RES = spi.odeint(diff_eqs,INPUT,t_range) print(RES) #Ploting
pl.plot(RES[:,0], '-bs', label='Susceptibles') # I change -g to g-- # RES[:,0], '-g',
pl.plot(RES[:,2], '-g^', label='Recovereds') # RES[:,2], '-k',
pl.plot(RES[:,1], '-ro', label='Infectious')
pl.legend(loc=0)
pl.title('SIR epidemic without births or deaths')
pl.xlabel('Time')
pl.ylabel('Susceptibles, Recovereds, and Infectious')
pl.savefig('2.1-SIR-high.png', dpi=900) # This does, too
pl.show()
所得结果如下图:

基本的传染病模型:SI、SIS、SIR及其Python代码实现的更多相关文章
- 传染病传播模型(SIS)Matlab代码
function spreadingability=sir(A,beta,mu) for i=1:length(A) for N=1:50%随机次数 InitialState=zeros(length ...
- matlab练习程序(传染病模型)
最近新型冠状病毒疫情越来越严重了,待在家中没法出去,学习一下经典传染病模型. 这里总结了五个模型,分别是SI模型,SIS模型,SIR模型,SIRS模型,SEIR模型. 这几种模型的特点先介绍一下. 首 ...
- 隐马尔科夫模型,第三种问题解法,维比特算法(biterbi) algorithm python代码
上篇介绍了隐马尔科夫模型 本文给出关于问题3解决方法,并给出一个例子的python代码 回顾上文,问题3是什么, 下面给出,维比特算法(biterbi) algorithm 下面通过一个具体例子,来说 ...
- 转:关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码
关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码 转: http://andyliuxs.iteye.com/blog/105174 ...
- TensorFlow 训练好模型参数的保存和恢复代码
TensorFlow 训练好模型参数的保存和恢复代码,之前就在想模型不应该每次要个结果都要重新训练一遍吧,应该训练一次就可以一直使用吧. TensorFlow 提供了 Saver 类,可以进行保存和恢 ...
- 传染病模型(SIR模型)
- 网络传播模型Python代码实现
SI模型 import numpy as np import matplotlib.pyplot as plt import smallworld as sw #邻接矩阵 a = sw.a # 感染率 ...
- 混合高斯模型:opencv中MOG2的代码结构梳理
/* 头文件:OurGaussmix2.h */ #include "opencv2/core/core.hpp" #include <list> #include&q ...
- 做量化模型Matlab、R、Python、F#和C++到底选择哪一个?
MATLAB是matrix&laboratory两个词的组合,意为矩阵工厂(矩阵实验室).是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它将数 ...
随机推荐
- 你为什么必须(从现在开始就)掌握linux
写在前面 在我看来,人人都应该学习linux,但这不是本文探讨的重点.本文主要从软件测试人员的角度谈谈学习和掌握linux的重要性.必要性.紧迫性. 另外: 这里所说的linux系统,是unix系统和 ...
- web工作过程
了解浏览器与服务器进行通信和访问的过程 打开浏览器-输入URL地址 当我们访问一个网页时,如http://www.baidu.com,这个网址包含四个部分的内容: 1.第一部分:协议类型:这里是htt ...
- ajax传数组到后台,后台springmvc接收数组参数
var ids= new Array(); $("input[class='detailCheck']:checked").each(function(i,k){ var ...
- 你真的知道组件中的v-model吗?
v-model的神奇 html <div id="app"> <input v-model="poin"> {{ poin }} < ...
- 用JQuery写的滚动条,可以改变样式哦!
很早之前在做项目的时候要用到自定义的滚动条,可是现在的CSS2只能改改颜色什么的,对于改变形状或者更高级的用法根本不可能实现,没办法只能自己写一个了.(好像CSS3可以该形状,不过没研究过有兴趣的童鞋 ...
- msf 之 webshell 提权
首先建议一下到网上查查meterpreter的用法在看这篇文章,理解为什么要使用msf来提权(因为msf里边有个meterpreter很强大 ^_^)Metasploit 拥有msfpayload 和 ...
- datatables 学习笔记1 基础篇
本文共3部分:基本使用|遇到的问题|属性表 1.DataTables的默认配置 $(document).ready(function() { $('#example').dataTable(); } ...
- Linux版微信
一.到github上下载https://github.com/geeeeeeeeek/electronic-wechat/releases下载linux-x64.tar.gz,具体根据操作系统是32位 ...
- Hibernate入门(三)
一 Hibernate生成器类 Hibernate中,标签id中的generator标签用于生成持久化类对象的唯一标识.所有的生成器类都实现了org.hibernate.id.IdentifierGe ...
- python 自定义回调函数
回调函数用起来比较爽.特别是在js中,满世界全是回调,那么在python中,怎么来优雅地实现自己的回调函数呢 下面贴一个我写的例子 class BaseHandler(object): def cra ...