0% found this document useful (0 votes)
83 views19 pages

SIR Models: An Introduction: Tingda Wang July 2020

The document introduces SIR models, which are used to model disease spread. SIR models divide a population into three categories: susceptible (S), infected (I), and recovered (R). The sizes of these groups change over time according to transitions between the groups. The document discusses modeling SIR systems using ordinary differential equations (ODEs), stochastic probability distributions, and individual agents. It provides the deterministic ODE equations that model changes in S, I, and R over time and includes Matlab code to simulate the ODE model.

Uploaded by

Zan Dao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views19 pages

SIR Models: An Introduction: Tingda Wang July 2020

The document introduces SIR models, which are used to model disease spread. SIR models divide a population into three categories: susceptible (S), infected (I), and recovered (R). The sizes of these groups change over time according to transitions between the groups. The document discusses modeling SIR systems using ordinary differential equations (ODEs), stochastic probability distributions, and individual agents. It provides the deterministic ODE equations that model changes in S, I, and R over time and includes Matlab code to simulate the ODE model.

Uploaded by

Zan Dao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

SIR Models: An Introduction

Tingda Wang
July 2020

1 Introduction
The classic model for disease modeling is a family of models called “SIR models”
that was pioneered by Kermack and McKendrick in 1927.

These models try to predict things such as how a disease spreads, or the to-
tal number infected, or the duration of an epidemic, and to estimate various
epidemiological parameters. Such models can also show how different public
health interventions may affect the outcome of the epidemic. For example, we
can determine what the most efficient technique is for issuing a limited number
of vaccines in a given population.

To understand this model, we will first look at its most fundamental form, as a
deterministic model consisting of ordinary differential equations (ODE). Then,
we will look at its stochastic representation as well as modeling individual agents
directly.

2 Model Variables
The SIR model of an epidemic focuses on populations of individuals which are
divided into three categories: those who are Susceptible to infection with a
disease, those who are Infected and are infectious with the disease as well as
those who have Recovered from the disease and are immune to further infec-
tion.

The sizes of the populations of susceptible (S), infected (I), and recovered (R)
individuals are the variables of this model that will change over discrete time
steps. We will assume they are related by the following transitions:

S → I: Susceptible S people are infected at a rate proportional to the number


of infected I. The rate determining constant depends on the frequency of inter-
personal contacts and the ease with which the pathogen is transmitted.

I → R: Infected people recover from the disease at a rate dependent on the

1
clearance of the pathogen from a typical individual.

We can model this SIR system through many ways: we will look at the contin-
uous deterministic approach, the stochastic probability distribution approach,
and the individual agent approach.

Note also that the three variables are a function of time. Therefore, we de-
note them as S(t), I(t), R(t).

3 Deterministic Model: ODE Equations


For the continuous deterministic approach, which is by far the most well known,
we can describe our model as a system of ODEs. These differential equations
determine the rate of change of each of the three populations, as a function of
the three variables as well as some constant parameters.

The ODE system is as follows:

dS(t) a
= − I(t)S(t) (1)
dt N
dI(t) a
= I(t)S(t) − bI(t) (2)
dt N
dR(t)
= bI(t) (3)
dt

where S(t), I(t), and R(t) are the Susceptible, Infected, and Recovered pop-
ulations respectively. a is the rate of infection and b is the rate of recovery.
Both are assumed to be constant.

Note that both constants are factored by N , the total population, so N =


S + I + R. This is an important point, as we are able to derive one of the
variables S(t), I(t), and R(t) if we know the other two. This will come in handy
in the subsequent models to come.

To understand how the ODE equations model an epidemic, it is most important


to look at the first equation, the change in S(t). This cannot be positive and
its decrease represents the increase in the newly infected. Now how do people
get infected? An infected person in I(t) must infect a person in S(t), hence
the I(t)S(t) term. As each infected person has a limited ability to infect the
population, this term must be multiplied by a constant factored by the total
population, hence the a/N .

Looking at the change in R, the model assumes the sick are going to recover at

2
a steady rate by a factor of b, hence rI(t).

Knowing the above, the change in infected is just newly infected subtracting
those recovered. Hence, we have the set of equations above.

Obviously, this model makes certain assumptions such as all the sick recover
at a constant rate independent of the population. Also, there are no treatment
effects yet but we will incorporate how policy or social distancing can impact
the disease progression, in more advanced models.

3.1 Matlab code


Using Matlab, we can solve the system of ODEs for a given set of starting
parameters.

