Course contents 04 / 15

04 / 15 · Bayesian Linear Models

Bayesian linear regression

A Gaussian prior on the weights of a linear model gives a Gaussian posterior whose mean is the ridge estimate, a predictive with error bars, and an evidence that sets both precisions without a validation set.

On this page 8

Fit a cubic to eight noisy points of sin⁡2.5x\sin 2.5x, all of them in [−1,1][-1, 1]. Ridge regression returns one curve. At x=0x = 0, among the data, its prediction is good; at x=1.5x = 1.5, beyond the last point, it is a guess. Ridge reports both with the same confidence, and its penalty must still be chosen by cross-validation, refitting over a grid of values on many splits.

Keep the whole posterior over the weights instead of its mode, and both problems have exact answers. With the true noise level known, the predictive standard deviation is 0.2280.228 at x=0x = 0 and 2.502.50 at x=1.5x = 1.5: eleven times larger where the model knows less. The penalty follows from the evidence, the quantity the previous note used to compare models. Everything is exact because everything is Gaussian: the noise, the prior, and therefore the posterior, the predictive and the evidence.

The model: a Gaussian prior over the weights#

The data are inputs x1,…,xnx_1, \dots, x_n with real targets t=(t1,…,tn)⊤t = (t_1, \dots, t_n)^\top. A fixed feature map φ(x)∈Rm\varphi(x) \in \mathbb R^m, for example (1,x,x2,x3)(1, x, x^2, x^3), turns each input into a feature vector, and the design matrix Φ∈Rn×m\Phi \in \mathbb R^{n \times m} has φ(xi)⊤\varphi(x_i)^\top as its ii-th row. The likelihood says that each target is the linear prediction plus independent Gaussian noise; the prior says that the weights w∈Rmw \in \mathbb R^m are independently Gaussian around zero:

p(t∣w,β)=N(t∣Φw, β−1I),p(w∣α)=N(w∣0, α−1I).\begin{aligned} p(t \mid w, \beta) &= \mathcal N\big(t \mid \Phi w,\ \beta^{-1} I\big), \\ p(w \mid \alpha) &= \mathcal N\big(w \mid 0,\ \alpha^{-1} I\big) . \end{aligned}

The model needs to be linear only in ww. The features can be any fixed functions, polynomials, splines or the Gaussian bumps of the kernels note, and everything below holds unchanged.

The prior pulls every weight towards zero, including the weight of the constant feature 1, the intercept. That makes the fit depend on where the origin of tt lies. With the constant feature alone and nn points, the posterior below gives the intercept the mean nβtˉ/(α+nβ)n\beta\bar t/(\alpha + n\beta), with tˉ\bar t the mean target; for three points, α=1\alpha = 1 and β=4\beta = 4, that is 1213tˉ\frac{12}{13}\bar t, so adding 100 to every target moves the prediction by only 92.392.3. An unpenalized intercept, as ridge regression recommends, gets its own prior N(0,α0−1)\mathcal N(0, \alpha_0^{-1}) with α0→0\alpha_0 \to 0, a flat prior. Everything below then holds with diag⁡(α0,α,…,α)\operatorname{diag}(\alpha_0, \alpha, \dots, \alpha) in place of αI\alpha I, as in the relevance vector machine. The posterior stays proper as long as the data determine the intercept, but the evidence goes to zero with α0\alpha_0, so it cannot then compare models.

The two precisions are hyperparameters. β−1\beta^{-1} is the noise variance. α−1\alpha^{-1} is the prior variance of each weight, which says how large we expect the weights to be before seeing the data. Both are fixed until the last section, which estimates them.

Tools: matrix derivatives and Gaussian identities#

Six facts carry every derivation of this section of the course. Each is a standard identity or follows in one line.

1. Gradients of linear and quadratic forms#

For a vector bb and a matrix AA,

∇w b⊤w=b,∇w w⊤Aw=(A+A⊤) w,\begin{aligned} \nabla_w\, b^\top w &= b, \\ \nabla_w\, w^\top A w &= (A + A^\top)\, w , \end{aligned}

which is 2Aw2Aw for symmetric AA; the matrix calculus note of the Mathematics course derives both. Expanding the squared error,

∇w∥t−Φw∥2=∇w(t⊤t−2w⊤Φ⊤t+w⊤Φ⊤Φw)=2Φ⊤(Φw−t).\begin{aligned} \nabla_w \lVert t - \Phi w\rVert^2 &= \nabla_w\big(t^\top t - 2 w^\top \Phi^\top t + w^\top \Phi^\top\Phi w\big) \\ &= 2\Phi^\top(\Phi w - t) . \end{aligned}

2. Derivatives of a log-determinant#

For a symmetric positive definite matrix A(θ)A(\theta) that depends on a scalar θ\theta,

∂∂θlog⁡det⁡A(θ)=tr⁡(A−1∂A∂θ).\frac{\partial}{\partial \theta} \log \det A(\theta) = \operatorname{tr}\Big(A^{-1} \frac{\partial A}{\partial \theta}\Big) .

The log-determinant section of the same note derives it for any invertible matrix. As a check, take A=αI+MA = \alpha I + M with MM fixed and eigenvalues λj\lambda_j. Then log⁡det⁡A=∑jlog⁡(α+λj)\log\det A = \sum_j \log(\alpha + \lambda_j), and its derivative in α\alpha is ∑j1/(α+λj)=tr⁡(αI+M)−1\sum_j 1/(\alpha + \lambda_j) = \operatorname{tr}(\alpha I + M)^{-1}, the formula with ∂A/∂α=I\partial A/\partial\alpha = I.

3. Completing the square#

If a density over ww has the logarithm

log⁡p(w)=−12 w⊤Pw+b⊤w+const\log p(w) = -\tfrac12\, w^\top P w + b^\top w + \text{const}

with PP symmetric positive definite, then p(w)=N(w∣P−1b, P−1)p(w) = \mathcal N(w \mid P^{-1} b,\ P^{-1}). The proof is one line:

