TExcelTAD
Home › Differential Equations › Epidemic Models: SIR

Epidemic Models: SIR

The SIR model (Kermack and McKendrick, 1927) splits a population of size NN into Susceptible, Infected and Recovered compartments. It is a three-dimensional nonlinear system, yet its key predictions (the threshold R0=1R_0 = 1, the peak, the final size, herd immunity) can be derived by hand.

The equations

Infection happens when susceptible and infected people meet, at a rate proportional to SISI; infected people recover at rate γ\gamma:

S′=−βSIN,I′=βSIN−γI,R′=γI.S' = -\frac{\beta SI}{N}, \qquad I' = \frac{\beta SI}{N} - \gamma I, \qquad R' = \gamma I.

S+I+R=NS + I + R = N stays constant (add the equations). 1γ\frac{1}{\gamma} is the mean infectious period, and

R0=βγR_0 = \frac{\beta}{\gamma}

is the basic reproduction number: the number of people one infected person infects in a fully susceptible population.

The threshold

I′=γI(R0SN−1)I' = \gamma I\left(R_0\frac{S}{N} - 1\right). At the start S≈NS \approx N, so the infection grows if R0>1R_0 > 1 and dies out if R0<1R_0 < 1. It keeps growing while SN>1R0\frac{S}{N} > \frac{1}{R_0}; the peak is exactly when SN=1R0\frac{S}{N} = \frac{1}{R_0}. Vaccinating a fraction 1−1R01 - \frac{1}{R_0} in advance prevents any outbreak: that is the herd immunity threshold.

python · try it yourself
import numpy as np
from scipy.integrate import solve_ivp

N, beta, gamma = 1_000_000, 0.3, 0.1          # R0 = 3, infectious for 10 days on average
def sir(t, y):
    S, I, R = y
    return [-beta * S * I / N, beta * S * I / N - gamma * I, gamma * I]

sol = solve_ivp(sir, (0, 300), [N - 10, 10, 0], dense_output=True, rtol=1e-9, atol=1e-6)
t = np.linspace(0, 300, 30001)
S, I, R = sol.sol(t)
k = np.argmax(I)
print(f"peak on day {t[k]:.1f}: {I[k]:,.0f} infected at once")
print(f"S/N at the peak = {S[k] / N:.4f} (theory 1/R0 = {gamma / beta:.4f})")
print(f"total ever infected: {R[-1] / N:.1%}")
peak on day 61.4: 300,466 infected at once
S/N at the peak = 0.3334 (theory 1/R0 = 0.3333)
total ever infected: 94.0%

The final size equation

Dividing S′S' by R′R' gives dSdR=−R0SN\frac{dS}{dR} = -\frac{R_0S}{N}, a separable equation: S=S0e−R0R/NS = S_0e^{-R_0R/N}. At the end I=0I = 0, so the fraction zz of the population ever infected solves

1−z=e−R0z.1 - z = e^{-R_0 z}.

For R0=3R_0 = 3 this gives z≈0.94z \approx 0.94: far more than the herd immunity threshold of 23\frac{2}{3}, because the epidemic "overshoots" (people infected after the peak still infect others).

python · try it yourself
import numpy as np
from scipy.optimize import brentq

for R0 in (1.5, 2.0, 3.0, 5.0):
    z = brentq(lambda z: 1 - z - np.exp(-R0 * z), 1e-9, 1)
    print(f"R0 = {R0}: final size {z:.4f}, herd immunity threshold {1 - 1 / R0:.4f}")
R0 = 1.5: final size 0.5828, herd immunity threshold 0.3333
R0 = 2.0: final size 0.7968, herd immunity threshold 0.5000
R0 = 3.0: final size 0.9405, herd immunity threshold 0.6667
R0 = 5.0: final size 0.9930, herd immunity threshold 0.8000

Interventions

Reducing contacts lowers β\beta, and therefore the effective reproduction number Rt=R0SNR_t = R_0\frac{S}{N}. A temporary intervention flattens the peak (protecting hospital capacity), but if it ends while SN>1R0\frac{S}{N} > \frac{1}{R_0}, a second wave follows.

