Skip to content
BIASED

Bayesian linear regression

A Gaussian prior on the weights and Gaussian noise make everything exact: the posterior $\mathcal N(m_N, S_N)$, the link to ridge regression, predictions with honest error bars, the evidence of the model and choosing $\alpha$ and $\beta$ from the data.

The model: noise, features and a prior

The topic Bayesian model selection left us with a tool, the evidence, and a promise to use it on the workhorse of machine learning. Linear regression is the right place: with Gaussian noise and a Gaussian prior every integral in it is taken in closed form. The posterior, the predictive distribution and the evidence all come out exactly, so nothing has to be approximated yet.

Each object xix_i is turned into a feature vector ϕ(xi)∈Rd\phi(x_i) \in \mathbb R^d: the raw features, a constant and xx, powers of xx, any fixed functions. The rows ϕ(xi)⊤\phi(x_i)^\top form the design matrix Φ∈Rn×d\Phi \in \mathbb R^{n \times d}; for plain linear regression Φ=X\Phi = X. The targets are

ti=w⊤ϕ(xi)+εi,εi∼N(0,β−1),t_i = w^\top \phi(x_i) + \varepsilon_i, \qquad \varepsilon_i \sim \mathcal N(0, \beta^{-1}),

with independent noise terms, so the likelihood is p(t∣w,β)=N(t∣Φw,β−1In)p(t \mid w, \beta) = \mathcal N(t \mid \Phi w, \beta^{-1} I_n). The model is linear in ww, not in xx: a cubic in xx is still linear regression.

The prior is the one from The Bayesian approach, where its MAP estimate turned out to be the L2 penalty:

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

Both α\alpha and β\beta are precisions. The noise variance is β−1\beta^{-1}, and the prior variance of each weight is α−1\alpha^{-1}. A large α\alpha is a tight prior that holds the weights near zero; a large β\beta means we trust each target.

Running example. It stays with us for this topic and the next one. Three points and a line with an intercept:

x=(−1, 0, 1),t=(−1, 1, 2),ϕ(x)=(1,x)⊤,α=2,β=1.x = (-1,\, 0,\, 1), \quad t = (-1,\, 1,\, 2), \quad \phi(x) = (1, x)^\top, \quad \alpha = 2, \quad \beta = 1.

Then

Φ=(1−11011),Φ⊤Φ=(3002),Φ⊤t=(23).\Phi = \begin{pmatrix} 1 & -1 \\ 1 & 0 \\ 1 & 1 \end{pmatrix}, \qquad \Phi^\top\Phi = \begin{pmatrix} 3 & 0 \\ 0 & 2 \end{pmatrix}, \qquad \Phi^\top t = \begin{pmatrix} 2 \\ 3 \end{pmatrix}.

The columns of Φ\Phi are orthogonal, so every matrix below stays diagonal and the arithmetic fits on a napkin. Least squares gives w^ML=(Φ⊤Φ)−1Φ⊤t=(2/3,  3/2)⊤\hat w_{\text{ML}} = (\Phi^\top\Phi)^{-1}\Phi^\top t = (2/3,\; 3/2)^\top.

The posterior over the weights

The posterior is proportional to the likelihood times the prior. Take the logarithm and drop everything that does not depend on ww:

log⁡p(w∣t,α,β)=−12[β∥t−Φw∥2+α w⊤w]+const.\log p(w \mid t, \alpha, \beta) = -\frac{1}{2}\Big[\beta \lVert t - \Phi w \rVert^2 + \alpha\, w^\top w\Big] + \text{const}.

Each term carries its own precision. Some lecture notes pull a common factor β\beta out of the whole bracket; that is a typo, because the prior knows nothing about the noise.

The bracket is quadratic in ww, so the posterior is Gaussian. Expand it:

w⊤(αI+βΦ⊤Φ)w−2β w⊤Φ⊤t+β t⊤t.w^\top\big(\alpha I + \beta \Phi^\top\Phi\big) w - 2\beta\, w^\top \Phi^\top t + \beta\, t^\top t.

The exponent of N(m,S)\mathcal N(m, S) is −12(w−m)⊤S−1(w−m)=−12[w⊤S−1w−2w⊤S−1m]+const-\frac12 (w - m)^\top S^{-1}(w - m) = -\frac12 \big[w^\top S^{-1} w - 2 w^\top S^{-1} m\big] + \text{const}. Matching the quadratic and the linear terms gives

p(w∣t,α,β)=N(w∣mN,SN),SN−1=αI+βΦ⊤Φ,mN=βSNΦ⊤t.p(w \mid t, \alpha, \beta) = \mathcal N(w \mid m_N, S_N), \qquad S_N^{-1} = \alpha I + \beta \Phi^\top\Phi, \qquad m_N = \beta S_N \Phi^\top t.