%filename: SIRode.m
% Function for solving SIR system of ODEs
function SIRode
clear x;

% number of time steps


tsteps=20;

% time span
tspan = [0 tsteps];

% starting values
S = 999;
I = 1;
R = 0;

% integrate with the ode solver


[t, x] = ode45(@(t, x) odef(t, x), tspan, [S I R]);

% plot the results


plot(t, x,’-o’)
xlabel(’t’);

% defines ode system


function funcs = odef(t, x)

% constants
a = 2 * log(2);
b = log(2);
N = 1000;

% define the function on the right side of the ode


% NOTE: x(1) = S, x(2) = I, x(3) = R
funcs = zeros(3,1);

3
funcs(1) = (-1 * a / N) * x(2) * x(1);
funcs(2) = ((a / N) * x(2) * x(1)) - (b * x(2));
funcs(3) = b * x(2);

3.2 Results

Figure 1: ODE results from Matlab code. The blue line is the S population,
orange line I, and yellow line R.

From Figure 1, we can see the results of the ODE solver that attempts to
find solutions for the above system of equations. Note how S(t) decreases as
I(t) increases. At first R(t) is flat but after some time of I(t) being nonzero,
R(t) begins to increase.

At any time, the sum of S, I, R equals the total population of 1000.

The bell shaped curve of I(t) shows herd immunity which is a form of indi-
rect protection from infectious disease that occurs when a large percentage of
a population has become immune to an infection after getting infected. When
a large proportion of individuals possess immunity, such people being unlikely
to contribute to disease transmission, chains of infection are more likely to be

4
disrupted, which causes the number of infected to fall and eventually reach near
zero levels.

4 Stochastic Model: Probabilistic Model Using


ODE
One way we can develop the ODE model, and also build the intuition for the
next set of models is to consider the event of an infection as a chance event
determined by a fixed probability. In other words, the event of a S(t) person
being infected by a I(t) person follows a Bernoulli distribution of some fixed
probability, B(p). The same can be said for an infected I(t) going to R(t).

In fact, we can transform our deterministic model into a probabilistic one by


considering a small enough time interval dt such that the change in the infected
population is at most 1. In doing so, our system of ODE equations will give us
the probabilities we need:

a
p = dS(t) = I(t)S(t)dt (4)
N

Then, all we need to do is iterate over these extremely small time steps, and
at each timestep, sample from the Bernouli distribution to determine whether
an additional S individual will be infected and whether an additional I infected
individual will recover.

Note also, that due to the probabilities, we may not see an increase in infection
if the first infected recovers before infecting anyone. To make sure the system
moves, we can enforce the rule that the population cannot fully recover and the
number of infected cannot reach 0.

Now let us look at the matlab code for more details.

4.1 Matlab Code

% Code for stochastic simulation of ODE.


% Approach #1: We assume that not all infected individuals can recover.
% The probability of number of recovered individuals increasing
% by 1 is b*(I(t)-1)*dt, rather than b*(I(t))*dt.

close all; clear all

a=2*log(2); % a parameter in ODE


b=log(2); % b parameter in ODE

5
N=1000; % total population

T_max=20; % max time = 20 weeks


t = 1; % time stepper
dt = min(1/(N*a)/10,1/(N*b)/10); % choose small enough dt
nstep = round(T_max/dt); % number of time steps

S = zeros(nsteps, 1); % susceptible pop


R = zeros(nsteps, 1); % recovered pop

% initial conditions
S(1) = 999;
R(1)=0;

I = N-S(:)-R(:); % I is constrained by N, a constant

% average over several stochastic realizations


S_av = zeros(nsteps, 1);
R_av = zeros(nsteps, 1);
I_av = zeros(nsteps, 1);

jmax = 50; % number of stochastic realizations


for j = 1:jmax

S = zeros(nsteps, 1);
R = zeros(nsteps, 1);

S(1) = 1000;
R(1) = 0;

I = N-S(:)-R(:);
t=1;

while (t<nsteps)
r1=rand(1); % random number for S switch
r2=rand(1); % random number for R switch

if (r1<a/N*(S(t))*(I(t))*dt )
S(t+1) = S(t)-1;
else
S(t+1) =S(t);
end

if (r2< b*(I(t)-1)*dt ) % note that we use I(t)-1 instead of I(t), to


ensure that the population can’t become fully recovered
% otherwise it’s possible for no dynamics to happen
R(t+1)= R(t)+1;
else

6
R(t+1)=R(t);
end

I(t+1) = N-S(t+1)-R(t+1); % update I

t = t+1;
end

