All published worksheets from http://sagenb.org
Image: ubuntu2004
The SIR Model
Randall Pruim
Calvin College
This worksheet is based on a worksheet by Tim McLarnan and Anand Pardhanani (Earlham College). You can search for Earlham in the published worksheets at sagemath.org/pub. You will find several worksheets, including one about SIR models.
The Earlham worksheet shows how to use and ODE solver with the SIR model as an example. I have made several changes to that code and also demonstrate how to fit the same model via stochastic simulation.
SIR Models
SIR models are a type of compartment model describing the impact of an infectious disease on a population. This model has three compartments: Susceptible, Infected, and Recovered. Individuals can move from one compartment to another, and the rates at which they do so are determined by the proportion of the population in each compartment. For example, when more individuals are infected, the rate of transfer from Susceptible to Infected (and also from Infected to Recovered) increases. In fact our simple model is entirely driven by this transfer, since it is a model in which Recovered individuals are immune and so cannot become Infected or Susceptible again.
A Differential Equations Model
One of the simplest SIR models is the Kermack-McKendrick model, which is described by the following system of differential equations.
$$
\begin{array}{lcrcr}
S'(t)&=&-\beta \cdot S\cdot I \; \\
I'(t)&=&\beta \cdot S\cdot I &-& \gamma I \; \\
R'(t)&=& &+& \gamma I \; .
\end{array}
$$
This model says that Susceptibles become Infected at a rate proportional to the number of potential contacts (roughly the product of and , and that Infecteds recover at a fixed rate .
A solution using Sage
The function sir_ode takes as arguments the initial numbers of susceptible (), infected (), and recovered () people, and the length of time over which you want data, and the model parameter ( and ). The output is a plot showing as a solid black line, as dashed red line, and as a dash-dot blue line.
The Earlham Example
Here's an example in which we follow a population that starts with 45400 susceptible people, 2100 infected people, and 2500 recovered people over a period of 300 days using the default values of and . This example was used in the orignal Earlham SIR worksheet.
Other Examples
We can adjust any of the initial values or parameters of the model and try again.
A Stochastic Model
Alternatively, we implement a stochastic model. In this model we use a binomial distribution to determine how many individualas move from Susceptible to Infected and from Infected to Recovered.
$$
\begin{align*}
\pi_{SI} &= \beta * I \\
\pi_{IR} & = \gamma
\end{align*}
$$
This model is equivalent to the differential equation model above. The implementation is straightforward.
The function below runs both models and is a convenient way to make comparisons.
The differences are miniscule for a large population.
But in small populations, the law of averages doesn't help us out and the stochasitic nature of the second model is easier to see.
Reproducible "random" results
We can set the seed of the random number generator (located in the numpy module) if we want reproducable random results.