The first formula is the familiar "precisions add" from the Normal mean in Conjugate priors and the exponential family: the prior precision αI\alpha I plus the data precision βΦ⊤Φ=β∑iϕ(xi)ϕ(xi)⊤\beta\Phi^\top\Phi = \beta\sum_i \phi(x_i)\phi(x_i)^\top. Each new point adds its own term βϕϕ⊤\beta\phi\phi^\top and sharpens the posterior along the direction ϕ\phi it measures.

Sequential updating works as before. With a prior N(m0,S0)\mathcal N(m_0, S_0) the same algebra gives SN−1=S0−1+βΦ⊤ΦS_N^{-1} = S_0^{-1} + \beta\Phi^\top\Phi and mN=SN(S0−1m0+βΦ⊤t)m_N = S_N\big(S_0^{-1}m_0 + \beta\Phi^\top t\big), so today's posterior is tomorrow's prior.

In the running example:

SN−1=(2+3002+2),SN=(1/5001/4),mN=SN(23)=(0.40.75).S_N^{-1} = \begin{pmatrix} 2 + 3 & 0 \\ 0 & 2 + 2 \end{pmatrix}, \qquad S_N = \begin{pmatrix} 1/5 & 0 \\ 0 & 1/4 \end{pmatrix}, \qquad m_N = S_N \begin{pmatrix} 2 \\ 3 \end{pmatrix} = \begin{pmatrix} 0.4 \\ 0.75 \end{pmatrix}.

Least squares said (0.67, 1.5)(0.67,\, 1.5). The prior pulls both weights toward zero, and with three points and a fairly tight prior (standard deviation 1/2≈0.711/\sqrt2 \approx 0.71) it pulls hard. At the end of the topic we will ask the data whether α=2\alpha = 2 was a sensible choice.

The posterior mean is ridge regression

Factor β\beta out of SN−1S_N^{-1}:

mN=(Φ⊤Φ+αβI)−1Φ⊤t.m_N = \Big(\Phi^\top\Phi + \frac{\alpha}{\beta} I\Big)^{-1}\Phi^\top t.

This is ridge regression with λ=α/β\lambda = \alpha/\beta. The Bayesian approach already showed that a Gaussian prior gives an L2 penalty; now the coefficient is explicit: the noise variance divided by the prior variance. A Gaussian is symmetric with a single peak, so its mode and its mean coincide: mNm_N is the MAP estimate and the posterior mean at once.

What the formula says:

  • Only the ratio α/β\alpha/\beta enters mNm_N. SNS_N depends on both precisions separately.
  • As α→0\alpha \to 0 (a flat prior), mNm_N tends to least squares, provided Φ⊤Φ\Phi^\top\Phi is invertible. As nn grows, βΦ⊤Φ\beta\Phi^\top\Phi grows with it while α\alpha stays fixed, so the prior washes out.
  • Every weight is shrunk, the intercept included. In the running example the intercept's posterior mean is β∑iti/(α+3β)=35 tˉ\beta \sum_i t_i/(\alpha + 3\beta) = \tfrac{3}{5}\,\bar t instead of tˉ\bar t. Shift all targets by a constant and the predictions move by only three fifths of it. In practice one centres the targets or gives the intercept its own, much weaker prior.

So far the Bayesian answer is ridge regression. The difference shows up in the next blocks. Ridge returns a point; the posterior also returns SNS_N, and with it error bars for predictions and a way to choose λ\lambda from the training data alone.

Prediction: noise plus uncertainty about the weights

For a new input x∗x_* with features ϕ∗=ϕ(x∗)\phi_* = \phi(x_*) we want the target, not the weights:

p(t∗∣x∗,t)=∫p(t∗∣w,x∗) p(w∣t) dw.p(t_* \mid x_*, t) = \int p(t_* \mid w, x_*)\, p(w \mid t)\, dw.

The integral can be skipped. Write the posterior weights as w=mN+δw = m_N + \delta with δ∼N(0,SN)\delta \sim \mathcal N(0, S_N), and the new noise as ε∗∼N(0,β−1)\varepsilon_* \sim \mathcal N(0, \beta^{-1}), independent of δ\delta:

t∗=mN⊤ϕ∗+ϕ∗⊤δ+ε∗.t_* = m_N^\top\phi_* + \phi_*^\top\delta + \varepsilon_*.

A linear combination of independent Gaussians is Gaussian; means add and variances add:

p(t∗∣x∗,t)=N(t∗∣mN⊤ϕ∗,  σ2(x∗)),σ2(x∗)=1β+ϕ∗⊤SNϕ∗.p(t_* \mid x_*, t) = \mathcal N\big(t_* \mid m_N^\top\phi_*,\; \sigma^2(x_*)\big), \qquad \sigma^2(x_*) = \frac{1}{\beta} + \phi_*^\top S_N \phi_*.