−12(w−P−1b)⊤P(w−P−1b)=−12w⊤Pw+b⊤w−12b⊤P−1b,\begin{aligned} -\tfrac12 (w - P^{-1}b)^\top &P (w - P^{-1}b) \\ &= -\tfrac12 w^\top P w + b^\top w - \tfrac12 b^\top P^{-1} b , \end{aligned}

and the last term is a constant. The matrix of the quadratic term is the precision, and the mean is the precision’s inverse times the linear term.

4. The Gaussian integral#

For the same PP and bb,

∫Rme−12w⊤Pw+b⊤w dw=(2π)m/2det⁡(P)1/2 e12b⊤P−1b,\int_{\mathbb R^m} e^{-\frac12 w^\top P w + b^\top w}\, dw = \frac{(2\pi)^{m/2}}{\det(P)^{1/2}}\, e^{\frac12 b^\top P^{-1} b} ,

because the integrand is exp⁡(12b⊤P−1b)\exp(\frac12 b^\top P^{-1} b) times an unnormalized N(P−1b,P−1)\mathcal N(P^{-1}b, P^{-1}), whose normalizing constant is (2π)m/2det⁡(P)−1/2(2\pi)^{m/2}\det(P)^{-1/2} (the density).

5. The linear-Gaussian marginal#

If w∼N(m0,S0)w \sim \mathcal N(m_0, S_0) and, given ww, t=Φw+εt = \Phi w + \varepsilon with independent noise ε∼N(0,β−1I)\varepsilon \sim \mathcal N(0, \beta^{-1} I), then

t∼N(Φm0, β−1I+ΦS0Φ⊤).t \sim \mathcal N\big(\Phi m_0,\ \beta^{-1} I + \Phi S_0 \Phi^\top\big) .

Φw\Phi w is an affine map of a Gaussian, Gaussian with mean Φm0\Phi m_0 and covariance ΦS0Φ⊤\Phi S_0 \Phi^\top; adding independent Gaussian noise keeps it Gaussian and adds the covariances.

6. Woodbury and the determinant lemma#

For an invertible n×nn \times n matrix AA, an invertible m×mm \times m matrix CC and matrices U∈Rn×mU \in \mathbb R^{n \times m}, V∈Rm×nV \in \mathbb R^{m \times n},

(A+UCV)−1=A−1−A−1U(C−1+VA−1U)−1VA−1,det⁡(A+UCV)=det⁡(C−1+VA−1U)det⁡(C)det⁡(A).\begin{aligned} (A + UCV)^{-1} &= A^{-1} - A^{-1} U \big(C^{-1} + V A^{-1} U\big)^{-1} V A^{-1}, \\ \det(A + UCV) &= \det\big(C^{-1} + V A^{-1} U\big) \det(C) \det(A) . \end{aligned}

The first, the Woodbury identity of the matrices note, is checked by multiplying the right side by A+UCVA + UCV and simplifying to II. The second follows from the two block factorizations of (A−UVC−1)\begin{pmatrix} A & -U \\ V & C^{-1} \end{pmatrix}. Eliminating the lower-left block first, and the upper-right block first, its determinant is

det⁡(A) det⁡(C−1+VA−1U)=det⁡(C−1) det⁡(A+UCV),\begin{aligned} &\det(A)\, \det\big(C^{-1} + V A^{-1} U\big) \\ &= \det\big(C^{-1}\big)\, \det\big(A + U C V\big) , \end{aligned}

the product of a diagonal block and its Schur complement either way; multiplying by det⁡C\det C gives the lemma. When A−1A^{-1} is easy, a multiple of II say, and m≪nm \ll n, they turn the inverse and the determinant of an n×nn \times n matrix into those of an m×mm \times m one, or the other way round.

Facts 3–5 are the Gaussian computations of the multivariate Gaussian note written as recipes.

The posterior over the weights#

By Bayes’ formula the log-posterior is the log-likelihood plus the log-prior, up to a constant. We drop log⁡p(t)\log p(t) and the normalizing constants, expand ∥t−Φw∥2\lVert t - \Phi w\rVert^2, drop βt⊤t/2\beta t^\top t/2, which does not contain ww, and group the quadratic and linear terms:

log⁡p(w∣t)=−β2∥t−Φw∥2−α2∥w∥2+const=−β2(w⊤Φ⊤Φ w−2w⊤Φ⊤t)−α2 w⊤w+const=−12 w⊤(αI+βΦ⊤Φ) w+w⊤βΦ⊤t+const.\begin{aligned} \log p(w \mid t) &= -\frac{\beta}{2}\lVert t - \Phi w\rVert^2 - \frac{\alpha}{2}\lVert w\rVert^2 + \text{const} \\ &= -\frac{\beta}{2}\big(w^\top \Phi^\top \Phi\, w - 2 w^\top \Phi^\top t\big) - \frac{\alpha}{2}\, w^\top w + \text{const} \\ &= -\frac12\, w^\top \big(\alpha I + \beta \Phi^\top \Phi\big)\, w + w^\top \beta \Phi^\top t + \text{const} . \end{aligned}

This is the form of fact 3, with precision P=αI+βΦ⊤ΦP = \alpha I + \beta\Phi^\top\Phi and linear term b=βΦ⊤tb = \beta\Phi^\top t:

p(w∣t)=N(w∣μ,Σ),Σ=(αI+βΦ⊤Φ)−1,μ=β Σ Φ⊤t\boxed{ \begin{aligned} p(w \mid t) &= \mathcal N(w \mid \mu, \Sigma), \\ \Sigma &= \big(\alpha I + \beta \Phi^\top \Phi\big)^{-1}, \\ \mu &= \beta\, \Sigma\, \Phi^\top t \end{aligned} }

Φ⊤Φ\Phi^\top\Phi is positive semidefinite, so Σ−1\Sigma^{-1} is positive definite for any α>0\alpha \gt 0, and Σ\Sigma exists even with more features than points.

Because the precision is a sum over points, the data can be processed one point at a time. With the posterior after nn points used as the prior for point n+1n + 1,

