Epidemic Models: SIR
The SIR model (Kermack and McKendrick, 1927) splits a population of size into Susceptible, Infected and Recovered compartments. It is a three-dimensional nonlinear system, yet its key predictions (the threshold , 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 ; infected people recover at rate :
stays constant (add the equations). is the mean infectious period, and
is the basic reproduction number: the number of people one infected person infects in a fully susceptible population.
The threshold
. At the start , so the infection grows if and dies out if . It keeps growing while ; the peak is exactly when . Vaccinating a fraction in advance prevents any outbreak: that is the herd immunity threshold.
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 by gives , a separable equation: . At the end , so the fraction of the population ever infected solves
For this gives : far more than the herd immunity threshold of , because the epidemic "overshoots" (people infected after the peak still infect others).
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 , and therefore the effective reproduction number . A temporary intervention flattens the peak (protecting hospital capacity), but if it ends while , a second wave follows.
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 (), 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:
A disease has a mean infectious period of 5 days and per day. What is , and what fraction must be immune to prevent outbreaks?
Answer
; 60 %.
Solution
, so and .
Exercise 2: At the peak
In an SIR epidemic with , what fraction of the population is still susceptible at the peak?
Answer
25 %.
Solution
The peak is at .
Exercise 3: Final size with vaccination (code)
With and a fraction vaccinated beforehand, the final size among the unvaccinated solves . Compute for and .
Answer
for and for .
Solution
Vaccination reduces the effective to : 2.1 and 1.2.
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…
- is large
The number of infected people peaks when…
For , the herd immunity threshold is…
- 1/3
- 2/3
- 94 %
- 100 %