S1445 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 2πtan−1⁡(2σ/s).
  • 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?

Name it

What the lab computes

  • Viterbi: in log space, δ1(k)=ln⁡πk+ln⁡Bk(o1) and δt(j)=maxi⁡[δt−1(i)+ln⁡Aij]+ln⁡Bj(ot), remembering each maximizing i; the best path is traced back from the largest final δ. It costs NK2 operations against the KN paths of brute force.
  • Metropolis: propose x′=x+ϵ with ϵ∼𝒩(0,s2), and accept with probability min⁡(1,p(x′)/p(x)), comparing logs; a rejected proposal repeats the current state.
  • Gibbs: for a standard bivariate Gaussian with correlation ρ, alternate x∼𝒩(ρy,1−ρ2) and y∼𝒩(ρx,1−ρ2). 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 α=12ln⁡1−ϵϵ, multiplies each example's weight by e−αyh(x) 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?