Σn+1−1=Σn−1+β φ(xn+1)φ(xn+1)⊤,\Sigma_{n+1}^{-1} = \Sigma_n^{-1} + \beta\, \varphi(x_{n+1}) \varphi(x_{n+1})^\top ,

and the end result is the posterior of all the points at once. The added matrix is positive semidefinite, so Σn+1−1⪰Σn−1\Sigma_{n+1}^{-1} \succeq \Sigma_n^{-1}, and since inversion reverses the order of positive definite matrices, Σn+1⪯Σn\Sigma_{n+1} \preceq \Sigma_n: a new point can only reduce the uncertainty about the weights, in every direction.

Fit a line, φ(x)=(1,x)\varphi(x) = (1, x), through three points x=(−1,0,1)x = (-1, 0, 1), t=(−1,0.5,2)t = (-1, 0.5, 2), with α=1\alpha = 1 and β=4\beta = 4, a noise standard deviation of 0.50.5. The inputs are centred, so every matrix is diagonal:

Φ⊤Φ=diag⁡(3,2),Φ⊤t=(1.5, 3)⊤,Σ−1=diag⁡(1+4⋅3, 1+4⋅2)=diag⁡(13,9),μ=4 Σ (1.5, 3)⊤=(6/13, 12/9)=(0.462, 1.333).\begin{aligned} \Phi^\top \Phi &= \operatorname{diag}(3, 2), \\ \Phi^\top t &= (1.5,\ 3)^\top, \\ \Sigma^{-1} &= \operatorname{diag}(1 + 4 \cdot 3,\ 1 + 4 \cdot 2) = \operatorname{diag}(13, 9), \\ \mu &= 4\, \Sigma\, (1.5,\ 3)^\top = (6/13,\ 12/9) = (0.462,\ 1.333) . \end{aligned}

Least squares gives (0.5, 1.5)(0.5,\ 1.5). The posterior mean shrinks the intercept by 12/1312/13 and the slope by 8/98/9, more where the data carry less precision. The posterior standard deviations are 1/13=0.2771/\sqrt{13} = 0.277 for the intercept and 1/31/3 for the slope.

Figure

Bayesian linear regression from the prior to the prediction

Bayesian linear regression from the prior to the predictionA line w₀ + w₁x through three points x = (−1, 0, 1), t = (−1, 0.5, 2). The prior precision αI = diag(1, 1) and the data precision βΦᵀΦ = diag(12, 8) add to the posterior precision diag(13, 9). The posterior mean is μ = Σ βΦᵀt = (0.462, 1.333). At the new inputs 0, 1, 3 the predictive mean is 0.462, 1.795, 4.462 and the variance 0.327, 0.438, 1.327.−101x−10.52tαI = diag(1, 1)prior precision, α = 11βΦᵀΦ = diag(12, 8)data precision, β = 4+Σ⁻¹ = diag(13, 9)posterior precision2βΦᵀt = (6, 12)Σ ·0.4621.333μ3013x*μᵀφ, 1/β + φᵀΣφpredictive mean0.4621.7954.462predictive variance0.3270.4381.3274Bayesian linear regression from the prior to the predictionA line w₀ + w₁x through three points x = (−1, 0, 1), t = (−1, 0.5, 2). The prior precision αI = diag(1, 1) and the data precision βΦᵀΦ = diag(12, 8) add to the posterior precision diag(13, 9). The posterior mean is μ = Σ βΦᵀt = (0.462, 1.333). At the new inputs 0, 1, 3 the predictive mean is 0.462, 1.795, 4.462 and the variance 0.327, 0.438, 1.327.−101x−10.52tβΦᵀΦ = diag(12, 8)β = 4+Σ⁻¹ = diag(13, 9)posterior precision2αI = diag(1, 1)α = 11βΦᵀt = (6, 12)Σ ·0.4621.333μ3013x*μᵀφ, 1/β + φᵀΣφ0.4621.7954.462predictive mean0.3270.4381.327predictive variance4
A line through three centred points, α = 1, β = 4. Green: data; amber: prior; blue: posterior; red: prediction. The precisions add to diag(13, 9), the mean is (6/13, 12/9), and the predictive variance 0.25 + 1/13 + x²/9 at x = 3 is four times that at x = 0.

The posterior mean is the ridge estimate#

The mode of a Gaussian is its mean, so the MAP estimate and the posterior mean coincide. Dividing the precision and the linear term by β\beta,

μ=(Φ⊤Φ+αβI)−1Φ⊤t.\mu = \big(\Phi^\top \Phi + \tfrac{\alpha}{\beta} I\big)^{-1} \Phi^\top t .

This is the ridge estimate with penalty λ=α/β\lambda = \alpha/\beta: the noise variance divided by the prior variance, as in the MAP section of the probability course. In the worked example, λ=1/4\lambda = 1/4, and ridge gives (1.5/3.25, 3/2.25)=(0.462, 1.333)(1.5/3.25,\ 3/2.25) = (0.462,\ 1.333), the same vector. A noisier problem (smaller β\beta) or a tighter prior (larger α\alpha) is a stronger penalty. The two limits recover the extremes: as α→0\alpha \to 0, μ\mu tends to the least-squares solution when Φ⊤Φ\Phi^\top\Phi is invertible, and as α→∞\alpha \to \infty, the posterior collapses onto w=0w = 0.

The Bayesian reading adds everything except the mean. The covariance Σ\Sigma, which the Bayesian reading of ridge mentions in passing, gives an error bar for every weight and, below, for every prediction. The ridge shrinkage factors σj2/(σj2+λ)\sigma_j^2/(\sigma_j^2 + \lambda) of the regularization note, whose sum is the effective degrees of freedom, reappear in the evidence, now with a formula for the best α\alpha.

The predictive distribution#

A new target at an input x∗x_*, with feature vector φ∗=φ(x∗)\varphi_* = \varphi(x_*), is t∗=w⊤φ∗+ε∗t_* = w^\top \varphi_* + \varepsilon_*. Here ww is distributed as the posterior, and the fresh noise ε∗∼N(0,β−1)\varepsilon_* \sim \mathcal N(0, \beta^{-1}) is independent of it. Fact 5 with the single row φ∗⊤\varphi_*^\top in place of Φ\Phi, mean μ\mu and covariance Σ\Sigma gives the predictive distribution:

