Skip to content
BIASED

Bayesian inference

The parameter as a random variable — prior times likelihood, the Beta–Binomial update with pseudo-counts, posterior mean versus MAP (and ridge regression as MAP), credible intervals, the posterior predictive, and why the prior fades as data accumulate.

Three heads and a single point

The topic Maximum likelihood gave a general recipe: choose the parameter under which the observed data are most probable. For a coin with unknown probability of heads θ\theta and kk heads in nn tosses, the recipe gives θ^=k/n\hat\theta = k/n, so 7 heads in 10 tosses give 0.70.7.

Now borrow a coin, toss it three times and get three heads. Maximum likelihood answers θ^=1\hat\theta = 1: tails is impossible. Nobody believes that. Before the first toss we already knew something about coins: most land heads about half the time, and two-headed ones are rare. The likelihood has no slot for this knowledge.

There is a second gap. The answer is a single number. It looks just as sure after 3 tosses as after 3000, and the asymptotic normality from the previous topic only speaks about large samples.

Bayesian inference closes both gaps with one move: the unknown parameter θ\theta is treated as a random variable. Its distribution before the data, the prior, holds what we knew. The data turn it into the posterior, and every answer is read off the posterior: a point estimate, an interval, a forecast for the next toss. This is the degree-of-belief reading of probability from the topic What probability is. The rules of probability stay exactly the same.

From prior to posterior

The topic Conditional probability and Bayes' formula turned a base rate of 0.010.01 for a disease into a probability of about 0.090.09 after a positive test. The base rate was a prior, and the test result updated it. Here the hypothesis is a value of θ\theta, and the evidence is the whole sample x=(x1,…,xn)x = (x_1, \dots, x_n). Bayes' formula for densities reads

p(θ∣x)=p(x∣θ) p(θ)p(x),p(x)=∫p(x∣θ) p(θ) dθ.p(\theta \mid x) = \frac{p(x \mid \theta)\, p(\theta)}{p(x)}, \qquad p(x) = \int p(x \mid \theta)\, p(\theta)\, d\theta.

Each piece has a name.

  • p(θ)p(\theta) is the prior: which values of θ\theta were plausible before the data.
  • p(x∣θ)=L(θ)p(x \mid \theta) = L(\theta) is the likelihood, the same function that maximum likelihood maximized.
  • p(θ∣x)p(\theta \mid x) is the posterior.
  • p(x)p(x) is the evidence, or marginal likelihood: the probability of the data averaged over the prior.

The evidence does not depend on θ\theta. So the shape of the posterior comes from the numerator alone:

p(θ∣x)∝L(θ) p(θ).p(\theta \mid x) \propto L(\theta)\, p(\theta).

In words: the posterior is likelihood times prior, rescaled to integrate to one.

A toy example shows the mechanics. Suppose the coin can only have θ∈{0.25,0.5,0.75}\theta \in \{0.25, 0.5, 0.75\}, and the prior gives these values probabilities 0.250.25, 0.50.5 and 0.250.25. One toss lands heads, so L(θ)=θL(\theta) = \theta.

θ\theta prior L(θ)L(\theta) prior × likelihood posterior
0.250.25 0.250.25 0.250.25 0.06250.0625 0.1250.125
0.50.5 0.50.5 0.50.5 0.250.25 0.50.5
0.750.75 0.250.25 0.750.75 0.18750.1875 0.3750.375

The fourth column sums to p(x)=0.5p(x) = 0.5, and dividing by it gives the posterior. One head shifted the mass toward larger θ\theta but ruled nothing out.

Beta in, Beta out

A real coin may have any θ∈[0,1]\theta \in [0, 1], so the prior needs a density on [0,1][0, 1]. The topic Continuous distributions introduced the natural family, the Beta distribution:

p(θ)=θa−1(1−θ)b−1B(a,b),a,b>0.p(\theta) = \frac{\theta^{a - 1} (1 - \theta)^{b - 1}}{B(a, b)}, \qquad a, b > 0.

The constant B(a,b)B(a, b) only makes the density integrate to one. For whole aa and bb it equals (a−1)! (b−1)!/(a+b−1)!(a - 1)!\,(b - 1)!/(a + b - 1)!. Beta⁡(1,1)\operatorname{Beta}(1, 1) is the uniform prior, and Beta⁡(2,2)\operatorname{Beta}(2, 2) is a low hump centred at 0.50.5: "probably near one half, but not sure".

Multiply the prior by the likelihood of kk heads in nn tosses, L(θ)=θk(1−θ)n−kL(\theta) = \theta^k (1 - \theta)^{n - k}, and keep only the factors that depend on θ\theta:

p(θ∣x)∝θa+k−1(1−θ)b+n−k−1,θ∣x∼Beta⁡(a+k, b+n−k).p(\theta \mid x) \propto \theta^{a + k - 1} (1 - \theta)^{b + n - k - 1}, \qquad \theta \mid x \sim \operatorname{Beta}(a + k,\ b + n - k).

The posterior is a Beta again. A prior family with this property for a given likelihood is called conjugate. The update needs no integral: every head adds one to aa, every tail adds one to bb. The uniform prior turns into Beta⁡(1+k,1+n−k)\operatorname{Beta}(1 + k, 1 + n - k). With the Beta⁡(2,2)\operatorname{Beta}(2, 2) prior, three heads in three tosses give Beta⁡(5,2)\operatorname{Beta}(5, 2). Seventy heads in a hundred tosses give Beta⁡(72,32)\operatorname{Beta}(72, 32).

Data often arrive in batches. If the tosses are independent given θ\theta, the posterior after one batch serves as the prior for the next, and the end result is the same as updating on all the data at once.

Why conjugate pairs exist, and for which likelihoods, is the subject of the topic Conjugate priors and the exponential family in the course Bayesian Methods in Machine Learning.

Pseudo-counts

The mean of Beta⁡(a,b)\operatorname{Beta}(a, b) is a/(a+b)a/(a + b). So the posterior mean of θ\theta is

E[θ∣x]=a+ka+b+n=a+ba+b+n⋅aa+b+na+b+n⋅kn.\mathbb E[\theta \mid x] = \frac{a + k}{a + b + n} = \frac{a + b}{a + b + n} \cdot \frac{a}{a + b} + \frac{n}{a + b + n} \cdot \frac{k}{n}.

The right-hand side is a weighted average of the prior mean a/(a+b)a/(a + b) and the MLE k/nk/n. The prior weighs as much as a+ba + b tosses, aa of them heads, so aa and bb act as pseudo-counts. The real data weigh nn.

With the Beta⁡(2,2)\operatorname{Beta}(2, 2) prior, three heads in three tosses give the posterior mean 5/7≈0.7145/7 \approx 0.714 instead of 11. Four imagined tosses still outweigh three real ones. After 70 heads in 100 the posterior mean is 72/104≈0.69272/104 \approx 0.692, close to the MLE 0.70.7. The pattern is general: the prior matters when data are few and fades as nn grows.

There is a second way to count, and it is worth knowing so as not to be confused. The density has exponents a−1a - 1 and b−1b - 1. Compared with the uniform Beta⁡(1,1)\operatorname{Beta}(1, 1), whose exponents are zero, the prior Beta⁡(2,2)\operatorname{Beta}(2, 2) multiplies in the likelihood of one extra head and one extra tail. Both readings are correct; they count from different baselines. In the posterior mean the prior's weight is a+ba + b.

Posterior mean or MAP

Sometimes one number is needed after all, and the posterior offers several. The posterior mean minimizes the expected squared error. The posterior mode is the maximum a posteriori (MAP) estimate. Taking the logarithm of likelihood times prior gives

θ^MAP=arg⁡max⁡θ(ℓ(θ)+log⁡p(θ)),\hat\theta_{\text{MAP}} = \arg\max_\theta \big( \ell(\theta) + \log p(\theta) \big),

the log-likelihood plus the log-prior. The evidence drops out because it does not depend on θ\theta. With a flat prior, log⁡p(θ)\log p(\theta) is constant, and MAP coincides with the MLE.

For Beta⁡(a,b)\operatorname{Beta}(a, b) with a,b>1a, b > 1 the mode is (a−1)/(a+b−2)(a - 1)/(a + b - 2). After 70 heads in 100 with the Beta⁡(2,2)\operatorname{Beta}(2, 2) prior, the posterior Beta⁡(72,32)\operatorname{Beta}(72, 32) has mode 71/102≈0.69671/102 \approx 0.696 and mean ≈0.692\approx 0.692. With many data the estimates nearly coincide. With few data the posterior is skewed, and its mean and mode drift apart.

The same formula explains a standard trick of machine learning. Take a linear model with inputs xix_i, targets yiy_i and Gaussian noise of variance σ2\sigma^2, and put the prior w∼N(0,τ2I)w \sim \mathcal N(0, \tau^2 I) on the weights. The negative log-posterior is

12σ2∑i=1n(yi−w⊤xi)2+12τ2∥w∥2+const.\frac{1}{2\sigma^2} \sum_{i=1}^{n} (y_i - w^\top x_i)^2 + \frac{1}{2\tau^2} \lVert w \rVert^2 + \text{const}.

Minimizing it is ridge regression, the least-squares loss plus a penalty λ∥w∥2\lambda \lVert w \rVert^2. Weight decay in neural networks is the same Gaussian prior. A Laplace prior p(wj)∝e−∣wj∣/cp(w_j) \propto e^{-\lvert w_j \rvert / c} gives the L1 penalty of the lasso. Regularization is a prior in disguise. A MAP estimate is still a single point, though: it throws away the width of the posterior.

