SIR模型II

article/2025/9/26 9:42:23

SIR II (Epidemic modeling)



by Zhuofei(fregot@icloud.com)



jupyter notebook link


在这个模型中,考虑一个在一定空间内的流行病模型。人只能与附近的人交互作用,所以给人在一定的空间里随机运动。

首先,在一个2维平面,随机游走:

using Plots
using Compose
using Cairo
using Fontconfig
struct Coordinate{T}x::Ty::T
end#定义 +
function Base.:+(a::Coordinate, b::Coordinate)return Coordinate(a.x+b.x, a.y+b.y)
endpossiblemoves = [Coordinate( 1, 0), Coordinate( 0, 1), Coordinate(-1, 0), Coordinate( 0,-1),]
#每一步随机取possiblemoves中的一个,加上去,形成随机游走function randwalk(P::Coordinate, n::Int)accumulate(+, rand(possiblemoves, n); init = P)
end#绘图
function p_tuple(c)c.x, c.yend
P = plot(ratio=1)
pline = randwalk(Coordinate(4,4), 1000)
figure = plot!(P, p_tuple.(pline),label=nothing, linewidth=2, linealpha=LinRange(1.0, 0.2, length(pline)))
#savefig(figure,"figure1.png")

在这里插入图片描述

function p_plot(p::Plots.Plot, line::Vector)plot!(p, p_tuple.(line), label=nothing,linewidth=2, linealpha=LinRange(1.0, 0.2, length(pline)))
end
p1 = plot(ratio=1)for i ∈ 1:10p_plot(p1, randwalk(Coordinate(4,4), 1000))
end
p1

在这里插入图片描述


给定一个空间,点在空间中移动,一旦超过边界,返回点在边界上:

空间大小为: L × L L\times L L×L

function boundary(c::Coordinate, L::Number)x, y = [c.x, c.y]if c.x < -Lx = -Lelseif c.x > Lx = Lendif c.y < -Ly = -Lelseif c.y > Ly = Lendreturn Coordinate(x, y)
end
boundary (generic function with 1 method)

然后利用边界对随机游走进行限制

function randwalk_boundary(P::Coordinate, n::Int, L::Number)res = accumulate(+, rand(possiblemoves, n); init = P)return boundary.(res, L)
end
p1 = plot(ratio=1)for i ∈ 1:10p_plot(p1, randwalk_boundary(Coordinate(4,4), 1000, 20))
end
p1

在这里插入图片描述



定义一个结构,每个人包括一个位置坐标和一个感染状况。

@enum InfectionStatus S I R
abstract type AbstractAgent end
mutable struct Agent <: AbstractAgentposition::Coordinatestatus::InfectionStatus
end

初始化,生成一堆人,随机挑选一个设为感染者:

function initialize(N::Number, L::Number)res = []for i ∈ 1:Npush!(res, Agent(Coordinate(rand(-L:L), rand(-L:L)), S))endranNum = rand(1:N)res[ranNum].status = Ireturn res
end
initialize (generic function with 1 method)
initialize(3, 10)
3-element Array{Any,1}:Agent(Coordinate{Int64}(-7, -5), S)Agent(Coordinate{Int64}(-5, 5), I)Agent(Coordinate{Int64}(-6, 10), S)

对每一种人用不同的颜色表示:

#deepskyblue,salmon,limegreen
color(s::InfectionStatus) = if s == S"deepskyblue"elseif s == I"salmon"else"limegreen"endcolor(a::Agent) = color(a.status)
color (generic function with 2 methods)
#将初始化的人,可视化
function visualize(agents::Vector, L)p = plot(ratio=1, framestyle = :box, xaxis=nothing, yaxis=nothing)c = color.(agents)l = length(agents)scatter!([agents[i].position.x for i=1:l], [agents[i].position.y for i=1:l], c=c)return p
end
N = 20
L = 10
visualize(initialize(N, L), L)

在这里插入图片描述



像之前模型一那样,定义传染的函数:

abstract type AbstractInfection endstruct CollisionInfectionRecovery <: AbstractInfectionp_infection::Float64p_recovery::Float64
end
function bernoulli(p)return rand() ≤ p
end
bernoulli (generic function with 1 method)
#重合有几率感染
function interact!(agent::Agent, source::Agent, infection::CollisionInfectionRecovery)if source.position == agent.position && bernoulli(infection.p_infection)if agent.status == S && source.status == Iagent.status = Ielseif source.status == S && agent.status == Isource.status = Iendelseif agent.status == I && bernoulli(infection.p_recovery)agent.status = Rendreturn (agent, source)
end
interact! (generic function with 1 method)
function step!(agents::Vector, L::Number, infection::AbstractInfection)ranNum = rand(1:length(agents)) agents[ranNum].position += rand(possiblemoves)agents[ranNum].position = boundary(agents[ranNum].position, L)for i ∈ 1:length(agents)if i ≠ ranNumagents[i], agents[ranNum] = interact!(agents[i], agents[ranNum], infection)endendreturn agents
end
step! (generic function with 1 method)
function sweep!(agents::Vector, L::Number, infection::AbstractInfection)for i ∈ 1:length(agents)agents = step!(agents, L, infection)endreturn agents
end
sweep! (generic function with 1 method)
pandemic = CollisionInfectionRecovery(0.5, 10^-5)
function SimulateTest(N::Int64, L::Number, ksweeps::Number)agents = initialize(N, L)plot_before = visualize(agents, L) # replace with your codefor i ∈ 1:ksweepsagents = sweep!(agents, L, pandemic)endplot_after = visualize(agents, L)p = plot(plot_before, plot_after)return p
endSimulateTest(50, 40, 1000)#需要按照WebIO扩展
#@manipulate for ksweeps ∈ 1:10000
#    SimulateTest(50, 40, ksweeps)
#end

在这里插入图片描述

#Count
k_sweep_max = 10000
function countingSIR(agents::Vector)result_S, result_I, result_R = 0, 0, 0for i in agentsif i.status == Sresult_S += 1elseif i.status == Iresult_I += 1elseif i.status == Rresult_R += 1endendreturn (result_S, result_I, result_R)
end#40人, 30×30面积
N = 40
L = 30agents = initialize(N, L)
result_S = Int[]
result_I = Int[]
result_R = Int[]
for t ∈ 1:k_sweep_maxagents = sweep!(agents, L, pandemic)s, i, r = countingSIR(agents)push!(result_S, s)push!(result_I, i)push!(result_R, r)
end
p = plot(leg=:outerright)
plot!(result_S, label="S")
plot!(result_I, label="I")
plot!(result_R, label="R")

在这里插入图片描述

N = 100
L = 50
x = initialize(N, L)
pandemicI = CollisionInfectionRecovery(0.5, 0.00001)Ss, Is, Rs = Int[], Int[], Int[]Tmax = 100@gif for t ∈ 1:Tmaxfor i ∈ 1:50Nstep!(x, L, pandemicI)endresult_SIR = countingSIR(x)push!(Ss, result_SIR[1])push!(Is, result_SIR[2])push!(Rs, result_SIR[3])l = visualize(x, L)r = plot(xlim=(1, Tmax), ylim=(1, N))plot!(r, 1:t, Ss, color=color(S), label="S")plot!(r, 1:t, Is, color=color(I), label="I")plot!(r, 1:t, Rs, color=color(R), label="R")plot(l, r)
end

在这里插入图片描述


现在添加社交,社交概率:越大越可能接触

mutable struct SocialAgent <: AbstractAgentposition::Coordinatestatus::InfectionStatusnum_infected::Intsocial_score::Float64
end
position(a::SocialAgent) = a.position
color(a::SocialAgent) = color(a.status)
function initialize_social(N, L)res = []for i ∈ 1:Npush!(res, SocialAgent(Coordinate(rand(-L:L), rand(-L:L)), S, 0, rand(0.1:0.01:0.5)))endranNum = rand(1:N)res[ranNum].status = Ireturn res
end#社交概率和感染概率决定是否被感染
function interact!(agent::SocialAgent, source::SocialAgent, infection::CollisionInfectionRecovery)total_scores = agent.social_score + source.social_scoreif source.position == agent.position && bernoulli(infection.p_infection) && bernoulli(total_scores)if agent.status == S && source.status == Iagent.status = Ielseif source.status == S && agent.status == Isource.status = Iendelseif agent.status == I && bernoulli(infection.p_recovery)agent.status = Rendreturn (agent, source)
end

Simulate

N = 50
L = 50global social_agents = initialize_social(N, L)
x = copy(social_agents)
pandemicI = CollisionInfectionRecovery(0.5, 0.00001)Ss, Is, Rs = Int[], Int[], Int[]Tmax = 200@gif for t ∈ 1:Tmaxfor i ∈ 1:50Nstep!(x, L, pandemicI)endresult_SIR = countingSIR(x)push!(Ss, result_SIR[1])push!(Is, result_SIR[2])push!(Rs, result_SIR[3])l = visualize(x, L)r = plot(xlim=(1, Tmax), ylim=(1, N))plot!(r, 1:t, Ss, color=color(S), label="S")plot!(r, 1:t, Is, color=color(I), label="I")plot!(r, 1:t, Rs, color=color(R), label="R")plot(l, r, size=(1000, 600))
end

