HMC, NUTS and MCMC Diagnostics
Lesson 3 ended on a warning: a trace can look settled while exploring only half the posterior, and 20,000 steps of a crawling walk can be worth a few hundred honest draws. This lesson delivers the two things that warning demands. First, a faster engine: the physics-flavored sampler inside Stan and every serious Bayesian tool, which you will build in base R and run on Asha's order data. Second, a trust dashboard: the checks a professional runs before believing any chain.
By the end of this lesson you will be able to:
- Measure how much information a correlated chain really holds, by computing effective sample size from the chain itself
- Explain how Hamiltonian Monte Carlo turns sampling into physics, and why its far-away proposals still get accepted
- Say exactly what NUTS and warmup automate, and read a divergence warning as evidence rather than noise
- Catch a chain that looks healthy but is lying, by running several chains from dispersed starts and computing R-hat
- Run the full pre-flight dashboard (R-hat, effective sample size, divergences, overlaid traces) before trusting any fit
Prerequisites: Lessons 1 to 3 of this course (prior, likelihood, posterior, credible intervals, and the fifteen-line Metropolis sampler you built and tuned), plus base R functions, loops and matrices.
Below is the machine from Lesson 3. Every idea in this lesson is already visible inside it: toggle the step size and watch what the trace and the histogram do. Today you learn to read those two pictures the way a professional does, and to build the engine that makes them look right by design.
Two unknowns, and a walk that seems fine
Lessons 2 and 3 fixed the spread of Asha's order values at $18 by decree, so that her average order value \(\mu\) was the only unknown. Nobody hands you the spread in real life. The honest model for her store treats both as unknown: \(\mu\), the true long-run average order value, and \(\sigma\) (sigma), the true dollar spread of individual orders around that average. The redesigned checkout's first full week gives her 60 orders to learn both from. Each lesson runs in a fresh interactive R session, so build the week right here (a stand-in ledger with the same shape as hers: whole-dollar orders, most between $30 and $90):
The Metropolis rule needs nothing new. The log-numerator now takes two arguments, with a prior for each unknown: the average near $55 but loosely (a normal prior with standard deviation 10), and for the spread, which must be positive, an exponential prior that keeps it in the tens of dollars (mean $20). The sampler is the one you built in Lesson 3, except each proposal nudges BOTH unknowns at once:
By every Lesson 3 standard this run is a success. Acceptance is 0.28, right in the zone Lesson 3 promised for models with several unknowns. The average order lands at $54.66 with a credible interval of ($49.9, $59.5), and the spread lands near $19, both hugging the data. Look at the trace, and at something new: with two unknowns, each draw is a PAIR, so the posterior is a cloud of plausible (average, spread) combinations:
A settled trace, sensible numbers, a tidy cloud. Lesson 3 would stamp this and go home. Now look closer, because this chain is quietly wasting almost 90 percent of its work.