Skip to content
BIASED

The Bayesian approach

A coin lands heads three times in a row. On this example: prior, likelihood, posterior and evidence; why the MLE is overconfident, where the prediction 4/5 comes from, how MAP differs from full inference, why a Gaussian prior is an L2 penalty, and how to update beliefs batch by batch.

Parameters are uncertain too

One question runs through this course: how do we reason and predict when the parameters of a model are themselves uncertain, and what do we do when the exact computation is out of reach? The first half answers the first part exactly. The second half builds approximations and scales them to big data and neural networks. This topic sets up the vocabulary on the smallest example there is.

A friend tosses a coin three times and gets three heads. Let xi∈{0,1}x_i \in \{0, 1\} be the outcome of toss ii, with 11 for heads, and let θ\theta be the probability of heads. The model says the tosses are Bernoulli⁡(θ)\operatorname{Bernoulli}(\theta) and independent given θ\theta. A sequence with ss heads in nn tosses then has probability

p(X∣θ)=θs(1−θ)n−s.p(X \mid \theta) = \theta^{s} (1 - \theta)^{n - s}.

The frequentist reading treats θ\theta as a fixed unknown constant. Only the data are random, and an estimate is judged by how it would behave over many imagined repetitions of the experiment. The standard estimate is the maximum likelihood estimate (MLE): the θ\theta that maximizes the log-likelihood

ℓ(θ)=log⁡p(X∣θ)=slog⁡θ+(n−s)log⁡(1−θ).\ell(\theta) = \log p(X \mid \theta) = s \log\theta + (n - s) \log(1 - \theta).

The logarithm does not move the maximum and turns products into sums. For our three heads out of three, ℓ(θ)=3log⁡θ\ell(\theta) = 3\log\theta keeps growing all the way to θ=1\theta = 1, so θ^ML=1\hat\theta_{\text{ML}} = 1. The model is now certain that tails is impossible. Three tosses do not justify that.

The Bayesian reading uses probability for a degree of belief given the available information. Then nothing stops us from giving θ\theta itself a distribution: it describes what we know and do not know about this coin. The rules of probability are the same in both readings; what changes is which quantities get distributions. Two people with different information may reasonably hold different probabilities for the same event: someone who has seen the coin's previous hundred tosses knows more than we do.

With plenty of data and a correct model the two readings mostly agree. They differ most on small samples like ours.

Bayes' formula

The product rule writes the joint distribution of data and parameter in two ways:

p(X,θ)=p(X∣θ) p(θ)=p(θ∣X) p(X).p(X, \theta) = p(X \mid \theta)\, p(\theta) = p(\theta \mid X)\, p(X).

Dividing by p(X)p(X) gives Bayes' formula:

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.

For a discrete θ\theta the integral is a sum. Four pieces, four names:

  • p(θ)p(\theta) is the prior: what we considered plausible before seeing XX.
  • p(X∣θ)p(X \mid \theta) is the likelihood: how well each value of θ\theta explains the data we have. As a function of XX it is a distribution. As a function of θ\theta, with XX fixed, it is not.
  • p(θ∣X)p(\theta \mid X) is the posterior: our uncertainty about θ\theta after seeing XX.
  • p(X)p(X) is the evidence, or marginal likelihood: the probability of the data averaged over the prior. It does not depend on θ\theta, so for the posterior it is a normalizing constant in the denominator. The topic Bayesian model selection will use it to compare models.

In short, p(θ∣X)∝p(X∣θ) p(θ)p(\theta \mid X) \propto p(X \mid \theta)\, p(\theta): the posterior is likelihood times prior, renormalized.

Why the conditional p(θ∣X)p(\theta \mid X) and not the joint p(θ,X)p(\theta, X)? The data are already observed, and we want beliefs about θ\theta given them. The joint also describes uncertainty about XX and is normalized over both arguments at once.

A discrete warm-up. Suppose the friend's coin is either fair, θ=0.5\theta = 0.5, or a trick coin with θ=0.9\theta = 0.9, and before the tosses you give the trick coin probability 0.20.2. After three heads:

coin prior likelihood θ3\theta^3 prior × likelihood
fair, θ=0.5\theta = 0.5 0.80.8 0.1250.125 0.10.1
trick, θ=0.9\theta = 0.9 0.20.2 0.7290.729 0.14580.1458

The evidence is the sum of the last column, p(X)=0.2458p(X) = 0.2458. The posterior probability of the trick coin is 0.1458/0.2458≈0.5930.1458 / 0.2458 \approx 0.593. Three heads took it from one in five to more than a half.

The posterior of a coin

Now let θ\theta be any number in [0,1][0, 1]. We know nothing about the coin in advance, so we take the uniform prior p(θ)=1p(\theta) = 1 on [0,1][0, 1]. For three heads Bayes' formula gives

p(θ∣X)=θ3⋅1∫01u3 du=θ31/4=4θ3.p(\theta \mid X) = \frac{\theta^3 \cdot 1}{\int_0^1 u^3\, du} = \frac{\theta^3}{1/4} = 4\theta^3.

