Pandemic modeling - SIR Julia

code
julia
data-science
modeling
Author

Brynjar Smári Bjarnason

Published

Thu 5. Sep, 2024

Modified

Mon 30. Sep, 2024

First implementation of an epidemiology model, SIR.

About

In this notebook I implement a basic SIR model.

SIR model

From Wikipedia: SIR.

The SIR model without vital dynamics: The dynamics of an epidemic, for example the flu, are often much faster than the dynamics of birth and death, therefore, birth and death are often omitted in simple compartmental models. The SIR system without so-called vital dynamics (birth and death, sometimes called demography) described above can be expressed by the following set of ordinary differential equations:

Code
using ModelingToolkit, DifferentialEquations, Plots, ForwardDiff, ModelingToolkitStandardLibrary, Latexify
using ModelingToolkit: D_nounits as D, t_nounits as t

@mtkmodel SIR begin
  @parameters begin
    β
    γ
  end 
  @variables begin
    S(t)
    I(t)
    R(t)
  end
  @equations begin
    D(S) ~ -β*S*I
    D(I) ~ β*S*I - γ*I
    D(R) ~ γ*I
  end
end

@mtkbuild sir = SIR()
latexify(sir)

\[\begin{align} \frac{\mathrm{d} S\left( t \right)}{\mathrm{d}t} &= - S\left( t \right) I\left( t \right) \beta \\ \frac{\mathrm{d} I\left( t \right)}{\mathrm{d}t} &= - I\left( t \right) \gamma + S\left( t \right) I\left( t \right) \beta \\ \frac{\mathrm{d} R\left( t \right)}{\mathrm{d}t} &= I\left( t \right) \gamma \end{align}\]

Code
plot(solve(ODEProblem(sir, [10000, 50, 0], (0,20), [.00010, .2])))
Code
plot(solve(ODEProblem(sir, [10000, 50, 0], (0,40), [.00005, .2])))

At time \(t\) the SIR model is characterised by \(S(t)\) for the number of susceptible in the population, \(I(t)\) for the number of infected, and \(R(t)\) for the number of recovered or deceased (or immune) individuals out of the population \(N = S(t)+I(t)+R(t)\).

People move through these stages as follows:

Grid of \(\beta\) and \(\gamma\)

Back to top