Model fitting and inference for infectious disease dynamics
Last time we saw that the posterior distribution of \(\theta\), given observed data, is
\[p(\theta \mid \text{data}) \propto p(\text{data} \mid \theta)\, p(\theta)\]
Our aim is to draw samples from this distribution.
\[m = \frac{1}{N_\text{Lon}} \sum_{i \in \text{Lon}} h_i \;\approx\; \frac{1}{N_S} \sum_{i \in S} h_i\]
We can do exactly the same thing to study a posterior distribution.
Explore a distribution by drawing from it. More samples, better picture:

Sampling blindly from the prior spends almost all its effort on parameter values that fit the data badly. We need something that concentrates where the posterior is.


Rejection sampling uses a proposal distribution \(q(\theta)\) which


Accepted points build up a sample from \(f(\theta)\).

In Markov chain Monte Carlo (MCMC) we do not need one proposal density \(q(\theta)\) that bounds \(f(\theta)\) everywhere.
Instead we build a chain of samples, where each proposed \(\theta^*\) depends on the previous one: the proposal density takes the form \(q(\theta^* \mid \theta)\).
A commonly used MCMC algorithm is Metropolis-Hastings.
Its acceptance rate is carefully derived to ensure unbiased samples.

If \(q(\theta^* \mid \theta)\) is symmetric, then
\[r = \min\left(1, \frac{f(\theta^*)}{f(\theta)}\right)\]
If \(q(\theta^* \mid \theta)\) is asymmetric, then
\[r = \min\left(1, \frac{f(\theta^*)\,q(\theta \mid \theta^*)}{f(\theta)\,q(\theta^* \mid \theta)}\right)\]

You will write Metropolis-Hastings once, to know what it does. After that, use a probabilistic programming language.
In Julia that is Turing.jl. You declare the model and it works out the rest:
| conditions the model on data; sample() then runs whichever algorithm you name. The same model definition works with MH, RAM or NUTS.
The proposal covariance has to match the shape of the posterior.
With 2 parameters that is 3 numbers to tune: two variances and a covariance.
With 10 parameters it is 55.
And you generally do not know the posterior scale before you have sampled it.
Adaptive MCMC alters the proposal distribution while the chain is running.
Start with a large symmetric variance and scan around to find a mode.
Then alter the shape of the proposal to match the covariance of the accepted values.
Eventually the proposal density should match the shape of the target density.


Metropolis-Hastings proposes a direction at random and hopes for the best.
But for a model built from operations we can differentiate — including an ODE model, since the solver propagates derivatives — we can compute the gradient of the log-posterior.
The gradient tells us which way is uphill.
Instead of a random step, simulate a puck rolling on the negative log-posterior surface.
Both the direction and the length of each move now come from the posterior itself, rather than from a proposal we had to guess.
NUTS, the No U-Turn Sampler, is HMC with two things automated:
The result usually just works, with no proposal tuning at all. It is the default you should reach for.
| Sampler | Best for | Limitations |
|---|---|---|
| MH | Simple models, discrete parameters, learning MCMC | Needs manual tuning; slow in many dimensions |
| RAM | Low-dimensional models where gradients are unavailable | Still a random walk |
| NUTS | Continuous parameters, differentiable models | No discrete parameters; needs gradients |
Rule of thumb: start with NUTS. Fall back to RAM or MH when your model has discrete parameters or a stochastic simulator that breaks differentiation — as the particle filter will later in the course.
In the practical you will
Markov chain Monte Carlo