Credible intervals

A point estimate hides how sure we are; the posterior shows it. A credible interval is a range that contains θ\theta with a stated posterior probability. The usual choice is central: cut off posterior mass 0.0250.025 in each tail. For Beta⁡(72,32)\operatorname{Beta}(72, 32), after 70 heads in 100 tosses, the central 95% credible interval is

P(0.601≤θ≤0.777∣x)=0.95.P(0.601 \le \theta \le 0.777 \mid x) = 0.95.

The endpoints are quantiles of the posterior, and any statistics library computes them, for example scipy.stats.beta(72, 32).ppf([0.025, 0.975]). After only three heads in three, the posterior Beta⁡(5,2)\operatorname{Beta}(5, 2) gives the much wider interval [0.359,0.957][0.359, 0.957]: three tosses say little.

The reading is direct. Given this prior and these data, θ\theta lies in the interval with probability 0.950.95. Since θ\theta is a random variable here, the sentence makes sense as stated.

The same posterior answers any other question about θ\theta. Does the coin favor heads? Compute P(θ>0.5∣x)P(\theta > 0.5 \mid x), the posterior mass to the right of one half. For Beta⁡(72,32)\operatorname{Beta}(72, 32) it is about 0.999970.99997.

Predicting the next toss

Often the question is not about θ\theta at all but about the next observation. The posterior predictive distribution averages the model's prediction over all values of θ\theta, each weighted by its posterior density:

p(xnew∣x)=∫p(xnew∣θ) p(θ∣x) dθ.p(x_{\text{new}} \mid x) = \int p(x_{\text{new}} \mid \theta)\, p(\theta \mid x)\, d\theta.

For one toss P(xnew=1∣θ)=θP(x_{\text{new}} = 1 \mid \theta) = \theta, so the probability that the next toss is heads is ∫θ p(θ∣x) dθ\int \theta\, p(\theta \mid x)\, d\theta, the posterior mean. After three heads in three with the Beta⁡(2,2)\operatorname{Beta}(2, 2) prior it is 5/7≈0.7145/7 \approx 0.714. The plug-in forecast, which substitutes the MLE for θ\theta, says 11 and calls tails impossible. Averaging keeps the uncertainty about θ\theta inside the forecast. A forecast for several future tosses works the same way: the integral then contains the probability of the whole future sequence given θ\theta.

You may have used this average without the name. A spam filter that estimates the share of spam emails containing a word as (count+1)/(emails+2)(\text{count} + 1)/(\text{emails} + 2) uses the posterior mean under a uniform prior. This is add-one, or Laplace, smoothing: a word never seen in spam does not get probability zero.

Conjugate pairs are the exception, not the rule. For logistic regression or a neural network, the integrals in p(x)p(x) and in the predictive have no formula. Then the posterior is sampled, which the last topic of this course, Markov chain Monte Carlo, shows how to do, or it is approximated by variational inference or the Laplace approximation. The course Bayesian Methods in Machine Learning covers all of these.

When the prior fades

The prior is the most criticized part of the method: two analysts with different priors get different posteriors from the same data. With few data this effect is real, so the prior has to be stated and checked. A useful check is to vary it and see how far the conclusions move. A weakly informative prior encodes only scale and plausibility, such as "a coin is not two-headed".

With many data the prior fades. Its pseudo-counts a+ba + b stay fixed while nn grows. Under regularity conditions the Bernstein–von Mises theorem makes this precise: the posterior approaches the normal distribution

N(θ^MLE, 1nI(θ^MLE)),\mathcal N\Big(\hat\theta_{\text{MLE}},\ \frac{1}{n I(\hat\theta_{\text{MLE}})}\Big),

the same one as in the asymptotic normality of the MLE from the topic Maximum likelihood. For the coin I(θ)=1/(θ(1−θ))I(\theta) = 1/(\theta(1 - \theta)). After 70 heads in 100 the standard deviation is 0.7⋅0.3/100≈0.046\sqrt{0.7 \cdot 0.3 / 100} \approx 0.046, and the interval 0.7±1.96⋅0.046=[0.61,0.79]0.7 \pm 1.96 \cdot 0.046 = [0.61, 0.79] is close to the credible interval [0.601,0.777][0.601, 0.777].

The numbers nearly agree, but the readings do not. The credible interval is a probability statement about θ\theta, given these data and a prior. The interval around the MLE can be read with no prior at all, as a statement about a procedure: over repeated samples, intervals built this way cover the true θ\theta in 95% of cases. The next topic, Confidence intervals and the bootstrap, builds such intervals, explains this reading carefully, and shows how the bootstrap builds them from the data alone when no formula is at hand.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.