1 / 27SAMPLING / A FIELD GUIDESee how samplers think.Seven Monte Carlo methods, each running live on a target you can see.How do I draw from \(\pi\)?What does each draw cost?Four chain samplers on one correlated Gaussian, then rejection, importance weights and a particle filter—each checked against a known answer.01Chains02Weights03Choosing01 The idea & the targets →02 Gibbs & Metropolis–Hastings →03 HMC & slice sampling →04 Four chains, measured →05 Rejection, importance, SMC →06 Choose & diagnose →27 SLIDES · 7 LIVE LABS · SEEDED BROWSER SAMPLERSBai Liping · adapted from the original Sampling Playground · reviewed 24 Sep 2026
2 / 27CONTENTSWhat’s inside.Every entry opens its slide. Each live lab follows its introduction.03 A sampler turns an integral into an average04 Seven methods, two families05 One narrow ridge for all four chains06 Gibbs: condition, then draw · LIVE LAB08 Gibbs is Metropolis–Hastings that never rejects09 Metropolis–Hastings: propose, then judge · LIVE LAB11 HMC: follow the gradient, then correct · LIVE LAB13 Slice: sample uniformly under the curve · LIVE LAB15 High acceptance is not fast mixing16 Rejection: keep what lands under \(\pi\) · LIVE LAB18 Importance sampling: reweight every draw · LIVE LAB20 SMC: propagate, weight, resample · LIVE LAB22 Seven samplers, side by side23 Choose by what your problem gives you24 Never stop at “it ran”25 Where these samplers show up26 What to keep27 References
3 / 2701 · THE IDEAA sampler turns an integral into an average.The cloud of samples stands in for the distribution; methods differ in how they produce it.MONTE CARLO IN ONE LINE\[\mathbb E_\pi[f]=\int f(x)\,\pi(x)\,dx\;\approx\;\frac1N\sum_{n=1}^{N}f\big(x^{(n)}\big)\quad\text{or}\quad\sum_{n=1}^{N}\bar w_n\,f\big(x^{(n)}\big)\]WHAT YOU USUALLY HAVEAn unnormalized density \(\tilde\pi(x)=Z\,\pi(x)\) whose constant \(Z\) is unknown.Every method here touches \(\tilde\pi\) only through ratios, conditionals, gradients or self-normalized weights, so \(Z\) cancels or never appears.WHAT COUNTS AS A SAMPLEChain · correlated draws; average after burn-in.Exact draws · independent, equally weighted.Weighted draws · independent, unequal weights \(\bar w_n\).Particles · a weighted population moved through time.
4 / 2701 · THE ATLASSeven methods, two families.They return different objects, so they fail in different ways.CHAIN SAMPLERS · ONE STATE AT A TIMEGibbs — exact conditional draws, one coordinate at a time.Metropolis–Hastings — propose, then accept or stay.Hamiltonian Monte Carlo — gradient-guided trajectories.Slice — uniform draws under the density curve.Output: a correlated Markov chain whose stationary distribution is \(\pi.\)POPULATION METHODS · MANY DRAWS AT ONCERejection — keep proposals that land under the target.Importance — keep every proposal and reweight it.Sequential Monte Carlo — propagate, weight and resample a particle population.Output: exact independent draws, weighted draws, or a weighted population that tracks a sequence of targets.
5 / 2702 · THE CHAIN TARGETOne narrow ridge for all four chains.A correlated Gaussian is easy to check against—and hard for coordinate-wise moves.THE CHAIN TARGET\[\pi(x)\propto\exp\!\left(-\tfrac12\,x^{\mathsf T}\Sigma^{-1}x\right),\qquad \Sigma=\begin{bmatrix}1&\rho\\ \rho&1\end{bmatrix},\qquad \rho=0.88\]WHAT EACH SAMPLER USESGibbs uses the conditionals \(x_1\mid x_2\sim\mathcal N(\rho x_2,\,1-\rho^2)\).HMC uses the gradient \(\nabla\log\pi(x)=-\Sigma^{-1}x\).Metropolis–Hastings and slice sampling only evaluate \(\tilde\pi\).Contours in the labs are Mahalanobis radii 1 and 2: they enclose 39% and 86% of the mass.WHY IT IS HARDAlong the ridge the standard deviation is \(\sqrt{1+\rho}=1.37\); across it only \(\sqrt{1-\rho}=0.35\).Axis-aligned moves have conditional width \(\sqrt{1-\rho^2}=0.47\), so they must zig-zag.Every chain starts at \((-2.35,\,-2.05)\), 2.35 Mahalanobis units out along the ridge.
6 / 2703 · GIBBS SAMPLINGGibbs: condition, then draw.Replace one coordinate at a time with an exact draw from its full conditional.ONE MOVE = ONE EXACT CONDITIONAL DRAW\[x_1'\sim\mathcal N\!\left(\rho\,x_2,\;1-\rho^2\right),\qquad x_2'\sim\mathcal N\!\left(\rho\,x_1',\;1-\rho^2\right)\]KEY IDEAFreeze every coordinate but one and redraw it from its full conditional \(\pi(x_j\mid x_{-j}).\)Nothing is rejected and there is no step size to tune. The lab alternates \(x_1\) and \(x_2\) (a systematic scan).WHAT TO WATCH NEXTMoves are horizontal or vertical, about \(\pm2\sqrt{1-\rho^2}=\pm0.95\) wide. The green bell is the conditional being sampled.Raise \(\rho\) to 0.95: the bell narrows to \(\pm0.62\) while the ridge stays long, so progress slows even though every draw is accepted.
7 / 2703 · GIBBS SAMPLING · EXPERIMENTGibbs: condition, then draw.Step the chain and watch the green conditional it draws from; then drag \(\rho\) toward 0.95.
8 / 2703 · GIBBS SAMPLINGGibbs is Metropolis–Hastings that never rejects.Propose from the exact conditional and the acceptance ratio cancels to one.ONE CANCELLATION\[q(x'\mid x)=\pi(x_j'\mid x_{-j})\,\mathbb 1[x'_{-j}=x_{-j}],\qquad \pi(x)=\pi(x_j\mid x_{-j})\,\pi(x_{-j})\]\[\frac{\pi(x')\,q(x\mid x')}{\pi(x)\,q(x'\mid x)}=\frac{\pi(x_j'\mid x_{-j})\,\pi(x_{-j})\;\pi(x_j\mid x_{-j})}{\pi(x_j\mid x_{-j})\,\pi(x_{-j})\;\pi(x_j'\mid x_{-j})}=1\]WHY IT MATTERSThe exact conditional already proposes in the right proportion, so there is nothing left to correct. That is the whole difference between Gibbs and a general Metropolis–Hastings update.THE PRICEThe full conditional must be available and sampleable. Correlation is not removed—only rejection is. When conditionals are awkward, a Metropolis step inside Gibbs is common.
9 / 2704 · METROPOLIS–HASTINGSMetropolis–Hastings: propose, then judge.Any proposal becomes a valid sampler once an acceptance step corrects it.ACCEPT OR STAY\[\alpha(x\to x')=\min\!\left(1,\;\frac{\tilde\pi(x')\,q(x\mid x')}{\tilde\pi(x)\,q(x'\mid x)}\right),\qquad x'=x+\sigma\varepsilon,\;\;\varepsilon\sim\mathcal N(0,I)\]KEY IDEAFor the symmetric random walk the \(q\) terms cancel: uphill moves are always accepted, downhill moves with probability \(\tilde\pi(x')/\tilde\pi(x)\).A rejection repeats the current state—that repeat is part of the sample. Only ratios of \(\tilde\pi\) are needed.WHAT TO WATCH NEXTThe dashed circle is \(2\sigma\) around the current state; rust rings are rejected proposals.Shrink \(\sigma\) to 0.15: nearly everything is accepted, but the chain crawls. Grow it to 2.5: most proposals leave the ridge and are rejected. Default \(\sigma=0.72\).
10 / 2704 · METROPOLIS–HASTINGS · EXPERIMENTMetropolis–Hastings: propose, then judge.Step until you see a rejection, then compare \(\sigma=0.15\) with \(\sigma=2.5\).
11 / 2705 · HAMILTONIAN MONTE CARLOHMC: follow the gradient, then correct.Momentum and simulated dynamics turn many gradient evaluations into one distant proposal.SIMULATE, THEN ACCEPT BY ENERGY\[H(x,p)=-\log\tilde\pi(x)+\tfrac12\,p^{\mathsf T}p,\qquad p\sim\mathcal N(0,I),\qquad \alpha=\min\!\left(1,\;e^{-\Delta H}\right)\]LEAPFROG\[p\leftarrow p+\tfrac{\varepsilon}{2}\nabla\log\tilde\pi(x),\;\;x\leftarrow x+\varepsilon p,\;\;p\leftarrow p+\tfrac{\varepsilon}{2}\nabla\log\tilde\pi(x)\]Repeated \(L\) times. Leapfrog is reversible and volume-preserving, so only the energy error \(\Delta H\) enters the test. Lab: \(\varepsilon=0.16,\;L=9\).WHAT TO WATCH NEXTThe amber path is the latest trajectory; its \(\Delta H\) is printed at the end.Raise \(\varepsilon\): the energy error grows and rejections appear. Beyond \(\varepsilon=2\sqrt{1-\rho}\approx0.69\) leapfrog is unstable on this target. NUTS chooses the path length automatically.
12 / 2705 · HAMILTONIAN MONTE CARLO · EXPERIMENTHMC: follow the gradient, then correct.Step and watch each trajectory, then push \(\varepsilon\) past 0.69.
13 / 2706 · SLICE SAMPLINGSlice: sample uniformly under the curve.An auxiliary height turns density evaluation into a uniform draw.TWO UNIFORM DRAWS\[u\sim\mathcal U\big(0,\,\tilde\pi(x)\big),\qquad x'\sim\mathcal U\big\{x:\tilde\pi(x)\ge u\big\}\]STEP OUT, THEN SHRINKPlace a width-\(w\) bracket at random around \(x\); step it out until both ends leave the slice, with a budget of \(m=20\) widths split at random between the sides.Draw uniformly inside; after each miss, shrink the bracket toward \(x\). The lab updates one coordinate at a time, \(w=1.7\).WHAT TO WATCH NEXTThe green band is the exact slice; the grey bracket is what stepping out found; rust crosses are misses.Nothing is rejected: misses cost density evaluations instead. Try \(w=0.3\) (more stepping out) and \(w=4\) (more shrinking). The move adapts; the zig-zag remains.
14 / 2706 · SLICE SAMPLING · EXPERIMENTSlice: sample uniformly under the curve.Step and read each bracket and miss; then compare \(w=0.3\) with \(w=4\).
15 / 2707 · FOUR CHAINS, MEASUREDHigh acceptance is not fast mixing.Seeded runs at \(\rho=0.88\): 20,000 moves after 1,000 burn-in. Effective sample size of \(x_1\) by Geyer’s initial monotone sequence.PER MOVEHMC mixes about 5× faster than Gibbs and 14× faster than random-walk Metropolis. Gibbs and slice accept everything and still zig-zag.PER EVALUATION · AND IN HIGH DIMENSIONIn 2-D a gradient costs about one density, and Gibbs leads per evaluation. The gradient pays off as dimension grows: random-walk Metropolis work scales as \(d^2\), HMC as \(d^{5/4}.\)
16 / 2708 · REJECTION SAMPLINGRejection: keep what lands under \(\pi\).Exact, independent draws—as long as the envelope really covers the target.KEEP A DRAW IF IT LANDS UNDER THE TARGET\[x\sim q,\quad u\sim\mathcal U(0,1),\quad\text{keep }x\text{ if }u\le\frac{\tilde\pi(x)}{M\,q(x)},\qquad\text{valid when }\tilde\pi(x)\le M\,q(x)\;\;\forall x\]THE POPULATION TARGET\[\pi(x)=0.55\,\mathcal N(-1.35,0.6^2)+0.45\,\mathcal N(1.15,0.45^2)\]Proposal \(q=\mathcal N(0,1.5^2)\). The tightest constant is \(M=\sup\pi/q=2.23\), so the acceptance rate is \(1/M=45\%.\)WHAT TO WATCH NEXTBlue points are kept; their histogram matches \(\pi\). The lab starts after 1,000 proposals: 44% kept.Loosen the envelope: acceptance falls as \(1/M\), exactness stays. Push the slack below 1: the envelope dips under \(\pi\) (shaded) and the kept draws follow \(\min(\pi,Mq)\) instead.
17 / 2708 · REJECTION SAMPLING · EXPERIMENTRejection: keep what lands under \(\pi\).Loosen the envelope, then push its slack below 1 and compare the histogram with \(\pi\).
18 / 2709 · IMPORTANCE SAMPLINGImportance sampling: reweight every draw.Weights repair the mismatch between an easy proposal and the target.WEIGHT, SELF-NORMALIZE, CHECK THE BALANCE\[w_n=\frac{\tilde\pi(x_n)}{q(x_n)},\;\;x_n\sim q,\qquad \hat\mu=\frac{\sum_n w_n f(x_n)}{\sum_n w_n},\qquad \mathrm{ESS}=\frac{\left(\sum_n w_n\right)^2}{\sum_n w_n^2}\]KEY IDEAEvery draw is kept. Self-normalizing removes \(Z\), at the cost of a small finite-sample bias.For \(q=\mathcal N(0,1.5^2)\) the large-sample ESS fraction is \(1/\mathbb E_q[w^2]=65\%\): a weight-degeneracy diagnostic, not a general function-specific standard-error guarantee.WHAT TO WATCH NEXTAfter 1,000 draws: ESS 641, \(\hat\mu=−0.066\pm0.055\) vs −0.225—2.9 standard errors off, a 0.4% event (5% of 400 other seeds land beyond 2). Press New seed.Narrow \(q\) below \(s=0.6\): weights become unbounded; \(\mathbb E_q[w^2]\approx1.5\times10^5\) at \(s=0.5\), infinite below \(0.6/\sqrt2.\)
19 / 2709 · IMPORTANCE SAMPLING · EXPERIMENTImportance sampling: reweight every draw.Narrow the proposal below \(s=0.6\), then shift its mean; watch the stems and the ESS.
20 / 2710 · SEQUENTIAL MONTE CARLOSMC: propagate, weight, resample.A weighted particle population follows a posterior that changes with every observation.BOOTSTRAP PARTICLE FILTER\[x_t^{(i)}\sim p\big(x_t\mid x_{t-1}^{(i)}\big),\qquad w_t^{(i)}\propto w_{t-1}^{(i)}\,p\big(y_t\mid x_t^{(i)}\big),\qquad\text{resample when }\mathrm{ESS}_t\lt N/2\]THIS MODEL HAS AN EXACT ANSWER\[x_t=0.92\,x_{t-1}+0.45\,\varepsilon_t,\qquad y_t=x_t+0.5\,\nu_t\]Linear and Gaussian, so the Kalman filter gives the exact posterior (steady-state sd 0.38). The particle filter can be checked against it: that gap is pure Monte Carlo error.WHAT TO WATCH NEXTRust dashes: exact Kalman mean. Blue: the 44-particle mean. Default run: 28 resamplings in 60 steps, error vs the exact mean 0.10.Never resample: ESS collapses to 1.0 and the error grows to 0.60. 200 particles: 0.05.
21 / 2710 · SEQUENTIAL MONTE CARLO · EXPERIMENTSMC: propagate, weight, resample.Run to \(t=60\), then switch to “never” resample; vary \(N\) and compare with the exact mean.
22 / 2711 · COMPARESeven samplers, side by side.What each needs, what it returns, and how it typically fails.
23 / 2711 · CHOOSEChoose by what your problem gives you.Start from the access you have to the target, not from which sampler sounds sophisticated.Whatever you choose, verify it: several chains, trace plots, effective sample size, \(\widehat R\)—and weight balance for weighted methods.
24 / 2712 · DIAGNOSENever stop at “it ran”.Four checks that matter after sampling.01 · BURN-INEarly draws remember the arbitrary start. Discarding them removes initialization bias—but cannot repair a chain that never mixed.02 · MIXINGRun several chains from dispersed starts. Slow wandering, sticky runs or chains that disagree mean the sample is incomplete.03 · EFFECTIVE SAMPLE SIZEConverts correlated or weighted draws into an approximate count of independent ones. Vehtari et al. recommend ESS above 400 for reliable summaries.04 · CONVERGENCERank-normalized \(\widehat R\) below 1.01 supports—but does not prove—that the chains reached the same target.
25 / 2713 · ON THIS SITEWhere these samplers show up.Three other notes use the same ideas; the links open in this tab.EXTENDED-OBJECT TRACKINGMCMC over partitionsThe partition toolbox runs a Metropolis–Hastings-style split/merge chain and a Gibbs-style sampler over measurement partitions. Mixing and multiple-chain checks apply unchanged.Open /eo-mtt/ →BP VS PMBMWeighted hypothesesPMBM keeps a weighted set of global association hypotheses; practical implementations generate high-weight children by ranked assignment or sampling. These are hypothesis weights, not importance weights.Open /bp-vs-pmbm/ →FRAME REGISTRATIONAnnealing as continuationThe annealed soft-target heuristic widens, then narrows, a Gaussian kernel—an optimization cousin of the tempering sequences used by SMC samplers.Open /frame-registration/ →
26 / 2714 · TAKEAWAYSWhat to keep.Four distinctions to carry into any sampling problem.1 · MATCH THE METHOD TO YOUR ACCESSConditionals → Gibbs. Gradients → HMC/NUTS. Densities only → Metropolis–Hastings or slice. A good proposal → importance sampling. A sequence → SMC.2 · ACCEPTANCE IS NOT EFFICIENCYGibbs and slice accept every move and still zig-zag. Compare effective sample size per unit of work.3 · WEIGHTS NEED TAILSAn importance proposal lighter-tailed than the target gives unbounded or infinite-variance weights. Watch the ESS and the largest weight.4 · CHECK AGAINST A KNOWN ANSWERTest a sampler where the answer is exact—a Gaussian, a mixture, a Kalman filter—before trusting it where it is not.
27 / 27REFERENCE DESKRead the methods. Know the simplifications.Primary sources for the algorithms; the labs are independent teaching implementations.Geman & Geman 1984 · Stochastic relaxation, Gibbs distributions, and the Bayesian restoration of images. IEEE TPAMI 6(6). ↗Metropolis et al. 1953 · Equation of state calculations by fast computing machines. J. Chem. Phys. 21. ↗Hastings 1970 · Monte Carlo sampling methods using Markov chains and their applications. Biometrika 57(1). ↗Neal 2011 · MCMC using Hamiltonian dynamics. Handbook of Markov Chain Monte Carlo, ch. 5. ↗Hoffman & Gelman 2014 · The No-U-Turn Sampler: adaptively setting path lengths in Hamiltonian Monte Carlo. JMLR 15. ↗Neal 2003 · Slice sampling. Annals of Statistics 31(3). ↗Owen 2013, ch. 9 · Monte Carlo theory, methods and examples, ch. 9: importance sampling. ↗Gordon, Salmond & Smith 1993 · Novel approach to nonlinear/non-Gaussian Bayesian state estimation. IEE Proc. F 140(2). ↗Doucet & Johansen 2011 · A tutorial on particle filtering and smoothing: fifteen years later. ↗Vehtari et al. 2021 · Rank-normalization, folding, and localization: an improved R-hat for assessing convergence of MCMC. Bayesian Analysis 16(2). ↗Original Sampling Playground · The interactive page this deck was adapted from; samplers re-implemented and re-verified here. ↗SCOPEImplemented: Gibbs, random-walk MH, fixed-length HMC, stepping-out slice, rejection, self-normalized importance sampling and a bootstrap particle filter with an exact Kalman reference. Not implemented: NUTS, adaptation, multiple-chain \(\widehat R\).