γ = 1 / infectious period. Without other boxes, R₀ = β / γ and the epidemic stops while some people were never infected.
Epidemic ModelingCore models
Module 3Compartmental models
The workhorse of epidemic modeling. We build the SIR model from its assumptions, derive R0, the epidemic threshold and the final-size relation, and see why epidemics stop before everyone is infected. Then we add boxes (births and deaths, reinfection, latency, infectious stages, hospital outcomes, age groups, mosquitoes), let transmission change with seasons, interventions and behavior, bring in chance with stochastic simulation and early extinction, and finish with the next-generation matrix, the general recipe for R0.
Key ideas, one per episode
- Ep. 7SIR from scratch: R0 = β/γ, growth needs S > 1/R0, and the epidemic stops before everyone is infected.
- Ep. 8Births refill susceptibles, fading immunity brings waves back, and latency changes the timing, not the total.
- Ep. 9Stages, outcomes, groups and vectors: hidden stages sustain spread, hospitals lag, and bites count twice.
- Ep. 10β moves: seasons as a yearly wave, interventions as steps, behavior as feedback; R(t) is not R0.
- Ep. 11Chance decides whether an outbreak takes off (about 1/R0 fizzle); the equations describe it if it does.
- Ep. 12The next-generation matrix: R0 is its dominant eigenvalue, and it shows where transmission lives.
Textbook: Chapter 3 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 7 · 4:32
SIR from scratch and the meaning of R₀
The workhorse of epidemic modeling, built from scratch: three boxes, five assumptions, two rates.
SIR from scratch: R0 = β/γ, growth needs S > 1/R0, and the epidemic stops before everyone is infected.
TranscriptEpisode 7
Compartmental models. Welcome to Module three. For the next six episodes, we build the field's workhorse: compartmental models. We start with the simplest one, the SIR model. Three boxes, two arrows, and a surprising amount of insight.
Three boxes, five assumptions. Split the population into three groups. Susceptible people can catch it. Infected people can pass it on. Removed people are out of the game: recovered, isolated, or dead. The basic model makes five simplifying assumptions. Everyone mixes with everyone, equally. New infections follow mass action. And nobody is born, dies of other causes, or moves away. Recovery gives permanent immunity. And the time someone stays infectious is random, with the same chance of recovering every day. None of these is literally true. They give us a clean baseline, and every later model relaxes one of them.
The SIR equations. Now the equations. Susceptible people become infected at rate beta, times S, times I. Infected people recover at rate gamma, times I. Beta packs two things together: how often people meet, and the chance that a meeting passes the infection on. One over gamma is the average time someone stays infectious. One choice matters more than it looks. Written like this, transmission grows with the size of the crowd. Divide by the population, and each person makes a fixed number of contacts, however big the town is.
R0, the threshold and the peak. At the start, almost everyone is susceptible, so each case infects about beta over gamma people in total. That's the basic reproduction number, R₀. In our run, beta is 0.3 per day, and people stay infectious for ten days. So R₀ is three. Another way to see it: ten contacts a day, a three percent chance of passing it on per contact, for ten days. Ten times 0.03 times ten is three. And R₀ is not speed. Early on, infections grow at rate beta minus gamma, 0.2 per day here, so they double about every three and a half days. Two diseases with the same R₀ can grow at very different speeds. Infections grow only while the susceptible share is above one over R₀. Here, one third. As susceptibles run out, the effective number, R₀ times the susceptible share, falls. It crosses one exactly at the peak. In our run, around day thirty eight. So the peak isn't when the virus gets tired. It's when there aren't enough susceptible people left for each case to replace itself.
Final size and overshoot. Here's the second surprise. The epidemic ends while some people were never infected. Take time out of the equations and you get the final size relation. The total share infected depends only on R₀. An R₀ of 1.5 infects about fifty eight percent. Two, about eighty percent. Three, about ninety four percent. And notice the overshoot. With R₀ three, herd immunity is reached when two thirds are immune, but the epidemic runs on to ninety four percent, because so many people are still infected at that moment. That gap matters for policy. Vaccinating two thirds before an outbreak prevents it. Reaching two thirds immune by infection costs another quarter of the town on top.
Counts can't go negative. One sanity check before we build bigger models: counts can't go negative. In the exact equations, they never do. S can only shrink toward zero, R only grows, and I can never cross below zero. So if your simulation shows a negative number of susceptible people, the model isn't wrong. Your numerical method is. Take ten-day steps with the simplest method, and S drops below zero. More in Module twelve.
What we observe. And in real life, we never see S, I and R. We see reported cases, hospital admissions and deaths. So applied models add an observation layer on top: only a share of infections gets reported, with delays and noise. The SIR flows stay the same; the layer links them to the data. Modules seven and eight live there.
Recap. So: three boxes, two rates. R₀ is beta over gamma, the threshold is a susceptible share of one over R₀, and the final size depends only on R₀. Next time, we add more boxes: births and deaths, reinfection, and a latent period.
Real diseases come back. The basic SIR model has one wave, and then it's over forever. Real diseases come back. Today, three ways to bring them back, and one way to slow them down. We add births and deaths, reinfection, and a latent period.
Births, deaths, endemic waves. First, births and deaths. Babies arrive susceptible, and everyone dies of other causes at some rate. Now the susceptible pool keeps refilling, so the disease can settle in for good. That's an endemic disease. At balance, the susceptible share is exactly one over R₀. In our town, R₀ is five, so a fifth of people are susceptible at any time. It also tells us the average age at infection: roughly life expectancy divided by R₀ minus one. With a fifty-year life expectancy, about twelve and a half years. The higher R₀, the earlier in life people catch it. That's why measles, with a very high R₀, used to be a disease of childhood. Started near that balance, the disease comes back in waves every four to six years, each one a bit smaller, as it settles down. No seasons, no behavior change. The waves come from births alone, slowly refilling the pool. Near balance, theory predicts a period of about 3.7 years for these waves. Our run starts a bit away from balance, so its first waves are slower, and they speed up as they settle.
How much vaccination. This also tells us how much vaccination we need. Vaccinate a share of newborns, and R₀ effectively shrinks by that share. To eliminate the disease, push it below one. With R₀ five, that's eighty percent of newborns.
Reinfection: SIS and SIRS. Second, reinfection. If recovery gives no immunity at all, people go straight back to susceptible. That's the S I S model. With R₀ three, it settles at two thirds of people infected at any time. A steady state, with no waves. Some bacterial infections behave roughly like this, since recovering from them leaves little protection. If immunity lasts a while and then fades, that's S I R S. In our run, immunity lasts a year on average. After the big first wave, smaller waves follow about every nine months, and it settles near two percent infected. Common colds and seasonal coronaviruses are closer to this picture: you can catch the same virus again a year or two later.
Latency: SEIR. Third, latency. Newly infected people usually can't infect others right away. So we add an exposed box, E, between S and I. With the same R₀ of three and five days of latency, growth slows down a lot. Cases double every seven days, instead of every three and a half. The peak comes on day seventy four instead of day thirty eight, and it's lower: twenty percent infected at once, instead of thirty. But the final size is the same, ninety four percent. Latency changes the timing, not the total.
Erlang stages, and which box. One more refinement. In a single exposed box, people leave at a constant rate, so some leave after one day and some after twenty. Real latent periods are more regular. The trick is to split E into several stages in a row. Same average, much less spread. With four stages, the peak in our run moves from day seventy four to day eighty. Small, but it matters when you fit real data. So which box do you add? It depends on the disease and the question. Births and deaths for long time scales, waning for diseases you can catch again, latency whenever the delay to infectiousness matters for timing or control.
Recap. So: births refill the susceptible pool and make a disease endemic. Fading immunity brings waves back. Latency stretches the timeline without changing the total. Next time, more classes: different kinds of infected people, hospital outcomes, age groups, and mosquitoes.
Not every infection is the same. Not every infected person is the same. Some never feel sick, some spread it before they know it, and some end up in hospital. Today, we give each of them a box.
Several infectious stages. Picture a toy disease. Everyone spends two days infectious before symptoms. Then seven in ten get sick for five days, and three in ten never show symptoms and spread at half the rate. Add up the contribution of each stage, and R₀ comes out at about 1.9. About a third of that comes before symptoms, more than half from people with symptoms, and twelve percent from people who never feel ill. Now suppose people with symptoms isolate after one day. R₀ drops to about 1.04. Much better, but still above one. The hidden stages keep it going. The structure of the infected classes decides what control can achieve.
Hospital, deaths and delays. Next, outcomes. Instead of one removed box, we track who goes to hospital, and who dies. In our run, three percent of infections need a hospital bed for ten days, and half a percent are fatal, about three weeks after infection. New infections peak on day eighty six. Hospital occupancy peaks on day one hundred four, at about five hundred fifty beds per hundred thousand people. Deaths peak on day one hundred six. A word on two ratios. The infection fatality ratio divides deaths by all infections. The case fatality ratio divides deaths by reported cases. Early in an outbreak, when few mild cases are tested, the second can be many times the first. That lag is the point. By the time hospitals are fullest, new infections have been falling for almost three weeks.
Structured populations. Third, structure. People differ by age, risk or place, and they mix unevenly, as we saw in Module two. So we split the population into groups, each with its own S, I and R, linked by a contact matrix. In our two-group town, a quarter of people are children, and they have many more contacts. Ninety five percent of children get infected, against seventy three percent of adults. With more groups, say ten age bands, we get ten copies of the equations, and a ten by ten matrix of who infects whom. Bigger, but the same idea. One R₀ for the whole town hides that. The last episode of this module shows how to get one honest number out of a matrix like this.
Vectors: mosquitoes. Last, vectors. Malaria and dengue need a mosquito in between. People infect mosquitoes, and mosquitoes infect people. So we couple two models: S I R for people, and S I for mosquitoes, which usually stay infected for the rest of their short lives. Mosquitoes add their own clock too. After biting an infected person, the parasite needs days to develop inside the mosquito, and many mosquitoes die before it's ready. Toy numbers: one infected person infects ten mosquitoes, and each infected mosquito infects 0.3 people. From person to person, over the two steps, that's three. Halve the number of mosquitoes, and it drops to 1.5. But halve the bites, with bed nets, and it drops to 0.75. Below one. Bites count twice: a mosquito bites to get infected, and bites again to pass it on. That's why the biting rate appears squared in the classic Ross Macdonald formula.
Recap. So: split the infected by stage, track outcomes with their delays, split the population into groups, and couple hosts with vectors. Next time, we let the rates change over time: seasons, interventions and behavior.
Beta is never constant. So far, beta has been a constant. In real life, it never is. Winter comes, schools close, people get scared. Today, beta becomes a function of time.
Seasons. Start with seasons. Many respiratory infections spread better in winter: people crowd indoors, and some viruses survive longer in cold, dry air. Schools add their own rhythm. Classic studies of measles found that epidemics followed the school calendar: transmission rose when terms started, and fell during holidays. A simple model: beta goes up and down once a year, twenty percent above and below its average. From peak to trough, that's a ratio of 1.5. Combine that with immunity that fades after about a year. Without seasons, our model settles at a steady 1.6 percent infected. With seasons, it locks into a yearly wave, peaking at about eight percent, right around the time transmission is highest. A modest swing in beta becomes a big swing in cases, because the epidemic amplifies it. And the timing of the yearly peak is set by the season, not by when the first case arrived. In our run, the first wave peaks on day thirty nine; a few years later, every peak lands within a day of the yearly high point of transmission.
Interventions as steps. Next, interventions. The simplest way is piecewise: beta drops on the day a measure starts, and comes back on the day it ends. In reality, measures switch on gradually, and not everyone complies. A smoother ramp, or a compliance share, can capture that. But steps are a good first sketch. In our run, a measure cuts transmission from day thirty to day ninety. A twenty percent cut lowers the peak from thirty percent to twenty two. A forty percent cut brings it down to thirteen. But a sixty percent cut does worse than forty: the peak is fifteen percent, on day one hundred fifteen. Strong but short, and most people are still susceptible when it ends. More is not always better, if it stops too soon. Strength and timing work together. This is the same lesson as in Module two, now written as a beta that changes on chosen days. We'll optimize those days properly in Module ten.
Feedback and the product form. Third, beta can depend on the epidemic itself. When cases rise, people cut contacts, and beta falls. We built exactly this in Module two. Then beta is no longer a function of time alone. It depends on the state of the system, and that feedback can create plateaus and new waves. A handy way to write all of this: beta of t equals a baseline, times a season factor, times a policy factor, times a behavior factor. Each factor is a small model of its own.
R0 versus R(t). One warning about words. R₀ is defined at the start, with everyone susceptible and nothing changed. Once beta changes, what you measure is the effective reproduction number, R t. It includes immunity, interventions, behavior and season, all at once. Estimating R t from data is a big topic of its own: you need the delays from infection to report, and the typical gap between one infection and the next. We'll see the tools in Module seven. So when the news says R is now 0.9, it means R t, not R₀.
Recap. So: seasons as a smooth yearly wave, interventions as steps, and behavior as feedback. Same model, with a beta that moves. Next time, we stop pretending that everything is smooth and certain. Chance.
One case: fizzle or take off. In the equations, one infected person in a town of a thousand always starts an epidemic, if R₀ is above one. In reality, the first case often just recovers, and that's the end of it.
Random events, Gillespie. To capture that, we switch from smooth flows to random events. People are whole numbers. Either someone gets infected, or someone recovers. Each event has a rate: beta times S times I over N for an infection, gamma times I for a recovery. The time to the next event is random, and so is which event it is. Simulating this one event at a time is called the Gillespie algorithm. Run it many times, and you get a whole distribution of outbreaks, instead of one curve. There are faster cousins, too. Tau-leaping jumps ahead by small time steps and draws how many events happened. Chain binomial models, like the classic Reed Frost model, move in whole generations.
Early extinction and 1/R0. We ran it two thousand times in a town of a thousand, starting from one case, with R₀ of two. In forty nine percent of the runs, the outbreak died out on its own after a handful of cases. The rest took off. Theory predicts this. Early on, the outbreak is a branching process, and with these assumptions the chance of extinction is one over R₀. One half. With R₀ three, we got thirty two percent, against a third in theory. And starting from three cases instead of one, the chance drops to about one in eight.
Tiny or big outbreaks. So the outcomes split in two. Either a tiny outbreak, or a big one. Almost nothing in between. And when it takes off, the big outbreak matches the smooth model closely: about eighty percent infected on average, just like the equations. The equations describe what happens if it takes off. Chance decides whether it takes off.
Timing spread and bands. Timing varies too. Among the big outbreaks with R₀ two, the peak came on day sixty six on average, give or take fifteen days. That matters for planning. A single smooth curve hides that spread. Instead of one curve, report a band: the range where, say, ninety percent of the simulated epidemics fall. That band is an honest picture of chance alone, before we even question the parameters.
When chance matters. So when do you need a stochastic model? When numbers are small: the first cases, a small community, the tail end of an epidemic, or elimination. Small numbers also cause fade-out. After a big wave, cases can drop so low that the chain breaks by chance. For measles, studies found it kept circulating without fade-out only in cities of a few hundred thousand people or more. That's the critical community size. When numbers are large, the smooth equations give a good average, and they're much cheaper to run. And remember Module two: if spread is uneven, early extinction gets even more likely, and the outbreaks that survive are more explosive. This randomness comes from individual events, so it shrinks as the population grows. A different kind, like a random cold spell, hits everyone at once, and doesn't average out.
Recap. So: random events, many runs, a distribution. Early extinction with a chance of about one over R₀, and big outbreaks that look like the equations. Next time, the last tool of this module: the next generation matrix, for when one simple R₀ formula is not enough.
When beta over gamma breaks. R₀ equals beta over gamma works for one simple model. Add stages, age groups or mosquitoes, and that formula breaks. Today, the general recipe.
Generations and the matrix. The idea is to follow infections from generation to generation. For a typical infected person of each type, ask: how many new infected people of each type do they cause? Put those numbers in a matrix, called the next generation matrix. R₀ is its dominant eigenvalue: the long-run growth factor per generation, once the mix of infected types settles down.
F and V, and SEIR. To build it, split the equations of the infected boxes in two. F holds new infections arriving. V holds everything else: moves between infected boxes, and exits. The matrix is F times the inverse of V. In words: new infections per day, times the time spent in each infected box. Try it on S E I R. Only I creates new infections, and the latent box just delays things. The recipe gives R₀ equal to beta over gamma, the same as S I R. Latency cancels out. In symbols: F has beta in the corner, V has sigma and gamma, and the product gives sigma over sigma, times beta over gamma. The sigma, the latent rate, cancels.
The two-group town. Now our two-group town from two episodes ago. Children are a quarter of the town, but they meet each other a lot. One infected child infects about 2.1 children and 1.3 adults. One infected adult infects about 0.4 children and 1.3 adults. Neither group alone tells the story. Children alone would give 2.1, adults alone 1.3. The dominant eigenvalue of the matrix is about 2.5. And its eigenvector tells us who the cases are, generation after generation: about half children and half adults, though children are only a quarter of the town. The same recipe handles Episode three's toy disease: presymptomatic, symptomatic and asymptomatic stages are three infected boxes, and the matrix adds up their contributions to 1.9, as before.
Comparing policies. Now we can compare policies properly. Cut contacts among children by sixty percent, say by closing schools, and R₀ falls from 2.5 to 1.8. Cut contacts among adults by the same sixty percent, and it only falls to 2.4. Same cut, very different effect. The matrix shows where transmission really lives. You can push this further: ask how much R₀ changes when you change each entry of the matrix. Those sensitivities point to the transmission routes worth targeting first.
Vectors and conventions. One subtlety with vectors. In the next generation matrix, one generation is one step: person to mosquito, or mosquito to person. So for our mosquito toy, the matrix gives the square root of three, about 1.7, instead of three over two steps. Same threshold at one, different number. Always check which one a paper uses.
The fine print. And the fine print. The recipe describes the start of an outbreak, near a disease-free state. It doesn't give you the peak or the final size. And how you split F and V is a modeling choice. Different splits can give different numbers, with the same threshold at one.
Module 3 recap. That's Module three: SIR and its family, rates that change over time, chance, and a recipe for R₀ that works for any number of infected types. Next, Module four: space. Cities linked by travel, networks of people, and epidemics that move like waves across a map.
4:32Episode 7 · 3.1SIR from scratch and the meaning of R₀SIR from scratch: R0 = β/γ, growth needs S > 1/R0, and the epidemic stops before everyone is infected.
4:14Episode 8 · 3.2More states: births, deaths, reinfection, latencyBirths refill susceptibles, fading immunity brings waves back, and latency changes the timing, not the total.
3:54Episode 9 · 3.3More classes: stages, outcomes, structure, vectorsStages, outcomes, groups and vectors: hidden stages sustain spread, hospitals lag, and bites count twice.
3:56Episode 10 · 3.4When transmission changesβ moves: seasons as a yearly wave, interventions as steps, behavior as feedback; R(t) is not R0.
3:47Episode 11 · 3.5Chance matters: stochastic modelsChance decides whether an outbreak takes off (about 1/R0 fizzle); the equations describe it if it does.
3:50Episode 12 · 3.6The next-generation matrixThe next-generation matrix: R0 is its dominant eigenvalue, and it shows where transmission lives.
Interactive lab
SIR-family builder and chance
Build SIR, SEIR, SIRS and seasonal models from the same controls, and run hundreds of stochastic outbreaks to compare chance with the equations.
The builder re-solves the equations as you change R0 and the boxes; the chance panel runs Gillespie simulations in your browser and compares the share of outbreaks that fizzle with the 1/R0 rule.
One model, many boxes
Shares of a town, one in a thousand infected on day 0. Switch boxes on and off; the transmission rate β₀ is set so that R₀ is the value on the slider.
Latency adds E (infected, not yet infectious). Waning immunity sends R back to S. Births and deaths (life 50 years) refill S.
Transmission swings once a year, highest on day 0, 365, 730 and so on.
Solving…
All boxes
Shares of the town over time.
Infected, zoomed
Marks: waves (peaks above 0.1%, at least 60 days apart).
Challenges
Each one is checked on your current settings, in the builder or in the chance panel below.
- Keep R₀ at 3, switch latency on, and push the peak past day 100.
- With waning immunity and seasons on, make every yearly wave after the first bigger than 15%.
- In the chance panel, with R₀ above 1, make more than 60% of runs fizzle.
Running…
Final sizes
How many runs ended at each share infected (5% bins). A run that infects under 5% of the town counts as fizzled.
