Bayesian Statistics

Start with a belief, update it with data. Priors and posteriors, a Markov chain exploring a posterior live, and why pulling every estimate toward the average beats treating each alone

Bayesian Statistics

Every other method in this module treats the unknown as fixed and the data as random. The Bayesian turns that round: the data are what they are, and uncertainty about the unknown is itself described by a probability distribution, a prior before the data and a posterior after. That answers the question people actually ask (how likely is it that the effect is real?) at the price of having to say what you believed beforehand.

Priors Meet Data

Press Play and watch three analysts with three different priors be drawn onto the same answer. Then draw your own prior in the lower panel, press Rule out the truth, and see what no amount of data can fix.

Third prior
Data
After 12 observationsEstimate of θ95% intervalP(θ > ½)
Open-minded (flat prior)0.4290.192 to 0.68429.1%
Sceptic (prior peaked at ½)0.4810.348 to 0.61539.0%
Yours (the prior you draw)0.2900.176 to 0.410< 0.1%
Frequentist (p̂ and the Wilson interval)0.4170.193 to 0.680not a question it answers

What each interval claims. Credible interval: given this prior and these data, θ lies inside with probability 95%. Confidence interval: the recipe that produced it catches the true θ in 95% of repeated datasets, and says nothing about the probability for this one.

With 12 observations the priors still matter. The analysts give θ > ½ probabilities of 29%, 39%, < 0.1%, and there is a range of θ to which two of them give probabilities 86% apart. Keep the data coming.

Simulated yes/no observations with the true θ you set. Solid curves: each analyst’s posterior, in the colours of the table. Dashed curves in the same colours: their priors. Teal shading: the likelihood of the data, scaled to the same area; the open-minded posterior is exactly its outline. Teal discs along the top: the latest observations, filled for yes and hollow for no. White dashed line: the truth. The sceptic’s prior is Beta(20, 20), worth 40 imagined earlier observations split evenly.

Go deeper:Prior × Likelihood, and What the Interval Means

The whole method is one line: posterior ∝ prior × likelihood. For each candidate value of θ, multiply how plausible it was beforehand by the probability it gives to the data that were seen, then rescale so the total is 1. The demo does exactly that on a grid of 400 values, which is why a prior of any shape works. The result is a distribution for θ itself, so a statement such as “the probability that θ exceeds ½ is 97%” is meaningful. In the frequentist account θ is a fixed number and has no probability; what is random is the interval. The two 95% intervals in the table are often nearly equal and still make different claims.

When the prior is a beta distribution the arithmetic collapses. A Beta(a, b) prior and k successes in n trials give a Beta(a + k, b + n − k) posterior, so the prior acts like a + b imagined earlier observations, a of them successes. The flat prior is Beta(1, 1); the sceptic’s Beta(20, 20) is worth forty observations split evenly, which is why it takes a few hundred real ones to overrule it. The Bernstein–von Mises theorem makes the convergence precise: with enough data the posterior is approximately normal, centred near the maximum likelihood estimate with the same spread as its sampling distribution, whatever the prior, provided the prior gives the truth some probability.

That proviso is called Cromwell’s rule, a name given by Dennis Lindley after Oliver Cromwell’s plea of 1650: “think it possible that you may be mistaken.” A prior probability of exactly zero can never be revised upwards, because zero times anything is zero. Short of that, a confident wrong prior is only a handicap, and the data overcome it at a rate you can watch.

Thomas Bayes’s essay was published in 1763, two years after his death, communicated to the Royal Society by Richard Price. Pierre-Simon Laplace developed the same approach independently and applied it on a large scale. The standing objection is the prior: where does it come from, and why should your conclusion depend on it? The standing replies are that every analysis rests on assumptions and a prior states some of them openly, that the dependence can be shown by trying several priors as the demo does, and that with much data it hardly matters. See also the Probability module’s lesson on Bayes’ theorem and the Machine Learning module’s lesson on Bayesian inference, which introduce the theorem and conjugate updating.

A Markov Chain Explores

