pₕ: chance per day per infectious household member. k: town meetings a day, p: chance per meeting.
Epidemic ModelingCore models
Module 6Multi-agent simulation
Agent-based models simulate every person. We build agents with states, rules and neighbours, check that average agents give back the SIR equations, and see what heterogeneity changes. Agents can compute anything, which costs prediction, and their contact loops cost time. Then the building blocks: time steps or events, the update order, the neighbourhood, seeds and reproducibility, common random numbers for sharp comparisons, and richer agents. We finish with two case studies in a toy town (a flu-like illness with a school closure, a COVID-like illness with isolation and behaviour), seven design steps, sensitivity analysis, and the ethics and policy risks of these models.
Key ideas, one per episode
- Ep. 20Average agents give back SIR; spread-out agents do not. Agents can compute anything, and their contact loops cost time.
- Ep. 21The time step, the update order and the neighbourhood are modeling choices that change the epidemic.
- Ep. 22Seeds make runs reproducible; common random numbers make policy comparisons sharp; every richer rule needs data.
- Ep. 23Structure shows who is infected and where; delays decide what isolation can do; behaviour feeds back.
- Ep. 24Seven steps: a question with an output, coarse agents, contacts by setting, stage shapes, rules, a deliberate start, enough runs.
- Ep. 25Split uncertainty into inputs and chance, couple runs for sensitivity, protect privacy, report by group, and don't let detail pass for certainty.
Textbook: Chapter 6 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.
Watch
6 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 20 · 3:52
Why agents? Their power and their cost
Why simulate every person? We build agents with states, rules and neighbours, check that 10,000 average agents give back the SIR equations, show how spreading out susceptibility shrinks the epidemic, meet Rule 110 and what agents can compute, and count the cost of checking contacts.
Average agents give back SIR; spread-out agents do not. Agents can compute anything, and their contact loops cost time.
TranscriptEpisode 20
From boxes to agents. Welcome to Module six. Until now, our models moved people between boxes. Today, every person gets a little program of their own. Why would we do that, and what does it cost?
What an agent is. An agent is one simulated individual. It has a state, like susceptible or infected, plus anything else we care about: age, a household, a timer. It has rules for what it does each step. And it has neighbors: the agents it can meet. A scheduler decides when the agents act. Run the rules, and the epidemic curve comes out at the end. Nobody writes it down in advance.
Agents give back SIR. First check: can agents reproduce what we already know? Ten thousand agents. Each infectious agent meets ten random people a day and infects each susceptible one with a chance of two and a half percent. It recovers with a chance of ten percent a day. That's R₀ of 2.5. We ran it forty times, starting from ten cases. On average, eighty nine percent of the town got infected. The equations say the same: eighty nine percent. The peaks landed between day forty six and day sixty; the equations put it on day forty nine. So with everyone average and everyone mixing, agents just give back the SIR model, with some noise on top.
Nobody is average. The point of agents is that nobody has to be average. Keep the same average susceptibility, but spread it out: some people are hardly susceptible, some very much. Now only sixty percent get infected, not eighty nine. The least susceptible fifth: fourteen percent infected. The most susceptible fifth: ninety six percent. The susceptible get infected early, and the wave runs out of fuel sooner. An average hides who drives the epidemic. Agents keep that information.
Rule 110 and what agents can compute. How far can agents go? All the way. Here's a row of cells, each zero or one. At every step, each cell looks at itself and its two neighbors, and a fixed table gives its next value. This table is called Rule 110. Starting from a single live cell, it builds these patterns. And Matthew Cook proved that Rule 110 can carry out any computation at all. Treat each cell as an agent, and you have an agent model that can do anything a computer can. That's good news for flexibility, and bad news for prediction. No general method can tell you in advance whether an arbitrary agent model will even stop. So in practice we reason about the models we actually build.
The cost of contacts, and what to report. And those models cost time. The naive way checks every pair of agents every day. In our town that's fifty million pairs a day, and about 7.9 billion over one epidemic. Our agents only checked their ten contacts, so the work followed the infectious agents: about eight hundred ninety thousand checks per run. Nearly nine thousand times less. Grow the town tenfold, and the pairs grow a hundredfold, while the contact checks grow only tenfold. On a contact network, the work follows the edges of infectious agents instead. So when you report an agent model, report its size: how many agents, how many contacts or edges, how many steps, and which loop dominates the time and memory.
Recap. So: agents are individuals with states, rules and neighbors. With everyone average they give back the equations. Their power is heterogeneity and explicit contacts, and in principle they can compute anything. Their price is design effort and computing time. Next time: building one. How to handle time, who moves first, and the shape of the world the agents live in.
Three choices. Three choices in an agent model look like programming details: how time moves, who acts first, and what counts as a neighbor. Today we'll see that each one can change the epidemic.
Time: steps or events. Time first. The simplest choice is fixed steps: every step, every agent gets a chance to get infected and a chance to recover. Same rules, same town, only the step size changes. Steps of a tenth of a day give a peak of twenty four percent on day forty nine, just like the equations. Daily steps: twenty five percent, on day fifty one. Weekly steps: thirty five percent, on day sixty three. The step itself is a modeling choice. If a lot can happen within one step, a fixed step distorts the result. The alternative is to jump from event to event, as in the Gillespie algorithm from Module three. That's exact, but in a town of ten thousand you handle about eighteen thousand events one by one. Tau leaping sits in between: it lumps the events of a short leap together.
Who acts first. Second: who acts first. A line of two hundred cells, infection at the left end, and every infected cell infects its neighbors for sure. If all cells update at once, from the old state, the front moves one cell per step. That's synchronous updating. Now update one cell at a time, left to right, letting each cell see the new state. In a single step, the infection runs across all two hundred cells. Go right to left, and it moves one cell per step again. With a fresh random order each step, the front moves about 1.7 cells per step. The theory says e minus one, 1.72. The same happens in an SIR town. When everyone acts at once, the peak comes on day fifty six. When agents act one by one in a random order, a person infected in the morning can pass it on that afternoon, and the peak comes on day forty eight. Neither is wrong. But the update scheme is part of the model, so choose it on purpose, shuffle the order if you go one by one, and report it.
Geometry. Third: geometry. On a grid, who is your neighbor? Four cells, up, down, left and right? Or all eight around you? With certain infection, after sixteen steps, four neighbors give a diamond of five hundred forty five cells. Eight neighbors give a square of one thousand eighty nine. With a thirty percent chance per neighbor: one hundred eighteen against five hundred seven. Grids suit fields and buildings. For people, a contact network often matters more than distance, as we saw in Module four. And for crowds in a room, agents can move in continuous space.
Recap. So: steps or events, all at once or one by one, and which neighbors. Each choice changes the timing and the size of the epidemic. Next time: randomness. How to make a random model reproducible, and how to compare two policies without drowning in noise.
Dice everywhere. Agent models roll dice all the time. Today: where the dice come from, how to get the same run twice, and how to compare two policies when every run is different.
Seeds and reproducibility. Computers don't really roll dice. They use pseudo-random number generators: formulas that produce numbers that look random, starting from a seed. Same seed, same numbers, same run. We ran our two thousand agent town twice with seed forty two, and got exactly the same epidemic, agent for agent. Seed forty three gives a different one. So save your seeds. They are part of the result. And on parallel machines, give each process its own stream, or the runs depend on which process happened to be faster.
How different runs are. How different are the runs? Two hundred runs of the same town, R₀ of 1.5, five first cases. Twenty nine fizzled out early. The rest ended between forty five and sixty five percent infected, fifty eight on average. The spread of single runs was about four percentage points. The uncertainty of the average shrinks with the square root of the number of runs: 1.3 points with ten runs, 0.4 with a hundred.
Common random numbers. Now compare two policies. A town of four thousand with R₀ of two, and a policy that removes three percent of contacts. On average it saves about two percent of the town. Run each policy with its own random numbers, two hundred times, and pair the runs up. In thirty seven pairs, the policy looks harmful. Pure noise. Now use common random numbers: give both policies the same random numbers, agent by agent and day by day. Then only six pairs out of two hundred look harmful. The spread of the difference dropped from 2.1 points to 0.9. To get the same precision with independent runs, you'd need more than five times as many.
Richer agents. Finally, richer agents. Our agents follow fixed rules. Agents can also have beliefs, desires and intentions, they can learn from what happens to them, and lately some models even use language models to decide what an agent does. A small taste: let agents who met an infectious person in the last week cut their chance of getting infected in half. Same town, same R₀. The epidemic grows slower and lasts longer, and it reaches forty nine percent of the town instead of eighty. Behavior can matter as much as biology. But every new rule is a new assumption, and it needs data behind it.
Recap. So: seeds make runs reproducible, many runs measure the noise, common random numbers make comparisons sharp, and richer rules add realism at the price of more assumptions. Next time: two real case studies, influenza in a structured town, and COVID with behavior and testing.
FRED and Covasim. Two famous agent models shaped how we study epidemics: FRED for influenza and Covasim for COVID. Today we take the main idea of each, and build a toy version ourselves.
Our toy town. FRED builds synthetic copies of real cities: households, schools and workplaces, where the agents meet every day. Covasim adds detailed infection stages, testing and behavior. Our numbers come from our own small town, not from those tools. Our town: four thousand five hundred seventy eight people in nineteen hundred households, fifty three school classes, one hundred sixty eight workplaces, and random meetings around town. The values are made up to show mechanisms.
A flu-like illness. First, a flu-like illness: two days before becoming infectious, then four infectious days. Forty four percent of the town gets infected. Children: sixty eight percent. Adults: thirty nine. Seniors: thirteen. And we can ask where infection happens: forty four percent at home, nineteen percent at school, thirteen percent at work, and twenty four percent around town. A model with one infected box can't answer that question. A town of agents answers it directly.
Closing schools. Now close the schools for eight weeks, starting on the day two percent of the town has been infected. While they're shut, the epidemic stays low. When they reopen, it comes back, with a peak of 1.4% infectious on day one hundred twenty eight, against 3.8% on day fifty one without closure. In the end, thirty percent infected instead of forty four. The closure lowered the total, but it mostly moved the wave. When to reopen is as important as when to close.
A COVID-like illness and isolation. Second, a COVID-like illness in the same town: three days before becoming infectious, then two infectious days before symptoms, then five more. Thirty five percent never get symptoms, and they're half as infectious. Sixty six percent get infected, and twenty seven percent of infections come from people who don't have symptoms yet. Now let symptomatic people isolate, with eighty percent following the advice. Isolating on the first day of symptoms: thirty four percent infected. One day later: forty four. Two days: fifty one. Four days: sixty. Every day of delay costs. And the faster people isolate, the more of what remains comes from people without symptoms: thirty nine percent with same-day isolation. That's the part waiting for symptoms can never catch.
Behaviour feeds back. Finally, behavior. Let people cut their work and town contacts as reported cases rise, reported a week late. The peak drops from twelve percent infectious to 8.6%, and the total from sixty six percent to forty nine.
Recap. So: structure tells you who gets infected and where. Stages and delays decide what testing and isolation can do. And behavior feeds back on the epidemic, as in Module two, now inside each agent. Next time: designing your own agent model, step by step.
Seven steps. Building an agent model is a chain of decisions. Today, seven steps that keep each decision tied to the question you're asking.
A question that names its output. Step one: a question that names its output. What quantity, in which population, over what time, compared with what? And what must the model reproduce before you trust it? How do schools affect transmission is too vague. How much does closing schools for eight weeks change infections among adults over two hundred days, is a question a model can answer. And ask whether you need agents at all. If everyone can be treated as average, a compartmental model is clearer and faster.
Agents and contacts. Step two: the agents. Choose the coarsest agent that still expresses the mechanism: people, households or places. Then their state: health state, time in that state, age, household. Step three: who meets whom. If your intervention acts on a setting, like closing schools, you need contacts organized by setting.
Stage durations. Step four: the illness itself, and how long each stage lasts. Same average durations, two days before becoming infectious and four infectious days, but three shapes. Memoryless, exponential durations: the peak comes on day seventy two, at 2.5% infectious. Four stages in a row: day fifty four, at 3.9%. Fixed durations: day forty eight, at 4.4%. Part of the reason: with exponential durations, many infections are very short and a few very long, and the long ones spend extra days on family members who are already infected. If timing matters to your question, the shape matters.
Interventions as rule changes. Step five: behavior and interventions, written as rule changes. Vaccination lowers susceptibility. Isolation removes contacts. Masks lower the chance per contact.
The starting point. Step six: the starting point. Who lives where, and who the first cases are. We started outbreaks with two cases, a hundred times each. Two children in one school class: sixty seven outbreaks took off, and they reached two percent of the town on day twenty nine on average. Two random adults: fifty seven took off, day thirty. Two seniors: twenty seven, day thirty three. Where the first cases are changes whether an outbreak starts, and how fast. Treat it as a modeling decision, not a detail.
Outputs and how many runs. Step seven: run and collect. Decide in advance what you log and how often. Logging everything slows the model and invites fishing for results. And decide how many runs you need. Single runs of our town spread by about two percentage points. To pin the average down to plus or minus one point, you need fourteen runs. For half a point, fifty three.
Recap. So: a question with an output, the coarsest agents that work, contacts by setting, stage durations with the right shape, interventions as rule changes, a deliberate start, and an output plan with enough runs. Next time: how sensitive the answer is, and the ethics and risks of putting these models to use.
How much to trust the number. An agent model has dozens of uncertain inputs, and it gives a different answer every run. How much should anyone trust its number? And what can go wrong when it's used?
Inputs or chance. The spread in the output has two sources. Inputs we don't know exactly, and chance: same inputs, different runs. The total variance splits into the variance of the average across inputs, plus the average of the run-to-run variance. We drew thirty settings for school and town transmission, each between seventy and one hundred thirty percent of our values, and ran each eight times. The inputs explain forty five percent of the variance; chance explains the rest. Ninety percent of runs ended between thirty four and fifty one percent infected. So report that range, not one run's number. And the split tells you where to spend effort: on better data for the inputs, or on more runs.
Local sensitivity. Local sensitivity asks a narrower question: nudge one input, and how much does the output move? We raised town transmission by five percent and measured the change in the share infected. With independent runs, eleven of thirty estimates came out negative, as if more transmission meant fewer infections. The lowest was minus 0.8 and the highest 0.9, plus one wild value from a run that fizzled. With common random numbers, all thirty landed between zero and about 0.6. The average was 0.26. Couple the runs, or the noise swamps the effect. Inputs that jump, like schools open or closed, are compared as scenarios. Global methods such as Morris and Sobol come in Module eleven.
Privacy. Now the ethics. Realistic agents are built from data about real people, and realism can expose them. Even in our made-up town, an exact age, a household size and a neighborhood single out one person for a quarter of the town. Group ages into ten year bands, and that falls to 0.4%. Coarsen, aggregate, and treat synthetic populations as sensitive data.
Who carries the burden. The burden isn't even, either. In our flu-like runs, nineteen percent of people living alone got infected, against eighty percent of people in six person households. And interventions land unevenly too: closing schools costs some families far more than others. So report results by group, not only the total.
Policy risk. Finally, policy risk. A detailed model looks precise, and its output can be given more authority than its data support. State the question it was built for, the range of outcomes, and what it cannot tell you.
Recap. So: split the uncertainty into inputs and chance, couple runs when you measure sensitivity, protect the people behind the data, look at who carries the burden, and don't let detail pass for certainty. That closes Module six. Next, in Module seven, we turn from simulating epidemics to learning them from data.
3:52Episode 20 · 6.1Why agents? Their power and their costAverage agents give back SIR; spread-out agents do not. Agents can compute anything, and their contact loops cost time.
3:15Episode 21 · 6.2Building an agent-based modelThe time step, the update order and the neighbourhood are modeling choices that change the epidemic.
3:08Episode 22 · 6.3Randomness and reproducibilitySeeds make runs reproducible; common random numbers make policy comparisons sharp; every richer rule needs data.
3:14Episode 23 · 6.4Two case studies: flu-like illness and COVID-19Structure shows who is infected and where; delays decide what isolation can do; behaviour feeds back.
3:17Episode 24 · 6.5Designing your own simulationSeven steps: a question with an output, coarse agents, contacts by setting, stage shapes, rules, a deliberate start, enough runs.
3:12Episode 25 · 6.6Sensitivity, ethics and policy riskSplit uncertainty into inputs and chance, couple runs for sensitivity, protect privacy, report by group, and don't let detail pass for certainty.
Interactive lab
A town of agents
Simulate one agent per person in a town of households, see how stage durations change timing, and compare a policy with and without common random numbers.
The first panel runs one epidemic in a town of about 1,800 agents; the second runs pairs of epidemics with and without a policy, with independent or common random numbers. Everything runs in your browser.
A town of households, one agent per person
About 1,800 agents in 700 households, simulated one day at a time in your browser.
One stage is exponential (memoryless); more stages make durations more regular; fixed means always 2 and 4 days.
Every random number is tied to a seed, a day and an agent. Runs that share a seed share their luck, so their difference is sharp.
Running…
Infectious now
Share of the town infectious, day by day (first 200 days).
Challenges
Each one is checked on your current settings, here or in the comparison panel below.
- Without changing the three transmission sliders, find a run that peaks after day 70. Hint: stages and seeds.
- Make home the place of more than half of all infections.
- Compare a 5% cut in town meetings so that no pair of runs points the wrong way.
- Get the spread of the difference below 2 points.
Running…
Differences, pair by pair
Orange bars: pairs where the policy seemed to make things worse.