python · try it yourself
import numpy as np
from scipy.integrate import solve_ivp

N, gamma = 1_000_000, 0.1
def run(beta_of_t):
    f = lambda t, y: [-beta_of_t(t) * y[0] * y[1] / N, beta_of_t(t) * y[0] * y[1] / N - gamma * y[1], gamma * y[1]]
    s = solve_ivp(f, (0, 400), [N - 10, 10, 0], dense_output=True, max_step=0.5, rtol=1e-8)
    t = np.linspace(0, 400, 4001)
    S, I, R = s.sol(t)
    return I.max(), R[-1] / N

print("no intervention:       peak %8.0f, final %.1f%%" % (run(lambda t: 0.3)[0], 100 * run(lambda t: 0.3)[1]))
lock = lambda t: 0.12 if 40 <= t < 120 else 0.3
p, f = run(lock)
print("contacts cut, day 40-120: peak %8.0f, final %.1f%%" % (p, 100 * f))
no intervention:       peak   300466, final 94.0%
contacts cut, day 40-120: peak   144134, final 88.9%

Cutting contacts by 60 % for 80 days halves the peak, but the final size barely changes: when contacts return to normal, most people are still susceptible and the epidemic resumes.

Extensions

  • SEIR adds an Exposed (infected but not yet infectious) compartment.
  • SIRS lets immunity wane (R→SR \to S), producing endemic equilibria and recurring waves.
  • Vaccination, age structure and spatial spread add more compartments; the structure "rate in minus rate out" stays the same (First-Order Models).

In software

  • Public health agencies and research groups run compartmental models (often with many age and region groups) to plan hospital capacity; they are systems of ODEs integrated with solve_ivp-style solvers or stochastic simulations.
  • The same equations model the spread of malware through a network and the adoption of rumours or products in social networks.

Exercises

Exercise 1: R0R_0

A disease has a mean infectious period of 5 days and β=0.5\beta = 0.5 per day. What is R0R_0, and what fraction must be immune to prevent outbreaks?

Answer

R0=2.5R_0 = 2.5; 60 %.

Solution

γ=15\gamma = \frac{1}{5}, so R0=0.50.2=2.5R_0 = \frac{0.5}{0.2} = 2.5 and 1−12.5=0.61 - \frac{1}{2.5} = 0.6.

Exercise 2: At the peak

In an SIR epidemic with R0=4R_0 = 4, what fraction of the population is still susceptible at the peak?

Answer

25 %.

Solution

The peak is at SN=1R0\frac{S}{N} = \frac{1}{R_0}.

Exercise 3: Final size with vaccination (code)

With R0=3R_0 = 3 and a fraction vv vaccinated beforehand, the final size zz among the unvaccinated solves 1−z=e−R0(1−v)z1 - z = e^{-R_0(1 - v)z}. Compute zz for v=0.3v = 0.3 and v=0.6v = 0.6.

Answer

z≈0.822z \approx 0.822 for v=0.3v = 0.3 and z≈0.314z \approx 0.314 for v=0.6v = 0.6.

Solution

Vaccination reduces the effective R0R_0 to R0(1−v)R_0(1 - v): 2.1 and 1.2.

python · try it yourself
import numpy as np
from scipy.optimize import brentq
for v in (0.3, 0.6):
    Re = 3 * (1 - v)
    print(v, round(Re, 2), round(brentq(lambda z: 1 - z - np.exp(-Re * z), 1e-9, 1), 4))
0.3 2.1 0.8221
0.6 1.2 0.3137

Test yourself

In the SIR model, an outbreak grows initially when…

  1. R0>1R_0 > 1
  2. R0<1R_0 < 1
  3. γ>β\gamma > \beta
  4. NN is large

The number of infected people peaks when…

  1. S/N=1/R0S/N = 1/R_0
  2. S=0S = 0
  3. I=N/2I = N/2
  4. R=SR = S

For R0=3R_0 = 3, the herd immunity threshold is…

  1. 1/3
  2. 2/3
  3. 94 %
  4. 100 %