Press Play and follow the walker for a few steps, then fast-forward and watch its cloud take the shape of the map. Drag the step size to both extremes and read the traces, then move the temperature to see the risk with its uncertainty.

1,500
steps taken
55%
of proposals accepted
≈ 76
effective sample size for b, from 1,300 kept draws
−0.279
posterior mean of b (sd 0.124); maximum likelihood −0.232 (s.e. 0.108)
−0.54 to −0.07
95% credible interval for b; b is negative in 99.9% of kept draws
0.9999
posterior median risk at 31 °F; 95% interval 0.86 to > 0.9999
The chain is mixing: 55% of proposals accepted, traces that look like noise around a level, and autocorrelation that dies away within a few dozen lags. The posterior mean of the slope is −0.279, steeper than the maximum likelihood value −0.232, because the posterior has a long tail towards steep slopes that the dashed normal approximation misses.

Real data: the 23 shuttle flights before Challenger (Dalal, Fowlkes and Hoadley, 1989); the model is P(distress) = 1/(1 + e−(a + b·(t − 69.6))) with flat priors on a and b. Measuring temperature from its mean makes the posterior a tilted oval; with the usual intercept at 0 °F, which is 15.04 at the maximum, it would be a needle-thin ridge that this simple sampler could hardly follow. Blue map: the posterior, brighter where it is higher, with outlines enclosing 50%, 90% and 99% of the probability. White disc: the walker. Arrow: the latest proposal, emerald if accepted and rose if rejected. Amber: the draws and everything computed from them; grey: burn-in. White dashed lines and the white cross: maximum likelihood and its normal approximation. Teal dots: the flights, at 1 for distress and 0 for none. Moving the step slider keeps the walker where it is and starts a fresh record of 1,500 steps.

Go deeper:Sampling a Posterior You Cannot Write Down

For most real models the posterior has no formula. The likelihood can be computed at any parameter value, but the constant that makes the posterior total 1 is an integral nobody can do. The way out is to draw samples from the posterior instead: with enough draws, any summary (a mean, an interval, the probability of an event) is just a count. The algorithm here was invented for statistical physics at Los Alamos by Nicholas Metropolis, Arianna and Marshall Rosenbluth, and Augusta and Edward Teller (1953). W. K. Hastings generalised it in 1970. Alan Gelfand and Adrian Smith showed statisticians in 1990 that such methods solved their computational problem, and Bayesian methods spread rapidly afterwards.

Why it works: the rule “move to a higher point always, to a lower point with probability equal to the ratio” makes the flow of probability from any position x to any position y equal the flow back from y to x once the walker is distributed according to the posterior. This is detailed balance, and it makes the posterior the stationary distribution of the chain. In the long run the walker spends time in each region in proportion to its posterior probability. Only ratios of the posterior are ever used, so the unknown constant cancels and never has to be computed. See also the Probability module’s lesson on Markov chains.

The draws are not independent. Each is a small move from the last, or no move at all, so a chain of ten thousand steps carries less information than ten thousand independent draws; the effective sample size estimates how much less from the autocorrelations. The early steps depend on the starting point and are discarded as burn-in. And although traces that look like steady noise are reassuring, the output of a chain can show that it has failed to converge but can never prove that it has: a chain can sit in one part of a posterior for a long time and look perfectly healthy. Modern samplers such as Hamiltonian Monte Carlo, used by the Stan software, use the gradient of the posterior to take long, well-aimed steps.

On these data, long runs of the sampler put the posterior mean of the slope near −0.29 with a standard deviation near 0.13, against a maximum likelihood estimate of −0.232 with standard error 0.108, and give a probability of about 99.9% that the slope is negative. The difference is not a disagreement about the data. The likelihood is lopsided, with a long tail towards steep slopes; the maximum sits at its peak, the mean is pulled into the tail, and the standard error describes a symmetric bell that is not quite there. With flat priors the two approaches are reading the same surface. The posterior for the risk at 31 °F is piled up against 1 with a thin tail below it. As in the Models and Overfitting lesson, 31 °F is far colder than any flight in the data, and the S-shaped curve is an assumption there.

