Basic Prerequisites: 03 · Posterior Inference and Numerical Methods · Monte Carlo.


1. Intuition & Practical Objective

Conjugate priors give closed-form posteriors - but the interesting models (logistic regression, stochastic volatility, hierarchical models, anything with a nonlinear link) have no closed form, and the normalizing constant is an intractable integral in more than a few dimensions. Markov Chain Monte Carlo (MCMC) sidesteps the integral entirely: it constructs a Markov chain whose stationary distribution is exactly the posterior, and just runs it. Every sample is a draw from without ever computing .

The practical objective is to build and diagnose the two workhorse samplers: Metropolis–Hastings (propose, then accept or reject) and Gibbs (cycle through the full conditionals). Knowing when each is available - and how to tell that the chain has not converged - is the difference between a Bayesian analysis and a random-number generator.

The one-sentence essence. “Design a Markov chain with the posterior as its stationary distribution - MH does it with a proposal and an accept/reject step, Gibbs does it by sampling each full conditional - then prove convergence before you believe a single number.”


2. Mathematical Ground Truth & Derivations

The target and the idea (Tsay §12.1). We cannot sample directly, but if we build a chain with transition kernel satisfying the detailed-balance (reversibility) condition with target ,

then is stationary for the chain: if then . Running the chain long enough (after a burn-in ) yields approximately iid draws from , and the ergodic theorem replaces the intractable integral with a time average:

Metropolis–Hastings (Tsay §12.4.1–12.4.2). Propose from a proposal kernel , then accept with probability

  • Symmetric proposal ( symmetric, e.g. random walk , ): the ratio collapses to - this is Metropolis.
  • Asymmetric proposal: the Hastings correction restores detailed balance; e.g. independence MH or proposals on a constrained space.
  • The normalizing constant cancels, because appears as a ratio - this is the whole reason MCMC exists.

Gibbs sampling (Tsay §12.2). Partition . Iterate: for each block , draw

Each full conditional is itself a posterior at the block level and is often a conjugate family (Normal, Inverse-Gamma, Beta, Dirichlet, …). Gibbs is MH with the full conditional as the proposal, and the acceptance probability is exactly 1 - no rejections, no tuning. After burn-in , the mean is Tsay eq. (12.3):

Gibbs needs the full conditionals to be samplable; when one is not (e.g. an MA or GARCH coefficient), one replaces that step with a Metropolis or Griddy Gibbs update (evaluate the univariate conditional on a grid, invert the CDF numerically - Tsay §12.4.3). MCMC for latent variables is often written as data augmentation (Tanner–Wong), with EM as its deterministic special case.

Convergence diagnostics (BDA3 Ch 11). MCMC gives correlated draws, so the naive standard error is wrong. Two standard checks:

  • Gelman–Rubin: run chains from overdispersed starts; compare between-chain variance and within-chain variance :
  • Effective sample size from the autocorrelation : - the number of independent draws the chain is worth. Report , not .

Forward-filtering backward-sampling (FFBS; Tsay §12.8). For state-space / stochastic-volatility models the state path is drawn jointly (a Gibbs step over the whole latent path):

  1. Forward: run the Kalman filter to get filtered moments for all .
  2. Backward: sample , then for sample from the Markov property .

Carter–Kohn (1994) / Frühwirth-Schnatter (1994). This is the Bayesian counterpart of the Kalman smoother (see Kalman Filtering) and the engine of Bayesian stochastic-volatility estimation.


3. Computational Implementation - MH and Gibbs, from scratch

Three experiments: (a) random-walk Metropolis on a bimodal target, swept over three proposal scales, showing the mixing/acceptance trade-off and a biased estimate when mixing fails; (b) Gibbs on a bivariate normal, checking the recovered correlation; (c) Gibbs on the normal model with unknown mean and variance (semi-conjugate prior), sampling an Inverse-Gamma by transform. Stdlib only.

What the output proves.

  • PART A - the mixing trade-off is real. With a tiny step () the chain accepts 81% of proposals but cannot cross the gap between the two modes, so it reports (target ) and understates the variance ( vs ). With and it mixes across both modes and nails the truth (, ). High acceptance is not convergence - a chain that never moves is perfectly “accepted.”
  • PART B - Gibbs recovers the joint exactly: correlation vs target , variances , means . No tuning, no rejections.
  • PART C - the semi-conjugate normal model yields posterior (shrunk below toward the prior mean 0) and ; the sampler handles the unknown variance that would break a fixed- formula.

4. Failure Modes & First-Principles Breakdowns

  1. Pseudo-convergence / poor mixing (the failure). A chain can sit in one mode for the whole run and report a confident, wrong answer. The diagnostic is not acceptance rate - it is multi-chain (start chains far apart), trace plots, and comparing to a longer run. The bimodal example above is the textbook case.
  2. Forgetting burn-in (or using too little). Starting values are not draws from the posterior. Discard the burn-in and, better, check that estimates are stable across burn-in lengths. “Started at the MLE” is not an excuse - the chain needs to explore.
  3. Autocorrelation invalidating . Gibbs draws are serially correlated, so the effective sample size is far below . Report ESS and inflate intervals accordingly; naive intervals are too narrow.
  4. Reducible chains. If the proposal never reaches part of the support (or the chain is periodic), the stationary distribution is not the target, and the ergodic theorem does not apply. Verify detailed balance is satisfied, and use enough chains to detect it.
  5. Bad proposal scaling in high dimensions. A random-walk MH needs its proposal scale to decay as (optimal acceptance , Roberts–Gelman–Gilks 1997); a badly chosen gives near-zero or near-one acceptance in high dimensions. Gibbs or HMC replaces blind proposals with dimension-aware moves.
  6. Gibbs fails when full conditionals are not available or are nearly deterministic. Strongly correlated parameters (e.g. and in the normal model, or hierarchical centering) make Gibbs crawl; the funnel geometry of hierarchical models is the classic offender (§06). Reparameterize (non-centered) or use HMC.
  7. Confusing the posterior with the MLE. MCMC targets the posterior, not the maximum-likelihood point. If you want MLE, use an optimizer (see Numerical Optimization); MCMC gives the whole distribution and the MLE is a by-product.

5. References

  • Tsay, Ruey S., Analysis of Financial Time Series (3rd ed.), Ch 12
  • Robert, C. P. & Casella, G., Monte Carlo Statistical Methods (2nd ed.)
  • Gelman et al., Bayesian Data Analysis (3rd ed.)
  • Hastie, Tibshirani & Friedman, ESL
  • McElreath, Statistical Rethinking (2nd ed.)
  • Glasserman, Monte Carlo Methods in Financial Engineering

6. Connected Graph Bridges