p(t∗∣x∗,t)=∫N(t∗∣w⊤φ∗, β−1) N(w∣μ,Σ) dw=N(t∗∣μ⊤φ∗, σ∗2),σ∗2=1β+φ∗⊤Σ φ∗\boxed{ \begin{aligned} p(t_* \mid x_*, t) &= \int \mathcal N\big(t_* \mid w^\top\varphi_*,\ \beta^{-1}\big)\, \mathcal N(w \mid \mu, \Sigma)\, dw \\ &= \mathcal N\big(t_* \mid \mu^\top \varphi_*,\ \sigma_*^2\big), \\ \sigma_*^2 &= \frac{1}{\beta} + \varphi_*^\top \Sigma\, \varphi_* \end{aligned} }

The same result needs no integral. Write the posterior weights as w=μ+ξw = \mu + \xi with ξ∼N(0,Σ)\xi \sim \mathcal N(0, \Sigma). Then t∗=μ⊤φ∗+φ∗⊤ξ+ε∗t_* = \mu^\top\varphi_* + \varphi_*^\top\xi + \varepsilon_* is a constant plus two independent zero-mean Gaussians, so it is Gaussian by fact 5. Its mean is μ⊤φ∗\mu^\top\varphi_*. Variances of independent terms add, and the linear map ξ↦φ∗⊤ξ\xi \mapsto \varphi_*^\top\xi has the variance φ∗⊤Σφ∗\varphi_*^\top\Sigma\varphi_*, so the variance is φ∗⊤Σφ∗+1/β\varphi_*^\top\Sigma\varphi_* + 1/\beta.

The variance has two parts. 1/β1/\beta is the noise in the new target, which no amount of data removes. φ∗⊤Σφ∗\varphi_*^\top\Sigma\varphi_* is the uncertainty about the weights, seen through the feature vector of the new input.

The noise-free value of the regression function, f∗=w⊤φ∗f_* = w^\top\varphi_*, is the same decomposition without ε∗\varepsilon_*, so its posterior has only the second part:

p(f∗∣x∗,t)=N(f∗∣μ⊤φ∗, φ∗⊤Σ φ∗).p(f_* \mid x_*, t) = \mathcal N\big(f_* \mid \mu^\top\varphi_*,\ \varphi_*^\top\Sigma\,\varphi_*\big) .

A band for the curve uses this variance, a band for a new observation adds 1/β1/\beta. A plug-in prediction at w=μw = \mu keeps the 1/β1/\beta and loses φ∗⊤Σφ∗\varphi_*^\top\Sigma\varphi_*.

Why the uncertainty grows away from the data#

Let λ1,…,λm\lambda_1, \dots, \lambda_m be the eigenvalues of βΦ⊤Φ\beta\Phi^\top\Phi and uju_j its eigenvectors. Then Σ−1=αI+βΦ⊤Φ=∑j(α+λj) ujuj⊤\Sigma^{-1} = \alpha I + \beta\Phi^\top\Phi = \sum_j (\alpha + \lambda_j)\, u_j u_j^\top, so Σ\Sigma has the same eigenvectors, with eigenvalues 1/(α+λj)1/(\alpha + \lambda_j), and

φ∗⊤Σ φ∗=∑j=1m(uj⊤φ∗)2α+λj.\varphi_*^\top \Sigma\, \varphi_* = \sum_{j=1}^m \frac{(u_j^\top \varphi_*)^2}{\alpha + \lambda_j} .

λj=uj⊤(βΦ⊤Φ) uj=β∑i(uj⊤φ(xi))2\lambda_j = u_j^\top (\beta\Phi^\top\Phi)\, u_j = \beta \sum_i (u_j^\top \varphi(x_i))^2 measures how much the training inputs vary along uju_j. A new input whose feature vector lies along well-measured directions, with large λj\lambda_j, costs little. Components along poorly measured directions are divided by little more than α\alpha.

For the line with centred inputs the eigenvectors are the axes, and

φ∗⊤Σ φ∗=1α+βn+x∗2α+β∑ixi2,\varphi_*^\top \Sigma\, \varphi_* = \frac{1}{\alpha + \beta n} + \frac{x_*^2}{\alpha + \beta \sum_i x_i^2} ,

a parabola in x∗x_* with its minimum at the centre of the data: the error in the slope is multiplied by the distance from the centre. In the worked example, the variance is 0.25+1/13+x∗2/90.25 + 1/13 + x_*^2/9:

x∗x_*predictive meanpredictive variance
00.4620.327
11.7950.438
34.4621.327

At x∗=3x_* = 3, three times as far from the centre as the farthest training point, the weight uncertainty 1/13+1=1.0771/13 + 1 = 1.077 is more than four times the noise variance.

For polynomial features the same happens faster, because the high powers of x∗x_* grow quickly outside the range of the data. The figure fits a cubic to the first 2, 8 and 32 of 32 noisy points:

Figure

The predictive band narrows as the data grow

The predictive band narrows as the data growThree panels for 2, 8 and 32 points: the predictive mean of a cubic model and the band of ± 2 predictive standard deviations. With two points on the left the band is narrow only near them and wide elsewhere; with 32 points it is narrow over the whole range of the data and widens beyond its ends.−202−101n = 2−101n = 8−101n = 32predictive meanmean ± 2 sdsin 2.5xdataThe predictive band narrows as the data growThree panels for 2, 8 and 32 points: the predictive mean of a cubic model and the band of ± 2 predictive standard deviations. With two points on the left the band is narrow only near them and wide elsewhere; with 32 points it is narrow over the whole range of the data and widens beyond its ends.−202−101n = 2−202−101n = 8−202−101n = 32predictive meanmean ± 2 sdsin 2.5xdata
Cubic features, prior N(0, I) (α = 1), known noise precision β = 25, fitted to the first n of 32 noisy points of sin 2.5x (dashed). Band: predictive mean ± 2 sd. The sd at x = 0 is 0.488, 0.228, 0.206 for n = 2, 8, 32; at x = 1.5, beyond the data, 4.16, 2.50, 0.67.

