Stats 401 - Week 9 - Part 2
Overview
Miles Chen explains how the Metropolis algorithm uses log probabilities to sample from Bayesian posteriors without deriving their normalizing constants, first checking the method on a beta-binomial baseball example and then applying it to exponential arrival data. He also shows how proposal width affects chain mixing and effective sample size, and demonstrates Metropolis sampling on a cipher-breaking task that scores substitution keys using English letter-pair frequencies and a word lexicon.
Key takeaways
- Log-space Metropolis acceptance avoids numerical underflow: compare candidate and current log weights, accepting a lower-weight state with probability exp(candidate log weight minus current log weight).
- For the baseball example, a Beta(81, 218) prior and 5 hits in 10 at-bats yield an exact Beta(86, 224) posterior, which validates the estimates from a 15,000-step Metropolis chain.
- The server-arrival example shows that an unnormalized prior-times-likelihood target is enough for sampling: an Exponential(1) prior and a one-minute arrival observation produce a posterior mean rate near 1.5.
- Proposal width creates a trade-off: tiny moves have high acceptance but high autocorrelation, while overly wide moves are frequently rejected; effective sample size is more informative than raw chain length alone.
- A substitution cipher can be searched by swapping two mappings at a time and scoring English bigrams and words, avoiding brute-force enumeration of 26! keys.
- Language-based cipher scores are imperfect: bigram frequencies and lexicon weights can rank misspellings above the correct text, so scoring parameters affect the key the chain ultimately favors.
Chapters
- Multiplying many small probabilities can cause floating-point underflow, so the algorithm compares log weights instead.
- For a symmetric proposal, calculate the log-weight difference between candidate and current states, then accept automatically if it is positive.
- If the difference is negative, accept with probability exp(difference); for a difference of -2, that probability is about 0.135.
- The target log weight combines the likelihood of 5 hits in 10 at-bats with a Beta(81, 218) prior.
- A proposal adds a uniform random offset between minus half the width and plus half the width to the current theta.
- Proposals outside the valid interval from 0 to 1 receive a log weight of negative infinity and are rejected.
- The chain records each state and whether the proposed move was accepted, repeating the current state after a rejection.
- The example starts at theta = 0.1, runs 15,000 steps, uses proposal width 0.17, and discards the first 500 steps as burn-in.
- The chain moves toward the posterior's high-probability region near theta = 0.277, then explores around it.
- Flat sections in the trace mark rejected proposals, because the chain reuses its previous state.
- The conjugate analytic posterior is Beta(86, 224), allowing exact calculations such as its mean and interval endpoints.
- Miles Chen compares those answers with both 30,000 direct posterior draws and about 14,500 retained Metropolis draws.
- The Metropolis sample closely follows the exact beta density, illustrating that the sampler needs the likelihood and prior but not the normalized posterior.
- The second Bayesian example treats lambda as a request arrival rate and models waiting times with an exponential distribution.
- The prior for lambda is Exponential(rate = 1), and the observed event is that a request arrived within one minute.
- The event likelihood is 1 - exp(-lambda), producing a posterior that is not immediately recognizable as a standard distribution.
- The unnormalized posterior weight multiplies the Exponential(1) prior density by the probability 1 - exp(-lambda) of an arrival by one minute.
- The implementation adds log densities and log probabilities, and assigns negative infinity to invalid lambda values below zero.
- A symmetric uniform random-walk proposal reuses the same Metropolis accept-or-reject procedure as the baseball example.
- The server example runs 20,000 steps with proposal width 5 and discards the first 500 as burn-in.
- The retained draws estimate the posterior mean of lambda at about 1.5 and the chance of another request within a minute at about two-thirds.
- The estimates agree with the calculated exact answers, showing how sampling can avoid difficult posterior normalization and integration.
- A tiny proposal width in the baseball example accepts about 99.7% of moves, but the resulting states barely change and are highly autocorrelated.
- A width that is too large often proposes theta values outside 0 to 1 or far into low-probability regions, causing long repeated-state plateaus.
- Trace plots and chains from different starting points help assess behavior; effective sample size estimates how many independent draws the correlated chain represents.
- Metropolis-Hastings extends the basic algorithm to asymmetric proposal distributions.
- Its acceptance calculation accounts for the relative probabilities of proposing the candidate from the current state and proposing the reverse move.
- Miles Chen keeps the course examples focused on symmetric proposals, where the proposal correction cancels.
- A monoalphabetic substitution cipher has 26! possible letter mappings, so randomly guessing complete keys is impractical.
- The search starts from a key and proposes a neighboring key by swapping the mappings of two randomly selected letters.
- Each candidate key is scored as a probabilistic explanation of the original English message, making cipher decoding a Metropolis sampling problem.
- The letter-pair score uses English bigram frequencies estimated from the English translation of War and Peace; common pairs such as TH score better than unusual pairs such as Q followed by a non-U letter.
- A lexicon adds evidence for recognized words, while unknown strings receive a very small probability such as 10^-10.
- The method sums log scores across the text and accepts worse-scoring swaps probabilistically, allowing exploration beyond only locally improving moves.
- Repeated letter swaps improve the key's score until the cipher reveals Shakespeare's “To be or not to be” passage and a passage from Pride and Prejudice.
- The recovered text becomes recognizable even though the best-scoring key can still contain errors, such as a distorted spelling of “question.”
- Bigram frequencies and word weights can favor plausible letter patterns over correct spelling, so adjusting the lexicon contribution changes which keys the sampler favors.
Transcript excerpt
0:03 Right. When um as we move forward through this Metropolis algorithm, we're going to be dealing with a bunch of probabilities and when you multiply a whole bunch of probabilities, those probabilities can end up getting really, really, really tiny. And so um we we often run up if if we don't if we leave them in their regular um decimal form, then the um we run into underflow uh problems where the uh floating point number system in our computers can't represent them accurately. So a lot of times what we will do is we work with logs and logs will convert the um the small probability to a
0:53 basically a negative number and then we can um add or subtract the uh the negative numbers. So instead of um doing a a weight value of the proposed divided by the weight value of the current, we'll take the log log weight of the proposed minus the log weight of the current and then um we will compare the log of our random uniform value um versus that difference or um or zero. And um and so, you know, for example, if uh the current log weight is -12 and the proposed log weight is -14, then um there's a difference of -2. And so if we take the uh
1:42 um uh probability of that, okay, the um if we exponentiate it, e to the -2, uh we would get something around 0.135 and that means we would accept um you know, a proposed value with a with only around 13% probability. And if if we reject it, then we recycle the the same value. Okay. So um this is um how we're going to um go ahead and and try it out using the um the beta binomial example with the the baseball,
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Miles Chen.