Bai Liping

Monte Carlo, made visible

See how
samplers think.

Explore Gibbs sampling—and the six other dominant methods—one step at a time. All seven run live in your browser: four chain samplers in the lab, and the weighted and particle methods in a second lab below. Every demo is the genuine algorithm, not an animation of one.

Rebuilt as a single self-contained note from the original Sampling Playground: same correlated-Gaussian target, same four chains with identical parameters — plus live demos for the three population methods the original only described.

Condition, then draw

Gibbs sampling

Live sampler

These four are the chain samplers. The other three of the seven — importance, rejection, and SMC — are population methods and run in their own lab below ↘.

conditional
draw

Freeze one coordinate, draw the other from its exact conditional distribution, then swap. Every proposal is accepted.

Step 09/ 72
Target correlation ρ = 0.88
Acceptance 100% built into the conditional draw
ESS proxy 7 / 10 roughly independent information
Characteristic move horizontal / vertical watch the path above

Measured on this page's seeds (ρ = 0.88, all 72 steps): Gibbs accepts every draw by construction, but its lag-1-autocorrelation ESS proxy is only 4 of 73 visible points — the coordinate-wise zig-zag pays for the free acceptances. Random-walk Metropolis–Hastings accepts 42% of its σ = 0.72 proposals and does no better (ESS proxy 4). HMC accepts 99% of its 9-leapfrog-step trajectories at ε = 0.16 and reaches an ESS proxy of 28 — the gradient buys distant moves that survive the accept test. Slice sampling accepts everything and lands at an ESS proxy of 6: it adapts its scale, but still moves one coordinate at a time. A separately seeded 20,000-move Gibbs run recovers the target's moments — variances within 0.01 of 1, correlation within 0.004 of 0.88 — so the chains are sampling the right thing, slowly or quickly. The ESS proxy uses only the lag-1 autocorrelation of x₁; treat it as a mixing hint, not a certified effective sample size.

01

The central idea

A sampler turns a difficult distribution into a sequence of manageable moves.

The cloud of visited points becomes an empirical approximation of the target. The methods differ mainly in how they choose the next point—and therefore in what they require, how quickly they mix, and where they struggle.

02

The sampler atlas

Seven methods worth knowing.

These are not seven interchangeable buttons. They belong to different families, make different assumptions, and sometimes return fundamentally different objects: a chain, exact independent draws, weighted samples, or a particle population.

01MCMC · conditional

Gibbs

x′ ∼ p(x | y),  y′ ∼ p(y | x′)

Replace one variable at a time using an exact conditional draw.

Best when
Models with easy full conditionals; latent-variable and hierarchical models.
Watch out
Strongly correlated variables can make coordinate-wise progress painfully slow.
02MCMC · accept/reject

Metropolis–Hastings

accept with  min(1, π(x′)q(x|x′) / π(x)q(x′|x))

Correct almost any proposal mechanism with an acceptance probability.

Best when
A dependable general-purpose baseline when only an unnormalized density is known.
Watch out
Proposal scale is delicate: too small crawls, too large rejects.
03MCMC · gradient

HMC & NUTS

H(x, p) = −log π(x) + ½pᵀp

Use gradients and auxiliary momentum to propose distant, informed moves.

Best when
Smooth, differentiable, medium- to high-dimensional Bayesian posteriors.
Watch out
Needs gradients and tuning; discrete or discontinuous variables are awkward.
04MCMC · auxiliary variable

Slice sampling

u ∼ Uniform(0, π(x));  x′ ∼ {x : π(x) ≥ u}

Turn density evaluation into uniform sampling under the density curve.

Best when
One-dimensional or coordinate updates where proposal-scale tuning is undesirable.
Watch out
Finding and shrinking the slice can cost many density evaluations.
05Weighted · independent

Importance sampling

wᵢ ∝ π(xᵢ) / q(xᵢ),  xᵢ ∼ q

Draw from an easier proposal and repair the mismatch with weights.

Best when
Estimating expectations, evidence, and reusing independently drawn samples.
Watch out
In high dimensions, a few enormous weights can carry nearly everything.
Open below ↘Independent / population method
06Exact · independent

Rejection sampling

accept if  u ≤ π(x) / Mq(x)

Keep proposal draws in proportion to how well they match the target.

