Skip to content
BIASED

Bayesian linear regression

Block 1 of 7~2 min

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.

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