As the data grow, φ∗⊤Σφ∗\varphi_*^\top\Sigma\varphi_* falls towards zero wherever the data are dense, and σ∗2\sigma_*^2 falls to the noise floor 1/β1/\beta and no lower. Away from the data the band stays open.

Evidence maximization for the hyperparameters#

The evidence, or marginal likelihood, is the probability of the targets with the weights integrated out:

p(t∣α,β)=∫p(t∣w,β) p(w∣α) dw.p(t \mid \alpha, \beta) = \int p(t \mid w, \beta)\, p(w \mid \alpha)\, dw .

It is a function of the hyperparameters only. Choosing them by maximizing it is type-II maximum likelihood, or empirical Bayes: maximum likelihood one level up, with the weights integrated out instead of fitted. The model selection note explains why maximizing over ww as well would fail, and maximizes the evidence numerically. Here the posterior gives the evidence a form that explains what it trades off, and a fast iteration for its maximum.

The evidence in two forms#

Fact 5 with the prior mean 00 and covariance α−1I\alpha^{-1} I gives the evidence directly as a Gaussian over tt, the form of the model selection note:

p(t∣α,β)=N(t∣0, C),C=β−1I+α−1ΦΦ⊤.\begin{aligned} p(t \mid \alpha, \beta) &= \mathcal N\big(t \mid 0,\ C\big), \\ C &= \beta^{-1} I + \alpha^{-1} \Phi \Phi^\top . \end{aligned}

This form needs the inverse and the determinant of an n×nn \times n matrix. A second form needs only m×mm \times m ones. Write the integrand as (β/2π)n/2(α/2π)m/2e−E(w)(\beta/2\pi)^{n/2} (\alpha/2\pi)^{m/2} e^{-E(w)}, with

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 .

EE is quadratic in ww. Its gradient vanishes at the posterior mean μ\mu, and its Hessian is αI+βΦ⊤Φ=Σ−1\alpha I + \beta\Phi^\top\Phi = \Sigma^{-1}, so its Taylor expansion around μ\mu is exact:

E(w)=E(μ)+12(w−μ)⊤Σ−1(w−μ).E(w) = E(\mu) + \tfrac12 (w - \mu)^\top \Sigma^{-1} (w - \mu) .

The remaining integral is fact 4, (2π)m/2det⁡(Σ)1/2(2\pi)^{m/2}\det(\Sigma)^{1/2}. Taking the logarithm,

log⁡p(t∣α,β)=m2log⁡α+n2log⁡β−E(μ)−12log⁡det⁡Σ−1−n2log⁡2π\boxed{ \begin{aligned} \log p(t \mid \alpha, \beta) ={}& \frac{m}{2}\log\alpha + \frac{n}{2}\log\beta - E(\mu) \\ &- \frac12 \log\det\Sigma^{-1} - \frac{n}{2}\log 2\pi \end{aligned} }

This is the route of Bishop (2006, Section 3.5.1) and of Vetrov and Kropotov’s lecture 4. The Code section checks that the two forms agree.

Fit times an Occam factor#

Regroup the boxed formula. With log⁡p(t∣μ,β)=n2log⁡β2π−β2∥t−Φμ∥2\log p(t \mid \mu, \beta) = \frac n2 \log\frac{\beta}{2\pi} - \frac\beta2 \lVert t - \Phi\mu\rVert^2 and log⁡p(μ∣α)=m2log⁡α2π−α2∥μ∥2\log p(\mu \mid \alpha) = \frac m2 \log\frac{\alpha}{2\pi} - \frac\alpha2 \lVert\mu\rVert^2, the two terms below add up to it, the m2log⁡2π\frac m2 \log 2\pi cancelling:

log⁡p(t∣α,β)=log⁡p(t∣μ,β)⏟fit+log⁡[p(μ∣α) (2π)m/2det⁡(Σ)1/2]⏟Occam factor.\begin{aligned} \log p(t \mid \alpha, \beta) ={}& \underbrace{\log p(t \mid \mu, \beta)}_{\text{fit}} \\ &+ \underbrace{\log\Big[p(\mu \mid \alpha)\, (2\pi)^{m/2} \det(\Sigma)^{1/2}\Big]}_{\text{Occam factor}} . \end{aligned}

The first term is the best fit the model achieves. The second multiplies the prior density at the posterior mean by the volume of the posterior. It is the fraction of the prior’s probability that lands on the weights the data allow, and it is small when the prior is much broader than the posterior (MacKay, 2003, Chapter 28). This is the coin’s peak-times-width argument, now exact. A very broad prior (small α\alpha) does not improve the fit any further but makes the Occam factor tiny, and the evidence prefers the prior that is just broad enough:

Figure

The evidence as a function of the prior precision

The evidence as a function of the prior precisionThree curves over α from 10⁻⁴ to 30: the log evidence, which peaks at α = 0.555; the fit term, nearly constant for small α and falling for large α; and the log Occam factor, which falls by 4.6 per decade as α becomes small.−30−20−1001010⁻⁴10⁻³10⁻²10⁻¹110prior precision α (log scale)α̂ = 0.555ln p(t | α, β)fit: ln p(t | μ, β)Occam factorThe evidence as a function of the prior precisionThree curves over α from 10⁻⁴ to 30: the log evidence, which peaks at α = 0.555; the fit term, nearly constant for small α and falling for large α; and the log Occam factor, which falls by 4.6 per decade as α becomes small.−30−20−1001010⁻⁴10⁻³10⁻²10⁻¹110prior precision α (log scale)α̂ = 0.555ln p(t | α, β)fit: ln p(t | μ, β)Occam factor
The 32 points and cubic features of the previous figure, β held at its fitted value 22.29. The log evidence (black) is the fit at the posterior mean (blue) plus the log Occam factor (amber), which falls by (m/2) ln 10 = 4.6 per decade as α shrinks. The maximum is at α = 0.555.