There are two sources of uncertainty. The first term is the noise of the new observation; it would remain even if we knew ww exactly. The second term is our uncertainty about ww. For the noise-free value f∗=w⊤ϕ∗f_* = w^\top\phi_*, the regression line itself, only the second term remains.

Plugging in mNm_N and keeping 1/β1/\beta loses the second term. That is the gap between MAP and the posterior predictive from The Bayesian approach, now with a formula attached.

In the running example ϕ∗=(1,x∗)⊤\phi_* = (1, x_*)^\top and SNS_N is diagonal, so ϕ∗⊤SNϕ∗=1/5+x∗2/4\phi_*^\top S_N\phi_* = 1/5 + x_*^2/4:

  • at x∗=0x_* = 0 the mean is 0.40.4 and the variance is 1+0.2=1.21 + 0.2 = 1.2;
  • at x∗=2x_* = 2 the mean is 0.4+2⋅0.75=1.90.4 + 2 \cdot 0.75 = 1.9 and the variance is 1+0.2+1=2.21 + 0.2 + 1 = 2.2.

Why uncertainty grows away from the data

In the running example the weight term 1/5+x∗2/41/5 + x_*^2/4 is a parabola: smallest at the centre of the data and growing quadratically outside it. The general picture comes from an eigendecomposition. Let βΦ⊤Φ\beta\Phi^\top\Phi have eigenvalues λj\lambda_j and orthonormal eigenvectors uju_j. Then SN−1=αI+βΦ⊤ΦS_N^{-1} = \alpha I + \beta\Phi^\top\Phi has the same eigenvectors with eigenvalues α+λj\alpha + \lambda_j, and

ϕ∗⊤SNϕ∗=∑j=1d(uj⊤ϕ∗)2α+λj.\phi_*^\top S_N\phi_* = \sum_{j=1}^{d} \frac{(u_j^\top\phi_*)^2}{\alpha + \lambda_j}.

A direction the data measured well (large λj\lambda_j) contributes little. A direction the data did not measure (λj≈0\lambda_j \approx 0) keeps the prior variance 1/α1/\alpha. Far from the data, ϕ∗\phi_* points largely along such poorly measured directions, and the band widens.

Three consequences.

  • Noise is irreducible. As data from the same range keep arriving, every λj\lambda_j grows in proportion to nn, the weight term goes to zero and σ2(x∗)→1/β\sigma^2(x_*) \to 1/\beta.
  • Error bars ignore the targets. SNS_N contains no tt: the width depends on where the inputs are and on α\alpha, β\beta, not on how well the line fits. The fit enters only once β\beta itself is learned from the residuals, in the last block.
  • Error bars are only as good as the features. Take localized features, Gaussian bumps exp⁡(−(x−ck)2/(2s2))\exp\big(-(x - c_k)^2/(2s^2)\big) with centres ckc_k inside the data. Far from every centre ϕ∗≈0\phi_* \approx 0, so the mean falls back to 00 and the weight term vanishes. The model is most confident exactly where it knows nothing. Gaussian processes, later in the course, fix this by putting the prior on functions instead of on a fixed set of bumps.

The evidence of the linear model

In Bayesian model selection the evidence scored a model by how well it predicted the data before seeing them. For linear regression it is available in closed form. Under the model, t=Φw+εt = \Phi w + \varepsilon with independent w∼N(0,α−1Id)w \sim \mathcal N(0, \alpha^{-1} I_d) and ε∼N(0,β−1In)\varepsilon \sim \mathcal N(0, \beta^{-1} I_n). A linear map of Gaussians is Gaussian, so

p(t∣α,β)=N(t∣0,C),C=β−1In+α−1ΦΦ⊤,p(t \mid \alpha, \beta) = \mathcal N(t \mid 0, C), \qquad C = \beta^{-1} I_n + \alpha^{-1}\Phi\Phi^\top, log⁡p(t∣α,β)=−12[nlog⁡2π+log⁡det⁡C+t⊤C−1t].\log p(t \mid \alpha, \beta) = -\frac12\Big[n\log 2\pi + \log\det C + t^\top C^{-1} t\Big].

CC is n×nn \times n, one row per object; SNS_N is d×dd \times d, one row per weight. They are different matrices. The two terms pull in opposite directions: t⊤C−1tt^\top C^{-1}t rewards a model that can explain tt, and log⁡det⁡C\log\det C penalizes a model that spreads its probability over too many possible datasets. This is the automatic Occam's razor from that topic.

When n≫dn \gg d it is cheaper to stay in weight space. The same quantity equals

