Sampling What You Cannot Normalise

W04 · Part 1 — Metropolis–Hastings, and why it cannot generate an image

Uma Satyaranjan · Soma S Dhavala

Invalid Date

Where we are

  • W03 ended badly on purpose: estimating pdatap_{\text{data}} in high dimension is hopeless
  • But generation never needed the density — it needed samples
  • Today: the classical answer, and the exact point where it stops

Everything this course does reduces to one request: produce a draw from a distribution. Today we solve that request in the case where you can write the target down — and find that the case we care about is not that one.

The setting

You can evaluate the target, but only up to a constant:

π(x)=π̃(x)Z,Z=∫π̃(x)dx\pi(x) \;=\; \frac{\tilde\pi(x)}{Z}, \qquad Z = \int \tilde\pi(x)\,dx

  • π̃\tilde\pi — cheap, closed form, evaluable anywhere
  • ZZ — an integral over the whole space that nobody can do

This is not a special case. It is the generic case.

Three places it shows up

Bayesian posterior

p(θ∣𝒟)=p(θ)L(𝒟∣θ)p(𝒟),p(𝒟)=∫p(θ)L(𝒟∣θ)dθp(\theta \mid \mathcal{D}) = \frac{p(\theta)\,L(\mathcal{D}\mid\theta)}{p(\mathcal{D})}, \qquad p(\mathcal{D}) = \int p(\theta) L(\mathcal{D}\mid\theta)\, d\theta

Numerator: prior × likelihood, both chosen by you. Denominator: intractable.

Energy-based model — π(x)∝e−E(x)\pi(x) \propto e^{-E(x)}, and ZZ is the partition function

Conditioning — π(x)∝p(x)\pi(x) \propto p(x) restricted to a region AA; the mass p(A)p(A) is unknown

Why not rejection sampling?

Note 06: draw x∼qx \sim q, accept if u<π(x)/Mq(x)u < \pi(x)/Mq(x). Exact — and useless.

Target 𝒩(0,Id)\mathcal{N}(0,I_d), proposal 𝒩(0,σ2Id)\mathcal{N}(0,\sigma^2 I_d): acceptance rate σ−d\sigma^{-d}.

dd σ=1.1\sigma = 1.1 σ=1.5\sigma = 1.5
1 0.91 0.67
10 0.39 0.017
100 7×10−57 \times 10^{-5} 2×10−182 \times 10^{-18}

A proposal ten percent too wide is already dead at d=100d = 100. Every draw is a fresh, independent gamble against the same odds — nothing is ever learned.

The fix, in one sentence

Stop starting over. Move from where you already are.

A Markov chain: the next state depends on the past only through the present.

ℙ(xt+1∈A∣xt,…,x0)=∫AK(xt,y)dy\mathbb{P}(x_{t+1} \in A \mid x_t, \dots, x_0) \;=\; \int_A K(x_t,y)\,dy

The kernel K(x,y)K(x,y) is all the chain is.

A chain is a sequence of distributions

Run the kernel forward from any starting law p0p_0:

pt+1(y)=∫pt(x)K(x,y)dxp_{t+1}(y) \;=\; \int p_t(x)\,K(x,y)\,dx

There is not one distribution here — there are p0,p1,p2,…p_0, p_1, p_2, \dots flowing.

Note 06 computed this flow exactly for Gibbs on a bivariate normal: xt∣x0∼𝒩(ρ2tx0,1−ρ4t)x_t \mid x_0 \sim \mathcal{N}\big(\rho^{2t}x_0,\; 1-\rho^{4t}\big). Mean decays, variance climbs. Burn-in is the stretch where the flow is still visibly incomplete.

Stationarity

π\pi is stationary for KK when one more step changes nothing:

π(y)=∫π(x)K(x,y)dx\boxed{\;\pi(y) \;=\; \int \pi(x)\,K(x,y)\,dx\;}

Add irreducibility (can reach anywhere) and aperiodicity (no fixed cycle) and the chain is ergodic: pt→πp_t \to \pi from any start, and

1T∑t=1Tf(xt)→𝔼π[f(X)]\frac1T \sum_{t=1}^{T} f(x_t) \;\longrightarrow\; \mathbb{E}_\pi[f(X)]

The design problem, run backwards

  • Usual question: here is a chain — what is its stationary distribution?
  • Our question: here is the distribution — build me a chain

Checking π=πK\pi = \pi K directly means an integral over the whole space, which is the thing we are trying to avoid. We need a condition that is local, so it can be engineered pointwise.

Detailed balance

A chain is reversible with respect to π\pi if for every pair of states

