Epidemic ModelingPolicy and practice

Module 12Computational considerations

The computer side of epidemic modeling. With our own code and timings, we write one agent model in four languages and check that all give the same answer, keep a model's parts apart and catch a planted bug with a conservation test, see Euler's method overshoot and go negative while Runge–Kutta holds, meet stiff models and the solvers built for them, compare exact and tau-leaping stochastic simulation, speed code up by profiling, better algorithms, batching and sparse structure, and calibrate at scale with exact gradients, emulators and history matching.

By the end you should be able to choose tools and structure for a modeling project, test a model as well as its code, pick a solver and step size that are stable and accurate enough, find and remove the real bottleneck in slow code, and plan a calibration that the cost of a run can afford.

Key ideas, one per episode

  • Ep. 49The same model ran about 40 times faster in C than in plain Python loops, with the same answer; a planted bug left the infection curve perfect and was caught by a one-line conservation check.
  • Ep. 50Euler with 7-day steps overshot the peak by 60% and went below zero; Runge–Kutta with 4-day steps stayed within 0.1%. A stiff solver needed about 30 times fewer evaluations.
  • Ep. 51A grid of cells gave the same epidemic as checking every pair, 8.6 times faster for 2,000 people; batching was 85 times faster, and a sparse network needed 12 MB instead of 80 GB.
  • Ep. 52With exact gradients a 26-parameter fit took 75 runs instead of about 2,000; an emulator trained on 50 runs ruled out half of the settings in under a hundredth of a second.

Textbook: Chapter 12 of T. Lazebnik, Epidemic Modeling: Principles, Algorithms, and Applications (Academic Press, 2026). The episodes and slides follow the chapter’s topics in our own words, figures and simulations; nothing is copied from the book.

The title slide of Module 12: Computational considerations

36 slides, written to be read on your own, with worked examples, “try it” boxes, our own small simulations and check-yourself questions (answers at the end). PDF, 0.7 MB.

Watch

4 short episodes for this module. Pick one from the list, and open the transcript to read along: each timestamp plays the episode from there. The narration is generated with a voice clone of Teddy, used with his permission.

Episode 49 · 2:35

Languages, libraries and architecture

The computer side of epidemic modeling: one agent-based model written in plain Python, NumPy, JavaScript and C and timed, why tested libraries matter, how to keep a model's parts apart, and a planted bug that only a conservation check catches.

The same model ran about 40 times faster in C than in plain Python loops, with the same answer; a planted bug left the infection curve perfect and was caught by a one-line conservation check.

TranscriptEpisode 49
0:00

Same math, different code. The same epidemic model can take a tenth of a second to run, or a few thousandths. Same math, different code. Welcome to Module twelve: the computer side of epidemic modeling.

0:18

One model, four languages. Choosing a language is a trade between how fast the code runs and how fast you can write it. We wrote one agent based model, ten thousand people for two hundred days, four times: in plain Python, in Python with NumPy, in JavaScript, and in C. C ran a whole epidemic in under four thousandths of a second. Plain Python loops took almost forty times longer. NumPy, which hands the loops to compiled code, came within a factor of two of C. JavaScript, which runs the labs on our website, was not far behind. And all four agreed: about ninety two percent of the town infected. That's the first rule: a faster version is only worth having if it gives the same answer.

0:58

Libraries. Libraries matter as much as languages. Well used solvers, random number generators and statistics packages have been tested by thousands of people. Writing your own is a great way to learn, and a risky way to publish.

1:10

Architecture. Then architecture. Keep the parts of a model apart: the transmission mechanism, the parameters, the observation model that turns infections into reported cases, the inference code, and the outputs. Each part can then be tested, swapped and reused. Settings live in files, not inside the code, so every run records exactly what it used: the parameters, the data version, and the random seed.

1:32

Test the model. And test the model, not just the code. Here's a bug we planted on purpose: one term in an SEIR model takes recoveries from the exposed group, instead of the infectious group. The infection curve looks perfect: same peak, same day. But people are quietly disappearing. By the end, the town has lost more than a third of its population. A one line check, does everyone still add up to the whole town, flags it at the very first step. Another check compares the final size with the formula from Module three; a correct model agrees to within a millionth. Checks like these, run automatically after every change, catch exactly the errors that look plausible.

2:08

Next. Next: numerical stability, and how a bigger time step can make your epidemic go negative.

Interactive lab

Step sizes and chance

Change the step size of Euler's method and of Runge–Kutta on the module's SIR town, and run hundreds of exact or tau-leaping stochastic outbreaks in towns of different sizes.

The first panel solves the module's SIR town with the step you choose and compares it with a solution made of tiny steps; the second runs 200 stochastic outbreaks, exactly or with tau-leaping. Everything runs in your browser.

Simple math

Step sizes and chance

The module's SIR town solved with two classic methods, and the same town simulated person by person. Everything runs in your browser.

Euler
next = now + step × slope now

One slope per step. Simple, but its error grows with the step, and large steps can push a compartment below zero.

Runge–Kutta (RK4)
next = now + step × average of 4 slopes

Four slopes per step, weighted 1, 2, 2, 1. Its error shrinks with the fourth power of the step.

Exact or fast chance
Gillespie: one event at a time

Tau-leaping draws many events per fixed step instead: much faster, but big steps distort the epidemic.

Peak
–
Peak error
–
Lowest value
–
Slope evaluations
–

Solving…

Share of the town infectious

Green: tiny steps. Orange: Euler. Blue: Runge–Kutta. Dots mark the steps.

Challenges

Each one is checked on your current settings, in its own panel.

  1. Make Euler push the number of infectious people below zero.
  2. Find a step of at least 2 days where Runge–Kutta's peak is within 1% while Euler's is off by more than 10%.
  3. Chance: with one first infection and R₀ = 2, run the exact method in a town of at least 1,000. About half the runs should fizzle out (between 40% and 60%).
  4. Chance: use tau-leaping with steps of at least 2 days and find settings where more than half the runs try to remove more people than there are.
Fizzled out
–
Infected (outbreaks)
–
Went below zero
–
Events or steps per run
–

Running…

25 runs and the equations

Thin lines: share infectious in single runs. Dark line: the equations.