本文主要参考博客: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

  1. # -*- coding: utf-8 -*-
  2.  
  3. import scipy.integrate as spi
  4. import numpy as np
  5. import pylab as pl
  6.  
  7. beta=1.4247
  8. """the likelihood that the disease will be transmitted from an infected to a susceptible
  9. individual in a unit time is β"""
  10. gamma=0
  11. #gamma is the recovery rate and in SI model, gamma equals zero
  12. I0=1e-6
  13. #I0 is the initial fraction of infected individuals
  14. ND=70
  15. #ND is the total time step
  16. TS=1.0
  17. INPUT = (1.0-I0, I0)
  18.  
  19. def diff_eqs(INP,t):
  20. '''The main set of equations'''
  21. Y=np.zeros((2))
  22. V = INP
  23. Y[0] = - beta * V[0] * V[1] + gamma * V[1]
  24. Y[1] = beta * V[0] * V[1] - gamma * V[1]
  25. return Y # For odeint
  26.  
  27. t_start = 0.0; t_end = ND; t_inc = TS
  28. t_range = np.arange(t_start, t_end+t_inc, t_inc)
  29. RES = spi.odeint(diff_eqs,INPUT,t_range)
  30. """RES is the result of fraction of susceptibles and infectious individuals at each time step respectively"""
  31. print(RES)
  32.  
  33. #Ploting
  34. pl.plot(RES[:,0], '-bs', label='Susceptibles')
  35. pl.plot(RES[:,1], '-ro', label='Infectious')
  36. pl.legend(loc=0)
  37. pl.title('SI epidemic without births or deaths')
  38. pl.xlabel('Time')
  39. pl.ylabel('Susceptibles and Infectious')
  40. pl.savefig('2.5-SI-high.png', dpi=900) # This does increase the resolution.
  41. pl.show()

结果如下图所示

  在早期,受感染个体的比例呈指数增长, 最终这个封闭群体中的每个人都会被感染,大概在t=16时,群体中所有个体都被感染了。

  2.    SIS model

  1. # -*- coding: utf-8 -*-
  2.  
  3. import scipy.integrate as spi
  4. import numpy as np
  5. import pylab as pl
  6.  
  7. beta=1.4247
  8. gamma=0.14286
  9. I0=1e-6
  10. ND=70
  11. TS=1.0
  12. INPUT = (1.0-I0, I0)
  13.  
  14. def diff_eqs(INP,t):
  15. '''The main set of equations'''
  16. Y=np.zeros((2))
  17. V = INP
  18. Y[0] = - beta * V[0] * V[1] + gamma * V[1]
  19. Y[1] = beta * V[0] * V[1] - gamma * V[1]
  20. return Y # For odeint
  21.  
  22. t_start = 0.0; t_end = ND; t_inc = TS
  23. t_range = np.arange(t_start, t_end+t_inc, t_inc)
  24. RES = spi.odeint(diff_eqs,INPUT,t_range)
  25.  
  26. print(RES)
  27.  
  28. #Ploting
  29. pl.plot(RES[:,0], '-bs', label='Susceptibles')
  30. pl.plot(RES[:,1], '-ro', label='Infectious')
  31. pl.legend(loc=0)
  32. pl.title('SIS epidemic without births or deaths')
  33. pl.xlabel('Time')
  34. pl.ylabel('Susceptibles and Infectious')
  35. pl.savefig('2.5-SIS-high.png', dpi=900) # This does increase the resolution.
  36. pl.show()

运行之后得到结果如下图:

  由于个体被感染后可以恢复,所以在一个大的时间步,上图是t=17,系统达到一个稳态,其中感染个体的比例是恒定的。因此,在稳定状态下,只有有限部分的个体被感染,此时并不意味着感染消失了,而是此时在任意一个时间点,被感染的个体数量和恢复的个体数量达到一个动态平衡,双方比例保持不变。请注意,对于较大的恢复率gamma,感染个体的数量呈指数下降,最终疾病消失,即此时康复的速度高于感染的速度,故根据恢复率gamma的大小,最终可能有两种可能的结果。

  3.    SIR model

  1. # -*- coding: utf-8 -*-
  2.  
  3. import scipy.integrate as spi
  4. import numpy as np
  5. import pylab as pl
  6.  
  7. beta=1.4247
  8. gamma=0.14286
  9. TS=1.0
  10. ND=70.0
  11. S0=1-1e-6
  12. I0=1e-6
  13. INPUT = (S0, I0, 0.0)
  14.  
  15. def diff_eqs(INP,t):
  16. '''The main set of equations'''
  17. Y=np.zeros((3))
  18. V = INP
  19. Y[0] = - beta * V[0] * V[1]
  20. Y[1] = beta * V[0] * V[1] - gamma * V[1]
  21. Y[2] = gamma * V[1]
  22. return Y # For odeint
  23.  
  24. t_start = 0.0; t_end = ND; t_inc = TS
  25. t_range = np.arange(t_start, t_end+t_inc, t_inc)
  26. RES = spi.odeint(diff_eqs,INPUT,t_range)
  27.  
  28. print(RES)
  29.  
  30. #Ploting
  31. pl.plot(RES[:,0], '-bs', label='Susceptibles') # I change -g to g-- # RES[:,0], '-g',
  32. pl.plot(RES[:,2], '-g^', label='Recovereds') # RES[:,2], '-k',
  33. pl.plot(RES[:,1], '-ro', label='Infectious')
  34. pl.legend(loc=0)
  35. pl.title('SIR epidemic without births or deaths')
  36. pl.xlabel('Time')
  37. pl.ylabel('Susceptibles, Recovereds, and Infectious')
  38. pl.savefig('2.1-SIR-high.png', dpi=900) # This does, too
  39. pl.show()