I_av = (I_av+I); % add results to averages


S_av = (S_av+S);
R_av = (R_av+R);

end

I_av=I_av/jmax; % divide by jmax to get averages


S_av=S_av/jmax;
R_av=R_av/jmax;

t_vec=linspace(1,nsteps*dt,nsteps);

figure;
plot(t_vec,I_av, ’-’);
hold on

plot(t_vec,S_av, ’--’);
plot(t_vec,R_av, ’.’);

hold off

xlabel(’time steps’);
title(’stochastic simulation’);
legend([’I(t)’;’S(t)’;’R(t)’])
xlim([0,T_max])

saveas(gcf,’stochastic_1’,’epsc’)
saveas(gcf,’stochastic_1’,’png’)

% Approach #2: We assume that all infected individuals can recover.


% So the probability of # of recovered individuals increasing
% by 1 is b*(I(t))*dt.

S = zeros(nsteps, 1); % susceptible pop


R = zeros(nsteps, 1); % recovered pop

S(1) = 1000;
R(1)=0;

I = N-S(:)-R(:); % I is constrainted by N = const.

7
S_av = zeros(nsteps, 1); % average over several stochastic realizations
R_av = zeros(nsteps, 1);
I_av=zeros(nsteps, 1);

for j = 1:jmax

S = zeros(nsteps, 1);
R = zeros(nsteps, 1);

S(1) = 999;
R(1)=0;

I = N-S(:)-R(:);
t=1;

while (t<nsteps)
r1=rand(1); % random number for S switch
r2=rand(1); % random number for R switch

if (r1<a/N*S(t)*I(t)*dt )
S(t+1) = S(t)-1;
else
S(t+1) =S(t);
end

if (r2< b*(I(t))*dt )
R(t+1)= R(t)+1;
else
R(t+1)=R(t);
end

I(t+1) = N-S(t+1)-R(t+1); % update I

t = t+1;
end

I_av = (I_av+I); % add results to averages


S_av = (S_av+S);
R_av = (R_av+R);

end

I_av=I_av/jmax; % divide by jmax to get averages


S_av=S_av/jmax;
R_av=R_av/jmax;

t_vec=linspace(1,nsteps*dt,nsteps);

8
figure;
plot(t_vec,I_av, ’-’);
hold on

plot(t_vec,S_av, ’--’);
plot(t_vec,R_av, ’.’);

hold off

xlabel(’time steps’);
title(’stochastic simulation’);
legend([’I(t)’;’S(t)’;’R(t)’])
xlim([0,T_max])

saveas(gcf,’stochastic_2’,’epsc’)
saveas(gcf,’stochastic_2’,’png’)

4.2 Results
Looking at Figure 2, we see the results differ quite a bit from the deterministic
ODE approach. Due to the probabilistic nature of the model, it is possible that
a disease simply doesn’t take off due to chance events and the number of in-
fected quickly reaches zero. Here, the model predicts a much dampened spread
of the disease. However, note how simply enforcing the number of infected never
reaches 0, we can ensure such rare events don’t occur in the probabilistic setting.

In Figure 3, we see that the predicted results match the results in our determin-
istic model earlier. Therefore, we have developed a probabilistic counterpart
of the ODE deterministic models. With the added stochasticity, we are now
able to incorporate unpredictable factors and see how that changes the model
results.

5 Stochastic Model: Reed-Frost Model


With the intuition we developed earlier, we can take a deeper look at stochastic
models.

The Reed–Frost Model is one of the simplest stochastic epidemic models. It


was formulated by Lowell Reed and Wade Frost in 1928 and describes the evo-
lution of an infection in generations.

Each infected individual at time step t independently infects each susceptible


individual in the population with some probability p.

The individuals that become infected by the individuals in generation t then

9
Figure 2: Stochastic modeling when we assume population can fully recover. I
can reach zero.

become the infected population in generation t + 1 and the individuals in gen-


eration t are removed from the epidemic process.

Note the important difference here: the infected are only infectious for one
time step. Afterwards, they become part of the recovered population. Having
this assumption makes the math derivation easier.

5.1 Model Specifics


The following assumes some familiarity with probability and random variables:

Suppose for any infected individual, his probability of infecting any one in the
susceptible population is p. Then we can represent each of such an event as
Bernoulli independently identically distributed random variables (think of it as
a coin toss) with probability p of infection, and 1 − p of not being infected.
Denote this as Bern(p).

Then in turn, at each time step t, the probability that an individual in St


