Model fitting and inference for infectious disease dynamics
In the practical you used Metropolis-Hastings with a Gaussian proposal distribution to infer a single parameter, \(R_0\).
In this session we will
To infer several parameters at once we can use a multivariate Gaussian.
\(\mu = \begin{bmatrix} 3 & 2 \end{bmatrix}\), \(\Sigma = \begin{bmatrix} 25 & 0 \\ 0 & 9 \end{bmatrix}\)

\(\mu = \begin{bmatrix} 3 & 2 \end{bmatrix}\), \(\Sigma = \begin{bmatrix} 10 & 5 \\ 5 & 5 \end{bmatrix}\)

For accurate and efficient MCMC we tune the variance and the covariance of the proposal distribution.

Key characteristics:
If the variance is too small, the chain will be slow to reach the target distribution.


If the variance is too high, many proposed values will be rejected and the chain will stick in one place for many steps.


If the variance is just right, the chain efficiently explores the full shape of the target distribution.


Try several proposal distributions (pilot runs), aiming for an acceptance rate between 24% and 40%.
Running mean — the posterior mean recomputed as the chain grows.
It should settle and stay settled. Still drifting after thousands of iterations means the chain has not forgotten where it started, or has not finished exploring.
Autocorrelation — how correlated a sample is with the one \(k\) steps back.
It should decay towards zero within a few tens of lags. Still high at lag 100 means successive samples carry almost no new information, and your effective sample size is a small fraction of the iterations you ran.
We can start our MCMC chain anywhere.
It can take a while to reach and explore the target density \(f(\theta)\).
Throw away the early samples: the burn-in phase.
How much to discard?

In MCMC each sample depends on the one before: auto-correlation.
Reduce auto-correlation by thinning, retaining only every \(n\)-th sample.
The information content of a set of MCMC samples is given by the effective sample size (ESS).
How does each of these influence accuracy and efficiency?
A single chain can look perfectly healthy and still be stuck in the wrong place.
Run several chains from different starting points. If they agree, that is evidence they have found the same distribution. If they disagree, at least one of them is wrong.
Four chains is a reasonable default.
Compare the variance within each chain to the variance between chains.
\[\hat{R} \approx \sqrt{\frac{\text{between-chain} + \text{within-chain}}{\text{within-chain}}}\]
Rule of thumb: \(\hat{R} < 1.01\). Anything above 1.1 is a red flag.
Modern rank-normalised R̂ and ESS: Vehtari et al. (2021).
MCMC samples are correlated, so \(N\) samples are worth fewer than \(N\) independent draws.
ESS estimates how many independent draws your chain is equivalent to.
Bulk ESS describes the centre of the distribution; tail ESS describes the extremes, and matters if you care about credible interval endpoints.
ESS tells you how much information you have. MCSE turns that into the uncertainty on your estimate:
\[\text{MCSE} \approx \frac{\text{posterior SD}}{\sqrt{\text{ESS}}}\]
If the MCSE on your posterior mean is large relative to the effect you are reporting, you have not sampled enough — regardless of what the trace plot looks like.
Specific to HMC and NUTS. The sampler simulates a trajectory; a divergence means that simulation became numerically unstable and flew off.
This usually means the posterior has a region of sharp curvature — a funnel or a narrow ridge — where the step size that works elsewhere is far too large.
A handful of divergences may be tolerable. Many means the sampler is systematically avoiding part of the posterior, so your samples are biased, not merely noisy.
If the posterior has several separated modes, a chain can settle in one and never see the others.
Multiple chains from different starting points are the standard detection: if they land in different modes, R̂ will be large.
Fixes are harder than detection. Reparameterisation sometimes helps; otherwise you need samplers designed to cross between modes.
In the practical you will
MCMCChainsMCMC diagnostics