π(x)K(x,y)=π(y)K(y,x)\boxed{\;\pi(x)\,K(x,y) \;=\; \pi(y)\,K(y,x)\;}

At equilibrium, mass flowing x→yx \to y equals mass flowing y→xy \to x. Every pairwise exchange balances.

It implies stationarity, in one line:

∫π(x)K(x,y)dx=∫π(y)K(y,x)dx=π(y)∫K(y,x)dx=π(y)\int \pi(x)K(x,y)\,dx = \int \pi(y)K(y,x)\,dx = \pi(y)\int K(y,x)\,dx = \pi(y)

The algorithm

Pick any proposal q(y∣x)q(y \mid x) you can sample from. From state xtx_t:

  1. Propose y∼q(⋅∣xt)y \sim q(\cdot \mid x_t)
  2. Compute

α(xt,y)=min{1,π(y)q(xt∣y)π(xt)q(y∣xt)}\boxed{\;\alpha(x_t,y) \;=\; \min\!\left\{1,\; \frac{\pi(y)\,q(x_t \mid y)}{\pi(x_t)\,q(y \mid x_t)}\right\}\;}

  1. Draw u∼Uniform(0,1)u \sim \mathrm{Uniform}(0,1): accept xt+1=yx_{t+1} = y if u<αu < \alpha, otherwise xt+1=xtx_{t+1} = x_t

The constant cancels

π\pi enters only through a ratio:

π(y)π(x)=π̃(y)/Zπ̃(x)/Z=π̃(y)π̃(x)\frac{\pi(y)}{\pi(x)} \;=\; \frac{\tilde\pi(y)/Z}{\tilde\pi(x)/Z} \;=\; \frac{\tilde\pi(y)}{\tilde\pi(x)}

For a Bayesian posterior with a symmetric proposal the rule is literally

α=min{1,p(θ′)L(𝒟∣θ′)p(θ)L(𝒟∣θ)}\alpha \;=\; \min\!\left\{1,\;\frac{p(\theta')\,L(\mathcal{D}\mid\theta')}{p(\theta)\,L(\mathcal{D}\mid\theta)}\right\}

Prior × likelihood at two points. The evidence integral — the entire reason the posterior was intractable — never appears.

Rejection is not a discarded draw

A rejected proposal means the chain emits the current state again.

  • Staying put is a legal move, and a genuine sample
  • It is how the chain gives extra weight to high-density regions
  • Recording only accepted states is the most common implementation bug — and it silently biases every estimate

Why it works

For y≠xy \ne x the kernel is K(x,y)=q(y∣x)α(x,y)K(x,y) = q(y\mid x)\,\alpha(x,y). Push the positive factor inside the min\min:

π(x)q(y∣x)min{1,π(y)q(x∣y)π(x)q(y∣x)}=min{π(x)q(y∣x),π(y)q(x∣y)}\pi(x)\,q(y\mid x)\min\!\left\{1, \frac{\pi(y)q(x\mid y)}{\pi(x)q(y\mid x)}\right\} = \min\big\{\pi(x)q(y\mid x),\; \pi(y)q(x\mid y)\big\}

The right-hand side is symmetric in x↔yx \leftrightarrow y — swapping labels swaps the two arguments of a min\min and changes nothing.

So π(x)K(x,y)=π(y)K(y,x)\pi(x)K(x,y) = \pi(y)K(y,x): detailed balance, hence stationarity. ◼\blacksquare

The acceptance rule was reverse-engineered to make that min\min appear.

Random-walk Metropolis

Symmetric proposal q(y∣x)=𝒩(y;x,τ2I)q(y\mid x) = \mathcal{N}(y; x, \tau^2 I), so the qq-ratio is 1:

α=min{1,π(y)π(x)}\alpha \;=\; \min\!\left\{1, \frac{\pi(y)}{\pi(x)}\right\}

  • Always move uphill
  • Move downhill with probability equal to the density ratio

The downhill move is not a defect — it is the difference between sampling and optimisation. A hill-climber reports the mode; a sampler must visit the tails in proportion to their mass.

Two more special cases

Independence sampler — q(y∣x)=q(y)q(y\mid x) = q(y), ignoring the current state. Then α=min{1,w(y)/w(x)}\alpha = \min\{1, w(y)/w(x)\} with w=π/qw = \pi/q: accept if the candidate’s importance weight beats the incumbent’s.

Gibbs is MH with α≡1\alpha \equiv 1 — propose one coordinate from its exact conditional π(yi∣x−i)\pi(y_i \mid x_{-i}); substitute into the ratio and everything cancels.