在这里插入图片描述

降低社交概率:

N = 50
L = 40x = initialize_social(N, L)
for i in 1:Nx[i].social_score *= 0.25
end
pandemicI = CollisionInfectionRecovery(0.5, 0.00001)Ss, Is, Rs = Int[], Int[], Int[]Tmax = 200@gif for t ∈ 1:Tmaxfor i ∈ 1:50Nstep!(x, L, pandemicI)endresult_SIR = countingSIR(x)push!(Ss, result_SIR[1])push!(Is, result_SIR[2])push!(Rs, result_SIR[3])l = visualize(x, L)r = plot(xlim=(1, Tmax), ylim=(1, N))plot!(r, 1:t, Ss, color=color(S), label="S")plot!(r, 1:t, Is, color=color(I), label="I")plot!(r, 1:t, Rs, color=color(R), label="R")plot(l, r, size=(1000, 600), title="Lock")
end

在这里插入图片描述


http://chatgpt.dhexx.cn/article/X2q9oeMa.shtml

相关文章

SIR信息传播模型

SIR信息传播模型 SIR模型及python复现SIR模型SIR数学模型传播动力学方程python实现 模拟社交网络中SIR模型的信息传播过程 SIR模型及python复现 SIR模型 SIR模型是传染病模型中的经典模型&#xff0c;可以用在传染病过程中的模拟预测&#xff0c;也可以用作抽象表达社交网络中…

SIR模型 matlab模拟

需要一个单独的m文件&#xff1a; %即写上三个微分方程 function ySIRModel(t,x,lambda,mu) y[-lambda*x(1)*x(2),lambda*x(1)*x(2)-mu*x(2),mu*x(2)]; 再进行作图 >> ts0:1:100; >> lambda0.00001; >> mu1/14; >> x0[45400,2100,2500]; >> […

SIR及SEIR建模的简单示例

目录 概述1.一些定义1.1 一些名词1.2 一些符号1.3 一些定义 2.方法论2.1 SIR2.2 SEIR2.3 代际传播2.3.1 传播矩阵 3 模型实现3.1 参数设定3.2 SIR&#xff08;1&#xff09;模型&#xff08;2&#xff09;参数&#xff08;3&#xff09;计算&#xff08;4&#xff09;绘图 3.3 …

SIR模型python实现

python新手&#xff0c;代码不规范之处敬请见谅&#xff0c;第一次发帖排版也不太懂&#xff0c;各位将就看。 参考&#xff1a; SIR模型和Python实现_阿丢是丢心心的博客-CSDN博客_sir模型 【保姆级教程】使用python实现SIR模型&#xff08;包含数据集的制作与导入及最终结…

SI,SIS,SIR,SEIRD模型

SI&#xff0c;SIS&#xff0c;SIR&#xff0c;SEIRD模型 因为个人工作需要系统地整理SI&#xff0c;SIR以及SEIR模型&#xff0c;故对三个模型进行原理介绍以及对比。文中关于SI&#xff0c;SIS&#xff0c;SIR的所有的截图都来自西工大肖华勇老师在慕课上的分享&#xff0c;…

【数学建模】传染病SIR模型

SIR模型 经典的SIR模型是一种发明于上个世纪早期的经典传染病模型&#xff0c;此模型能够较为粗略地展示出一种传染病的发病到结束的过程&#xff0c;其核心在于微分方程&#xff0c;其中三个主要量S是易感人群&#xff0c;I是感染人群&#xff0c;R是恢复人群 这三个量都是跟…

SIR模型简单了解(Susceptible Infected Recovered Model)

SIR模型定义 SIR模型是一种传播模型&#xff0c;是信息传播过程的抽象描述。 SIR模型是传染病模型中最经典的模型&#xff0c;其中S表示易感者&#xff0c;I表示感染者&#xff0c;R表示移除者。 S&#xff1a;Susceptible&#xff0c;易感者 I&#xff1a;Infective&#xf…

【保姆级教程】使用python实现SIR模型(包含数据集的制作与导入及最终结果的可视化)

目录 一、SIR模型介绍 二、Python实现SIR模型 1.制作自己的数据集的两种方法&#xff08;csv格式&#xff09; &#xff08;1&#xff09;excel转为csv格式 &#xff08;2&#xff09;通过python对csv格式文件进行内容修改 2.导入数据集 &#xff08;1&#xff09;具体代码如下…

SIR模型和Python实现

