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 bytes

The 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#

  1. 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.

  2. 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.

    1. 1The imported series: where it came from, its first and last day, and what it is compared with.
    2. 2Compare with: the stock or variable the measurements are of.
    3. 3Choose file adds another series.
  3. 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#

  1. 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.

  2. 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.

    1. 1The fitted value, with its ± range.
    2. 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)
05001K1.5K2K2.5K3K01020304050607080daypeople ill
Show the numbers
dayReported illContact rate 0.35 (the example's)Contact rate 0.419 (fitted)
0101010
5452840
1015078153
15567211550
2016125361566
25275611662734
30291819262798
35183122652073
40126720101326
458071497793
50–1011460
55–647262
60–403148
65–24684
70–14947
75–9026
80–5415
The case counts with the model before and after fitting. Hover to read any day. The example’s contact rate, 0.35, peaks a week late and 650 people low; the fitted 0.419 runs through the counts.

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
05001K1.5K2K2.5K3K01020304050607080daypeople illdata ends
Show the numbers
dayReported ill (first 15 days used)Fitted to all 46 daysFitted to the first 15 days
0101010
5454040
10150153157
15567550532
20161215661213
25275627341452
30291827981012
3518312073533
4012671326249
45807793111
50–46048
55–26221
60–1489
65–844
70–472
75–261
80–150
Fitted to the first 15 days only (the dark dots), the model matches them better than the full fit does, and gets the outbreak wrong. The faint dots are the days it didn’t see.

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.