is not infected would be 1 − (1 − p)It−1 . We can think of this as the outcome
where we get It−1 tails from It−1 consecutive independent coin tosses. Denote

10
Figure 3: Stochastic modeling when we assume population cannot fully recover.
So I > 0.

this as Bern(1 − (1 − p)It−1 ).

Therefore, at each time step, the new infected population would be


St−1
X
It = Bern(1 − (1 − p)It−1 ). (5)
1

Note that this is just the Binomial Distribution Binom(St−1 , 1−(1−p)It−1 ).


Hence, the model arrives as the following set of equations:

St = St−1 − It (6)
It = Binom(St−1 , 1 − (1 − p)It−1 ) (7)
Rt = Rt−1 + It−1 (8)
Here all infected are only infectious for one time step and then become part
of the recovered. The S population gradually becomes infected according to the
binomial process.

11
This model you see here is the stochastic Reed-Frost model, which is a chain bi-
nomial model, and is part of a large class of stochastic models known as Markov
chain models. A Markov chain is defined as a stochastic process with the prop-
erty that the future state of the system is dependent only on the present state
of the system and conditionally independent of all past states. This is known
as the memoryless property.

5.2 Matlab Code

% Reed Frost Model


N=1000; % total pop
Nt=10; % time steps
p=0.005; % probability of infection
S=zeros(Nt,1);
I=S;
I(1,1)=1; % initial number of infected

R=zeros(Nt,1);
S(1,1)=N-I(1,1);

% probability of not getting infected per contact


q=1-p;

% iterate thru timesteps


for nt=2:Nt
k=1-q^(I(nt-1,1));
new_I=binornd(S(nt-1,1), k);
I(nt,1)=new_I;
R(nt,1)=R(nt-1,1)+I(nt-1,1);
S(nt,1)=S(nt-1,1)-new_I;
end

figure;
plot(1:Nt,S,1:Nt,I,1:Nt,R);
xlabel(’time step’);
title(’reed frost’);
legend([’S(t)’;’I(t)’;’R(t)’])
xlim([0,Nt])

5.3 Results
Running the model through a binomial random variable yields a similar result
as our previous works. However, due to the stochastic nature of the model, each
instance will yield a slightly different set of curves.

In fact, try running several instances of the model and plot the resulting I(t)
curves together. How do they compare?

12
Figure 4: Reed Frost Model Results

6 Simulations: Individual Agent Model


The models of an epidemic we have thus far developed didn’t involve individual
members of the population, but only dealt with aggregate descriptions of them,
such as the number who are sick at any time, or the probability that a typical
member of the population was sick.

In individual agent models the entities, hence variables, of the model are the
individuals themselves. The population, then, is a set of these variables. The
individuals might be represented by a single variable, for example the state de-
scriptors of the specific individual as to being susceptible, infected or recovered,
or a complex data structure of descriptors, such as age, sex, height, weight, etc.
The dynamics of the model are represented by selecting individuals from the
population set and changing their attributes on the basis of the attribute values
of others in the population, often based on heuristics.

An individual-agent model typically is represented as a computer program. In


it one sets up an initial population of individuals and iterates over discrete steps
in time the two processes of changing the individual attributes according to the
dynamics of the model and taking aggregate statistics on the population. These

13
statistics would be presented as the results of the model. Note that the results
are aggregate in nature, and don’t differ from the ordinary differential equation
and stochastic models.

Further, even if an individual-agent model is made comparable to its determin-


istic counterparts, the individual agent model may demonstrate qualitatively
different aggregate results due to its representation as a natural system of many
smaller individual components. Therefore, such models may provide greater
detail and insight into how a system moves in relation to the distribution of it’s
smaller components.

Individual-agent models are often called simulations and rightly so. Proponents
of this type of modeling stress the high degree of realism possible in the com-
plex dynamic functions one can incorporate as computer programs. However,
applied mathematicians frown on them because the complex dynamic functions
usually prohibit any formal analysis. Therefore, running the simulation pro-
gram with specific parameter values is pretty much all you can do with it. It is
also unreliable to generalize from a small number of runs of the program. One
usually would need to run multiple instances and average the results.

6.1 Model Specifics


Each individual is represented in the model as a single entity or variable. It
takes on an alphabetic character attributes according to the following: s if the
individual is susceptible, i if they are infected and infectious and r if they have
recovered and are immune. The population is a vector of length n, the popula-
tion size, of these variables.

At each time step, each individual contacts some number of random individ-
uals in the population each time step. The number could be an attribute of the
individual, but for simplicity this model assumes it is a constant over the pop-
ulation. We denote this as nc. This number represents the frequency of contact.

