Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Projects

Project: Fit an Epidemic Model to Data

Simulate an SIR epidemic with solve_ivp, fit its parameters with least_squares, quantify the uncertainty and forecast the outbreak with a confidence band.

In this project you model an outbreak in a town of 50,000 people. The first month of daily case counts is in, the numbers are climbing fast, and the health office wants answers: how contagious is the disease, when will it peak, and how sure are you?

The tools come from earlier chapters. Solving ODEs with solve_ivp turns a differential-equation model into a trajectory. Curve Fitting and Least Squares adjusts the model's parameters until the trajectory matches the data. The real lesson of the project, however, is about honesty: a fitted model gives a single best curve, but early in an epidemic many different parameter sets fit equally well, and a good analyst shows that.

As in the previous project, the data are synthetic, generated from known parameters, so we can check every conclusion against the truth.

flowchart LR
    A["SIR equations"] --> B["solve_ivp<br/>simulate"]
    B --> C["least_squares<br/>fit beta, gamma"]
    C --> D["Jacobian + bootstrap<br/>uncertainty"]
    D --> E["Forecast with<br/>confidence band"]
    F["Observed cases<br/>days 0 to 32"] --> C

Simulation is the forward problem; fitting runs it in a loop to solve the inverse problem.

Step 1: The SIR model

The classic SIR model splits the population into three groups: Susceptible, Infectious and Recovered (or removed). Two parameters govern the flow between them:

  • beta is the transmission rate: how many people an infectious person infects per day while everyone is susceptible.
  • gamma is the recovery rate: 1/gamma is the average number of days someone stays infectious.

The equations, with population size N, are

TEXT
dS/dt = -beta * S * I / N
dI/dt =  beta * S * I / N - gamma * I
dR/dt =  gamma * I

The ratio R0 = beta / gamma is the basic reproduction number: the number of people one case infects in a fully susceptible population. If R0 > 1 the outbreak grows. Early on S is almost N, so infections grow like exp((beta - gamma) * t), a rate we will meet again in the uncertainty analysis.

Python Step 1: simulate an SIR epidemic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The infectious curve rises, peaks and falls, even though nothing was done to stop it: the epidemic burns out because there are fewer and fewer susceptible people left to infect. The peak occurs when S/N falls to about 1/R0, the herd-immunity threshold, which the printed numbers confirm (the small gap comes from reading the peak off daily samples).

Step 2: Generate the observations

Real case counts are noisy: reporting delays, testing differences and random chance all perturb them. We multiply the true infectious count by a log-normal factor with about 10% spread. Noise that is proportional to the value is typical of count data, and it tells us how to measure the fit: compare logarithms, so that a 10% error counts the same on day 5 (few cases) as on day 30 (thousands).

The crucial constraint is that we only see the first 32 days, which ends before the peak on day 36. Later days are held back to test the forecast.

Python Step 2: what the health office sees

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On the log axis the points lie close to a straight line, the signature of exponential growth. A straight line has two parameters, an intercept and a slope, and the slope tells us only the difference beta - gamma. Keep that in mind: it is the reason the individual rates will turn out to be hard to pin down.

Step 3: Fit the model with least_squares

least_squares needs a function that returns the vector of residuals for given parameters. Ours simulates the epidemic for the trial (beta, gamma) and returns the difference of logs between model and data on the observed days. We supply a starting guess and bounds so the optimizer cannot wander into nonsense such as negative rates.

Python Step 3: fit beta and gamma

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fit converges in a handful of model runs, and R0 comes out close to the truth. The residual standard deviation is close to the noise level we injected, a good sign that the model structure is right. But notice how the growth rate beta - gamma matches the true 0.25 much more closely than beta and gamma match theirs. That is not an accident.

Step 4: How uncertain are the parameters?

Near the solution, least_squares has a Jacobian J (how each residual changes with each parameter). From it, the standard linear-regression approximation gives the parameter covariance:

cov = inverse(J.T @ J) * sigmaยฒ, where sigmaยฒ is the residual variance (2 * cost / (n_points - n_params)).

The square roots of the diagonal are standard errors, and the off-diagonal term gives the correlation between the estimates. A correlation near +1 means the data constrain a combination of the parameters much better than each one alone.

