5 Monte Carlo methods: Inference by sampling: Being a tour guide for unknown Bayesian posteriors
This chapter introduces Monte Carlo sampling as the practical answer to Bayesian posteriors that cannot be worked out in closed form. Earlier examples were easy because conjugate priors made the posterior analytically tractable, but realistic models often require priors that better reflect domain knowledge, even when that breaks conjugacy. In those cases, the posterior is still defined by Bayes’ theorem, but only up to a constant that is hard to compute. Instead of solving the posterior exactly, we generate samples from it and use those samples as a stand-in for the full distribution.
The chapter then explains why sampling works and why Markov chain Monte Carlo is so useful. Good samples should ideally be independent and identically distributed, but that is difficult to obtain directly from complex posteriors, especially in high-dimensional spaces where grid-based methods become infeasible. MCMC avoids this curse of dimensionality by constructing a chain that moves through the parameter space, spending more time in regions of higher posterior density. The Metropolis–Hastings algorithm is presented as a concrete example: propose a move from the current state, compare posterior densities through an accept/reject rule, and either move or stay put. This produces dependent samples, but if designed well, the chain still approximates the target posterior effectively.
The final part focuses on how to use MCMC responsibly in practice. It recommends discarding early samples during burn-in, running multiple chains from different starting points, checking convergence with statistics such as R-hat, and evaluating effective sample size to judge how informative the draws are. It also emphasizes inspecting trace plots for good mixing, where chains should move around the target distribution smoothly rather than get stuck, and performing posterior predictive checks to verify that the model reproduces observed data well. Using PyMC and ArviZ, the chapter shows how these ideas are implemented in software, making sampling-based Bayesian inference both practical and diagnostically sound.
Comparison between a posterior distribution available in closed form and Monte Carlo samples. The samples’ histogram matches the true PDF, and the various statistics from the samples give reasonable approximations to the corresponding true quantities.
We need exponentially more samples for a grid approximation as the dimensionality grows.
Differences between i.i.d. sampling (left) and MCMC (right). MCMC generates a chain of dependent samples (order indicated by arrows) that approximates the distribution.
Comparison between samples during and after the burn-in period. Burn-in samples might not represent the target distribution well, so we should remove these initial samples prior to any subsequent analysis.
Various statistics describing the posterior samples generated by PyMC. The R̂ statistic at the end indicates that our MCMC chains have converged, while ESS statistics tell us about the effective sample size.
Left: the distribution of the posterior samples of 𝑝 from the four chains. Right: The trace plot of these chains, which shows hairy caterpillar patterns indicating good mixing.
Posterior predictive check plot generated by ArviZ. The observed data lies inside the range simulated by the posterior predictive samples, indicating that our model passes the posterior predictive check.
Summary
- Monte Carlo (MC) methods approximate posteriors by generating samples instead of deriving them analytically.
- Conjugate priors allow exact posteriors, but many real-world problems require non-conjugate priors that make closed-form solutions impossible.
- MC sampling works even when we know only the unnormalized posterior, allowing us to compute statistics like means, credible intervals, and tail probabilities.
- Independent and identically distributed (i.i.d.) samples are the gold standard, but are unavailable when the posterior is not analytically tractable; Markov chain Monte Carlo (MCMC) generates dependent samples that still approximate the target.
- Grid approximation works only in low dimensions and wastes computation on low-density regions.
- MCMC uses a chain of dependent samples to efficiently explore the posterior’s high-density regions.
- Metropolis–Hastings builds this chain by proposing moves from a proposal distribution and accepting/rejecting based on posterior density ratios.
- Discarding burn-in MCMC samples removes bias from poor starting points.
- Running multiple MCMC chains avoids dependence on any single initialization.
- We can assess MCMC convergence using R̂ (close to 1) and sampling efficiency using ESS (at least 400 for bulk and tail).
- When checking mixing via trace plots, look for “hairy caterpillars,” not flat lines or jumps.
- Perform posterior predictive checks to ensure simulated data matches observed data.
- PyMC provides simple, readable syntax and integrates tightly with ArviZ for diagnostics and visualization.
Grokking Bayes ebook for free