log⁡p(t∣α,β)=d2log⁡α+n2log⁡β−E(mN)−12log⁡det⁡SN−1−n2log⁡2π,\log p(t \mid \alpha, \beta) = \frac{d}{2}\log\alpha + \frac{n}{2}\log\beta - E(m_N) - \frac12\log\det S_N^{-1} - \frac{n}{2}\log 2\pi, E(w)=β2∥t−Φw∥2+α2∥w∥2.E(w) = \frac{\beta}{2}\lVert t - \Phi w\rVert^2 + \frac{\alpha}{2}\lVert w\rVert^2.

Here −E(mN)-E(m_N) is the fit at the best weights, and d2log⁡α−12log⁡det⁡SN−1\frac{d}{2}\log\alpha - \frac12\log\det S_N^{-1} is the log Occam factor, the log of the ratio of posterior volume to prior volume. It is never positive, because every eigenvalue of SN−1S_N^{-1} is at least α\alpha. The constants αd/2\alpha^{d/2} and βn/2\beta^{n/2} come from normalizing the prior and the likelihood. They are easy to lose, and without them the evidence depends on α\alpha and β\beta in the wrong way.

In the running example

C=I3+12 ΦΦ⊤=(20.500.51.50.500.52),log⁡p(t∣α=2,β=1)≈−5.04,C = I_3 + \tfrac12\,\Phi\Phi^\top = \begin{pmatrix} 2 & 0.5 & 0 \\ 0.5 & 1.5 & 0.5 \\ 0 & 0.5 & 2 \end{pmatrix}, \qquad \log p(t \mid \alpha = 2, \beta = 1) \approx -5.04,

and the weight-space formula gives the same number.

Choosing $\alpha$ and $\beta$ by the evidence

Now treat the evidence as a function of α\alpha and β\beta and maximize it. This is empirical Bayes from Bayesian model selection: the weights are integrated out, the hyperparameters are point estimates.

Use the eigenvalues λj\lambda_j of βΦ⊤Φ\beta\Phi^\top\Phi from the previous block; then log⁡det⁡SN−1=∑jlog⁡(α+λj)\log\det S_N^{-1} = \sum_j\log(\alpha + \lambda_j). Differentiate the weight-space form with respect to α\alpha. The dependence through mNm_N drops out, because mNm_N minimizes EE:

d2α−12∥mN∥2−12∑j1α+λj=0⟹α∥mN∥2=∑jλjα+λj.\frac{d}{2\alpha} - \frac12\lVert m_N\rVert^2 - \frac12\sum_j\frac{1}{\alpha + \lambda_j} = 0 \quad\Longrightarrow\quad \alpha\lVert m_N\rVert^2 = \sum_j \frac{\lambda_j}{\alpha + \lambda_j}.

The derivative with respect to β\beta is similar (each λj\lambda_j is proportional to β\beta). Together they give the fixed-point updates

γ=∑j=1dλjα+λj,α←γ∥mN∥2,1β←∥t−ΦmN∥2n−γ.\gamma = \sum_{j=1}^{d}\frac{\lambda_j}{\alpha + \lambda_j}, \qquad \alpha \leftarrow \frac{\gamma}{\lVert m_N\rVert^2}, \qquad \frac{1}{\beta} \leftarrow \frac{\lVert t - \Phi m_N\rVert^2}{n - \gamma}.

γ\gamma is the effective number of parameters. Each of its terms lies between 00 and 11: close to 11 for a direction the data determine well (λj≫α\lambda_j \gg \alpha), close to 00 where the prior dominates. The β\beta update is the familiar unbiased noise-variance estimate with n−dn - d replaced by n−γn - \gamma. Since mNm_N and γ\gamma depend on α\alpha and β\beta, we iterate: posterior, then γ\gamma, then new hyperparameters, then the posterior again.

In the running example at α=2\alpha = 2, β=1\beta = 1: λ=(3,2)\lambda = (3, 2), so γ=3/5+2/4=1.1\gamma = 3/5 + 2/4 = 1.1, and ∥mN∥2=0.16+0.5625=0.7225\lVert m_N\rVert^2 = 0.16 + 0.5625 = 0.7225. The update α←1.1/0.7225≈1.52\alpha \leftarrow 1.1/0.7225 \approx 1.52 says the prior was too tight. Run to convergence, the iteration reaches α^≈0.79\hat\alpha \approx 0.79, β^≈5.9\hat\beta \approx 5.9 and γ≈1.89\gamma \approx 1.89: both weights are now well determined. The log evidence rises from −5.04-5.04 to −4.55-4.55.

Two caveats. With three points these estimates are rough, and in general the evidence need not be concave in the hyperparameters, so the iteration finds a local maximum. A fully Bayesian treatment would put priors on α\alpha and β\beta as well.

One α\alpha for all weights treats every feature alike: an irrelevant feature is shrunk exactly as much as a useful one. Give each weight its own precision αi\alpha_i and maximize the evidence again. The precisions of useless weights run off to infinity, their posteriors collapse onto zero, and those features switch off. That is the topic The relevance vector machine.

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.