ID-5-02 (TO) · Mathematical Foundations of Diffusion Models · W03 demo

The Sampling Lab

Two ways to draw from a distribution you can write down but cannot sample directly. One throws darts and keeps the good ones; the other walks. Neither ever estimates a density — and watching the second one converge is watching a probability distribution evolve in time.

Act IRejection sampling: propose, then judge

The setup is Bayesian and deliberately minimal. The proposal is a standard normal prior; an observation at −1 supplies the likelihood; the target is the posterior they imply.

q(x) = N(x; 0, 1)   ·   L(x) = exp(−(x+1)²/2s²)   ·   p(x) ∝ q(x)L(x)

Draw a candidate from the prior, then accept it with probability L(x)/max L. Accepted points are distributed exactly as the posterior — no normalising constant is ever computed, and no density is ever estimated.

Darts under the envelope

envelope M·q target posterior p likelihood L (scaled)
Every dart lands uniformly under the envelope. Those falling under p are kept, the rest discarded, and the histogram of survivors climbs onto the posterior. Narrow the likelihood and the acceptance rate collapses — that collapse is the whole weakness of the method, and it gets exponentially worse with dimension.

Act IIGibbs: a walk, and a distribution in time

Rejection sampling throws away work. A Markov chain instead moves: from the current point, draw each coordinate from its conditional given the others. For a bivariate normal with correlation ρ both conditionals are normal with known mean and standard deviation, which is exactly when Gibbs is available:

x | y ∼ N(ρy, 1−ρ²)      y | x ∼ N(ρx, 1−ρ²)

One chain, walking

The walk moves one axis at a time, which is why the path is a staircase. Raise ρ and the steps get shorter relative to the spread: the chain still gets there, but it takes far longer, and neighbouring draws are strongly correlated.

Now the part worth pausing on. Run 4,000 independent chains from the same starting point and ask not where any one of them is, but how they are distributed at step t. That distribution is a density evolving in time, and here it is known exactly:

xt | x0 ∼ N( ρ2t x0,   1 − ρ4t )

The distribution at step t

4,000 chains at step t exact pt stationary N(0,1)
The mass starts as a spike at x₀ and slides toward the origin while spreading, mean decaying like ρ2t and variance climbing to one. Burn-in is not a superstition — it is the length of time this picture is still visibly wrong.

A chain is a time series, and its law obeys a differential equation. What you just watched — pt flowing toward p∞ — is the discrete-time shadow of the Fokker–Planck equation of Module 5. Langevin dynamics is the continuous version, and it needs only ∇x log p, the score. The forward process of a diffusion model is this same picture run deliberately in the easy direction; the reverse process is the hard one.