一、SIR模型介绍 SIR模型时传染病中最基础最核心的模型&#xff0c;研究的是某个封闭地区的疫情传播规律。 SIR模型的动力学关系如下图&#xff1a; 健康人数S的变化与 健康人数S和正感人数I的乘积&#xff08;代表健康人数和正感人数的接触&#xff09;成正比&#xff0c;其…

SIR传染病模型(微分方程系列1)

一&#xff1a;基本参数 SIR模型是常见的一种描述传染病传播的数学模型&#xff0c;其基本假设是将人群分为以下三类&#xff1a; S:(Susceptible):易感人群&#xff0c;指未得病者&#xff0c;但缺乏免疫能力&#xff0c;与感病者接触后容易受到感染。 I:(Infective):患病人…

【整站下载器】小飞兔整站下载V5.0

小飞兔整站下载是一款只需输入一个网址就能下载一个网站的软件&#xff0c;它可以从Internet的任何地方抓回你想要的任何文件&#xff0c;整站下载主要是用来快速搭建网站、深层分析网站、网站克隆等。

【小飞兔整站下载】整站下载器哪个好用_整站下载工具哪个好

小飞兔整站下载是一款可以下载整个网站内容的软件&#xff0c;你只要输入一个网址&#xff0c;软件能自动分析网站链接、图片、样式、文件等资源&#xff0c;并能将整个网站下载到本地&#xff0c;能在本地正常跳转、浏览。 官网&#xff1a;https://xft.fzxgj.top/ 直接上图…

快速获取一个网站的所有资源,图片,html,css,js等等

今天介绍一款软件,可以快速获取一个网站的所有资源,图片,html,css,js… 以获取某车官网为例 我来展示一下这个软件的功能. 1、输入网站地址 2、下载文件 到此,爬取网站就结束了,有些网站的资源使用的是国外的js,css,速度会有些差异,但效果都是一样的.爬取下来就能使用.放…

小飞升值记——(5)

目录 一&#xff1a;学习痕迹 二&#xff1a;学习结语 一&#xff1a;学习痕迹 二&#xff1a;学习结语 1.注意断句 2.注意refill的读音&#xff0c;重音在后面

根据url一键爬取前端页面资源文件,恐怖如斯-----小飞兔

前言 有一天你在网上发现一个很好看的前端页面,你想要弄下来在自己的项目上使用,于是你去查看源码,复制html代码和资源文件,过程非常的麻烦,而且很可能缺胳膊少腿,这里我给大家推荐一款可以一键爬取前端页面资源文件的工具给大家,希望对大家有所帮助. 下载 https://download.cs…

小飞兔整站下载如何设置特殊标签?

小飞兔整站下载如何设置特殊标签&#xff1f; 为什么提供自定义标签的功能&#xff1f;如何设置自定义标签&#xff1f; 为什么提供自定义标签的功能&#xff1f; 随着html5网站的快速发展&#xff0c;很多网站做了格式各样的特殊标签&#xff0c;而我们需要匹配特殊标签的资源…

c语言兔子繁殖问题pia,上海虹口幼升小转学报名,在喂养幼兔的时候我们要注意。...

上海虹口幼升小转学报名&#xff0c;在喂养幼兔的时候我们要注意&#xff0c;在喂养幼兔的时候我们要注意&#xff0c;不能直接用自来水为给幼兔喝&#xff0c;因为幼鸡初期的消化食物是很慢的&#xff0c;做好室内清洁卫生并对猫咪进行隔离幼猫在有猫癣的时候&#xff0c;这样…

最新版本 Stable Diffusion 开源 AI 绘画工具之汉化篇

✨ 目录 &#x1f388; 汉化预览&#x1f388; 下载插件方法一&#x1f388; 下载插件方法二&#x1f388; 下载插件方法三&#x1f388; 简单汉化&#x1f388; 双语汉化 &#x1f388; 汉化预览 在上一篇文章中&#xff0c;我们安装好了 Stable Diffusion 开源 AI 绘画工具但…

StyleGAN 调整面部表情,让虚拟人脸更生动

✨ 目录 🎈 人脸表情🎈 调整步骤🎈 调整结果🎈 人脸表情 通过上一篇文章 StyleGAN 生成的人脸:https://tinygeeker.blog.csdn.net/article/details/129667295人脸图片都是比较中规中矩的,如果能够给人脸增加一些表情的话,会让人脸显得更加的自然和逼真那么调整人脸的…

springMVC小bug webapp/.html访问不到

我们要访问 test.html 启动tomcat 404 web服务器中也有一个web。xml 父servlet&#xff1a;是给所有web.xml准备服务静态资源的 但是子类也是 一个/ 这个/会覆盖父xml 父类有jsp不会拦截&#xff0c;子类没有重写所有会访问.jsp 两个解决方法&#xff1a;方法1&#…