Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 39 · Projects and Practice

Project: Epidemic Modeling with SIR

Model an epidemic with the SIR equations: simulate, derive R0, peak and final size, test interventions and vaccination, and extend to SEIR using SciPy.

Few differential-equation models have been as visible as the SIR model of epidemics. It divides a population into three compartments and tracks how people move between them. Despite its simplicity it explains why epidemics have a peak, why they end before everyone is infected, and why vaccinating only part of the population can protect everyone. In this project you simulate it, check it against exact formulas and then use it to test interventions.

This is a worked example of the whole course: a nonlinear system, analyzed by phase-plane reasoning, solved with adaptive solvers and extended step by step.

The model

S = susceptible, I = infectious, R = removed (recovered or immune). The total N = S + I + R is constant.

TEXT
S' = -β S I / N
I' =  β S I / N - γ I
R' =  γ I

β is the transmission rate (contacts that could infect, per person and day) and γ the recovery rate; the infectious period lasts 1/γ days on average. Adding the three equations gives S' + I' + R' = 0: a conservation law that you can use to verify the simulation.

The key quantity is the basic reproduction number

TEXT
R0 = β / γ

the number of people one infectious person infects in a fully susceptible population. In the early phase I' = (β S/N - γ) I ≈ γ (R0 - 1) I: if R0 > 1 the infection grows exponentially, if R0 < 1 it dies out.

Python Simulate an epidemic

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The infection spreads slowly at first (only 10 people), explodes into a peak in the second month and then fades as susceptibles run out. The epidemic ends not because everyone was infected but because too few susceptibles remain to sustain it.

What the equations predict

Divide I' by S' to eliminate time: dI/dS = -1 + γN/(β S) = -1 + N/(R0 S). Integrate:

TEXT
I + S - (N/R0) ln S = const

This relation traces a curve in the (S, I) phase plane. It gives three exact results.

  1. Peak: I is maximal when I' = 0, i.e. when S = N/R0. For a start with nearly everyone susceptible, I_max = N (1 - (1 + ln R0)/R0).
  2. Herd immunity threshold: the infection declines once the susceptible fraction falls below 1/R0, so the immune fraction needed is 1 - 1/R0.
  3. Final size: the epidemic ends with I = 0; if r_∞ is the fraction ever infected, 1 - r_∞ = exp(-R0 r_∞) (for I(0) tiny).
Python Check peak and final size against theory

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Simulation and theory agree to the printed digits. Notice that for R0 = 4 about 98% of the population is eventually infected if nothing is done, while for R0 = 1.5 about 58% are.

Interventions

Social distancing lowers β. Vaccination moves people directly from S to R. Both can be explored by changing the model.

Time-varying transmission. Let β(t) drop from β0 to β1 at day t_c. The simplest way is a function that returns different values; for sharp changes tell the solver about it, for instance by integrating in two pieces.

Vaccination. Starting with a fraction v already immune, the effective reproduction number is R_eff = R0 (1 - v), and the epidemic cannot take off when v > 1 - 1/R0.

Python Lockdown timing and vaccination

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Acting early matters more than acting hard. With 70% vaccinated (above the threshold 1 - 1/3 = 0.667) the epidemic cannot get going, and even the unvaccinated stay safe: this is herd immunity. Halving β on day 60 helps far less than doing so on day 30 because most of the exponential growth has already happened.

SIR at a glance

S, I, R Susceptible, infectious, removed.
R0 Beta over gamma decides growth.
Peak Occurs when S equals N over R0.
Herd immunity One minus one over R0 must be immune.

The epidemic ends because susceptibles run out, not because everyone is infected.

1 / 2

Extending the model

Real diseases need more structure. Each extension is a new compartment and a few terms:

  • SEIR: add an exposed stage E (infected but not yet infectious) between S and I with rate σ: S' = -βSI/N, E' = βSI/N - σE, I' = σE - γI, R' = γI. It delays and flattens the peak.
  • Waning immunity (SIRS): R -> S at rate ω; the infection can become endemic.
  • Births and deaths: allow the disease to persist; R0 now includes the removal by death.
  • Age structure or networks: the contact structure matters; compartments become vectors.

Common mistakes

  • Using counts and fractions inconsistently (β S I versus β S I / N).
  • Reading R0 as a property of the disease alone: it depends on behavior and population too.
  • Forgetting that the model is deterministic: for small outbreaks chance matters, and a stochastic model is needed.
  • Fitting β to early data without allowing for under-reporting.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In the SIR model with R0 = 5, what fraction must be immune for herd immunity?

    The threshold is 1 - 1/R0 = 1 - 0.2 = 0.8.
  2. 2. The infectious count peaks when:

    I' = 0 when β S / N = γ, i.e. S = N γ / β = N / R0.
  3. 3. What is conserved in the SIR model?

    The three derivatives sum to zero, so total population is constant.
  4. 4. Compared with SIR, the SEIR model usually has:

    The latent stage delays infectiousness, so growth is slower and the peak comes later and is typically lower.

Summary

  • SIR splits the population into S, I, R with R0 = β/γ.
  • The peak occurs at S = N/R0; the final size solves 1 - r = exp(-R0 r).
  • Herd immunity requires a fraction 1 - 1/R0 to be immune.
  • Interventions change β or move people from S to R; timing matters a lot.
  • SEIR, SIRS and structured models extend the same equations.

Next: Project: Driven Oscillators and Resonance