Stein’s Paradox

Drag the shrinkage from 0% to 100% and watch the total squared error dip in between, near the James–Stein mark. Press New season a few times, then run 2,000 seasons and look at what happens to the best true hitter.

0.086
squared error, own averages (no pooling)
0.041
squared error at 66% shrinkage
0.041
squared error, James–Stein (66% shrinkage)
0.054
squared error, everyone at the grand mean
65%
best shrinkage for this season, knowing the truth
.038
spread of abilities estimated from the averages alone; truly .045
This season James–Stein pulls every average 66% of the way to the grand mean, and the total squared error falls by 53%, from 0.086 to 0.041. The best true hitter (truly .341) has hit .356; at the current setting his estimate is .289, which is further from the truth than his own average. Shrinkage promises a smaller total, not a better estimate for each player.

Simulated players, not real ones: true abilities are drawn from a normal distribution around .265 with the spread you set, and each player’s hits are binomial. Teal: averages so far. Amber: the estimates and their error curve. White rings: the truth, which a real analyst never sees. Pink: the player with the highest true ability. Violet: the James–Stein amount, (k − 3) × (variance of one average) ÷ (sum of squared distances of the k averages from their mean), capped at 100%, with the binomial variance at the grand mean plugged in. Emerald ring: the shrinkage that would have been best this season. Drag on the lower chart, or use the slider, to set the shrinkage yourself.

Go deeper:Why Borrowing from Everyone Helps

In 1956 Charles Stein proved something that statisticians found hard to believe. When three or more normal means are estimated at once, each from its own observation with the same known variance, the obvious estimate (take each observation as it stands) is inadmissible: another estimator has smaller expected total squared error whatever the true means are. W. James and Stein gave an explicit one in 1961. It pulls every observation towards a common point by a fraction that depends on how scattered the observations are compared with their noise. The paradox is that it helps even when the quantities have nothing to do with one another, and that no individual estimate is guaranteed to improve; only the total is.

Bradley Efron and Carl Morris made the result famous in 1975, and in a Scientific American article in 1977, with the batting averages of 18 major-league players after their first 45 at-bats of the 1970 season, which they used to predict the rest of the season. The demo simulates data of that shape and does not reproduce theirs. It also shrinks towards the players’ own grand mean, as they did. Estimating that mean costs one more player, so the multiplier is k − 3 and the guarantee begins at four; towards a fixed point it is k − 2 and begins at three. The theorem is exact for normal observations with a known common variance. Batting averages are binomial and their variance is estimated, so here it is a very good approximation, and the 2,000 seasons show how good.

The Bayesian reading removes the mystery. Suppose the players’ true abilities are themselves drawn from a distribution with mean μ and variance τ², and each average adds noise of variance σ². Then that distribution is a prior, and the posterior mean of each ability is the average pulled a fraction σ²/(σ² + τ²) of the way to μ. Nobody knows μ and τ², but twenty players reveal them: the scatter of the averages is about σ² + τ². Estimating the prior from the data is called empirical Bayes; giving μ and τ their own priors makes a hierarchical model. More at-bats make σ² smaller and call for less shrinkage, and a wider spread of talent does the same.

Shrinkage is regression to the mean put to work (see also: Regression): an extreme early average is partly ability and partly luck, and the luck will not repeat. The same idea is behind ridge regression and the “partial pooling” of multilevel models, which estimate a school or a hospital partly from its own data and partly from all the others.

Key Takeaways

  • Posterior ∝ prior × likelihood: a distribution for the unknown itself, so it can give the probability that a claim is true.
  • With enough data different priors lead to the same posterior, unless a prior gave the truth zero probability.
  • Markov chain Monte Carlo samples a posterior using only ratios of prior × likelihood; check the mixing before trusting the draws.
  • Estimating many things at once, pulling each towards the common mean lowers the total error, though not every individual one.

Spot a mistake or have an idea?

Search