The evidence is p(X)=1/4p(X) = 1/4: averaged over all possible coins, the uniform model gave three heads a one-in-four chance. A fair coin alone gives (1/2)3=1/8(1/2)^3 = 1/8. Comparing such numbers is how the topic Bayesian model selection chooses between models.

The posterior 4θ34\theta^3 belongs to a standard family. The Beta distribution on [0,1][0, 1] with parameters a,b>0a, b > 0 has density

Beta⁡(θ∣a,b)=θa−1(1−θ)b−1B(a,b),B(a,b)=Γ(a) Γ(b)Γ(a+b).\operatorname{Beta}(\theta \mid a, b) = \frac{\theta^{a - 1} (1 - \theta)^{b - 1}}{B(a, b)}, \qquad B(a, b) = \frac{\Gamma(a)\, \Gamma(b)}{\Gamma(a + b)}.

Its mean is a/(a+b)a/(a + b), and its mode is (a−1)/(a+b−2)(a - 1)/(a + b - 2) when a,b≥1a, b \ge 1 and a+b>2a + b > 2. The uniform prior is Beta⁡(1,1)\operatorname{Beta}(1, 1).

Here is the trick that saves us from integrals. Keep only the factors that depend on θ\theta, here θ3\theta^3, and match them with θa−1(1−θ)b−1\theta^{a - 1}(1 - \theta)^{b - 1}: we get a=4a = 4 and b=1b = 1. So the posterior is Beta⁡(4,1)\operatorname{Beta}(4, 1), and its normalizing constant comes from the family's formula: B(4,1)=3! 0!/4!=1/4B(4, 1) = 3!\, 0! / 4! = 1/4. Since the prior was one, this constant is exactly the evidence we computed above.

Predicting by averaging over the parameter

What will the next toss show? The Bayesian answer averages the predictions of every θ\theta, weighted by how plausible that θ\theta is now:

p(x∗∣X)=∫p(x∗∣θ) p(θ∣X) dθ.p(x^* \mid X) = \int p(x^* \mid \theta)\, p(\theta \mid X)\, d\theta.

This is the posterior predictive distribution. For the coin p(x∗=1∣θ)=θp(x^* = 1 \mid \theta) = \theta, so the predictive probability of heads is the posterior mean of θ\theta. After three heads:

p(x∗=1∣X)=∫01θ⋅4θ3 dθ=45.p(x^* = 1 \mid X) = \int_0^1 \theta \cdot 4\theta^3\, d\theta = \frac45.

Compare it with plug-in prediction: pick one value θ^\hat\theta and predict with p(x∗∣θ^)p(x^* \mid \hat\theta). With θ^ML=1\hat\theta_{\text{ML}} = 1 the plug-in says heads with probability 11 and tails with probability 00. If the fourth toss is tails, the plug-in's log loss −log⁡0-\log 0 is infinite. The predictive pays −log⁡(1/5)≈1.61-\log(1/5) \approx 1.61 nats. Averaging keeps the uncertainty about θ\theta, so the predictive is never more confident than the data allow.

Supervised learning uses the same form. With weights ww trained on inputs XX and targets TT, the prediction for a new input x∗x^* is

p(t∗∣x∗,X,T)=∫p(t∗∣x∗,w) p(w∣X,T) dw.p(t^* \mid x^*, X, T) = \int p(t^* \mid x^*, w)\, p(w \mid X, T)\, dw.

The topic Bayesian linear regression computes this integral exactly for a linear model.

MAP and other point estimates

Sometimes we need one number after all. The maximum a posteriori (MAP) estimate takes the peak of the posterior:

θ^MAP=arg⁡max⁡θp(θ∣X)=arg⁡max⁡θ[∑i=1nlog⁡p(xi∣θ)+log⁡p(θ)].\hat\theta_{\text{MAP}} = \arg\max_\theta p(\theta \mid X) = \arg\max_\theta \Big[ \sum_{i=1}^{n} \log p(x_i \mid \theta) + \log p(\theta) \Big].

Two details matter here. The evidence p(X)p(X) does not depend on θ\theta, so it drops out of the arg⁡max⁡\arg\max, but only there: p(θ∣X)=p(X∣θ) p(θ)p(\theta \mid X) = p(X \mid \theta)\, p(\theta) without the denominator is false. And the prior enters once for the whole sample, not once per observation.

With the uniform prior, log⁡p(θ)\log p(\theta) is constant on [0,1][0, 1], so MAP coincides with the MLE: after three heads θ^MAP=1\hat\theta_{\text{MAP}} = 1, the mode of Beta⁡(4,1)\operatorname{Beta}(4, 1). The predictive gave 4/54/5. So MAP alone does not cure overconfidence; the averaging does.