Best when
Low-dimensional targets with a tight, easy envelope.
Watch out
Acceptance collapses when the envelope is loose or dimension grows.
Open below ↘Independent / population method
07Particles · sequential

Sequential Monte Carlo

propagate → weight → resample

Move a population of weighted particles through time or intermediate targets.

Best when
State-space models, filtering, online inference, and multimodal target sequences.
Watch out
Particles can impoverish after repeated resampling; computation scales with population.
Open below ↘Independent / population method

Draw, then repair

The population methods, live

Live sampler
Method

Draws 0
Acceptance

Measured on this page's seeds. The static target is the bimodal mixture 0.55·N(−1.35, 0.60²) + 0.45·N(1.15, 0.45²) with proposal q = N(0, 1.5²). Rejection sampling under the numerically tightened envelope (M = 2.36) accepts 42.6% of 4,000 attempts, matching the 1/M = 42.4% the envelope predicts — and every kept point is an exact independent draw. Importance sampling keeps all 600 draws, but its Kish effective sample size is 388 (65% of nominal), and its estimate of E[x] sits at −0.10 against the true −0.23 — about 1.9 standard errors off, consistent with the ~0.07 standard error that a 388-draw effective size implies. The SMC panel filters the state-space model xₜ = 0.92·xₜ₋₁ + 0.45·ε with observations y = x + 0.5·ν using 44 particles; systematic resampling triggers whenever the weight ESS drops below N/2, which happens on 28 of 60 filtering steps, each time keeping on average only 45% distinct particles — the impoverishment the atlas card warns about, visible when duplicate particles stack at the same position. The weighted-particle mean still tracks the hidden state with RMS error 0.37 over the full 60-step run, under the 0.5 observation noise.

03

Choose by structure

What does your problem give you?

Start from the mathematical access you have—not from which sampler sounds most sophisticated.

Try NUTS / HMC Usually strongest

A 100-dimensional continuous posterior is differentiable.

Gradients prevent random-walk behavior, and NUTS adapts the trajectory length automatically.

Always verify with diagnostics: trace plots, multiple chains, effective sample size, and—when applicable—weight degeneracy.

A practical comparison of the dominant sampling families
MethodNeedsReturnsSignature strengthTypical failure
GibbsSampleable conditionalsCorrelated chainNo rejection or step-size tuningSlow zig-zagging
Metropolis–HastingsDensity ratiosCorrelated chainWorks with almost any proposalRandom walk or rejections
HMC / NUTSDensity + gradientsCorrelated chainEfficient long moves in high dimensionBad geometry or divergences
SliceDensity evaluationsCorrelated chainAdapts its own local scaleExpensive interval search
ImportanceProposal density + targetWeighted independent drawsParallel and reusableWeight collapse
RejectionBounded envelope Mq ≥ πExact independent drawsConceptually clean and exactVanishing acceptance
SMCProposal + incremental weightsWeighted particle populationOnline and multimodalParticle impoverishment

04

Never stop at “it ran”

Four words that matter after sampling.

01

Burn-in

Early draws may still remember the arbitrary starting point. Discarding them can remove initialization bias—but cannot repair a chain that never mixed.

02

Mixing

A well-mixing chain explores relevant regions quickly. Slow wandering, sticky runs, or separated chains warn that the sample is incomplete.

03

ESS

Effective sample size translates correlated or weighted draws into the approximate number of independent draws carrying the same information.

04

Convergence

Run multiple chains. Agreement, stable traces, and diagnostics such as R̂ near one support—not prove—that they reached the same target.

Where these samplers show up on this site

The particle population that SMC pushes through time is the same object the EO-MTT note wrestles with when it maintains uncertainty over measurement partitions, and the weighted hypothesis mixtures in the BP vs PMBM note are exactly the "keep several weighted explanations alive" move that importance weighting formalizes — PMBM's ranked global hypotheses are a discrete cousin of a particle population. The annealed soft-assignment EM in the frame-registration note shares SMC's tempering trick: walk a sequence of easier intermediate objectives toward the hard one instead of attacking it cold. This page was rebuilt from the original Sampling Playground; the four chain samplers here use the same algorithms and tuning constants, re-implemented as commented vanilla JavaScript you can read in this file's source.