所得结果如下图: 

基本的传染病模型:SI、SIS、SIR及其Python代码实现的更多相关文章

  1. 传染病传播模型(SIS)Matlab代码

    function spreadingability=sir(A,beta,mu) for i=1:length(A) for N=1:50%随机次数 InitialState=zeros(length ...

  2. matlab练习程序(传染病模型)

    最近新型冠状病毒疫情越来越严重了,待在家中没法出去,学习一下经典传染病模型. 这里总结了五个模型,分别是SI模型,SIS模型,SIR模型,SIRS模型,SEIR模型. 这几种模型的特点先介绍一下. 首 ...

  3. 隐马尔科夫模型,第三种问题解法,维比特算法(biterbi) algorithm python代码

    上篇介绍了隐马尔科夫模型 本文给出关于问题3解决方法,并给出一个例子的python代码 回顾上文,问题3是什么, 下面给出,维比特算法(biterbi) algorithm 下面通过一个具体例子,来说 ...

  4. 转:关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码

    关于Latent Dirichlet Allocation及Hierarchical LDA模型的必读文章和相关代码 转: http://andyliuxs.iteye.com/blog/105174 ...

  5. TensorFlow 训练好模型参数的保存和恢复代码

    TensorFlow 训练好模型参数的保存和恢复代码,之前就在想模型不应该每次要个结果都要重新训练一遍吧,应该训练一次就可以一直使用吧. TensorFlow 提供了 Saver 类,可以进行保存和恢 ...

  6. 传染病模型(SIR模型)

  7. 网络传播模型Python代码实现

    SI模型 import numpy as np import matplotlib.pyplot as plt import smallworld as sw #邻接矩阵 a = sw.a # 感染率 ...

  8. 混合高斯模型:opencv中MOG2的代码结构梳理

    /* 头文件:OurGaussmix2.h */ #include "opencv2/core/core.hpp" #include <list> #include&q ...

  9. 做量化模型Matlab、R、Python、F#和C++到底选择哪一个?

    MATLAB是matrix&laboratory两个词的组合,意为矩阵工厂(矩阵实验室).是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它将数 ...

随机推荐

  1. CentOS7使用rpm包安装MySQL

    说明 本文写于2017-05-20,使用MySQL-5.7.18.操作系统为64位CentOS Linux release 7.2.1511 (Core),以桌面形式安装. 卸载MariaDB Cen ...

  2. 为什么各大厂商要抢先跟进H.265?

    继爱奇艺.乐视等视频厂商宣布支持 H.265 高清视频后,2014 年 4 月,搜狐视频宣布正式上线视频行业首个 H.265 高清大片专区,可在线观看 200 余部当下最火的超高清大片.国外 BBC ...

  3. Unity User Group 北京站:《Unity5.6新功能介绍以及HoloLens开发》

    ​时间一转眼从春天来到了初夏,Unity User Group(以下简称UUG)活动也迎来了第七期.我们面向Unity3D开发从业者以及未来想从事Unity3D开发的学生群体的UUG活动这次仍然在海淀 ...

  4. css隐藏文字的小技巧

    前段时间,在做项目的时候,遇到一个问题.背景图片上有一个“立即注册”的按钮,需要点击.但是问题是:现在的图片是背景图片,如果图片是在html页面内的话,我们可以使用锚点来对图片添加链接.这个时候,我们 ...

  5. 浅谈MVC数据验证

    一.一般情况 对于使用过MVC框架的人来说,对MVC的数据验证不会陌生,比如,我有一个Model如下: public class UserInfo { [Required(ErrorMessage = ...

  6. Python 关于字符串处理技巧

    1.使用多个字符或字符串作为分隔符分割字符串 str.split()方法只能制定单个字符或字符串作为分割符, re.split()方法可以用正则表达式作为分割符, re.split()跟str.spl ...

  7. (文件)图片上传,Spring或SpringMVC框架

    spring或springMVC框架图片(文件)上传 页面部分,用一个简单的form表单提交文件,将图片或文件提交到服务端.一个输入框,用于输入图片的最终名称,一个file文件选择,用于选择图片. 页 ...

  8. python加载sqlite3报错:No module named _sqlite3

    环境为Ubuntu16.04 Apache2.4 Python2.7.13 django 1.8 今天部署apache+django,经过各种折腾,好不容易配置完了,发现错误Apache的日志里有一项 ...

  9. JavaScript数组知识点

    强类型语言数组特点:连续的,指定好长度, 还要规定好数据类型弱类型语言数组特点:不一定是连续的 可以不用指定长度 不限定数据类型(可以存储任意类型的数据)数组定义方式:1.var arr=new Ar ...

  10. php 面向对象的三大特性

    <?phpheader("Content-type:text/html;charset=utf-8");/*封装目的:为了使类更加安全做法:1.将成员变量变成私有2.做一个成 ...