流行病模型(SIR Model)
流行病模型(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)相关推荐
- 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 ...
- 基于pytorch的模型压缩和模型剪枝Model Prune示例
神经网络和卷积神经网络的模型剪枝Model Prune 1,神经网络和卷积神经网络模型剪枝方法. 2,可指定剪枝率进行定向剪枝,并输出剪枝后参数统计和finetune. 3,支持MLP, Lenet, ...
- Django(五)模型(model)系统 -- 常用字段和字段参数
Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...
- 参数化模型(parametric model)和非参数化模型non-parametric model)的区别?哪些模型是参数化模型,哪些模型是非参数化模型?
参数化模型(parametric model)和非参数化模型non-parametric model)的区别?哪些模型是参数化模型,哪些模型是非参数化模型? 统计学习模型又可以分为参数化模型(para ...
- Ember——在构建Ember应用程序时,我们会使用到六个主要部件:应用程序(Application)、模型(Model)、视图(View)、模板(Template)、路由(...
在构建Ember应用程序时,我们会使用到六个主要部件: 模板(Template).应用程序(Application).视图(View).路由(Routing).控制器(Controller)和模型(M ...
- 阅读类型HTML,W3C HTML5标准阅读笔记 – 元素分类与内容模型(Content Model)
HTML4中,元素被分成两大类: inline(内联元素)与block(块级元素).但在实际的开发过程中,因为页面表现的需要,前端工程师经常把inline元素的display值设定为block(比如a ...
- 教你吃透CSS的盒子模型(Box Model)
目录 CSS 盒子模型(Box Model) 元素的宽度和高度 浏览器的兼容性问题 CSS 盒子模型(Box Model) 所有HTML元素可以看作盒子,在CSS中,"box model&q ...
- 详解CSS的盒模型(box model) 及 CSS3新增盒模型计算方式box-sizing
W3C规范 一般来说,页面中的每一个元素都会形成一个矩形盒子,渲染引擎根据给定的样式确定这个盒子的呈现.通俗的来说,页面的布局就是一个个盒子的排列和摆放.掌握了盒子呈现的本质,布局也就轻而易举. 在 ...
- 判别模型和生成模型(Discriminative Model Generative Model)【转】
又是碰到了一些简单的基本概念,但是仔细想想发现自己没有理解透彻,Search一下,总结如下: [摘要] - 生成模型:无穷样本==>概率密度模型 = 产生模型==>预测 - 判别模型:有限 ...
最新文章
- 计算机count的功能是,全国计算机二级Access每日练习4
- vue页面翻页勾选的记忆功能
- 五个你绝不可忽视的HTML5特性
- html怎么控制进度条,HTML如何实现进度条?附源码
- 一个前端博客(9)——浏览器检测和加载
- sql命令(四)-操作数据表中的记录
- sql查询前50条_您必须知道的前50条SQL查询
- 腾讯云服务器安装AMH控制面板
- LaTeX 下载和安装
- 2018深圳杯数学建模A题--人才吸引力评价模型研究
- matlab 交互效应三维图,【MATLAB】使用MATLAB绘制心理学中的交互作用图
- php openldap支持ssl,基于OpenLDAP服务端和客户端的SSL/TLS的配置方法
- 修改war包中数据库配置信息
- 转:关于ASP操作Access数据库时出现死锁.ldb的解决方法
- 微型计算机的安装步骤,技嘉微型电脑BRIX BSi3H-6100黑苹果安装详细教程
- Win32显示隐藏任务栏
- java flappy bird_Java实例---flappy-bird实例解析
- LightGBM源码阅读+理论分析(处理特征类别,缺省值的实现细节)
- OpenCV笔记11:利用HSV颜色空间进行目标检测和目标跟踪
- 2020-3-17课堂笔记