Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 38 · Applications in Finance and Science

Simulating an Epidemic with the SIR Model

Simulate the spread of an infection with a random SIR model, see why many outbreaks fizzle out, and compare final sizes with the deterministic theory.

Does an infection spread, or fizzle out? A formula based on averages gives one curve. But real outbreaks are random: a single infected person might happen to recover before passing the disease on. Monte Carlo lets you simulate this randomness and answer questions such as "what is the chance an introduction becomes a big outbreak?" that an average-based model cannot.

This lesson uses the classic SIR model. The numbers are for teaching and are not a forecast of any real disease.

The SIR model

A population of N people is divided into three groups:

  • S (susceptible): can catch the infection.
  • I (infectious): currently carrying and spreading it.
  • R (recovered or removed): no longer infectious and immune.

Two parameters drive everything. The transmission rate β is the number of contacts per person per day that could transmit the infection, and the recovery rate γ is the reciprocal of the average infectious period (γ = 0.1 per day means about 10 days). The basic reproduction number is

Text
R₀ = β / γ

the average number of people one infectious person infects in a fully susceptible population.

flowchart LR
  S["S susceptible"] -->|"rate beta I / N"| I["I infectious"]
  I -->|"rate gamma"| R["R recovered"]

People move one way: S to I to R.

A chain-binomial simulation

We advance one day at a time. Each susceptible person becomes infected today with probability 1 − exp(−β·I/N), where I is today's number of infectious people. Each infectious person recovers with probability 1 − exp(−γ). Random counts come from binomial draws.

With N = 1000, β = 0.3, γ = 0.1, so R₀ = 3, and one initial infection, here are six runs.

Python Six stochastic SIR runs

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.

You will usually see two kinds of run: some outbreaks that die out almost immediately, and others that grow into a large wave. The same starting conditions give very different histories.

The chance of a big outbreak

When the infection first arrives, almost everyone is susceptible. Each infectious person then acts like an individual in a birth-death process: at rate β they "give birth" to a new infection, and at rate γ they "die" (recover). The first event is an infection with probability β/(β+γ) and a recovery with probability γ/(β+γ). A standard result for this process is that a single introduced case fails to start a major outbreak with probability

Text
q = γ / β = 1 / R₀      (for R₀ > 1)

So with R₀ = 3, about one introduction in three fizzles out by chance. We call a run a "major outbreak" if it infects at least 100 people, and compare final sizes with the deterministic theory: the fraction z of the population ever infected solves

Text
z = 1 − exp(−R₀ · z)       (for R₀ = 3, z ≈ 0.94)
Python How often does one case become a major outbreak?

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.

You should see roughly a quarter to a third of runs fizzling, in the neighborhood of 1/R₀ (our daily time steps shift it a little), and for the others a final size close to the deterministic z.

Why stochastic simulation matters here

  • Small populations or early outbreaks: randomness dominates, and averages mislead. A deterministic curve says "the disease takes off"; the simulation says "it takes off two times out of three".
  • Interventions: reduce β (by contact reduction) and rerun. Even R₀ slightly below 1 can produce a few cases by chance, but the outbreak dies out.
  • Extensions: add an exposed (E) stage, age groups, networks of contacts, vaccination, or spatial structure. Each is a few extra lines in the simulation loop.

The same simulation structure, with different state names, models chemical reactions, queueing networks and population genetics.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In the SIR model, R0 equals:

    Transmission rate divided by recovery rate, the average infections caused by one infectious person.
  2. 2. Why does the stochastic model sometimes show no outbreak even when R0 > 1?

    With only a few infectious people, chance events can end the chain of transmission.
  3. 3. The final-size equation for the fraction ever infected is:

    It balances the fraction escaping infection against the exposure created by those infected.
  4. 4. In the chain-binomial method, an infectious person recovers each day with probability:

    That is the chance an exponential waiting time with rate gamma ends within one day.

Summary

  • SIR splits people into S, I and R; R₀ = β/γ governs growth.
  • A day-by-day binomial simulation captures chance effects that average-based models hide.
  • With R₀ > 1 many introductions still die out; with exponential infectious periods the early extinction chance is 1/R₀.
  • Large outbreaks end near the deterministic final size z = 1 − exp(−R₀ z).

Next: Diffusion and Percolation