§17.3 Markov chain Monte Carlo
Sometimes there is no tractable way to draw exact samples from or even from a good importance distribution — the typical case for an energy-based model . Sampling an EBM directly is a chicken-and-egg problem: to sample you need , but to sample you need . Directed models sidestep this with ancestral sampling in topological order; undirected models cannot.
The escape is a Markov chain markov chain A state x repeatedly updated by a transition operator T(x′|x); used to approximately sample distributions (e.g. EBMs) that have no tractable exact sampler. defined in ch. 17 — open in glossary : hold a state , start it anywhere, and update it repeatedly with a transition operator transition operator T(x′|x): the conditional distribution giving the probability a Markov-chain update moves to x′ from x; as a matrix, A_{i,j}=T(i|j). defined in ch. 17 — open in glossary until it becomes (nearly) a fair sample. The family of algorithms doing this is Markov chain Monte Carlo markov chain monte carlo (mcmc) The family of algorithms that run a Markov chain whose stationary distribution is the target p, using its states as (approximate) Monte Carlo samples. defined in ch. 17 — open in glossary (MCMC).
The matrix view. Restrict to countably many states (index them by integers) and imagine infinitely many chains run in parallel; let be the fraction in state . One update multiplies that distribution by the transition matrix :
A Markov step is a matrix multiply (eqs 17.20–17.21)
| the distribution over states after t steps (across the parallel chains) | probability vector | |
| the transition matrix; each COLUMN is a probability distribution (where you go from that state) — a stochastic matrix | states × states | |
| applying the update t times = raising A to the t-th power — the whole trajectory is matrix exponentiation | states × states |
Because is a stochastic matrix stochastic matrix A matrix whose columns are probability distributions (the transition matrix A); by Perron–Frobenius its largest eigenvalue is 1. defined in ch. 17 — open in glossary (columns are distributions), the Perron–Frobenius theorem guarantees its largest eigenvalue is exactly . Diagonalizing, (eq 17.22): every eigenvalue below is raised to the -th power and decays to zero, leaving only the eigenvalue-1 direction — the stationary (equilibrium) distribution stationary distribution The equilibrium distribution v=Av (eq 17.23): the eigenvector of the transition matrix with eigenvalue 1, to which the chain converges. defined in ch. 17 — open in glossary :
The stationary distribution (eq 17.23)
| the equilibrium: applying the transition leaves the DISTRIBUTION unchanged (individual states still move) | probability vector | |
| an eigenvector equation with eigenvalue 1 — the fixed point the chain converges to | eigenvector |
Choose so this stationary equals the target . Watch a four-state chain converge — and watch mixing collapse as its two clusters decouple:
Each step multiplies the whole distribution by the stochastic matrix A. It converges to the eigenvalue-1 eigenvector (the uniform stationary distribution). The second eigenvalue |λ₂|sets the speed: as the coupling ε → 0 the clusters separate, |λ₂| → 1, and mixing time → ∞ — the chain never leaves cluster L.
Burn-in, thinning, and the mixing-time problem
Running a chain to equilibrium is called burning in burn-in The initial run of a Markov chain, discarded, needed to reach the equilibrium distribution before its samples are usable. defined in ch. 17 — open in glossary ; those early samples are discarded. After equilibrium the samples are identically distributed but successive ones are highly correlated, so a short run is unrepresentative — mitigate by keeping only every -th sample, or by running many chains in parallel (deep-learning practice uses around 100, one per minibatch example). How long is burn-in? The mixing time mixing time The number of steps a Markov chain needs to reach equilibrium; governed by the second-largest eigenvalue of A, but generally unknowable in practice. defined in ch. 17 — open in glossary is set by the second-largest eigenvalue of — but the state space is exponentially large, so we cannot compute it. We run “long enough” and check for mixing with heuristics (inspecting samples, measuring autocorrelation). Theory promises convergence, and little more.
§17.4 Gibbs sampling
We still need to choose a transition operator whose stationary distribution is our . There are two routes: derive from a learned EBM (this section), or parametrize and learn directly so its stationary distribution is implicitly the model (§20.12–20.13). The standard derived choice is Gibbs sampling: update one variable at a time from its conditional given its graph neighbors.
- 1initialize the state arbitrarily
- 2repeat until burned in, then keep samples:
- 3for each variable (or each conditionally-independent block):
- 4sample ⓘ
- 5emit as a sample (thinning as needed)ⓘ
Variables that are conditionally independent given their neighbors can be sampled together — block Gibbs sampling block gibbs sampling Gibbs sampling that resamples a whole conditionally-independent group at once (all h given v, then all v given h in an RBM), exploiting the model's factorial conditionals. defined in ch. 16 — open in glossary . The RBM is the model example: all hidden units are independent given the visibles (and vice versa), so each entire layer updates in one shot. Replay it — this is the Markov chain whose stationary distribution is the RBM:
No hidden–hidden or visible–visible edges means every unit in a layer is conditionally independent of the rest of its layer given the other layer — so the whole block samples at once, each unit from a simple sigmoid. Alternating h, v, h, v… is block Gibbs sampling, and it is what makes the RBM cheap to train (ch18).
How does this compare with the directed-model sampler?
| Aspect | Ancestral (directed) | MCMC / Gibbs (undirected) | |
|---|---|---|---|
| Exactness | sample quality | exact in one pass | approximate; fair only after burn-in |
| Cost | compute per sample | one forward pass | burn-in + decorrelation time |
| Applies to | model type | directed acyclic graphs only | any model with p̃ > 0 (EBMs) |
| Failure mode | what goes wrong | none (single pass) | slow mixing between modes |
Other transition operators exist — Metropolis–Hastings metropolis-hastings A widely-used MCMC transition-construction algorithm (common outside deep learning, where Gibbs sampling dominates). defined in ch. 17 — open in glossary is standard in many fields — but in the deep-learning approach to undirected models, Gibbs sampling is almost the only one used; better samplers remain a research frontier. Its Achilles’ heel is coming up next: when the distribution has well-separated modes, the chain mixes agonizingly slowly.
Check yourself — MCMC & Gibbs sampling
1.Why can't we use ancestral sampling on an energy-based (undirected) model?
2.In the matrix view v⁽ᵗ⁾ = Aᵗv⁽⁰⁾, why does the chain converge to a stationary distribution?
3.What sets a Markov chain's mixing time, and why is it a practical problem?
4.In block Gibbs sampling of an RBM, why can a whole layer be sampled at once?
5.Predict the outcome — in MarkovChainLab you lower the coupling ε toward 0 and step the chain (started in cluster L). What do the readouts show?