Type at least 2 letters. Enter opens the first result, Esc closes.

    Probability and Statistics · Lesson 15 · Monte Carlo and MCMC

    Markov chains and MCMC: Metropolis–Hastings, Gibbs and Hamiltonian Monte Carlo

    Advanced 90 min 5 interactive figures 7 videos Colab notebook

    Before this lesson: lesson 04 (joint and conditional distributions), lesson 05 (Gaussian conditionals), lesson 10 (posteriors known only up to a constant) and lesson 14 (Monte Carlo estimates, importance sampling and its effective sample size). From linear algebra: left and right eigenvectors (linear algebra 05). Time: about 90 minutes, plus the notebook.


    Why this matters #

    • Bayesian inference. Most posteriors have no closed form (lesson 10). Stan, PyMC and NumPyro sample them with Hamiltonian Monte Carlo (the NUTS variant). This is the standard way to fit hierarchical models, to calibrate the parameters of a physical model with uncertainty (a climate model component, a seismic source), and to get uncertainty for the last layer of a neural network.
    • When importance sampling fails. Importance sampling (lesson 14) needs a proposal close to the target. In high dimension the weights collapse onto a few samples. MCMC makes small local moves instead, so it does not need a global proposal.
    • Generative models. Energy-based and score-based models sample with Langevin dynamics, a gradient step plus Gaussian noise. This is the one-step version of HMC (generative models 06, generative models 09).
    • Markov chains everywhere. PageRank is the stationary distribution of a random surfer. A policy in a Markov decision process turns it into a Markov chain (reinforcement learning 01). An n-gram language model is a Markov chain on words.

    1. Predict first #

    2. The picture #

    A Markov chain moves probability mass. Write the distribution of the state XtX_t as a row vector μt\mu_t: entry ii is P(Xt=i)P(X_t = i). The transition matrix PP has entries Pij=P(Xt+1=j∣Xt=i)P_{ij} = P(X_{t+1} = j \mid X_t = i), so every row sums to 1. One step of the chain moves the mass: μt+1=μtP\mu_{t+1} = \mu_t P. Mass flows out of every state and into every state. The distribution stops changing when, at every state, the inflow equals the outflow. That distribution is the stationary distribution π\pi. (In this lesson π\pi is a distribution; the number 3.14…3.14\ldots appears only inside formulas such as 2π\sqrt{2\pi}.)

    Interactive figure: loading…

    Press Step and watch the blue bars μt\mu_t move toward the orange marks π\pi. The arrows are the transition probabilities, and the size of each circle is μt\mu_t. The lower chart shows the distance to π\pi on a log scale; it falls like ∣λ2∣t|\lambda_2|^t (dashed orange line). Edit the matrix or try the presets. Flip never converges (period 2), and Cycle has a stationary distribution but fails the detailed-balance check: its mass circulates in one direction.

    MCMC reverses the usual question. Usually PP is given, and you look for π\pi. In MCMC, the target π\pi is given (up to a constant), and you design PP so that π\pi is stationary. Then you run the chain and estimate Eπ[f]\mathbb{E}_\pi[f] by the time average 1n∑tf(Xt)\frac1n \sum_t f(X_t). The simplest design is random-walk Metropolis: from the current point xx, propose x′=x+szx' = x + s z with z∼N(0,1)z \sim \mathcal N(0, 1). If the target is higher at x′x', move. If it is lower, move only with probability π~(x′)/π~(x)\tilde\pi(x')/\tilde\pi(x); otherwise stay at xx and record xx again. Uphill moves are always taken, downhill moves sometimes. The chain spends more time where the target is high, in exactly the right proportions (section 3 proves this).

    Interactive figure: loading…

    Press Step a few times, then Run 1000. Each proposal is aqua if it is accepted and magenta if it is rejected. The blue histogram of the chain approaches the orange target. Move the step size ss: a tiny step is accepted almost always but crawls (see the trace); a huge step is almost always rejected and the trace has long flat parts. With the bimodal target and a small step, the chain stays in one mode for a long time.

    Gibbs sampling moves along the axes. In two dimensions, Gibbs sampling draws x1x_1 from its conditional distribution given the current x2x_2, then x2x_2 given the new x1x_1. Each move is parallel to an axis, so the path is a staircase. If the target is a narrow diagonal ridge (strong correlation), the steps are short and the chain needs many sweeps to travel along the ridge.

    Interactive figure: loading…

    Press Run 200 sweeps and watch the blue staircase inside the orange ellipses of the target. Then move ρ\rho toward 0.99: the steps get short, and the lag-1 autocorrelation of x1x_1 approaches ρ2\rho^2. Turn on independent draws (violet) to see how far apart independent samples are. The readout converts the autocorrelation into sweeps per effectively independent sample.

    HMC slides a puck on a landscape. Turn the target into a surface: the height at xx is U(x)=−log⁡π~(x)U(x) = -\log\tilde\pi(x). High probability means low height. Place a frictionless puck at the current point, give it a random kick (a momentum pp), and let it slide for a while. It speeds up in the valleys and slows down on the slopes, so it follows the shape of the distribution and travels far in one iteration. The computer cannot solve the motion exactly, so it takes small leapfrog steps. A final accept/reject step removes the small error of these steps.

    Interactive figure: loading…

    Press Step to draw a momentum and follow the violet leapfrog path. The end point is aqua if it is accepted and magenta if it is rejected. The readout shows the energy error ΔH\Delta H. Increase the step size ε\varepsilon past about 0.45 on the correlated Gaussian: the path explodes (a divergence) and every proposal is rejected. Run 100 also runs random-walk Metropolis with the same number of gradient-or-density evaluations; compare the two effective sample sizes.

    Quick check

    Random-walk Metropolis is at x=0x = 0 for the target π~(x)=e−x2/2\tilde\pi(x) = e^{-x^2/2}. It proposes x′=2x' = 2. What happens?

    3. Definitions and main results #

    Finite Markov chains #

    A Markov chain on the states {1,…,K}\{1, \dots, K\} is a sequence X0,X1,…X_0, X_1, \dots with the Markov property:

    P(Xt+1=j∣Xt=i,Xt−1,…,X0)=Pij.P(X_{t+1} = j \mid X_t = i, X_{t-1}, \dots, X_0) = P_{ij}.

    The next state depends only on the current state. Every row of PP sums to 1. By the law of total probability, μt+1(j)=∑iμt(i)Pij\mu_{t+1}(j) = \sum_i \mu_t(i) P_{ij}, that is μt+1=μtP\mu_{t+1} = \mu_t P, and so μt=μ0Pt\mu_t = \mu_0 P^t.

    • A stationary distribution is a probability vector π\pi with πP=π\pi P = \pi. So π\pi is a left eigenvector of PP for the eigenvalue 1 (an eigenvector of P⊤P^\top). Every finite chain has at least one. The eigenvalue 1 exists because P1=1P\mathbf 1 = \mathbf 1 (rows sum to 1), and the Perron–Frobenius theorem gives a left eigenvector with nonnegative entries.
    • Irreducible: every state can reach every other state in some number of steps. Then π\pi is unique and every πi>0\pi_i > 0. The mean time to return to state ii is 1/πi1/\pi_i.
    • Aperiodic: the period of state ii is the greatest common divisor of the times t≥1t \ge 1 with (Pt)ii>0(P^t)_{ii} > 0. A chain is aperiodic if every period is 1. In an irreducible chain, one state with Pii>0P_{ii} > 0 is enough.

    Convergence theorem. If a finite chain is irreducible and aperiodic, then μ0Pt→π\mu_0 P^t \to \pi for every start μ0\mu_0. The error shrinks geometrically, roughly like ∣λ2∣t|\lambda_2|^t, where ∣λ2∣<1|\lambda_2| < 1 is the second-largest absolute value among the eigenvalues of PP.

    Ergodic theorem. If a finite chain is irreducible, then for every start and every function ff,

    1n∑t=1nf(Xt)  ⟶  Eπ[f]=∑iπif(i)with probability 1.\frac1n \sum_{t=1}^{n} f(X_t) \;\longrightarrow\; \mathbb{E}_\pi[f] = \sum_i \pi_i f(i) \quad \text{with probability 1}.

    This is the law of large numbers for Markov chains, and it is what MCMC uses: time averages along one path estimate expectations under π\pi. (It holds even for periodic chains, where μt\mu_t itself does not converge.)

    Two states, by hand. Let P=(1−aab1−b)P = \begin{pmatrix} 1 - a & a \\ b & 1 - b \end{pmatrix} with a,b∈(0,1)a, b \in (0, 1).

    1. Write mt=μt(1)m_t = \mu_t(1). Then mt+1=(1−a) mt+b (1−mt)=b+(1−a−b) mtm_{t+1} = (1 - a)\,m_t + b\,(1 - m_t) = b + (1 - a - b)\,m_t.
    2. A fixed point satisfies m=b+(1−a−b) mm = b + (1 - a - b)\,m, so m=b/(a+b)m = b/(a + b). Hence π=1a+b(b, a)\pi = \frac{1}{a + b}(b,\ a).
    3. Subtract the fixed-point equation from step 1: mt+1−π1=(1−a−b) (mt−π1)m_{t+1} - \pi_1 = (1 - a - b)\,(m_t - \pi_1).
    4. So the error is multiplied by λ2=1−a−b\lambda_2 = 1 - a - b at every step, from any start. The eigenvalues of PP are 1 and 1−a−b1 - a - b. ■\blacksquare

    Why the conditions matter.

    • The flip chain P=(0110)P = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} is irreducible with the unique π=(12,12)\pi = (\tfrac12, \tfrac12). But it has period 2: started in state 1, P(Xt=1)P(X_t = 1) alternates between 0 and 1 and never converges. Its eigenvalues are 11 and −1-1, so ∣λ2∣=1|\lambda_2| = 1.
    • A reducible chain (two groups of states that never communicate) has one stationary distribution for each closed group, and every mixture of them. The long-run behaviour then depends on the start.
    Quick check

    A chain has eigenvalues 11, 0.90.9 and −0.3-0.3. About how many steps does it need to reduce the distance to π\pi by a factor of 1000?

    Detailed balance #

    Definition. A distribution π\pi satisfies detailed balance with PP if

    πiPij=πjPjifor all i,j.\pi_i P_{ij} = \pi_j P_{ji} \quad \text{for all } i, j.

    In words: in the long run, the flow of probability from ii to jj equals the flow from jj to ii, for every pair. A chain with this property is called reversible: run backwards in time, it has the same law.

    Theorem. Detailed balance implies stationarity: πP=π\pi P = \pi.

    Proof. Fix a state jj.

    1. (πP)j=∑iπiPij(\pi P)_j = \sum_i \pi_i P_{ij} (definition of the product).
    2. By detailed balance, each term πiPij\pi_i P_{ij} equals πjPji\pi_j P_{ji}. So (πP)j=∑iπjPji=πj∑iPji(\pi P)_j = \sum_i \pi_j P_{ji} = \pi_j \sum_i P_{ji}.
    3. Row jj of PP sums to 1, so (πP)j=πj(\pi P)_j = \pi_j. ■\blacksquare

    Detailed balance is sufficient, not necessary. The Cycle preset in the first figure moves 1→2→3→11 \to 2 \to 3 \to 1 with probability 0.9 and stays with probability 0.1. Its stationary distribution is uniform, but all flow goes one way around the cycle, so π1P12=0.3≠0=π2P21\pi_1 P_{12} = 0.3 \ne 0 = \pi_2 P_{21}. MCMC constructions still check detailed balance, because it is local: one pair of states at a time, without a sum over all states.

    Metropolis–Hastings #

    The target is π(x)=π~(x)/Z\pi(x) = \tilde\pi(x)/Z. You can evaluate π~\tilde\pi, but you do not know ZZ. For a posterior, π~(θ)=p(D∣θ) p(θ)\tilde\pi(\theta) = p(\mathcal D \mid \theta)\,p(\theta) and Z=p(D)Z = p(\mathcal D), the evidence (lesson 10). A proposal q(x′∣x)q(x' \mid x) is any distribution you can sample from.

    Algorithm (one step). From the current state xx:

    1. propose x′∼q(x′∣x)x' \sim q(x' \mid x);
    2. compute the acceptance probability
    α(x,x′)=min⁡(1, π~(x′) q(x∣x′)π~(x) q(x′∣x));\alpha(x, x') = \min\left(1,\ \frac{\tilde\pi(x')\, q(x \mid x')}{\tilde\pi(x)\, q(x' \mid x)}\right);
    1. with probability α\alpha move to x′x'; otherwise stay at xx, and record xx again.

    In code, compare logs: accept if log⁡u<log⁡π~(x′)−log⁡π~(x)+log⁡q(x∣x′)−log⁡q(x′∣x)\log u < \log\tilde\pi(x') - \log\tilde\pi(x) + \log q(x \mid x') - \log q(x' \mid x) with u∼Uniform(0,1)u \sim \text{Uniform}(0, 1). This avoids overflow and underflow.

    Theorem (Metropolis–Hastings). The Metropolis–Hastings kernel satisfies detailed balance with π\pi. So π\pi is a stationary distribution of the chain.

    Proof. We write the proof with densities; for a finite state space replace integrals by sums.

    1. For x′≠xx' \ne x, the chain moves from xx to x′x' with density K(x,x′)=q(x′∣x) α(x,x′)K(x, x') = q(x' \mid x)\,\alpha(x, x'). On a rejection it stays at xx; this adds a point mass at x′=xx' = x.
    2. Multiply by π(x)\pi(x) and move the factor inside the minimum:
    π(x) q(x′∣x) α(x,x′)=min⁡(π(x) q(x′∣x), π(x′) q(x∣x′)).\pi(x)\,q(x' \mid x)\,\alpha(x, x') = \min\big(\pi(x)\,q(x' \mid x),\ \pi(x')\,q(x \mid x')\big).

    The right side does not change if we swap xx and x′x'. So π(x)K(x,x′)=π(x′)K(x′,x)\pi(x)K(x, x') = \pi(x')K(x', x) for x′≠xx' \ne x. 3. The point mass at x′=xx' = x satisfies detailed balance trivially: both sides are the same term. 4. Integrate detailed balance over xx: ∫π(x)K(x,x′) dx=π(x′)∫K(x′,x) dx=π(x′)\int \pi(x)K(x, x')\,dx = \pi(x') \int K(x', x)\,dx = \pi(x'), because the kernel from x′x', including its rejection mass, has total probability 1. This is stationarity. ■\blacksquare

    Only ratios appear. π~(x′)/π~(x)=π(x′)/π(x)\tilde\pi(x')/\tilde\pi(x) = \pi(x')/\pi(x): the constant ZZ cancels. This is why MCMC works for posteriors. Stationarity alone does not give convergence: the chain must also be irreducible and aperiodic. With a Gaussian proposal on Rd\mathbb R^d and a positive target, both hold (rejections give aperiodicity).

    • Random-walk Metropolis. x′=x+szx' = x + s z with z∼N(0,I)z \sim \mathcal N(0, I). This proposal is symmetric, q(x′∣x)=q(x∣x′)q(x' \mid x) = q(x \mid x'), so α=min⁡(1,π~(x′)/π~(x))\alpha = \min(1, \tilde\pi(x')/\tilde\pi(x)). A small step ss: almost every move is accepted, but the moves are tiny. A large ss: almost every move is rejected. For a 1-D N(0,1)\mathcal N(0, 1) target the acceptance rate is 2πarctan⁡(2/s)\frac{2}{\pi}\arctan(2/s): 0.844, 0.442 and 0.205 for s=0.5,2.4,6s = 0.5, 2.4, 6. In dd dimensions the best step scales like 2.38/d2.38/\sqrt d, with an acceptance rate near 0.234 (Roberts, Gelman and Gilks, 1997), and the chain needs about O(d)O(d) steps per independent sample.
    • The Hastings correction q(x∣x′)/q(x′∣x)q(x \mid x')/q(x' \mid x) is needed when the proposal is not symmetric. Example: for a positive parameter, the multiplicative walk x′=x eszx' = x\,e^{s z} has the correction x′/xx'/x (check yourself, question 4).
    • Independence sampler. The proposal q(x′)q(x') does not depend on xx. Then α=min⁡(1,w(x′)/w(x))\alpha = \min\big(1, w(x')/w(x)\big) with the importance weight w=π~/qw = \tilde\pi/q of lesson 14. As in importance sampling, qq needs heavier tails than π\pi.
    Quick check

    You tune random-walk Metropolis on a 1-D target and reach an acceptance rate of 99%. What does this most likely mean?

    Gibbs sampling #

    Let x=(x1,…,xd)x = (x_1, \dots, x_d). The full conditional of coordinate ii is π(xi∣x−i)\pi(x_i \mid x_{-i}), where x−ix_{-i} is the vector of all other coordinates. Gibbs sampling updates one coordinate at a time with a draw from its full conditional. A sweep updates every coordinate once.

    Theorem. One Gibbs update is a Metropolis–Hastings step with acceptance probability 1.

    Proof. The proposal changes only coordinate ii: q(x′∣x)=π(xi′∣x−i)q(x' \mid x) = \pi(x'_i \mid x_{-i}), with x−i′=x−ix'_{-i} = x_{-i}.

    1. Write π(x)=π(x−i) π(xi∣x−i)\pi(x) = \pi(x_{-i})\,\pi(x_i \mid x_{-i}). Then π(x) q(x′∣x)=π(x−i) π(xi∣x−i) π(xi′∣x−i)\pi(x)\,q(x' \mid x) = \pi(x_{-i})\,\pi(x_i \mid x_{-i})\,\pi(x'_i \mid x_{-i}).
    2. In the same way, π(x′) q(x∣x′)=π(x−i) π(xi′∣x−i) π(xi∣x−i)\pi(x')\,q(x \mid x') = \pi(x_{-i})\,\pi(x'_i \mid x_{-i})\,\pi(x_i \mid x_{-i}), using x−i′=x−ix'_{-i} = x_{-i}.
    3. The two products are equal, so the MH ratio is 1 and α=1\alpha = 1. ■\blacksquare

    Each update leaves π\pi invariant, so a full sweep (one update after another) does too. A sweep in fixed order is in general not reversible, but it is still a valid sampler. Gibbs sampling is popular when the full conditionals have closed forms, as in conjugate hierarchical models, mixture models and topic models.

    Why Gibbs is slow for correlated targets. Take (X1,X2)(X_1, X_2) standard Gaussian with correlation ρ\rho. By lesson 05, X1∣X2=x2∼N(ρx2, 1−ρ2)X_1 \mid X_2 = x_2 \sim \mathcal N(\rho x_2,\ 1 - \rho^2), and the same with the roles swapped.

    1. One sweep: x1(t+1)=ρ x2(t)+1−ρ2 e1x_1^{(t+1)} = \rho\,x_2^{(t)} + \sqrt{1 - \rho^2}\,e_1, then x2(t+1)=ρ x1(t+1)+1−ρ2 e2x_2^{(t+1)} = \rho\,x_1^{(t+1)} + \sqrt{1 - \rho^2}\,e_2, with independent standard normal noise e1,e2e_1, e_2.
    2. Substitute the second line of the previous sweep into the first: x1(t+1)=ρ2x1(t)+noisex_1^{(t+1)} = \rho^2 x_1^{(t)} + \text{noise}. So the chain of x1x_1 is an AR(1) process with coefficient φ=ρ2\varphi = \rho^2.
    3. Its autocorrelation at lag tt is φt=ρ2t\varphi^t = \rho^{2t}. The next section turns this into an effective sample size: n (1−ρ2)/(1+ρ2)n\,(1 - \rho^2)/(1 + \rho^2).

    The picture: Gibbs moves parallel to the axes, and in a narrow diagonal ridge each move has length of order 1−ρ2\sqrt{1 - \rho^2}. The fixes are to update correlated coordinates together (blocking) or to change the coordinates (reparametrisation), for example rotate them so that they are uncorrelated.

    Hamiltonian Monte Carlo #

    Momentum. Add an auxiliary variable p∈Rdp \in \mathbb R^d, the momentum, with p∼N(0,I)p \sim \mathcal N(0, I), independent of xx. The joint density is ∝e−H(x,p)\propto e^{-H(x, p)} with the Hamiltonian (total energy)

    H(x,p)=U(x)+12 ∥p∥2,U(x)=−log⁡π~(x).H(x, p) = U(x) + \tfrac12\,\|p\|^2, \qquad U(x) = -\log\tilde\pi(x).

    UU is the potential energy and 12∥p∥2\tfrac12\|p\|^2 the kinetic energy. The xx-marginal of e−He^{-H} is π\pi: sample (x,p)(x, p) and drop pp. (Implementations often use a mass matrix MM: p∼N(0,M)p \sim \mathcal N(0, M) and kinetic energy 12p⊤M−1p\tfrac12 p^\top M^{-1} p. Here M=IM = I.)

    Exact dynamics. Hamilton's equations are x˙=p\dot x = p and p˙=−∇U(x)\dot p = -\nabla U(x). Their flow has three properties:

    1. it conserves HH (the derivative of HH along the flow is ∇U⋅p+p⋅(−∇U)=0\nabla U \cdot p + p \cdot (-\nabla U) = 0);
    2. it preserves volume in (x,p)(x, p) space (Liouville's theorem);
    3. it is reversible: flip the momentum at the end, run for the same time, and you return to the start.

    Together these mean the flow leaves e−He^{-H} invariant. Because HH is conserved, one trajectory stays on one energy level. A fresh momentum at every iteration (a Gibbs step on pp) moves the chain between levels.

    Leapfrog. The exact flow is not available, so we discretise it. With step size ε\varepsilon, one leapfrog step is

    p←p−ε2 ∇U(x),x←x+ε p,p←p−ε2 ∇U(x).p \leftarrow p - \tfrac{\varepsilon}{2}\,\nabla U(x), \qquad x \leftarrow x + \varepsilon\,p, \qquad p \leftarrow p - \tfrac{\varepsilon}{2}\,\nabla U(x).

    Consecutive half steps in pp merge, so LL steps cost L+1L + 1 gradient evaluations.

    • Volume preserving. Each of the three updates is a shear: it changes one block of variables by an amount that depends only on the other block. Its Jacobian matrix is block triangular with identity blocks on the diagonal, so its determinant is 1. The determinant of the composition is the product, also 1.
    • Reversible. Start from the end point with the momentum flipped, (x∗,−p∗)(x^*, -p^*), and run LL steps: you return to (x,−p)(x, -p). The three updates undo each other in reverse order.
    • Accurate. The energy error over a fixed time is O(ε2)O(\varepsilon^2), and it does not grow over long trajectories when ε\varepsilon is below a stability limit. Halving ε\varepsilon cuts the error by about 4. Above the limit (for a Gaussian, ε>2σmin⁡\varepsilon > 2\sigma_{\min}, twice the smallest standard deviation) the path explodes.

    HMC (one iteration). Draw p∼N(0,I)p \sim \mathcal N(0, I). Run LL leapfrog steps from (x,p)(x, p) to (x∗,p∗)(x^*, p^*). Accept x∗x^* with probability

    α=min⁡(1, exp⁡(H(x,p)−H(x∗,p∗))),\alpha = \min\Big(1,\ \exp\big(H(x, p) - H(x^*, p^*)\big)\Big),

    otherwise keep xx. This is a Metropolis–Hastings step on (x,p)(x, p) with the proposal (x∗,−p∗)(x^*, -p^*). The momentum flip makes the proposal map its own inverse, so it is symmetric. Volume preservation means no Jacobian factor appears. Without the correction, the O(ε2)O(\varepsilon^2) energy error would bias the samples.

    Why HMC scales. Random-walk Metropolis needs about O(d)O(d) steps per independent sample in dd dimensions. HMC uses the gradient to make long, directed moves. For i.i.d. product targets the cost grows like O(d1/4)O(d^{1/4}) leapfrog steps per sample, with the best acceptance rate near 0.65 (Beskos et al., 2013). The tuning parameters are ε\varepsilon, LL and MM. NUTS (the No-U-Turn Sampler, the default in Stan) chooses the trajectory length automatically, and tunes ε\varepsilon and MM during warm-up. A divergence is a trajectory whose energy error explodes. It warns that the chain cannot explore a region of high curvature, and the results may be biased.

    Langevin dynamics, MALA and Brownian motion

    HMC with a single leapfrog step (L=1L = 1) and a fresh momentum gives the update x′=x+ε22∇log⁡π~(x)+εzx' = x + \tfrac{\varepsilon^2}{2}\nabla\log\tilde\pi(x) + \varepsilon z with z∼N(0,I)z \sim \mathcal N(0, I). Write η=ε2\eta = \varepsilon^2: x′=x+η2∇log⁡π(x)+η zx' = x + \tfrac{\eta}{2}\nabla\log\pi(x) + \sqrt{\eta}\,z. This is the Euler step of the Langevin diffusion dXt=12∇log⁡π(Xt) dt+dBtdX_t = \tfrac12\nabla\log\pi(X_t)\,dt + dB_t, where BtB_t is Brownian motion: the limit of a random walk whose steps scale like Δt\sqrt{\Delta t} (lesson 06, the deeper block on random walks). The Langevin diffusion has π\pi as its stationary distribution. With a Metropolis–Hastings correction the update is MALA (Metropolis-adjusted Langevin algorithm). Without the correction, and with mini-batch gradients, it is SGLD (stochastic gradient Langevin dynamics). Score-based generative models run the same update with a learned score ∇log⁡p\nabla\log p in place of the true one. SDEs and their stationary distributions are in ML for science 12; score-based diffusion samplers in generative models 09.

    Diagnostics: effective sample size and split-R^\hat R #

    MCMC draws are correlated, and the chain needs time to forget its start. Two numbers tell you whether to trust the output.

    Effective sample size (ESS). Let the chain be stationary with variance σ2\sigma^2 and autocorrelation ρt\rho_t at lag tt (the correlation between xsx_s and xs+tx_{s+t}). Under mild conditions the Markov chain central limit theorem gives

    Var⁡(xˉn)≈σ2 τn,τ=1+2∑t≥1ρt.\operatorname{Var}(\bar x_n) \approx \frac{\sigma^2\,\tau}{n}, \qquad \tau = 1 + 2\sum_{t \ge 1} \rho_t .

    τ\tau is the integrated autocorrelation time. The ESS is n/τn/\tau: your nn correlated draws give the same error as n/τn/\tau independent ones. The Monte Carlo standard error of the mean is σ/ESS\sigma/\sqrt{\text{ESS}}.

    For an AR(1) chain with coefficient φ\varphi: ρt=φt\rho_t = \varphi^t, so τ=1+2φ/(1−φ)=(1+φ)/(1−φ)\tau = 1 + 2\varphi/(1 - \varphi) = (1 + \varphi)/(1 - \varphi) and ESS/n=(1−φ)/(1+φ)\text{ESS}/n = (1 - \varphi)/(1 + \varphi). For φ=0.9\varphi = 0.9, τ=19\tau = 19. For φ=−0.5\varphi = -0.5, ESS=3n\text{ESS} = 3n: negatively correlated draws can beat independent ones.

    Estimating τ\tau. The sample autocorrelations ρ^t\hat\rho_t at large lags are mostly noise, so the full sum is useless. Geyer's initial positive sequence adds pairs Γk=ρ^2k+ρ^2k+1\Gamma_k = \hat\rho_{2k} + \hat\rho_{2k+1} for k=0,1,…k = 0, 1, \dots while they are positive, and stops at the first Γk≤0\Gamma_k \le 0. Then τ^=−1+2∑kΓk\hat\tau = -1 + 2\sum_k \Gamma_k. (The −1-1 removes the double-counted ρ^0=1\hat\rho_0 = 1.)

    Split-R^\hat R. Run mm chains of length nn from dispersed starts. Split each chain into halves, to get 2m2m half-chains of length h=n/2h = n/2.

    1. WW = the mean of the 2m2m within-chain sample variances.
    2. B=h×B = h \times the sample variance of the 2m2m half-chain means.
    3. var⁡^+=h−1h W+Bh\widehat{\operatorname{var}}^+ = \frac{h - 1}{h}\,W + \frac{B}{h} and R^=var⁡^+/W\hat R = \sqrt{\widehat{\operatorname{var}}^+ / W}.

    If all chains explore the same distribution, B/hB/h is small and R^≈1\hat R \approx 1. Chains stuck in different regions make BB large, so R^>1\hat R > 1. The split also catches a chain that drifts: its two halves disagree, even when all chains drift the same way. Vehtari et al. (2021) recommend at least 4 chains, R^<1.01\hat R < 1.01, and an ESS above 400 (in total over the chains) before you trust the estimates.

    Interactive figure: loading…

    Choose a preset. Four random-walk Metropolis chains (four colours) start far apart. The first half of each chain is warm-up (shaded) and is dropped. Well mixed: the traces overlap like noise, R^≈1\hat R \approx 1, and the autocorrelations die fast. Too small step: the traces still remember their starts, and the autocorrelation decays slowly. Stuck in one mode: each chain looks fine alone, but the chains disagree, so R^\hat R is large. All in the same mode: the diagnostics pass, but half of the target is missing. Then make the chains longer: which problems go away?

    What diagnostics cannot see. If all chains are stuck in the same mode of a multimodal target, they agree and R^≈1\hat R \approx 1. A region that no chain reaches (for example the narrow neck of a funnel-shaped posterior) does not show up in the ESS. So: run several chains from dispersed starts, check divergences for HMC, and compare with what you know about the model.

    What to report. The sampler and its settings, the number of chains, the warm-up length and the number of kept draws, R^\hat R and ESS (bulk and tail) for every reported quantity, the number of divergences, and the Monte Carlo standard error of each estimate.

    Rank-normalised R^\hat R, bulk and tail ESS

    Classic R^\hat R uses means and variances, so it fails for heavy-tailed targets (infinite variance) and misses chains that differ only in their spread. Vehtari et al. (2021) first replace all draws by their normal scores: pool the draws of all chains, rank them, and map rank rr to Φ−1((r−3/8)/(S+1/4))\Phi^{-1}\big((r - 3/8)/(S + 1/4)\big), with SS the total number of draws. Split-R^\hat R on these scores is the bulk-R^\hat R. The same on the folded draws ∣x−median∣|x - \text{median}| checks the spread (tails). The reported R^\hat R is the maximum of the two. Bulk ESS is the ESS of the normal scores; tail ESS is the smaller ESS of the indicators 1{x≤q0.05}\mathbb 1\{x \le q_{0.05}\} and 1{x≤q0.95}\mathbb 1\{x \le q_{0.95}\}. ArviZ, Stan and PyMC report these by default.

    Quick check

    Four chains give split-R^=1.3\hat R = 1.3 for a parameter. What is the right reaction?

    Answers to section 1.

    1. Two thirds. With a=0.2a = 0.2 (dry to wet) and b=0.4b = 0.4 (wet to dry), π=(b,a)/(a+b)=(2/3,1/3)\pi = (b, a)/(a + b) = (2/3, 1/3). The chain is irreducible and aperiodic, so day 0 does not matter in the long run. Its influence decays like 0.4t0.4^t.
    2. About 100. The chain of x1x_1 is AR(1) with φ=ρ2=0.9801\varphi = \rho^2 = 0.9801. So ESS/n=(1−0.9801)/(1+0.9801)=0.0199/1.9801≈1/99.5\text{ESS}/n = (1 - 0.9801)/(1 + 0.9801) = 0.0199/1.9801 \approx 1/99.5.

    4. Worked example #

    (a) The weather chain. P=(0.80.20.40.6)P = \begin{pmatrix} 0.8 & 0.2 \\ 0.4 & 0.6 \end{pmatrix}, state 1 = dry, state 2 = wet.

    • Stationary distribution: π=(2/3, 1/3)\pi = (2/3,\ 1/3). Check the first entry of πP\pi P: 23⋅0.8+13⋅0.4=1.6+0.43=23\tfrac23 \cdot 0.8 + \tfrac13 \cdot 0.4 = \tfrac{1.6 + 0.4}{3} = \tfrac23. ✓
    • Detailed balance: π1P12=23⋅0.2=0.43\pi_1 P_{12} = \tfrac23 \cdot 0.2 = \tfrac{0.4}{3} and π2P21=13⋅0.4=0.43\pi_2 P_{21} = \tfrac13 \cdot 0.4 = \tfrac{0.4}{3}. ✓ (Every two-state chain with π>0\pi > 0 is reversible: there is only one pair, and stationarity forces the two flows to be equal.)
    • Speed: λ2=1−0.2−0.4=0.4\lambda_2 = 1 - 0.2 - 0.4 = 0.4. Starting dry, P(dry on day t)=23+13⋅0.4tP(\text{dry on day } t) = \tfrac23 + \tfrac13 \cdot 0.4^t. This gives 0.80.8, 0.720.72 and 0.67010.6701 for t=1,2,5t = 1, 2, 5.
    • Mean return times 1/πi1/\pi_i: 1.5 days back to a dry day, 3 days back to a wet day.

    (b) The Metropolis kernel as a matrix. Target π~=(1,2,4,2,1)\tilde\pi = (1, 2, 4, 2, 1) on states 1,…,51, \dots, 5, so π=(1,2,4,2,1)/10\pi = (1, 2, 4, 2, 1)/10. Proposal: left or right neighbour, probability 12\tfrac12 each; a move off the ends is rejected.

    • From state 3 (π~=4\tilde\pi = 4): to state 2, α=min⁡(1,2/4)=12\alpha = \min(1, 2/4) = \tfrac12, so P32=12⋅12=0.25P_{32} = \tfrac12 \cdot \tfrac12 = 0.25. In the same way P34=0.25P_{34} = 0.25, and P33=1−0.5=0.5P_{33} = 1 - 0.5 = 0.5.
    • From state 2 (π~=2\tilde\pi = 2): to state 3, α=min⁡(1,4/2)=1\alpha = \min(1, 4/2) = 1, so P23=0.5P_{23} = 0.5; to state 1, α=12\alpha = \tfrac12, so P21=0.25P_{21} = 0.25; and P22=0.25P_{22} = 0.25.
    • Detailed balance for the pair (2, 3): π2P23=0.2⋅0.5=0.1\pi_2 P_{23} = 0.2 \cdot 0.5 = 0.1 and π3P32=0.4⋅0.25=0.1\pi_3 P_{32} = 0.4 \cdot 0.25 = 0.1. ✓
    • The eigenvalues of this 5×55 \times 5 kernel are 1,0.75,0.5,0.25,01, 0.75, 0.5, 0.25, 0 (section 5 checks them), so the error shrinks like 0.75t0.75^t.

    (c) Three steps of random-walk Metropolis by hand. Target π~(x)=e−x2/2\tilde\pi(x) = e^{-x^2/2}, start x0=0x_0 = 0, step s=1s = 1, noise z=(1.2,−0.4,0.9)z = (1.2, -0.4, 0.9) and uniforms u=(0.3,0.9,0.5)u = (0.3, 0.9, 0.5). Accept if u<αu < \alpha.

    step xx x′=x+zx' = x + z log⁡π~(x′)π~(x)=−x′2−x22\log\frac{\tilde\pi(x')}{\tilde\pi(x)} = -\frac{x'^2 - x^2}{2} α\alpha uu result
    1 0 1.2 −0.72-0.72 0.487 0.3 accept: 1.2
    2 1.2 0.8 +0.40+0.40 1 0.9 accept: 0.8
    3 0.8 1.7 −1.125-1.125 0.325 0.5 reject: 0.8

    The chain is (1.2, 0.8, 0.8)(1.2,\ 0.8,\ 0.8). The rejected step records the current state again. Dropping repeated states would bias the chain toward regions where proposals are often accepted.

    (d) One HMC iteration by hand. Target N(0,1)\mathcal N(0, 1), so U(x)=x2/2U(x) = x^2/2 and ∇U(x)=x\nabla U(x) = x. Start at x=0x = 0 with the momentum draw p=1p = 1, step ε=0.5\varepsilon = 0.5, one leapfrog step (L=1L = 1).

    1. Energy at the start: H=12⋅02+12⋅12=0.5H = \tfrac12 \cdot 0^2 + \tfrac12 \cdot 1^2 = 0.5.
    2. Half step in momentum: p=1−0.25⋅0=1p = 1 - 0.25 \cdot 0 = 1.
    3. Full step in position: x=0+0.5⋅1=0.5x = 0 + 0.5 \cdot 1 = 0.5.
    4. Half step in momentum: p=1−0.25⋅0.5=0.875p = 1 - 0.25 \cdot 0.5 = 0.875.
    5. Energy at the end: H=12⋅0.25+12⋅0.765625=0.5078H = \tfrac12 \cdot 0.25 + \tfrac12 \cdot 0.765625 = 0.5078. The energy error is ΔH=0.0078\Delta H = 0.0078.
    6. Accept with probability min⁡(1,e−0.0078)=0.992\min(1, e^{-0.0078}) = 0.992.

    The exact flow would conserve HH and give α=1\alpha = 1. The leapfrog error is small, so almost every proposal is accepted, and the accept/reject step removes the remaining bias.

    (e) Split-R^\hat R catches a drift. One chain with draws (0,1,2,3)(0, 1, 2, 3). The halves are (0,1)(0, 1) and (2,3)(2, 3), so h=2h = 2.

    1. Within-half variances: 0.50.5 and 0.50.5, so W=0.5W = 0.5.
    2. Half means 0.50.5 and 2.52.5; their sample variance is 22, so B=2⋅2=4B = 2 \cdot 2 = 4.
    3. var⁡^+=12⋅0.5+4/2=2.25\widehat{\operatorname{var}}^+ = \tfrac12 \cdot 0.5 + 4/2 = 2.25 and R^=2.25/0.5=4.5≈2.12\hat R = \sqrt{2.25/0.5} = \sqrt{4.5} \approx 2.12.

    Without the split there is only one chain, no between-chain variance and no warning.

    Quick check

    In example (c), suppose the third uniform were u=0.2u = 0.2 instead of 0.50.5. What is the chain after three steps?

    5. In code #

    NumPy checks the hand computations. The notebook for this lesson has these cells and the exercises.

    import numpy as np
    np.set_printoptions(precision=4, suppress=True)
    
    # (a) weather chain: stationary distribution by a linear solve, and convergence
    P = np.array([[0.8, 0.2], [0.4, 0.6]])
    A = np.vstack([(P.T - np.eye(2))[:-1], np.ones(2)])   # pi (P - I) = 0, with one equation replaced by sum = 1
    pi = np.linalg.solve(A, [0.0, 1.0])
    print(pi, np.linalg.matrix_power(P, 5)[0])             # [0.6667 0.3333] [0.6701 0.3299]
    print(np.sort(np.linalg.eigvals(P).real))              # [0.4 1. ]
    
    # (b) the Metropolis kernel on 5 states is a matrix: check detailed balance
    pt = np.array([1, 2, 4, 2, 1.0])                       # unnormalised target
    K = len(pt)
    Pmh = np.zeros((K, K))
    for i in range(K):
        for j in (i - 1, i + 1):
            if 0 <= j < K:                                 # a move off the ends is rejected
                Pmh[i, j] = 0.5 * min(1.0, pt[j] / pt[i])
        Pmh[i, i] = 1.0 - Pmh[i].sum()                     # rejection mass stays at i
    pi5 = pt / pt.sum()
    flows = pi5[:, None] * Pmh                             # flows[i, j] = pi_i P_ij
    print(np.allclose(flows, flows.T), np.allclose(pi5 @ Pmh, pi5))   # True True
    print(np.sort(np.abs(np.linalg.eigvals(Pmh)))[::-1])   # [1.   0.75 0.5  0.25 0.  ]
    
    # (c) three random-walk Metropolis steps with fixed noise
    log_t = lambda x: -0.5 * x**2
    x, chain = 0.0, []
    for z, u in zip([1.2, -0.4, 0.9], [0.3, 0.9, 0.5]):
        xp = x + z
        if np.log(u) < log_t(xp) - log_t(x):               # log acceptance test
            x = xp
        chain.append(x)
    print(np.round(chain, 2))                              # [1.2 0.8 0.8]
    
    # (d) one leapfrog step for U(x) = x^2/2, then the MH correction
    x, p, eps = 0.0, 1.0, 0.5
    H = lambda x, p: 0.5 * x**2 + 0.5 * p**2
    H0 = H(x, p)
    p = p - eps / 2 * x; x = x + eps * p; p = p - eps / 2 * x
    print(x, p, H(x, p) - H0, np.exp(H0 - H(x, p)))        # 0.5 0.875 0.0078125 0.9922...
    
    # (e) split-R-hat of the drifting chain (0, 1, 2, 3)
    halves = np.array([[0.0, 1.0], [2.0, 3.0]])
    h = 2
    W = halves.var(axis=1, ddof=1).mean()
    B = h * halves.mean(axis=1).var(ddof=1)
    print(W, B, np.sqrt(((h - 1) / h * W + B / h) / W))  # 0.5 4.0 2.1213...
    

    A simulation checks the acceptance-rate formula of random-walk Metropolis and the AR(1) behaviour of Gibbs (ρ=0.99\rho = 0.99):

    import numpy as np
    
    def rwm(log_p, x0, n, s, rng):
        x, lp, out, acc = x0, log_p(x0), np.empty(n), 0
        for t in range(n):
            xp = x + s * rng.standard_normal()
            lpp = log_p(xp)
            if np.log(rng.random()) < lpp - lp:
                x, lp, acc = xp, lpp, acc + 1
            out[t] = x                                     # a rejection records x again
        return out, acc / n
    
    rng = np.random.default_rng(0)
    for s in (0.5, 2.4, 6.0):
        chain, acc = rwm(lambda x: -0.5 * x**2, 0.0, 50_000, s, rng)
        print(s, round(acc, 3), round(2 / np.pi * np.arctan(2 / s), 3))
    # about: 0.5 0.842 0.844 | 2.4 0.445 0.442 | 6.0 0.203 0.205  (Monte Carlo error about ±0.004)
    
    rho, n = 0.99, 200_000
    x1, x2, xs = 0.0, 0.0, np.empty(n)
    sd = np.sqrt(1 - rho**2)
    z = rng.standard_normal((n, 2))
    for t in range(n):
        x1 = rho * x2 + sd * z[t, 0]                       # x1 | x2 ~ N(rho x2, 1 - rho^2)
        x2 = rho * x1 + sd * z[t, 1]                       # x2 | x1, with the new x1
        xs[t] = x1
    print(round(np.corrcoef(xs[:-1], xs[1:])[0, 1], 3), rho**2)   # about 0.979, and 0.9801
    

    6. Where it shows up #

    • Probabilistic programming. Stan, PyMC and NumPyro run NUTS by default: 4 chains, a warm-up that tunes ε\varepsilon and the mass matrix, then R^\hat R, bulk and tail ESS and the divergence count in the summary table. Typical uses: hierarchical models for A/B tests over many groups, epidemiological models, calibration of physical model parameters (climate model components, a seismic source inversion), pharmacokinetics.
    • Gibbs sampling with conjugate conditionals. Latent Dirichlet allocation (topic models), Bayesian mixture models, Bayesian linear regression with a conjugate prior, and data augmentation for probit regression all have closed-form full conditionals.
    • Langevin dynamics in deep learning. Energy-based models and score-based diffusion models generate samples with (annealed) Langevin steps; SGLD gives approximate posterior samples of neural network weights from mini-batch gradients (generative models 06).
    • Markov chains as models. PageRank is the stationary distribution of a random surfer with random restarts (the restarts make the chain irreducible and aperiodic). A fixed policy turns an MDP into a Markov chain, and its stationary distribution weights the states in policy-gradient theorems (reinforcement learning 01). Hidden Markov models (speech, genomics) put a Markov chain under noisy observations.
    • Statistical physics. The original Metropolis algorithm (1953) sampled the Boltzmann distribution e−E/kTe^{-E/kT} of particle systems. The Ising model and molecular simulations still use Metropolis, Gibbs (heat-bath) and Hamiltonian (molecular dynamics) moves.

    7. Common mistakes #

    • Computing π\pi as a high power of PP. It fails for periodic chains and is slow when ∣λ2∣|\lambda_2| is close to 1. Solve π(P−I)=0\pi(P - I) = 0 with ∑iπi=1\sum_i \pi_i = 1.
    • Using a right eigenvector instead of a left one. P1=1P\mathbf 1 = \mathbf 1 always holds, so the right eigenvector for eigenvalue 1 is the constant vector. π\pi is the eigenvector of P⊤P^\top.
    • "Detailed balance is necessary for stationarity." It is only sufficient (the cycle chain).
    • Forgetting the Hastings correction for an asymmetric proposal, for example a random walk on log⁡x\log x for a positive parameter (the factor x′/xx'/x).
    • Not recording the current state after a rejection, or re-proposing until a move is accepted. The repeated values are part of the chain.
    • Tuning for a very high acceptance rate. Near 100% acceptance usually means tiny steps and slow mixing.
    • Adapting the step size forever. If the proposal keeps changing with the chain's history, the chain may no longer have π\pi as its stationary distribution. Adapt during warm-up only, then fix the settings.
    • Thinning to "fix" autocorrelation. Keeping every kk-th draw throws information away. It only saves memory.
    • Trusting one chain, or the old threshold R^<1.1\hat R < 1.1. Run at least 4 chains from dispersed starts and use R^<1.01\hat R < 1.01.
    • Ignoring divergences in HMC. Even a few divergences mean that part of the posterior was not explored.

    8. Check yourself #

    1. Find π\pi for P=(0.50.500.250.50.2500.50.5)P = \begin{pmatrix} 0.5 & 0.5 & 0 \\ 0.25 & 0.5 & 0.25 \\ 0 & 0.5 & 0.5 \end{pmatrix} without solving a 3×33 \times 3 system.
      AnswerThe chain moves only to neighbours, so try detailed balance: π1⋅0.5=π2⋅0.25\pi_1 \cdot 0.5 = \pi_2 \cdot 0.25 gives π2=2π1\pi_2 = 2\pi_1, and π2⋅0.25=π3⋅0.5\pi_2 \cdot 0.25 = \pi_3 \cdot 0.5 gives π3=π1\pi_3 = \pi_1. Normalise: π=(1/4, 1/2, 1/4)\pi = (1/4,\ 1/2,\ 1/4). Detailed balance implies stationarity, so this is π\pi.
    2. A chain has P=(0.90.10.50.5)P = \begin{pmatrix} 0.9 & 0.1 \\ 0.5 & 0.5 \end{pmatrix}. Give π\pi, λ2\lambda_2, and the number of steps until the distance to π\pi is below 1% of its starting value.
      Answera=0.1a = 0.1, b=0.5b = 0.5, so π=(0.5,0.1)/0.6=(5/6, 1/6)\pi = (0.5, 0.1)/0.6 = (5/6,\ 1/6) and λ2=1−0.6=0.4\lambda_2 = 1 - 0.6 = 0.4. The error is multiplied by 0.40.4 each step: 0.4t<0.010.4^t < 0.01 for t>ln⁡100/ln⁡2.5≈5.03t > \ln 100/\ln 2.5 \approx 5.03, so 6 steps.
    3. A Metropolis–Hastings sampler uses the symmetric proposal x′=x+szx' = x + s z on a target with π~(x)=0\tilde\pi(x) = 0 for x<0x < 0. The chain is at x=0.1x = 0.1 and proposes x′=−0.3x' = -0.3. What happens, and what goes wrong if you "reflect" the proposal to +0.3+0.3 instead?
      Answerπ~(x′)=0\tilde\pi(x') = 0, so α=0\alpha = 0: the move is rejected and the chain records 0.10.1 again. Reflecting x′↦∣x′∣x' \mapsto |x'| is also a valid symmetric proposal (the density of ∣x+sz∣|x + sz| at yy is symmetric in xx and yy for x,y≥0x, y \ge 0), so it is not wrong. What is wrong is to reject and then re-propose until a positive value appears: that changes the kernel near 0 and biases the samples.
    4. For a positive parameter you propose x′=x eszx' = x\,e^{s z} with z∼N(0,1)z \sim \mathcal N(0, 1). What is the Hastings correction?
      AnswerGiven xx, x′x' is log-normal: q(x′∣x)=1x′s2πexp⁡ ⁣(−(log⁡x′−log⁡x)22s2)q(x' \mid x) = \frac{1}{x' s\sqrt{2\pi}}\exp\!\big(-\frac{(\log x' - \log x)^2}{2s^2}\big). The exponent is symmetric in xx and x′x', so q(x∣x′)/q(x′∣x)=x′/xq(x \mid x')/q(x' \mid x) = x'/x, and α=min⁡(1,π~(x′) x′π~(x) x)\alpha = \min\big(1, \frac{\tilde\pi(x')\,x'}{\tilde\pi(x)\,x}\big). Equivalently, run a symmetric random walk on y=log⁡xy = \log x with the target π~(ey) ey\tilde\pi(e^y)\,e^y (the Jacobian of lesson 05).
    5. Why does the HMC acceptance probability contain no Jacobian term, and why is a fresh momentum drawn at every iteration?
      AnswerThe leapfrog map is volume preserving (a composition of shears with determinant 1), so the density of the proposed point changes only through e−He^{-H}. With the momentum flip the map is its own inverse, so the proposal is symmetric. A fresh momentum is needed because HH is (nearly) conserved along a trajectory: without it the chain stays on one energy level and cannot explore the whole target.
    6. Four chains give R^=1.00\hat R = 1.00 and an ESS of 3000 for every parameter. Name one way the result can still be wrong.
      AnswerAll four chains can sit in the same mode of a multimodal posterior (they agree with each other), or all can miss a hard region such as the neck of a funnel. R^\hat R and ESS only see the regions the chains visited. For HMC, check divergences; run more chains from more dispersed starts.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. What equation defines a stationary distribution π\pi of a finite Markov chain with transition matrix PP?

    2. Which two conditions make a finite chain converge to a unique π\pi from every start, and how fast?

    3. State detailed balance and what it implies.

    4. Compute π\pi for P=(0.80.20.40.6)P = \begin{pmatrix} 0.8 & 0.2 \\ 0.4 & 0.6 \end{pmatrix}.

    5. Write the Metropolis–Hastings acceptance probability.

    6. Why does Metropolis–Hastings not need the normalising constant ZZ?

    7. Why is a Gibbs update accepted with probability 1?

    8. Why is Gibbs slow for a bivariate Gaussian with ρ=0.99\rho = 0.99?

    9. Name three properties of the leapfrog integrator that HMC needs.

    10. How is the effective sample size computed from autocorrelations?

    Open the review →

    9. Interview angle #

    • "How do you sample from an unnormalised posterior?" — Metropolis–Hastings: the acceptance probability uses only π~(x′)/π~(x)\tilde\pi(x')/\tilde\pi(x), so ZZ cancels. Tune the step size, run several chains, check R^\hat R and ESS. In practice, use NUTS in Stan, PyMC or NumPyro.
    • "Prove that Metropolis–Hastings samples the target." — Show detailed balance: π(x)q(x′∣x)α(x,x′)=min⁡(π(x)q(x′∣x),π(x′)q(x∣x′))\pi(x)q(x' \mid x)\alpha(x, x') = \min(\pi(x)q(x' \mid x), \pi(x')q(x \mid x')) is symmetric; summing gives stationarity; irreducibility and aperiodicity give convergence.
    • "Gibbs, random-walk Metropolis or HMC?" — Gibbs when the full conditionals are easy (conjugate models), but it is slow for correlated coordinates. HMC for smooth, continuous, high-dimensional targets with gradients. Random-walk Metropolis for low dimension or when no gradient is available. Discrete parameters: Gibbs or marginalise them out.
    • "Importance sampling or MCMC for a posterior?" — Importance sampling in low dimension with a good heavy-tailed proposal: independent draws and an estimate of the evidence ZZ. MCMC in high dimension, where importance weights collapse (lesson 14).
    • "What is a good acceptance rate?" — About 0.44 for 1-D random-walk Metropolis, about 0.234 in high dimension, about 0.65 to 0.8 for HMC. A rate near 1 means tiny steps.
    • "Your chains have R^=1.2\hat R = 1.2. What do you do?" — Look at the trace plots, run longer, reparametrise (for example a non-centred parametrisation of a hierarchical model), use a better sampler, and check again. Do not drop chains.

    10. Practice #

    The notebook for this lesson has five exercises, each with a check cell and a folded solution:

    1. stationary_distribution by a linear solve and by an eigenvector of P⊤P^\top (not by PtP^t), tested on the weather chain, the periodic flip chain and a birth–death chain.
    2. metropolis_hastings with the log acceptance test; the acceptance rate against 2πarctan⁡(2/s)\frac2\pi\arctan(2/s) and on a banana-shaped target.
    3. gibbs_bivariate_gaussian and the lag-1 autocorrelation ρ2\rho^2 of its chain, for several ρ\rho.
    4. leapfrog, tested for reversibility and the O(ε2)O(\varepsilon^2) energy error; then a small HMC sampler on a correlated Gaussian.
    5. ess_autocorr (Geyer) and split_rhat, tested on AR(1) chains (ESS/n=(1−φ)/(1+φ)\text{ESS}/n = (1 - \varphi)/(1 + \varphi)) and on chains that are stuck or drift.

    11. Watch and read #

    The Markov property, transition matrices, μt+1=μtP\mu_{t+1} = \mu_t P and the stationary distribution as an eigenvector.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Irreducible and periodic chains, reversibility (detailed balance) and random walks on graphs.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A second, slower introduction with many small examples of states, transitions and n-step probabilities.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    Why we build a Markov chain whose stationary distribution is the target, and the role of burn-in.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The acceptance probability, and why it makes the target the stationary distribution of the chain.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    How Gibbs sampling updates one coordinate at a time from its conditional distribution.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The physical picture behind HMC, the role of the momentum, and why it explores faster than a random walk.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play