Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Follow the arrows, but keep shaking
Suppose you know, at every point, which direction is uphill in probability: the score. If you only climbed, every walker would end up stuck on the nearest peak. Langevin dynamics adds random shaking of just the right size, so that walkers climb towards likely regions but keep jiggling around them, and after a while their positions are spread exactly like the target distribution. The step size is the main knob: small steps are accurate but slow; big steps are fast but blur the result, and too big makes walkers fly off. In the demo, 200 walkers start in the left valley of a two-valley landscape. Pull the valleys apart and watch the crossings stop.
Langevin samplers sit inside score-based and diffusion generators, Bayesian neural networks (SGLD), molecular dynamics and uncertainty-aware planning. The same equation, with a physical potential, describes a particle in a viscous fluid at temperature T, which is where Paul Langevin introduced it in 1908.
- 1. Langevin dynamics needs…
- 2. Too large a Langevin step…
- 3. Chains struggle to cross between modes that are…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.ULA, MALA and SGLD
- Langevin diffusion: dx = ∇log p(x) dt + √2 dW_t. Its Fokker–Planck equation ∂q/∂t = −∇·(q ∇log p) + Δq has q = p as a stationary solution, and under mild conditions it converges to p.
- ULA: x_(k+1) = x_k + ε ∇log p(x_k) + √(2ε) ξ_k (Euler–Maruyama). Its stationary law is close to p but biased by O(ε); for N(0, 1/λ) the variance is 1/(λ(1 − ελ/2)). It is stable only for ε < 2/λ_max of the Hessian of −log p, and can be transient for light-tailed targets with steep gradients (Roberts and Tweedie, 1996).
- MALA: treat the ULA move as a proposal q(x′|x) = N(x + ε∇log p(x), 2εI) and accept with min(1, p(x′)q(x|x′)/(p(x)q(x′|x))): exact for any ε, optimal acceptance about 0.574 in high dimensions (Roberts and Rosenthal, 1998).
- Conditioning: the step is set by the stiffest direction while mixing time is set by the widest; the ratio is the condition number. Preconditioning (Riemann-manifold MALA, Girolami and Calderhead, 2011) or HMC helps.
- SGLD (Welling and Teh, 2011): replace ∇log p by a minibatch estimate of the log-posterior gradient and decrease ε; the injected noise dominates the gradient noise as ε → 0, giving posterior samples at SGD cost.
- Non-asymptotic guarantees: for strongly log-concave p, ULA reaches accuracy δ in total variation in a number of steps polynomial in dimension and 1/δ (Dalalyan, 2017; Durmus and Moulines, 2017). For multimodal targets, the escape time over a barrier ΔU grows like exp(ΔU) (Kramers, 1940).
- 1. ULA is stable on a Gaussian only if…
- 2. MALA removes ULA's bias by…
- 3. SGLD uses…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Langevin samplers in code
Exercise 1.
- One ULA step.
- One MALA step with the correct asymmetric proposal densities.
- The integrated autocorrelation time of a chain.
Practice.
- Tune ε by the stiffest direction (or by MALA acceptance around 0.5–0.6), then measure the autocorrelation time along the slowest direction.
- Run many chains from dispersed starts and compare them (R-hat); a single chain cannot reveal a missed mode.
- For posteriors, prefer NUTS (Stan, NumPyro, BlackJAX) when gradients are cheap and exact; use SGLD or SG-HMC for large datasets.
- In score-based generative models, the score is learned, so MALA’s ratio is unavailable; use small steps, noise annealing and a final denoising step.
- In JAX, BlackJAX provides MALA and SGLD kernels; in PyTorch, write the update by hand in a no-grad loop.
import jax, jax.numpy as jnp
def ula(key, x, grad_logp, eps, n):
def step(x, k):
x = x + eps * grad_logp(x) + jnp.sqrt(2 * eps) * jax.random.normal(k, x.shape)
return x, x
_, xs = jax.lax.scan(step, x, jax.random.split(key, n))
return xs # one sample per step- 1. A good MALA acceptance rate in high dimensions is about…
- 2. A missed mode is best detected by…
- 3. For learned scores, MALA is unavailable because…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Stationarity. For dx = b(x) dt + √2 dW the density evolves by ∂q/∂t = −∇·(b q) + Δq = ∇·(q(∇log q − b)). With b = ∇log p the bracket vanishes at q = p, so p is stationary; moreover d/dt KL(q‖p) = −E_q|∇log(q/p)|² ≤ 0 (the relative Fisher information), so KL decreases.
ULA’s bias on a Gaussian. For p = N(0, 1/λ), x′ = (1 − ελ)x + √(2ε)ξ is an AR(1) process with stationary variance v = 2ε/(1 − (1 − ελ)²) = 1/(λ(1 − ελ/2)). It requires |1 − ελ| < 1, i.e. ε < 2/λ. The x-autocorrelation is φ = 1 − ελ, so τ = (1 + φ)/(1 − φ) = (2 − ελ)/(ελ): small steps mean long autocorrelation.
MALA is exact. The MH acceptance with the true proposal densities enforces detailed balance with respect to p for any ε; the cost is rejections, which grow with ε and dimension.
Barriers. In ½N(−d, 1) + ½N(d, 1) the potential −log p has a barrier at 0 of height about d²/2 − log 2 relative to the wells (for well-separated modes). Kramers’ law makes the crossing rate decay like exp(−ΔU), so the time to equilibrate the mode weights explodes with d.
Exercise check. At ε = 0.18 on the stiff axis (λ = 10), 1/(10 × 0.1) = 1.0 is predicted and 0.999 was measured; on the wide axis (λ = 1), 1/(1 − 0.09) = 1.099 is predicted and 1.113 was measured.
- 1. The Langevin diffusion decreases KL(q‖p) at rate…
- 2. ULA's stationary variance on N(0, 1/λ) is…
- 3. Kramers' law says crossing rates decay like…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How do step size and mode separation limit Langevin sampling in practice?
History.
- P. Langevin, “Sur la théorie du mouvement brownien” (1908).
- H. A. Kramers, “Brownian motion in a field of force and the diffusion model of chemical reactions” (Physica, 1940).
- P. J. Rossky, J. D. Doll and H. L. Friedman, “Brownian dynamics as smart Monte Carlo simulation” (1978): the MH-corrected Langevin move.
- G. O. Roberts and R. L. Tweedie, “Exponential convergence of Langevin distributions and their discrete approximations” (Bernoulli, 1996): ULA and MALA.
- M. Welling and Y. W. Teh, “Bayesian learning via stochastic gradient Langevin dynamics” (ICML 2011).
- A. S. Dalalyan, “Theoretical guarantees for approximate sampling from smooth and log-concave densities” (JRSS-B, 2017).
Exercise 2.
- Part 1, conditioning: target N(0, diag(1, 0.1)); 200 chains, 800 steps, 200 discarded; ε ∈ (0.02, 0.1, 0.18, 0.22); ULA and MALA. Report the variances, MALA acceptance and the mean autocorrelation time of x₁ over 20 chains.
- Part 2, separation: target ½N((−d, 0), I) + ½N((d, 0), I), d ∈ (2, 3, 4); 500 ULA chains with ε = 0.1 for 1500 steps, all started at (−d, 0). Report the fraction that ever crossed x₁ = 0 and the fraction on the right at the end (target 0.5).
| measure | seed 0 | seed 1 | seed 2 |
|---|---|---|---|
| ULA var x₂ (true 0.1), ε = 0.02 / 0.1 / 0.18 | 0.1119 / 0.1997 / 0.9990 | 0.1101 / 0.1997 / 1.0021 | 0.1117 / 0.2010 / 0.9902 |
| ULA at ε = 0.22 | diverged | diverged | diverged |
| MALA var x₂ at ε = 0.22 (acceptance) | 0.0993 (0.454) | 0.1027 (0.454) | 0.1011 (0.454) |
| MALA acceptance, ε = 0.02 / 0.1 / 0.18 | 0.980 / 0.783 / 0.547 | 0.980 / 0.782 / 0.550 | 0.980 / 0.783 / 0.548 |
| ULA τ of x₁, ε = 0.02 / 0.1 / 0.18 | 67.9 / 16.4 / 11.4 | 59.7 / 18.4 / 10.9 | 57.0 / 17.6 / 10.7 |
| chains that ever crossed, d = 2 / 3 / 4 | 1.000 / 0.802 / 0.054 | 1.000 / 0.798 / 0.050 | 1.000 / 0.786 / 0.060 |
| fraction on the right, d = 2 / 3 / 4 | 0.506 / 0.438 / 0.042 | 0.482 / 0.424 / 0.036 | 0.486 / 0.454 / 0.044 |
- ULA’s bias is exactly as predicted: 0.1/(1 − 5ε) = 0.111, 0.2 and 1.0 at the three step sizes; at ε = 0.18 the stiff axis is ten times too wide. Past 2/λ_max = 0.2 it diverges.
- MALA is exact at every step size, paying with acceptance falling from 0.98 to 0.45.
- Conditioning: the wide axis mixes slowly because the step is capped by the narrow one (τ ≈ 57–68 at ε = 0.02, about 11 at ε = 0.18; theory gives 99 and 10.1, and the short chains underestimate the larger value).
- Barriers: going from d = 3 to d = 4 cut crossings from about 80% to 5–6%, and the estimated weight of the right mode fell from 0.42–0.45 to 0.04; for d = 4 the chains report a single mode with full confidence. This is the failure that annealing (NCSN, next page) is designed to fix.
Langevin dynamics in AI, robotics and aerospace
- Generative models: score-based and diffusion samplers are annealed Langevin or reverse-SDE integrators; corrector steps in predictor–corrector samplers are literally ULA.
- Bayesian deep learning: SGLD and its variants give posterior samples for uncertainty estimates in perception and navigation networks at minibatch cost.
- Molecular and materials simulation: Langevin thermostats sample configurations at temperature T; learned force fields plug straight in.
- Planning under uncertainty: Langevin-style sampling over trajectories (with a cost as energy) explores several homotopy classes of paths for robots and spacecraft rendezvous, but, as the exercise shows, separated solutions need annealing or restarts.
References
- P. Langevin (1908). “Sur la théorie du mouvement brownien.” Comptes Rendus de l’Académie des Sciences 146.
- H. A. Kramers (1940). “Brownian motion in a field of force and the diffusion model of chemical reactions.” Physica 7.
- G. O. Roberts and R. L. Tweedie (1996). “Exponential convergence of Langevin distributions and their discrete approximations.” Bernoulli 2(4).
- G. O. Roberts and J. S. Rosenthal (1998). “Optimal scaling of discrete approximations to Langevin diffusions.” JRSS-B 60.
- M. Welling and Y. W. Teh (2011). “Bayesian learning via stochastic gradient Langevin dynamics.” ICML.
- M. Girolami and B. Calderhead (2011). “Riemann manifold Langevin and Hamiltonian Monte Carlo methods.” JRSS-B 73.
- A. S. Dalalyan (2017). “Theoretical guarantees for approximate sampling from smooth and log-concave densities.” JRSS-B 79.