The fixed point#

Differentiate the boxed formula with respect to α\alpha. μ\mu depends on α\alpha, but EE is minimized at μ\mu, so its derivative through μ\mu vanishes and only the explicit α2∥μ∥2\frac\alpha2\lVert\mu\rVert^2 counts. Fact 2 gives ∂αlog⁡det⁡Σ−1=tr⁡Σ=∑j1/(α+λj)\partial_\alpha \log\det\Sigma^{-1} = \operatorname{tr}\Sigma = \sum_j 1/(\alpha + \lambda_j). Setting the derivative to zero and multiplying by 2α2\alpha,

m2α−12∥μ∥2−12∑j1α+λj=0,α∥μ∥2=m−∑jαα+λj=∑jλjα+λj=γ.\begin{aligned} \frac{m}{2\alpha} - \frac12\lVert\mu\rVert^2 - \frac12 \sum_{j} \frac{1}{\alpha + \lambda_j} &= 0, \\ \alpha\lVert\mu\rVert^2 &= m - \sum_j \frac{\alpha}{\alpha + \lambda_j} \\ &= \sum_j \frac{\lambda_j}{\alpha + \lambda_j} = \gamma . \end{aligned}

For β\beta, the explicit dependence is in n2log⁡β\frac n2\log\beta and in β2∥t−Φμ∥2\frac\beta2\lVert t - \Phi\mu\rVert^2. The eigenvalues λj\lambda_j are proportional to β\beta, so ∂βλj=λj/β\partial_\beta \lambda_j = \lambda_j/\beta, and

∂βlog⁡det⁡Σ−1=∑jλjβ(α+λj)=γβ.\partial_\beta \log\det\Sigma^{-1} = \sum_j \frac{\lambda_j}{\beta(\alpha + \lambda_j)} = \frac\gamma\beta .

Setting the derivative to zero gives n/β−∥t−Φμ∥2−γ/β=0n/\beta - \lVert t - \Phi\mu\rVert^2 - \gamma/\beta = 0. Together these are the fixed-point equations of MacKay (1992, Section 4):

γ=∑j=1mλjα+λj,α=γ∥μ∥2,1β=∥t−Φμ∥2n−γ\boxed{ \begin{aligned} \gamma &= \sum_{j=1}^m \frac{\lambda_j}{\alpha + \lambda_j}, \\ \alpha &= \frac{\gamma}{\lVert\mu\rVert^2}, \\ \frac{1}{\beta} &= \frac{\lVert t - \Phi\mu\rVert^2}{n - \gamma} \end{aligned} }

They are not closed-form solutions, because μ\mu and γ\gamma depend on α\alpha and β\beta. They are iterated: compute μ\mu and Σ\Sigma for the current values, then γ\gamma, then the new α\alpha and β\beta, and repeat. The eigenvalues of Φ⊤Φ\Phi^\top\Phi are computed once and rescaled by β\beta.

The effective number of parameters#

The quantity γ\gamma is the effective number of parameters. Each term λj/(α+λj)\lambda_j/(\alpha + \lambda_j) lies between 0 and 1. It is close to 1 when the data determine the weight along uju_j (λj≫α\lambda_j \gg \alpha), and close to 0 when the prior does (Bishop, 2006, Section 3.5.3). The terms are the ridge shrinkage factors of the previous section.

The two updates then read naturally. The noise estimate divides the residual sum of squares by n−γn - \gamma, just as the unbiased variance estimate of least squares divides by n−mn - m: γ\gamma degrees of freedom were spent on fitting. The prior variance 1/α=∥μ∥2/γ1/\alpha = \lVert\mu\rVert^2/\gamma is the mean squared weight over the well-determined directions. With many more points than features, every λj≫α\lambda_j \gg \alpha, γ≈m\gamma \approx m, and the updates need no eigenvalues: α=m/∥μ∥2\alpha = m/\lVert\mu\rVert^2 and 1/β=∥t−Φμ∥2/n1/\beta = \lVert t - \Phi\mu\rVert^2/n.

For the 32 points and cubic features of the figures, starting from α=β=1\alpha = \beta = 1, the iteration converges to:

QuantityValue
α\alpha0.5550.555
β\beta, and the noise sd 1/β1/\sqrt\beta22.2922.29, 0.2120.212 (true 0.20.2)
γ\gamma, of m=4m = 43.9453.945
log evidence at α=1\alpha = 1, β=25\beta = 25−6.92-6.92
log evidence at the maximum−6.43-6.43

Almost all four parameters are well determined. scikit-learn’s BayesianRidge, which runs the same iteration with nearly flat gamma priors on the two precisions, returns the same values.

The same maximum can be found with the EM algorithm, treating ww as a latent variable. Its E-step is the posterior above. Its M-step maximizes the expected log-prior m2log⁡α−α2E∥w∥2\frac m2 \log\alpha - \frac\alpha2 \mathbb E\lVert w\rVert^2, with E∥w∥2=∥μ∥2+tr⁡Σ\mathbb E\lVert w\rVert^2 = \lVert\mu\rVert^2 + \operatorname{tr}\Sigma under the posterior, and gives α=m/(∥μ∥2+tr⁡Σ)\alpha = m / (\lVert\mu\rVert^2 + \operatorname{tr}\Sigma). Since γ=m−∑jα/(α+λj)=m−αtr⁡Σ\gamma = m - \sum_j \alpha/(\alpha + \lambda_j) = m - \alpha\operatorname{tr}\Sigma, a stationary point of this update is a stationary point of α∥μ∥2=γ\alpha\lVert\mu\rVert^2 = \gamma. The relevance vector machine compares the two iterations.

Why the two forms of the evidence agree#

Optional depth: the matrix identities behind the two forms. The rest of the note does not depend on it.

Apply the determinant lemma to C=β−1I+Φ (α−1I) Φ⊤C = \beta^{-1} I + \Phi\, (\alpha^{-1} I)\, \Phi^\top, with A=β−1IA = \beta^{-1} I, U=ΦU = \Phi, V=Φ⊤V = \Phi^\top and α−1I\alpha^{-1} I as the middle matrix:

det⁡C=det⁡(αI+βΦ⊤Φ) α−mβ−n.\det C = \det(\alpha I + \beta\Phi^\top\Phi)\, \alpha^{-m} \beta^{-n} .

This matches the terms m2log⁡α+n2log⁡β−12log⁡det⁡Σ−1\frac m2 \log\alpha + \frac n2\log\beta - \frac12\log\det\Sigma^{-1}. Woodbury gives C−1=βI−β2ΦΣΦ⊤C^{-1} = \beta I - \beta^2 \Phi\Sigma\Phi^\top, and with βΦ⊤t=Σ−1μ\beta\Phi^\top t = \Sigma^{-1}\mu,

t⊤C−1t=β t⊤t−μ⊤Σ−1μ,2E(μ)=β t⊤t−2μ⊤βΦ⊤t+μ⊤Σ−1μ=β t⊤t−μ⊤Σ−1μ,\begin{aligned} t^\top C^{-1} t &= \beta\, t^\top t - \mu^\top \Sigma^{-1} \mu , \\ 2E(\mu) &= \beta\, t^\top t - 2\mu^\top \beta \Phi^\top t + \mu^\top \Sigma^{-1} \mu \\ &= \beta\, t^\top t - \mu^\top\Sigma^{-1}\mu , \end{aligned}

so the quadratic forms match as well.

Where it breaks: confident far from the data#

The growth of the error bars away from the data comes from the features. Replace the cubic by localized features: nine Gaussian bumps exp⁡(−(x−ck)2/(2⋅0.252))\exp\big(-(x - c_k)^2 / (2 \cdot 0.25^2)\big) with centres ckc_k evenly spaced on [−1,1][-1, 1]. Fit both models to the same 32 points, with α=1\alpha = 1 and β=25\beta = 25:

x∗x_*truth sin⁡2.5x∗\sin 2.5x_*bumps: mean ± sdcubic: mean ± sd
00.000−0.144 ± 0.218−0.066 ± 0.206
1.5−0.5720.069 ± 0.206−1.368 ± 0.668
2−0.9590.000 ± 0.200−6.721 ± 1.641
30.9380.000 ± 0.200−31.324 ± 5.790

Far from every centre, φ∗→0\varphi_* \to 0, so φ∗⊤Σφ∗→0\varphi_*^\top\Sigma\varphi_* \to 0, and the predictive variance falls to the noise floor 1/β1/\beta. At x∗=2x_* = 2 the bump model predicts 0.000±0.2000.000 \pm 0.200, exactly the noise level, and the truth lies 4.8 standard deviations away. The model is most confident exactly where it knows least (Bishop, 2006, Section 3.3.2). The cubic’s mean is far worse, but its band at least widens.

The cure is to stop fixing the features. A Gaussian process is this model with infinitely many features, and its predictive variance returns to the prior variance away from the data. The relevance vector machine keeps bumps on the training inputs and inherits the flaw.

Other limits come from the same assumptions. The closed forms need Gaussian noise and a linear model: outliers or classification targets break conjugacy, and the Laplace approximation takes over. The posterior costs the inverse of an m×mm \times m matrix, O(m3)O(m^3), which is expensive for tens of thousands of features. And type-II maximum likelihood plugs in point values of α\alpha and β\beta; with two hyperparameters and many points this costs little.

What you should remember#

  • Prior N(0,α−1I)\mathcal N(0, \alpha^{-1} I), noise precision β\beta: the posterior is N(μ,Σ)\mathcal N(\mu, \Sigma) with Σ−1=αI+βΦ⊤Φ\Sigma^{-1} = \alpha I + \beta\Phi^\top\Phi and μ=βΣΦ⊤t\mu = \beta\Sigma\Phi^\top t. Precisions add.
  • The posterior mean is the ridge estimate with λ=α/β\lambda = \alpha/\beta.
  • The predictive is N(μ⊤φ∗, 1/β+φ∗⊤Σφ∗)\mathcal N\big(\mu^\top\varphi_*,\ 1/\beta + \varphi_*^\top\Sigma\varphi_*\big): noise plus weight uncertainty, small along the directions the data measure.
  • Log evidence = fit at μ\mu + log Occam factor; exact here, because E(w)E(w) is quadratic.
  • MacKay’s fixed point: γ=∑jλj/(α+λj)\gamma = \sum_j \lambda_j/(\alpha + \lambda_j), α=γ/∥μ∥2\alpha = \gamma/\lVert\mu\rVert^2, 1/β=∥t−Φμ∥2/(n−γ)1/\beta = \lVert t - \Phi\mu\rVert^2/(n - \gamma); γ\gamma counts the well-determined parameters.
  • With localized features the error bars collapse to the noise level far from the data.

Code#

The worked example, the agreement with ridge, the predictive standard deviation of the cubic model as the data grow, the two forms of the evidence, MacKay’s fixed point against scikit-learn’s BayesianRidge, and localized features far from the data. The script runs in about a second; exact numbers can differ in the last digit across platforms and library versions.

import numpy as np
from scipy.stats import multivariate_normal
from sklearn.linear_model import BayesianRidge

def posterior(Phi, t, alpha, beta):
    """Posterior N(mu, S) of the weights under the prior N(0, I/alpha) and noise precision beta."""
    S = np.linalg.inv(alpha * np.eye(Phi.shape[1]) + beta * Phi.T @ Phi)
    return beta * S @ Phi.T @ t, S

def predictive(phi, mu, S, beta):
    """Predictive mean and variance at the feature vectors in the rows of phi."""
    return phi @ mu, 1 / beta + np.einsum('ij,jk,ik->i', phi, S, phi)

def log_evidence(Phi, t, alpha, beta):
    n, m = Phi.shape
    mu, S = posterior(Phi, t, alpha, beta)
    E = beta / 2 * np.sum((t - Phi @ mu) ** 2) + alpha / 2 * mu @ mu
    return m / 2 * np.log(alpha) + n / 2 * np.log(beta) - E + 0.5 * np.linalg.slogdet(S)[1] - n / 2 * np.log(2 * np.pi)