Gibbs never rejects. The price is that you must be able to sample every conditional exactly.

MALA: the score walks in

Nothing says the proposal has to be blind. Drift toward high density first:

y=x+τ22∇xlogπ(x)+τz,z∼𝒩(0,I)y \;=\; x + \tfrac{\tau^2}{2}\,\nabla_x \log \pi(x) + \tau\,z, \qquad z \sim \mathcal{N}(0,I)

then accept/reject as usual.

It needs only ∇xlogπ\nabla_x \log \pi — the score. The normalising constant differentiates away. Drop the accept/reject step and this is Langevin dynamics.

This is the closest classical ancestor of a score-based generative model. Module 4 learns that score from data instead of deriving it.

What it costs

Burn-in early samples come from pt≠πp_t \ne \pi and are discarded
Autocorrelation consecutive states are dependent by construction
Effective sample size ESS=T/(1+2∑kρk)\mathrm{ESS} = T \big/ \big(1 + 2\sum_k \rho_k\big)

Note 06’s Gibbs example: lag-1 autocorrelation ρ2\rho^2. A target with ρ=0.99\rho = 0.99 gives ESS about a fiftieth of the chain length.

Step size is a real dilemma

τ\tau acceptance what happens
too small →1\to 1 every move accepted, every move tiny — needs ∼(L/τ)2\sim (L/\tau)^2 steps to cross
too large →0\to 0 proposals land in the tails; the chain sticks for hundreds of steps
tuned ≈0.234\approx 0.234 the asymptotically optimal rate (Roberts, Gelman & Gilks, 1997)

The beginner’s instinct — “95% acceptance, it must be working” — is exactly backwards. It almost always means the chain is crawling.

And the curse, once more

Steps needed per independent sample, for product-form targets:

sampler scaling
random-walk Metropolis O(d)O(d)
MALA (adjusted Langevin) O(d1/3)O(d^{1/3})
Hamiltonian Monte Carlo O(d1/4)O(d^{1/4})

Against rejection sampling’s σ−d\sigma^{-d} this is a triumph — polynomial, not exponential. Against d≈105d \approx 10^5 for a modest image, it is still nowhere near enough.

The failure that hides best

A chain in one mode of a well-separated bimodal target must cross a low-density valley. The probability of proposing across it can be e−100e^{-100}.

The chain then looks perfectly healthy:

  • good acceptance rate
  • stable trace plot
  • tight autocorrelation

It has converged — to a mode, not to π\pi. Tempering, parallel chains and mode-hopping proposals help; none is a general solution. Remember this slide when we meet mode collapse in Part 2.

See it run

Rejection sampling as darts under an envelope; Gibbs as a staircase walk, with 4,000 parallel chains whose histogram at step tt is drawn against the exact ptp_t.

Now suppose every difficulty were solved

Perfect tuning. No multimodality. Dimension-free mixing.

It still cannot generate an image. The obstruction is not efficiency — it is the input.

MH answers:

“I can write the target down up to a constant. Give me samples.”

Generative modelling asks:

“I cannot write it down at all. I have 60,000 samples. Give me more.”

There is no π̃\tilde\pi for images

  • Nobody can supply a function that takes a 256×256×3256\times256\times3 array and returns a number proportional to how likely it is to be a photograph
  • That function is precisely what W03 proved we cannot estimate
  • MH converts a density into samples — and we have no density to feed it

And MH never learns. A million steps, then a fresh start, and you pay the full burn-in again. Nothing is trained, nothing amortised.

The pivot

In rejection sampling, in MH, in Gibbs — a human picks the proposal for convenience and hopes it resembles the target. In high dimension it does not.

So stop picking it. Learn it.

Which immediately raises the question that defines Part 2: if you never evaluate a density, how do you know your generator is any good? You cannot compute a likelihood, so you cannot maximise one.

Where this lands

Today Where it returns
Detailed balance / reversibility forward and reverse diffusion, Module 3
pt→πp_t \to \pi as a flow of laws the Fokker–Planck equation, Module 5
Langevin needing only ∇logπ\nabla \log \pi score matching, Module 4
Accept/reject patching a bad proposal why diffusion needs no such step
The ratio π(y)/π(x)\pi(y)/\pi(x), ZZ cancelling the discriminator’s logit, Part 2
A chain trapped in one mode mode collapse, Part 2

Metropolis–Hastings corrects a proposal chosen for convenience. A generative model learns one that was never wrong.

Reference: 08 · Metropolis–Hastings. Demo: The Sampling Lab. Next: 09 · Generative Adversarial Networks.