Which single number is "best" depends on what a wrong estimate costs, the loss function. The posterior mean minimizes the expected squared error (θ−θ^)2(\theta - \hat\theta)^2. The posterior median minimizes the expected absolute error ∣θ−θ^∣\lvert\theta - \hat\theta\rvert. The mode is what you get for the 0–1 loss, hit or miss. For Beta⁡(4,1)\operatorname{Beta}(4, 1) they are 11 for the mode, 2−1/4≈0.842^{-1/4} \approx 0.84 for the median and 0.80.8 for the mean.

Every point estimate throws something away: the width of the posterior, its skew, a second mode. The highest point of a density need not be where most of its mass is. A point estimate also cannot serve as the prior for the next batch of data, and comparing models by their best fit always favors the bigger model. The full posterior keeps all of this.

A Gaussian prior is L2 regularization

Now let the parameter be a weight vector w∈Rdw \in \mathbb R^d, of a linear model or a neural network. A common prior makes the weights independent Gaussians centred at zero:

p(w)=N(w∣0,α−1I),p(w) = \mathcal N(w \mid 0, \alpha^{-1} I),

where α\alpha is the prior precision, the inverse variance. Its negative logarithm is a scaled squared norm:

−log⁡p(w)=α2∥w∥2+d2log⁡2πα=α2∥w∥2+const.-\log p(w) = \frac{\alpha}{2} \lVert w \rVert^2 + \frac{d}{2} \log\frac{2\pi}{\alpha} = \frac{\alpha}{2} \lVert w \rVert^2 + \text{const}.

The constant does not depend on ww, so MAP becomes

w^MAP=arg⁡min⁡w[−∑i=1nlog⁡p(xi∣w)+α2∥w∥2],\hat w_{\text{MAP}} = \arg\min_w \Big[ -\sum_{i=1}^{n} \log p(x_i \mid w) + \frac{\alpha}{2} \lVert w \rVert^2 \Big],

the negative log-likelihood plus an L2 penalty. Weight decay and ridge regression are MAP estimates under a Gaussian prior. The precision sets the strength: a large α\alpha means a narrow prior that pulls the weights toward zero, and as α→0\alpha \to 0 the prior flattens and MAP turns into the MLE. In the same way, a Laplace prior p(wj)∝e−∣wj∣/cp(w_j) \propto e^{-\lvert w_j \rvert / c} gives an L1 penalty.

This also shows what regularization does not do. A regularized model is still a point estimate and still predicts by plug-in. Replacing it with a full posterior over ww is what the topics Bayesian linear regression, The Laplace approximation and Bayesian classification and, for neural networks, Bayesian deep learning do.

Sequential updating and what it costs

Data often arrive in portions. Let D1D_1 and D2D_2 be two batches that are conditionally independent given θ\theta: p(D1,D2∣θ)=p(D1∣θ) p(D2∣θ)p(D_1, D_2 \mid \theta) = p(D_1 \mid \theta)\, p(D_2 \mid \theta). Then

p(θ∣D1,D2)∝p(D2∣θ) p(D1∣θ) p(θ)∝p(D2∣θ) p(θ∣D1).p(\theta \mid D_1, D_2) \propto p(D_2 \mid \theta)\, p(D_1 \mid \theta)\, p(\theta) \propto p(D_2 \mid \theta)\, p(\theta \mid D_1).

Yesterday's posterior is today's prior. The result does not depend on how the data were split or in which order the batches came: it equals the posterior from all the data at once. For the coin, after three heads the posterior is Beta⁡(4,1)∝θ3\operatorname{Beta}(4, 1) \propto \theta^3. A fourth toss lands tails; multiplying by 1−θ1 - \theta gives θ3(1−θ)\theta^3 (1 - \theta), which is Beta⁡(4,2)\operatorname{Beta}(4, 2), the same as for all four tosses at once. This is how a Bayesian model learns online. Note that it needs the full posterior: from θ^MAP=1\hat\theta_{\text{MAP}} = 1 alone there is no way to continue.

The condition is independence given θ\theta. Whether the tosses are also independent when θ\theta is unknown is a separate question.

Where it breaks. Bayes' formula is exact for the model you wrote down, but it cannot repair a wrong model: with a misspecified likelihood the posterior confidently concentrates on the wrong answer. And it has a price. The prior has to be chosen and checked, and every step above needs an integral: the evidence ∫p(X∣θ) p(θ) dθ\int p(X \mid \theta)\, p(\theta)\, d\theta and the predictive ∫p(x∗∣θ) p(θ∣X) dθ\int p(x^* \mid \theta)\, p(\theta \mid X)\, d\theta. With mm binary unknowns, even a sum by brute-force enumeration has 2m2^m terms.

For the coin we were lucky: the likelihood θs(1−θ)n−s\theta^s (1 - \theta)^{n - s} times a uniform prior gave a Beta again, and every integral came from the Beta family's formula. When does this luck repeat, and which priors keep the posterior in their own family for which likelihoods? That is the next topic, Conjugate priors and the exponential family.

Check your understanding

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

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Beta–Binomial posterior after headsoptional

    Update Beta(α,β) after h heads and t tails; match mean and a 4/5-style predictive.

    ~20 min · easier on a computer · The matching course topic has been read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

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