From likelihood to posterior to MCMC
One running example (you observed k successes in n trials) shown three ways: the likelihood curve and its peak (the MLE), and a live Metropolis sampler whose draws pile up, one tick at a time, into the marginal posterior.
1. The likelihood and the MLE
2. A Metropolis sampler builds the marginal posterior
What the sampler does, one step at a time. It wanders over values of $p$, and the values it visits pile up into the posterior. Each step:
- Propose. From the current $p$, jump to a nearby candidate $p' = p + \text{(a random nudge of size "step")}$.
- Score. Compute the posterior ratio $r = \dfrac{\text{posterior}(p')}{\text{posterior}(p)}$. Because the unknown normalizing constant cancels, this is just $\dfrac{p'^{\,k}(1-p')^{\,n-k}}{p^{\,k}(1-p)^{\,n-k}}$, likelihood × (flat) prior.
- Accept or reject. If the candidate is more probable ($r \ge 1$), move there. If it is less probable, move there only with probability $r$ (flip a biased coin); otherwise stay put and record the current value again.
That is the entire algorithm. Downhill moves are sometimes accepted, which is what lets the chain explore the whole posterior instead of just climbing to the peak. Run it and watch the histogram fill in the exact posterior curve without ever computing the integral in Bayes' rule.
The lower panel is the marginal posterior of $p$: take every draw the chain has made, sort them into bins, and that shape is your answer. The orange tick on the axis marks where the newest draw landed, so you can watch single draws turning into a distribution. With one parameter the marginal posterior is the whole posterior; in the two-parameter version the marginals are shadows of a joint cloud, and reporting one means throwing the other parameter away.
Things to try
- The peak is the MLE. Drag p in panel 1 to the top of the likelihood curve. It lands on k/n.
- The 1.92 rule. Tick the log-likelihood box. Every p whose log-likelihood is within 1.92 of the peak is inside the 95% profile interval (1.92 is half of the 3.84 chi-square cutoff). Set k = 3, n = 10 and compare that lopsided interval with the symmetric Wald interval printed beside it.
- MCMC recovers the posterior. Hit Run and watch the teal histogram in panel 2 fill in the dashed black posterior curve. The MCMC never computes that curve. It just wanders and the samples pile up in the right shape.
- Read the interval off the draws. The shaded band is the middle 95% of the draws themselves, which is the credible interval you would report. Step slowly from Reset and watch it lurch about while the chain is short, then settle as the marginal fills in. Nothing here is computed from the analytic curve, which is the point: the same recipe works for posteriors that have no formula.
- Step size matters. Set the step very small: the chain crawls and the acceptance rate is high but mixing is slow. Set it very large: most proposals are rejected (low acceptance) and the chain gets stuck. A middling step (acceptance ≈ 20–45%) works best.
- More data → tighter posterior. Raise n (keeping k/n fixed) and both the likelihood and the posterior get narrower.