# the worked example: a line through three points, alpha = 1, beta = 4
Phi = np.array([[1, -1], [1, 0], [1, 1.0]])
t = np.array([-1, 0.5, 2.0])
mu, S = posterior(Phi, t, alpha=1.0, beta=4.0)
print(mu.round(4), np.diag(S).round(4))                                      # [0.4615 1.3333] [0.0769 0.1111]
print(np.linalg.solve(Phi.T @ Phi + 0.25 * np.eye(2), Phi.T @ t).round(4))   # [0.4615 1.3333]: ridge with lambda = alpha/beta
m, v = predictive(np.array([[1, 0], [1, 1], [1, 3.0]]), mu, S, beta=4.0)
print(m.round(3), v.round(3))                                                # [0.462 1.795 4.462] [0.327 0.438 1.327]

# 32 noisy points of sin(2.5 x), cubic features, alpha = 1 and the true noise precision beta = 25
rng = np.random.default_rng(3)
x = rng.uniform(-1, 1, 32).round(3)
t = (np.sin(2.5 * x) + 0.2 * rng.normal(size=32)).round(3)
feat = lambda x: np.vander(np.atleast_1d(x), 4, increasing=True)
grid = np.array([-1.5, -1.0, 0.0, 1.0, 1.5])
for n in (2, 8, 32):                       # predictive standard deviation at x = -1.5, -1, 0, 1, 1.5
    mu, S = posterior(feat(x[:n]), t[:n], 1.0, 25.0)
    print(n, np.sqrt(predictive(feat(grid), mu, S, 25.0)[1]).round(3))
# 2 [1.786 0.435 0.488 1.831 4.163] | 8 [1.585 0.364 0.228 0.747 2.501] | 32 [0.594 0.234 0.206 0.255 0.668]

# the evidence: the m x m form against the Gaussian N(0, I/beta + Phi Phi^T/alpha) over t
Phi = feat(x)
C = np.eye(32) / 25.0 + Phi @ Phi.T / 1.0
print(round(log_evidence(Phi, t, 1.0, 25.0), 4), round(multivariate_normal(np.zeros(32), C).logpdf(t), 4))   # -6.9188 -6.9188

# MacKay's fixed point for alpha and beta
alpha, beta = 1.0, 1.0
eig = np.linalg.eigvalsh(Phi.T @ Phi)
for _ in range(200):
    mu, S = posterior(Phi, t, alpha, beta)
    lam = beta * eig
    gamma = np.sum(lam / (lam + alpha))
    alpha, beta = gamma / (mu @ mu), (32 - gamma) / np.sum((t - Phi @ mu) ** 2)
print(round(alpha, 4), round(beta, 3), round(gamma, 3), round(log_evidence(Phi, t, alpha, beta), 4))   # 0.5551 22.294 3.945 -6.4286
br = BayesianRidge(fit_intercept=False, tol=1e-10, max_iter=1000).fit(Phi, t)
print(round(br.lambda_, 4), round(br.alpha_, 3))   # 0.5551 22.294: scikit-learn's lambda_ is the weight precision, alpha_ the noise precision

# localized features: nine Gaussian bumps of width 0.25 on [-1, 1], against the cubic, far from the data
centres = np.linspace(-1, 1, 9)
bumps = lambda z: np.exp(-(np.atleast_1d(z)[:, None] - centres) ** 2 / (2 * 0.25 ** 2))
far = np.array([0.0, 1.5, 2.0, 3.0])
for f in (bumps, feat):
    mu, S = posterior(f(x), t, 1.0, 25.0)
    m, v = predictive(f(far), mu, S, 25.0)
    print(m.round(3), np.sqrt(v).round(3))
# [-0.144  0.069  0.     0.   ] [0.218 0.206 0.2   0.2  ]: the bumps fall back to the noise level 0.2
# [ -0.066  -1.368  -6.721 -31.324] [0.206 0.668 1.641 5.79 ]

Next#

One precision α\alpha for all weights shrinks them all alike and keeps every feature in the model. Giving each weight its own precision, and maximizing the evidence over all of them, has a surprising effect: most precisions go to infinity, their weights are pinned at zero, and the model keeps only a few features. The next note derives this automatic relevance determination and applies it to kernel features, where it gives the relevance vector machine, a sparse alternative to the support vector machine with predictive variances.

References#

  • D. P. Vetrov, D. A. Kropotov. Bayesian Methods in Machine Learning (in Russian). Lecture course, Moscow State University, 2015. Lecture 4, slides (the matrix identities, Bayesian linear regression, the evidence by an exact Taylor expansion, the evidence updates).
  • Bayesian Methods in Machine Learning (in Russian). Lecture course, HSE University, 2021. Lecture notes, lecture 4 (matrix derivatives, the posterior of linear regression through its mode, the limits of the prior precision).
  • Конспект курса «Байесовские методы в анализе данных», 2026, github.com/bmml-team/bmml_ftda, лекция 2.
  • C. M. Bishop. Pattern Recognition and Machine Learning. Springer, 2006. Section 3.3 (Bayesian linear regression; 3.3.2, the predictive distribution and localized basis functions), Section 3.5 (the evidence approximation; 3.5.1, the evidence; 3.5.3, the effective number of parameters).
  • D. J. C. MacKay. Bayesian Interpolation. Neural Computation, 4(3), 415–447, 1992. Section 4 (choosing α\alpha and β\beta, the number of good parameter measurements).
  • D. J. C. MacKay. Information Theory, Inference, and Learning Algorithms. Cambridge University Press, 2003. Chapter 28 (model comparison and Occam’s razor).
  • K. P. Murphy. Probabilistic Machine Learning: Advanced Topics. MIT Press, 2023. Section 15.2.2 (conjugate priors for linear regression, the posterior predictive).
  • Cambridge 4F13 Probabilistic Machine Learning, 2025/26. Lecture “Bayesian inference and prediction in finite regression models”.