If the first individual is in the infected state and they contact an individual
in the susceptible state, the susceptible changes state to infected with some
given probability pt. Note that unlike our previous models, we separate the
two factors of transmission rate of the disease, the frequency of contacts and
the virulence (contagious strength) of the pathogen. In the stochastic model,
both factors are incorporated into the probability p.

Finally, after the contacts have been made, an infected individual has their
state changed from ‘i’ to ‘r’. Therefore, like the Reed-Frost model, each infected
recovers after one timestep.

14
6.2 Matlab Code
The code will be slightly more complex due to the nature of simulating individual
agents and having to aggregate the data (like a census). Therefore we will divide
the code into multiple parts for better readability and style.

function pop=initiate(s0,i0,r0)
% this fn creates an initial population vector for the epidemic
simulation
% each state’s population is appended to the growing vector, pop
% inputs are initial number of s, i, r individuals
% returns the pop vector
pop=[];
for i=1:s0
pop=[pop,’s’];
end
for i=1:i0
pop=[pop,’i’];
end
for i=1:r0
pop=[pop,’r’];
end

The next script is the core of the model, dealing with the simulation me-
chanics.

function pop2=epidemic(nc,pt,pop1)
% epidemic simulation which calculates a new pop2
% nc is number of contacts per infected
% pop1 is vector of ’s’, ’i’, and ’r’
% assume that all ’i’ cells go to ’r’ after each step
% each i makes nc random infectious contacts with other cells.
% ’s’ goes to ’i’ with probability pt if it is contacted by an ’i’.

pop2=pop1; % set up new pop vector


n=length(pop1);
for j=1:n
if pop1(j)==’i’
for c=1:nc % nc contacts
k=j;
while k==j % this avoids choosing a random k equal to j
(itself)
k=randi([1,n]); % random individual to contact
end
% rand<pt generates a Bernoulli rv
if pop1(k)==’s’ && rand<pt
pop2(k)=’i’; % infect the ’s’ cell
end
end
pop2(j)=’r’; %then the jth individual recovers

15
end
end

Now we need a function to count the individual’s data type and return the
results at the end.

function [s,i,r]=census(pop)
% counts the number of s, i and r cells in pop
s=0;
i=0;
r=0;
n=length(pop(1,:)); % pop is a 1 by n array
for j=1:n
if pop(j)==’s’
s=s+1;
elseif pop(j)==’i’
i=i+1;
elseif pop(j)==’r’
r=r+1;
end
end

Lastly, we can make a script that runs an instance of the simulation using
the functions above.

function [tt,results]=simulate(n, nc, pt)


% this function packages the simulation steps of initializing the
population
% vector and iterating the epidemic and census functions unit I=0
% the censuses are accumulated in a mx3 matrix, results
% where m is the number of time steps until infections reach 0
t=0;
pop=initiate(n,0,0); % set up an initial population of susceptible
pop(floor(n/2))=’i’; % put an infected individual in the middle
[s,i,r]=census(pop);
results=[s,i,r]; %start the results matrix
tt=t;
while i>0
pop=epidemic(nc,pt,pop);
[s,i,r]=census(pop);
results=[results;[s,i,r]];
t=t+1;
tt=[tt;t];
end

Now we can run the simulation.

% tried different simulations with different parameters

16
[tt, results] = simulate(10000, 20, .1);
figure(1);
plot(results, ’-o’)

figure(2);
[tt, results] = simulate(10000, 4, .5);
plot(results, ’-o’)

figure(3);
[tt, results] = simulate(10000, 8, .01);
plot(results, ’-o’)

6.3 Results
Looking at the individual agent simulated results, we see that figure 5 look
roughly identical to the previous models. However, note that the time span is
now longer, at more than 25 time steps. We used parameters (20, .1) and (4, .5).
Interestingly, both sets of parameters gave the exact same plot below. Note
that the product of the two parameters, the combined effect of the virility and
number of contact parameters, in this case, is the same.

Also, decreasing parameters too much can result in no outbreak at all, as seen
in 6.
Overall, individual agent models give you a lot of freedom to simulate events
and allows for a better look at population dynamics. However, the results don’t
have the mathematical backing that the ODE equations have. Therefore, all
you have is your intuition to interpret the results.

17
Figure 5: Individual Agent model results: nc = 20, pt = 0.1

18
Figure 6: Nothing happens when parameters too low: nc = 8, pt = 0.01

19

You might also like