S14 · Lesson 45 · Sampling lab
The most likely hidden-state path by dynamic programming checked against brute force, a random-walk Metropolis sampler whose acceptance rate matches theory, a Gibbs sampler's slow mixing under correlation, and AdaBoost with decision stumps
Lessons 40 to 44 described algorithms whose behavior is easiest to believe once you've run them: dynamic programming that finds the best of exponentially many state paths in linear time, Markov chains that sample a distribution they can only evaluate up to a constant, and boosting that builds a strong classifier from very weak ones. In this lab you'll write each in a few lines and check it against brute force or a closed-form prediction.
- A random-walk Metropolis sampler targets a Gaussian with standard deviation σ and proposes Gaussian steps of standard deviation s. The curve is its average acceptance rate against s/σ, which works out to .
- Tiny steps are almost always accepted but barely move; huge ones are almost always rejected. The best compromise for a one-dimensional Gaussian is s ≈ 2.4σ, at about 44% acceptance.
Predict
A Metropolis sampler for a Gaussian of standard deviation 0.5 proposes steps of standard deviation 1. About what fraction of proposals does it accept?
With steps twice the target's width, = 0.50: half the proposals are accepted. Task 2's sampler measures 50.3% over 20,000 steps. Halving the step raises acceptance to 70%, but the chain then moves more slowly; acceptance alone isn't the goal.
Name it
What the lab computes
- Viterbi: in log space, and , remembering each maximizing i; the best path is traced back from the largest final δ. It costs operations against the paths of brute force.
- Metropolis: propose with , and accept with probability , comparing logs; a rejected proposal repeats the current state.
- Gibbs: for a standard bivariate Gaussian with correlation ρ, alternate and . The x chain's lag-one autocorrelation is ρ².
- AdaBoost with stumps: each round picks the threshold and polarity with the lowest weighted error ε (thresholds at midpoints between distinct inputs, plus one below them all), gives it a vote , multiplies each example's weight by and renormalizes. Ties go to the first candidate found.
Check
Write viterbi(pi, A, B, obs) returning the most likely state path and its log probability.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write metropolis(logp, x0, step, n, seed): a random-walk Metropolis sampler using random.Random(seed), returning the n states visited and the acceptance rate.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write gibbs(rho, n, seed): n sweeps of a Gibbs sampler for a standard bivariate Gaussian with correlation ρ, starting at (0, 0), returning the list of (x, y) pairs.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write adaboost(x, y, rounds) for one-dimensional inputs and ±1 labels, returning a list of (threshold, polarity, α, ε) per round, where a stump predicts the polarity above the threshold and its negative below; and predict(model, x).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a Gibbs sampler mix slowly when the variables are strongly correlated?
Gibbs sampling moves one variable at a time, drawing it from its distribution given the others' current values. When two variables are strongly correlated, that conditional distribution is narrow: knowing y pins x down to within of ρy, so each update can shift x only a little. The joint distribution, though, stretches far along the correlated direction, and the sampler can only travel along it by many small alternating steps, like climbing a narrow diagonal staircase. Successive samples are then highly correlated (by ρ² per sweep for a Gaussian), and the number of effectively independent samples falls accordingly. Updating the variables jointly, or rotating to uncorrelated coordinates first, removes the problem.