To see this directly we also evaluate the fit quality along two different lines in parameter space: pairs with the same growth rate, versus pairs with the same beta but different gamma.

Python Step 4: standard errors, correlation and a flat valley

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two estimates are almost perfectly correlated, and the growth rate is determined to a fraction of a percent while beta and gamma individually carry errors of several percent. In the cost table, moving along the line beta - gamma = 0.25 costs very little, while stepping off it makes the fit much worse. The cost surface is a long, narrow valley: the data say "the growth rate is 0.25" loudly and "the split between beta and gamma" only faintly.

Warning: The covariance from the Jacobian is a linear approximation. It is fast and informative, but for strongly nonlinear problems or correlated parameters like these it can be too optimistic or too symmetric. Cross-check it with a resampling method such as the bootstrap in the next step.

Step 5: Forecast with a confidence band

To forecast honestly, we propagate the parameter uncertainty into the curve. A residual bootstrap does this without extra assumptions: build many synthetic datasets by adding randomly resampled residuals to the fitted model, refit each one, and simulate the whole epidemic for every refit. The spread of the resulting curves, say the 5th to 95th percentile, is the forecast band. (We use only 20 refits to stay within the browser's time limit; for a real report use hundreds.)

Then we compare with the 58 held-out days that the fit never saw.

Python Step 5: bootstrap band and held-out check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The forecast gets the timing of the peak right, with a band of plausible heights that includes the true value, and almost all of the held-out observations fall inside it. The naive "extend the straight line" prediction explodes to millions of cases in a town of 50,000, because it knows nothing about running out of susceptible people. That is the whole case for a mechanistic model: it carries the physics (here, depletion of susceptibles) into the future.

The band is wide around the peak. Fit again with data through day 45, which includes the peak, and the uncertainty shrinks dramatically; with only the early growth phase, an analyst who reports a single number for the peak height would be overstating what the data can say.

What you learned and where to go next

  • A mechanistic model plus solve_ivp plus least_squares is a general recipe: it works for chemical kinetics, drug concentrations, population models and many more.
  • Choose the residual to match the noise: log-differences for noise that scales with the signal.
  • Always examine parameter correlations. Strongly correlated parameters mean that the data identify a combination well and the individual values poorly.
  • A forecast without an uncertainty band is a guess; the bootstrap is a simple, assumption-light way to produce one.
  • Hold back some data to test the forecast. A model that fits the past can still fail on the future.

Ideas to extend it:

  • Add a fourth compartment E (exposed, infected but not yet infectious) to make an SEIR model.
  • Fit the initial number of cases i0 as a third parameter.
  • Let beta drop after a given day to model a lockdown, and fit the day and the size of the drop.
  • Replace the bootstrap with a profile of the cost along the valley and compare the intervals.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the SIR model, what does R0 = beta / gamma represent?

    R0 is the basic reproduction number. Values above 1 mean the outbreak grows; the infectious period is 1/gamma, a different quantity.
  2. 2. Why do the residuals in the fit compare logarithms of the model and the data?

    With multiplicative noise a 10% error is the same size in log space on every day, whereas raw differences would let the biggest counts dominate the fit.
  3. 3. The estimates of beta and gamma have a correlation of about 0.998. What does that tell you?

    Early exponential growth depends mainly on beta - gamma. Many (beta, gamma) pairs with the same difference fit almost equally well, so each one individually is uncertain.
  4. 4. Why does a simple straight-line extrapolation of the log-cases fail as a forecast?

    The exponential extrapolation has no mechanism for running out of people to infect. The SIR model has that depletion built in.

Summary

  • The SIR model, solve_ivp and least_squares combine into a simulate-compare-adjust loop for fitting differential-equation models to data.
  • Define residuals that match your noise (here log differences) and use bounds and sensible starting values.
  • Standard errors come from the Jacobian (inverse(J.T @ J) * sigmaยฒ); high correlation between parameters signals that only a combination, such as the growth rate beta - gamma, is well determined.
  • Propagate the uncertainty into the forecast, for example with a residual bootstrap, and check it against held-out data.
  • Report intervals, not single numbers, especially before the peak of an epidemic.

Next project: test whether a new web-page design really converts better in Project: Analyze an A/B Test.