Tutorials
Fitting an outbreak to case data
Updated 3 November 2026 · 5 min read
An outbreak model is only as good as its contact rate, and nobody can measure a contact rate directly. What you can measure is how many people are ill each day. In this tutorial you import six weeks of case counts, let Fit to Data find the contact rate that explains them, and then work out how far to trust what it found.
Before you start#
You need the outbreak example from the Models page, and this file of daily case counts:
outbreak-cases.csv46 days of people reported ill, day 0 to day 45 · 336 bytesThe counts are made up for teaching, and that is the point: they were made from the outbreak model with a contact rate of 0.42 a day, then given 6% random scatter, about what real reporting has. So you know the answer before you start, and you can see how close the fit gets.
Import the counts#
Open the outbreak example
On the Models page, open Outbreak in a town. It models a town of 10,000 people with 10 ill on day 0, a contact rate of 0.35 a day, and an illness that lasts 7 days.
Add the CSV
In the side panel, open Fit to Data. Under Add measurements from a CSV, set Compare with to Infected, the stock the counts are measurements of. Press Choose file and pick
outbreak-cases.csv.The panel confirms what it read: 46 points, day 0 to day 45. The series appears in the list, compared with Infected.
- 1The imported series: where it came from, its first and last day, and what it is compared with.
- 2Compare with: the stock or variable the measurements are of.
- 3Choose file adds another series.
See how far out the model is
Open Simulation again and look at the charts. The counts are drawn as dots against the model’s Infected line, and they don’t agree: the reports rise faster and peak higher and earlier than the model, which peaks at 2,265 on day 35. The summary under the canvas says the same in one number: R² = 0.444. The model explains less than half of the variation in the counts.
Before fitting: the reported counts (dots) against the model’s Infected line.
Fit the contact rate#
Tick what you don’t know
Under Calibrate, tick Contact rate and nothing else. The population, the number ill on day 0 and the length of the illness stay as they are. You are saying: I believe those, and I want the data to tell me the contact rate.
Press Fit
Press Fit 1 constant to data. Lsqcurvefit runs the model again and again, moving the contact rate within its range until the model’s Infected comes as close to the dots as it can. It takes a second or two.
- 1The fitted value, with its ± range.
- 2How well the model now follows the counts.
The contact rate comes back as 0.419 ± 0.0026 a day, with an R² of 0.990. The true value, 0.42, lies inside that range. The fitted value is written into the constant, so the model now runs with it: its peak moves to 2,921 people on day 28, where the reports are.
- Reported ill
- Contact rate 0.35 (the example's)
- Contact rate 0.419 (fitted)
Show the numbers
| day | Reported ill | Contact rate 0.35 (the example's) | Contact rate 0.419 (fitted) |
|---|---|---|---|
| 0 | 10 | 10 | 10 |
| 5 | 45 | 28 | 40 |
| 10 | 150 | 78 | 153 |
| 15 | 567 | 211 | 550 |
| 20 | 1612 | 536 | 1566 |
| 25 | 2756 | 1166 | 2734 |
| 30 | 2918 | 1926 | 2798 |
| 35 | 1831 | 2265 | 2073 |
| 40 | 1267 | 2010 | 1326 |
| 45 | 807 | 1497 | 793 |
| 50 | – | 1011 | 460 |
| 55 | – | 647 | 262 |
| 60 | – | 403 | 148 |
| 65 | – | 246 | 84 |
| 70 | – | 149 | 47 |
| 75 | – | 90 | 26 |
| 80 | – | 54 | 15 |
How far to trust it#
The ± is the part of the answer people skip, and it is the part that tells you whether the answer means anything. It says how far the scatter in the counts leaves the contact rate uncertain: about 95% of the time, the true value is within that range. Here the range is 0.4162 to 0.4215. Six weeks of daily counts pin a contact rate down very tightly.
What the ± can’t tell you is whether the model is right. It assumes the model is, and asks only how noisy the data are. A wrong model fitted to clean data comes back with a tight ±, around the wrong answer. The oil field tutorial shows exactly that, and how to catch it.
Try this#
Fit the length of the illness as well#
Tick Days ill too and press Fit 2 constants to data. With two values to find, the fit also starts from two spread-out points in their ranges and keeps the best, so it takes a few seconds longer. It finds a contact rate of 0.4213 ± 0.0033 and 6.87 ± 0.11 days ill, with R² 0.991.
The true length is 7 days, just outside its range. Two things are going on. A 95% range misses the truth one time in twenty by chance. And the ± assumes the scatter is the same size on every day, when here it grows with the count: 6% of 2,800 is a lot more than 6% of 50. The big counts around the peak count for more than they should. Calibration, uncertainty and validation goes further into this.
Fit with only the first two weeks#
Suppose you were fitting on day 14, before the peak. With both constants ticked, the fit matches the first 15 days beautifully, R² 0.997, and finds a contact rate of 0.57 ± 0.18 and 3.4 ± 2.0 days ill. Both are wrong, and the model it gives predicts a peak of 1,475 on day 24, half the real one.
- Reported ill (first 15 days used)
- Fitted to all 46 days
- Fitted to the first 15 days
Show the numbers
| day | Reported ill (first 15 days used) | Fitted to all 46 days | Fitted to the first 15 days |
|---|---|---|---|
| 0 | 10 | 10 | 10 |
| 5 | 45 | 40 | 40 |
| 10 | 150 | 153 | 157 |
| 15 | 567 | 550 | 532 |
| 20 | 1612 | 1566 | 1213 |
| 25 | 2756 | 2734 | 1452 |
| 30 | 2918 | 2798 | 1012 |
| 35 | 1831 | 2073 | 533 |
| 40 | 1267 | 1326 | 249 |
| 45 | 807 | 793 | 111 |
| 50 | – | 460 | 48 |
| 55 | – | 262 | 21 |
| 60 | – | 148 | 9 |
| 65 | – | 84 | 4 |
| 70 | – | 47 | 2 |
| 75 | – | 26 | 1 |
| 80 | – | 15 | 0 |
The reason is worth working through. Early in an outbreak, nearly everyone is still susceptible, and the number ill grows at a rate of contact rate minus one over days ill. For the truth that is 0.42 − 1/7 = 0.277 a day. For the early fit it is 0.573 − 1/3.42 = 0.281 a day: nearly the same. Every pair of values with that difference fits the first two weeks equally well, so the data can’t tell them apart, and the large ± values say so. They only separate once the outbreak starts to run out of people to infect.
That is the most useful thing a ± can do: warn you that the data you have can’t answer the question you are asking. When both values have ranges this wide, believe the ranges, not the values.
What you learned#
You imported measurements, compared the model with them, and fitted the one constant you didn’t know. You read the ± as well as the value, and you saw two ways it can mislead: scatter that grows with the count, and data that can’t separate two constants. The next tutorial goes back to building, and puts one model inside another.
See also
- Two towns, one outbreakPut a model inside another twice, connect the copies with a flow, and watch the second outbreak follow the first.
- Import measurements from a CSVGet a column of measurements into the model, drawn as dots against the line it should follow.
- Fit constants to measurementsChoose what to fit, run the fit, and read the ± and R² that come back.
- Calibration, uncertainty and validationWhat a fit can and can't tell you, and how to test a model on data it hasn't seen.