流行病模型(SIR Model)

by : ZhuoFei, Zhou

首先定义一个函数bernoulli(p)

#以概率p判断是否会被感染或恢复
function bernoulli(p::Number)if rand(1)[1] < preturn trueelsereturn falseend
end
bernoulli (generic function with 1 method)

恢复所需的时间函数recovery_time(p)

function recovery_time(p)if p ≤ 0throw(ArgumentError("p must be positive: p = 0 cannot result in a recovery"))endrecovered = falsedays = 0while !recoveredrecovered = bernoulli(p)days+=1endreturn days
end
recovery_time (generic function with 1 method)

测试一下这个函数的运行情况:

其中ppp为治愈概率,NNN为人数:

function do_experiment(p, N)return [recovery_time(p) for _=1:N]
end
do_experiment(0.5, 20);

Model

每个病原体都有自己的内部状态,模拟其感染状态,即“易感”、“感染”或“恢复”。我们想把它们分别编码为值’ S ', ’ I ‘和’ R ':

@enum InFectionStatus S I R

对于每一个病毒,我们都希望跟踪它的感染状态以及它在模拟过程中感染的其他病毒的数量。定义一个新的数据结构来保存一个个体的所有信息,如下所示:

mutable struct Agentstatus::InFectionStatusnum_infected::Int64
endAgent() = Agent(S,0)
Agent
#Agent状态变化
function set_status!(agent::Agent, new_status::InFectionStatus)agent.status = new_statusreturn agent# your code here
end#Agent 数量变化
function set_num_infected!(agent::Agent, new_num::Int64)agent.num_infected = new_numreturn agent
endfunction generate_agents(N::Integer)agents = [Agent() for _ in 1:N]set_status!(first(rand(agents,1)), I)return agentsend
generate_agents (generic function with 1 method)
#感染概率和恢复概率
abstract type AbstractInfection end
struct InfectionRecovery <: AbstractInfectionp_infectionp_recovery
end

一个类型为S的Agent,类型为I的Agent。它实现了两个人之间的单一(单边)交互:

  • 如果“病原体”易受感染,而“来源”具有传染性,则“来源”以既定的感染概率感染“病原体”。如果“源”成功感染了另一个代理,则必须更新其“num_infected”记录。

  • 如果“病原体”被感染,则它以相应的概率恢复。

  • 否则,什么都不会发生。

function interact!(agent::Agent, source::Agent, infection::InfectionRecovery)if agent.status == S && source.status == Iif bernoulli(infection.p_infection)set_status!(agent,I)set_num_infected!(source,source.num_infected+1)endelseif agent.status == Iif bernoulli(infection.p_recovery)set_status!(agent,R)endend
endfunction step!(agents::Vector{Agent}, infection::AbstractInfection)agent,source = rand(agents,2)interact!(agent,source,infection)return agents
endfunction sweep!(agents::Vector{Agent}, infection::AbstractInfection)for _ in 1:length(agents)step!(agents, infection)endreturn agents
end
sweep! (generic function with 1 method)

模型的模拟函数:

function simulation(N::Integer, T::Integer, infection::AbstractInfection)tot_S = zeros(T)tot_I = zeros(T)tot_R = zeros(T)agents = generate_agents(N)for i in 1:Tsweep!(agents,infection)tot_S[i] = length(filter(agent -> agent.status==S,agents))tot_I[i] = length(filter(agent -> agent.status==I,agents))tot_R[i] = length(filter(agent -> agent.status==R,agents))end# your code herereturn (S=tot_S, I=tot_I, R=tot_R)
end
simulation (generic function with 1 method)

测试:

using Plots
N = 1000
T = 1000
sim = simulation(N, T, InfectionRecovery(0.02, 0.002))result = plot(1:T, sim.S, ylim=(0, N), label="Susceptible")
plot!(result, 1:T, sim.I, ylim=(0, N), label="Infectious")
plot!(result, 1:T, sim.R, ylim=(0, N), label="Recovered")

function repeat_simulations(N, T, infection, num_simulations)N = 100T = 1000map(1:num_simulations) do _simulation(N, T, infection)endendsimulations = repeat_simulations(100, 1000, InfectionRecovery(0.02, 0.002), 20)p = plot()
for sim in simulationsplot!(p, 1:1000, sim.I, alpha=.5, label=nothing)
end
mean_I = sum(reduce(hcat, map(sim -> sim.I, simulations)),dims=2)./length(simulations)
plot!(p, mean_I, linewidth=3, label="mean I")
plot!(p, ylabel = "# of Infectious Agents")
p

流行病模型(SIR Model)相关推荐

  1. A Time-Dependent SIR Model for COVID-19 With Undetectable Infected Persons

    A Time-Dependent SIR Model for COVID-19 With Undetectable Infected Persons 带有无法检测到的感染者的COVID-19的时变SI ...

  2. 基于pytorch的模型压缩和模型剪枝Model Prune示例

    神经网络和卷积神经网络的模型剪枝Model Prune 1,神经网络和卷积神经网络模型剪枝方法. 2,可指定剪枝率进行定向剪枝,并输出剪枝后参数统计和finetune. 3,支持MLP, Lenet, ...

  3. Django(五)模型(model)系统 -- 常用字段和字段参数

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  4. 参数化模型(parametric model)和非参数化模型non-parametric model)的区别?哪些模型是参数化模型,哪些模型是非参数化模型?

    参数化模型(parametric model)和非参数化模型non-parametric model)的区别?哪些模型是参数化模型,哪些模型是非参数化模型? 统计学习模型又可以分为参数化模型(para ...

  5. Ember——在构建Ember应用程序时,我们会使用到六个主要部件:应用程序(Application)、模型(Model)、视图(View)、模板(Template)、路由(...

    在构建Ember应用程序时,我们会使用到六个主要部件: 模板(Template).应用程序(Application).视图(View).路由(Routing).控制器(Controller)和模型(M ...

  6. 阅读类型HTML,W3C HTML5标准阅读笔记 – 元素分类与内容模型(Content Model)

    HTML4中,元素被分成两大类: inline(内联元素)与block(块级元素).但在实际的开发过程中,因为页面表现的需要,前端工程师经常把inline元素的display值设定为block(比如a ...

  7. 教你吃透CSS的盒子模型(Box Model)

    目录 CSS 盒子模型(Box Model) 元素的宽度和高度 浏览器的兼容性问题 CSS 盒子模型(Box Model) 所有HTML元素可以看作盒子,在CSS中,"box model&q ...

  8. 详解CSS的盒模型(box model) 及 CSS3新增盒模型计算方式box-sizing

    W3C规范 一般来说,页面中的每一个元素都会形成一个矩形盒子,渲染引擎根据给定的样式确定这个盒子的呈现.通俗的来说,页面的布局就是一个个盒子的排列和摆放.掌握了盒子呈现的本质,布局也就轻而易举. 在 ...

  9. 判别模型和生成模型(Discriminative Model Generative Model)【转】

    又是碰到了一些简单的基本概念,但是仔细想想发现自己没有理解透彻,Search一下,总结如下: [摘要] - 生成模型:无穷样本==>概率密度模型 = 产生模型==>预测 - 判别模型:有限 ...

最新文章

  1. 计算机count的功能是,全国计算机二级Access每日练习4
  2. vue页面翻页勾选的记忆功能
  3. 五个你绝不可忽视的HTML5特性
  4. html怎么控制进度条,HTML如何实现进度条?附源码
  5. 一个前端博客(9)——浏览器检测和加载
  6. sql命令(四)-操作数据表中的记录
  7. sql查询前50条_您必须知道的前50条SQL查询
  8. 腾讯云服务器安装AMH控制面板
  9. LaTeX 下载和安装
  10. 2018深圳杯数学建模A题--人才吸引力评价模型研究
  11. matlab 交互效应三维图,【MATLAB】使用MATLAB绘制心理学中的交互作用图
  12. php openldap支持ssl,基于OpenLDAP服务端和客户端的SSL/TLS的配置方法
  13. 修改war包中数据库配置信息
  14. 转:关于ASP操作Access数据库时出现死锁.ldb的解决方法
  15. 微型计算机的安装步骤,技嘉微型电脑BRIX BSi3H-6100黑苹果安装详细教程
  16. Win32显示隐藏任务栏
  17. java flappy bird_Java实例---flappy-bird实例解析
  18. LightGBM源码阅读+理论分析(处理特征类别,缺省值的实现细节)
  19. OpenCV笔记11:利用HSV颜色空间进行目标检测和目标跟踪
  20. 2020-3-17课堂笔记

热门文章

  1. 81、基于STM32单片机智能台灯 PWM调光坐姿矫正 灯光控制定时台灯设计
  2. 4月30日世界表白日_世界表白日10月27日,一段感动到哭的表白
  3. UltraEdit安装详解
  4. 预估网站流量的方法 站长工具综合查询
  5. 深圳市专利申请流程图解,2021申请发明专利的流程
  6. 英语语法篇 - 助动词
  7. 欧暇·地中海酒店以全新面貌赢得行业认可,荣膺2022年度卓越休闲度假酒店品牌
  8. Git使用入门,使用原理解读及如何在GitLab、GitHub或者Stash上管理项目(一)
  9. 瑞吉外卖 —— 5、菜品管理
  10. linux系统编程---守护进程