Blog · updated 26 Oct 2026 · 49 min read

Gaussian processes, from first principles

From Bayesian linear regression to kernels, exact conditioning, the marginal likelihood and a working implementation. Every step derived, nothing skipped; every idea interactive.

Contents
  1. Notation
  2. 1. Gaussian facts you will need
  3. Reading a Gaussian off its exponent
  4. Linear maps and sums: convolution
  5. Multiplying densities: product
  6. Conditioning
  7. Matrix identities
  8. Putting it together: the linear-Gaussian model
  9. 2. Bayesian linear regression
  10. 3. From weight space to function space
  11. 4. Gaussian processes
  12. 5. Kernels encode assumptions
  13. 6. Conditioning
  14. Sampling the posterior: Matheron’s rule
  15. 7. The marginal likelihood
  16. 8. Implementation
  17. 9. Limitations
  18. Questions
  19. References

A Gaussian process (GP) is a prior over functions that stays tractable after conditioning on data. It returns a full predictive distribution at every input, so it reports where it is uncertain, not only what it predicts. Click to add data, drag a point, or switch the kernel:

The post builds the GP from Bayesian linear regression, derives the posterior and the marginal likelihood, and ends with an implementation and its failure modes. Everything rests on eight facts about Gaussians, proved in Section 1; later derivations cite them by number. Proofs are folded and written as numbered steps, each with its justification, so that no step is left to the reader.

Notation

Plain italic letters (aa, ss, σn\sigma_n) are scalars. Bold italic lower-case letters (x\boldsymbol{x}, y\boldsymbol{y}, μ\boldsymbol{\mu}) are column vectors; the dimension is stated where each is introduced. Bold upright capitals (K\mathbf{K}, X\mathbf{X}, Σ\boldsymbol{\Sigma}, Φ\boldsymbol{\Phi}) are matrices. I\mathbf{I} is the identity and 0\mathbf{0} a zero vector or matrix.

SymbolMeaning
X∈RN×D\mathbf{X} \in \mathbb{R}^{N\times D}, y∈RN\boldsymbol{y} \in \mathbb{R}^Ntraining inputs (one per row), noisy observations
xi∈RD\boldsymbol{x}_i \in \mathbb{R}^D, yi∈Ry_i \in \mathbb{R}the ii-th input and observation
x∗∈RD\boldsymbol{x}_* \in \mathbb{R}^D, f∗=f(x∗)f_* = f(\boldsymbol{x}_*)a test input, the latent value there
fX=f(X)∈RN\boldsymbol{f}_{\mathbf{X}} = f(\mathbf{X}) \in \mathbb{R}^Nlatent function values at the training inputs
σn2\sigma_n^2observation-noise variance
k(x,x′)k(\boldsymbol{x}, \boldsymbol{x}'), K=k(X,X)\mathbf{K} = k(\mathbf{X}, \mathbf{X})kernel, Gram matrix with entries Kij=k(xi,xj)K_{ij} = k(\boldsymbol{x}_i, \boldsymbol{x}_j)
k∗=k(X,x∗)∈RN\boldsymbol{k}_* = k(\mathbf{X}, \boldsymbol{x}_*) \in \mathbb{R}^N, k∗∗=k(x∗,x∗)k_{**} = k(\boldsymbol{x}_*, \boldsymbol{x}_*)cross-covariance vector, prior variance at x∗\boldsymbol{x}_*
Ky=K+σn2I\mathbf{K}_y = \mathbf{K} + \sigma_n^2\mathbf{I}covariance of the observations
μ∗\mu_*, σ∗2\sigma_*^2posterior mean and variance of f∗f_*
σf2\sigma_f^2, ℓ\ell, θ\boldsymbol{\theta}kernel amplitude, length-scale, all hyperparameters
ϕ(x)∈RM\boldsymbol{\phi}(\boldsymbol{x}) \in \mathbb{R}^M, Φ∈RN×M\boldsymbol{\Phi} \in \mathbb{R}^{N\times M}feature map, design matrix with rows ϕ(xi)⊤\boldsymbol{\phi}(\boldsymbol{x}_i)^\top
w∼N(0,Σw)\boldsymbol{w} \sim \mathcal{N}(\mathbf{0}, \boldsymbol{\Sigma}_w)weights and their prior
P\mathbf{P}, h\boldsymbol{h}a precision matrix and a linear coefficient (information form)

All covariance matrices are symmetric and positive definite unless stated. N(a∣μ,Σ)\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}, \boldsymbol{\Sigma}) is the Gaussian density in a∈RD\boldsymbol{a} \in \mathbb{R}^D:

N(a∣μ,Σ)=(2π)−D/2 ∣Σ∣−1/2exp⁡ ⁣(−12(a−μ)⊤Σ−1(a−μ)).\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}, \boldsymbol{\Sigma}) = (2\pi)^{-D/2}\,\lvert\boldsymbol{\Sigma}\rvert^{-1/2}\exp\!\Big(-\tfrac12(\boldsymbol{a} - \boldsymbol{\mu})^\top\boldsymbol{\Sigma}^{-1}(\boldsymbol{a} - \boldsymbol{\mu})\Big).

1. Gaussian facts you will need

Every result in this post is a sequence of the eight facts below.

Reading a Gaussian off its exponent

Expand the exponent of N(a∣μ,Σ)\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}, \boldsymbol{\Sigma}), term by term:

−12(a−μ)⊤Σ−1(a−μ)=−12(a⊤Σ−1a−a⊤Σ−1μ−μ⊤Σ−1a+μ⊤Σ−1μ)=−12 a⊤Σ−1a+a⊤Σ−1μ−12 μ⊤Σ−1μ.\begin{aligned} -\tfrac12(\boldsymbol{a} - \boldsymbol{\mu})^\top\boldsymbol{\Sigma}^{-1}(\boldsymbol{a} - \boldsymbol{\mu}) &= -\tfrac12\big(\boldsymbol{a}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{a} - \boldsymbol{a}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{\mu} - \boldsymbol{\mu}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{a} + \boldsymbol{\mu}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{\mu}\big) \\ &= -\tfrac12\,\boldsymbol{a}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{a} + \boldsymbol{a}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{\mu} - \tfrac12\,\boldsymbol{\mu}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{\mu} . \end{aligned}

The first line multiplies out the product. The second uses μ⊤Σ−1a=a⊤Σ−1μ\boldsymbol{\mu}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{a} = \boldsymbol{a}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{\mu}, which holds because μ⊤Σ−1a\boldsymbol{\mu}^\top\boldsymbol{\Sigma}^{-1}\boldsymbol{a} is a scalar, a scalar equals its own transpose, and (Σ−1)⊤=Σ−1(\boldsymbol{\Sigma}^{-1})^\top = \boldsymbol{\Sigma}^{-1}. So the coefficient of the quadratic term is the precision Σ−1\boldsymbol{\Sigma}^{-1}, and the coefficient of the linear term is Σ−1μ\boldsymbol{\Sigma}^{-1}\boldsymbol{\mu}. The converse is the workhorse of every Bayesian calculation below.

Lemma 1 (Completing the square)

Let P\mathbf{P} be symmetric positive definite and h\boldsymbol{h} a vector. For all a\boldsymbol{a},

−12 a⊤Pa+a⊤h=−12 (a−P−1h)⊤P (a−P−1h)+12 h⊤P−1h.-\tfrac12\,\boldsymbol{a}^\top\mathbf{P}\boldsymbol{a} + \boldsymbol{a}^\top \boldsymbol{h} = -\tfrac12\,(\boldsymbol{a} - \mathbf{P}^{-1}\boldsymbol{h})^\top\mathbf{P}\,(\boldsymbol{a} - \mathbf{P}^{-1}\boldsymbol{h}) + \tfrac12\,\boldsymbol{h}^\top\mathbf{P}^{-1}\boldsymbol{h}.

Consequently, a density with p(a)∝exp⁡ ⁣(−12a⊤Pa+a⊤h)p(\boldsymbol{a}) \propto \exp\!\big(-\tfrac12\boldsymbol{a}^\top\mathbf{P}\boldsymbol{a} + \boldsymbol{a}^\top \boldsymbol{h}\big) is N(P−1h, P−1)\mathcal{N}(\mathbf{P}^{-1}\boldsymbol{h},\, \mathbf{P}^{-1}).

Proof

Step 1. Name the centre. Let a0=P−1h\boldsymbol{a}_0 = \mathbf{P}^{-1}\boldsymbol{h}. Then

Pa0=h,a0⊤P=(Pa0)⊤=h⊤,a0⊤Pa0=h⊤P−1h.\mathbf{P}\boldsymbol{a}_0 = \boldsymbol{h}, \qquad \boldsymbol{a}_0^\top\mathbf{P} = (\mathbf{P}\boldsymbol{a}_0)^\top = \boldsymbol{h}^\top, \qquad \boldsymbol{a}_0^\top\mathbf{P}\boldsymbol{a}_0 = \boldsymbol{h}^\top\mathbf{P}^{-1}\boldsymbol{h} .

The middle identity uses P⊤=P\mathbf{P}^\top = \mathbf{P}.

Step 2. Expand the square on the right-hand side.

−12(a−a0)⊤P(a−a0)=−12(a⊤Pa−a⊤Pa0−a0⊤Pa+a0⊤Pa0)=−12(a⊤Pa−a⊤h−h⊤a+h⊤P−1h)=−12 a⊤Pa+a⊤h−12 h⊤P−1h.\begin{aligned} -\tfrac12(\boldsymbol{a} - \boldsymbol{a}_0)^\top\mathbf{P}(\boldsymbol{a} - \boldsymbol{a}_0) &= -\tfrac12\big(\boldsymbol{a}^\top\mathbf{P}\boldsymbol{a} - \boldsymbol{a}^\top\mathbf{P}\boldsymbol{a}_0 - \boldsymbol{a}_0^\top\mathbf{P}\boldsymbol{a} + \boldsymbol{a}_0^\top\mathbf{P}\boldsymbol{a}_0\big) \\ &= -\tfrac12\big(\boldsymbol{a}^\top\mathbf{P}\boldsymbol{a} - \boldsymbol{a}^\top \boldsymbol{h} - \boldsymbol{h}^\top \boldsymbol{a} + \boldsymbol{h}^\top\mathbf{P}^{-1}\boldsymbol{h}\big) \\ &= -\tfrac12\,\boldsymbol{a}^\top\mathbf{P}\boldsymbol{a} + \boldsymbol{a}^\top \boldsymbol{h} - \tfrac12\,\boldsymbol{h}^\top\mathbf{P}^{-1}\boldsymbol{h} . \end{aligned}

Reasons, line by line: (1) multiply out; (2) substitute the three identities of Step 1; (3) h⊤a=a⊤h\boldsymbol{h}^\top \boldsymbol{a} = \boldsymbol{a}^\top \boldsymbol{h}, the same scalar.

Step 3. Rearrange. Add 12h⊤P−1h\tfrac12\boldsymbol{h}^\top\mathbf{P}^{-1}\boldsymbol{h} to both sides of Step 2. This is the identity.

Step 4. The consequence. The term 12h⊤P−1h\tfrac12\boldsymbol{h}^\top\mathbf{P}^{-1}\boldsymbol{h} does not contain a\boldsymbol{a}, so it is absorbed into the proportionality constant:

p(a)∝exp⁡ ⁣(−12(a−a0)⊤P(a−a0)).p(\boldsymbol{a}) \propto \exp\!\big(-\tfrac12(\boldsymbol{a} - \boldsymbol{a}_0)^\top\mathbf{P}(\boldsymbol{a} - \boldsymbol{a}_0)\big).

Step 5. Identify the density. The right-hand side of Step 4 is the density of N(a0,P−1)\mathcal{N}(\boldsymbol{a}_0, \mathbf{P}^{-1}) without its constant. Two densities that are proportional and both integrate to one are equal, so p(a)=N(a∣P−1h,P−1)p(\boldsymbol{a}) = \mathcal{N}(\boldsymbol{a} \mid \mathbf{P}^{-1}\boldsymbol{h}, \mathbf{P}^{-1}).

Linear maps and sums: convolution

Lemma 2 (Affine transformation)

If a∼N(μ,Σ)\boldsymbol{a} \sim \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma}) and b=Ca+d\boldsymbol{b} = \mathbf{C}\boldsymbol{a} + \boldsymbol{d} for a fixed matrix C\mathbf{C} and vector d\boldsymbol{d}, then b∼N(Cμ+d,  CΣC⊤)\boldsymbol{b} \sim \mathcal{N}(\mathbf{C}\boldsymbol{\mu} + \boldsymbol{d},\; \mathbf{C}\boldsymbol{\Sigma}\mathbf{C}^\top).

Proof

Step 1. The mean. Expectation is linear, and C\mathbf{C}, d\boldsymbol{d} are constants:

E[b]=E[Ca+d]=C E[a]+d=Cμ+d.\mathbb{E}[\boldsymbol{b}] = \mathbb{E}[\mathbf{C}\boldsymbol{a} + \boldsymbol{d}] = \mathbf{C}\,\mathbb{E}[\boldsymbol{a}] + \boldsymbol{d} = \mathbf{C}\boldsymbol{\mu} + \boldsymbol{d} .

Step 2. The centred variable. Subtract the mean:

b−E[b]=Ca+d−Cμ−d=C(a−μ).\boldsymbol{b} - \mathbb{E}[\boldsymbol{b}] = \mathbf{C}\boldsymbol{a} + \boldsymbol{d} - \mathbf{C}\boldsymbol{\mu} - \boldsymbol{d} = \mathbf{C}(\boldsymbol{a} - \boldsymbol{\mu}).

Step 3. The covariance.

Cov⁡[b]=E[(b−E[b])(b−E[b])⊤]definition=E[C(a−μ)(a−μ)⊤C⊤]Step 2, and (Cu)⊤=u⊤C⊤=C E[(a−μ)(a−μ)⊤] C⊤linearity=CΣC⊤.\begin{aligned} \operatorname{Cov}[\boldsymbol{b}] &= \mathbb{E}\big[(\boldsymbol{b} - \mathbb{E}[\boldsymbol{b}])(\boldsymbol{b} - \mathbb{E}[\boldsymbol{b}])^\top\big] && \text{definition} \\ &= \mathbb{E}\big[\mathbf{C}(\boldsymbol{a} - \boldsymbol{\mu})(\boldsymbol{a} - \boldsymbol{\mu})^\top\mathbf{C}^\top\big] && \text{Step 2, and } (\mathbf{C}\boldsymbol{u})^\top = \boldsymbol{u}^\top\mathbf{C}^\top \\ &= \mathbf{C}\,\mathbb{E}\big[(\boldsymbol{a} - \boldsymbol{\mu})(\boldsymbol{a} - \boldsymbol{\mu})^\top\big]\,\mathbf{C}^\top && \text{linearity} \\ &= \mathbf{C}\boldsymbol{\Sigma}\mathbf{C}^\top . \end{aligned}

Step 4. The distribution is Gaussian. A distribution is determined by its characteristic function φ(t)=E[eit⊤(⋅)]\varphi(\boldsymbol{t}) = \mathbb{E}[e^{i \boldsymbol{t}^\top(\cdot)}], and for a∼N(μ,Σ)\boldsymbol{a} \sim \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma}) it is φa(t)=exp⁡ ⁣(it⊤μ−12t⊤Σt)\varphi_a(\boldsymbol{t}) = \exp\!\big(i \boldsymbol{t}^\top\boldsymbol{\mu} - \tfrac12\boldsymbol{t}^\top\boldsymbol{\Sigma}\boldsymbol{t}\big). Then

φb(t)=E[eit⊤(Ca+d)]=eit⊤d  E[ei(C⊤t)⊤a]t⊤Ca=(C⊤t)⊤a=eit⊤d  φa(C⊤t)=exp⁡ ⁣(it⊤(Cμ+d)−12 t⊤CΣC⊤t),\begin{aligned} \varphi_b(\boldsymbol{t}) &= \mathbb{E}\big[e^{i \boldsymbol{t}^\top(\mathbf{C}\boldsymbol{a} + \boldsymbol{d})}\big] = e^{i \boldsymbol{t}^\top \boldsymbol{d}}\;\mathbb{E}\big[e^{i (\mathbf{C}^\top \boldsymbol{t})^\top \boldsymbol{a}}\big] && \boldsymbol{t}^\top\mathbf{C}\boldsymbol{a} = (\mathbf{C}^\top \boldsymbol{t})^\top \boldsymbol{a} \\ &= e^{i \boldsymbol{t}^\top \boldsymbol{d}}\;\varphi_a(\mathbf{C}^\top \boldsymbol{t}) = \exp\!\big(i \boldsymbol{t}^\top(\mathbf{C}\boldsymbol{\mu} + \boldsymbol{d}) - \tfrac12\,\boldsymbol{t}^\top\mathbf{C}\boldsymbol{\Sigma}\mathbf{C}^\top \boldsymbol{t}\big), \end{aligned}

which is the characteristic function of N(Cμ+d,CΣC⊤)\mathcal{N}(\mathbf{C}\boldsymbol{\mu} + \boldsymbol{d}, \mathbf{C}\boldsymbol{\Sigma}\mathbf{C}^\top).

Lemma 3 (Sum of independent Gaussians (convolution))

If a∼N(μ1,Σ1)\boldsymbol{a} \sim \mathcal{N}(\boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1) and b∼N(μ2,Σ2)\boldsymbol{b} \sim \mathcal{N}(\boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2) are independent, then a+b∼N(μ1+μ2,  Σ1+Σ2)\boldsymbol{a} + \boldsymbol{b} \sim \mathcal{N}(\boldsymbol{\mu}_1 + \boldsymbol{\mu}_2,\; \boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2). Equivalently, the convolution of two Gaussian densities is a Gaussian density:

∫N(a∣μ1,Σ1)  N(z−a∣μ2,Σ2)  da=N(z∣μ1+μ2,  Σ1+Σ2).\int \mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1)\;\mathcal{N}(\boldsymbol{z} - \boldsymbol{a} \mid \boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2)\;da = \mathcal{N}(\boldsymbol{z} \mid \boldsymbol{\mu}_1 + \boldsymbol{\mu}_2,\; \boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2).
Proof

Step 1. Stack the two variables. Independent Gaussians are jointly Gaussian, with zero cross-covariance:

[ab]∼N ⁣([μ1μ2],  [Σ100Σ2]).\begin{bmatrix} \boldsymbol{a} \\ \boldsymbol{b} \end{bmatrix} \sim \mathcal{N}\!\left(\begin{bmatrix}\boldsymbol{\mu}_1\\\boldsymbol{\mu}_2\end{bmatrix},\; \begin{bmatrix}\boldsymbol{\Sigma}_1 & \mathbf{0}\\\mathbf{0} & \boldsymbol{\Sigma}_2\end{bmatrix}\right).

Step 2. Write the sum as a linear map. With C=[ I    I ]\mathbf{C} = [\,\mathbf{I}\;\;\mathbf{I}\,],

a+b=[II][ab].\boldsymbol{a} + \boldsymbol{b} = \begin{bmatrix}\mathbf{I} & \mathbf{I}\end{bmatrix}\begin{bmatrix} \boldsymbol{a} \\ \boldsymbol{b} \end{bmatrix}.

Step 3. Apply Lemma 2 for the mean. C[μ1μ2]=μ1+μ2\mathbf{C}\begin{bmatrix}\boldsymbol{\mu}_1\\\boldsymbol{\mu}_2\end{bmatrix} = \boldsymbol{\mu}_1 + \boldsymbol{\mu}_2.

Step 4. Apply Lemma 2 for the covariance. Multiply the blocks:

[II][Σ100Σ2][II]=[Σ1Σ2][II]=Σ1+Σ2.\begin{bmatrix}\mathbf{I} & \mathbf{I}\end{bmatrix}\begin{bmatrix}\boldsymbol{\Sigma}_1 & \mathbf{0}\\\mathbf{0} & \boldsymbol{\Sigma}_2\end{bmatrix}\begin{bmatrix}\mathbf{I}\\\mathbf{I}\end{bmatrix} = \begin{bmatrix}\boldsymbol{\Sigma}_1 & \boldsymbol{\Sigma}_2\end{bmatrix}\begin{bmatrix}\mathbf{I}\\\mathbf{I}\end{bmatrix} = \boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2 .

Step 5. Why this is a convolution. The event a+b=z\boldsymbol{a} + \boldsymbol{b} = \boldsymbol{z} happens for every pair (a, b=z−a)(\boldsymbol{a},\, \boldsymbol{b} = \boldsymbol{z} - \boldsymbol{a}). Summing the probability of all such pairs, and using independence to factor the joint density,

pa+b(z)=∫pa(a) pb(z−a) da,p_{a+b}(\boldsymbol{z}) = \int p_a(\boldsymbol{a})\,p_b(\boldsymbol{z} - \boldsymbol{a})\,da ,

which is the convolution integral in the statement. Steps 3 and 4 evaluate it.

Example 1 (The convolution integral, computed by hand in one dimension)

Take independent scalars a∼N(μ1,s12)a \sim \mathcal{N}(\mu_1, s_1^2) and b∼N(μ2,s22)b \sim \mathcal{N}(\mu_2, s_2^2). We compute p(z)=∫N(a∣μ1,s12) N(z−a∣μ2,s22) dap(z) = \int \mathcal{N}(a \mid \mu_1, s_1^2)\,\mathcal{N}(z - a \mid \mu_2, s_2^2)\,da directly, without Lemma 2.

Step 1. Write out the integrand. Let r=z−μ2r = z - \mu_2, so that (z−a)−μ2=r−a(z - a) - \mu_2 = r - a:

N(a∣μ1,s12) N(z−a∣μ2,s22)=12πs1s2exp⁡ ⁣(−12[(a−μ1)2s12+(r−a)2s22]).\mathcal{N}(a \mid \mu_1, s_1^2)\,\mathcal{N}(z - a \mid \mu_2, s_2^2) = \frac{1}{2\pi s_1 s_2}\exp\!\Big(-\frac12\Big[\frac{(a - \mu_1)^2}{s_1^2} + \frac{(r - a)^2}{s_2^2}\Big]\Big).

Step 2. Expand both squares.

(a−μ1)2s12+(r−a)2s22=a2−2aμ1+μ12s12+a2−2ar+r2s22.\frac{(a - \mu_1)^2}{s_1^2} + \frac{(r - a)^2}{s_2^2} = \frac{a^2 - 2a\mu_1 + \mu_1^2}{s_1^2} + \frac{a^2 - 2ar + r^2}{s_2^2} .

Step 3. Collect powers of aa.

=p a2−2q a+c,wherep=1s12+1s22=s12+s22s12s22,q=μ1s12+rs22=μ1s22+rs12s12s22,c=μ12s12+r2s22.= p\,a^2 - 2q\,a + c, \qquad\text{where}\qquad \begin{aligned} p &= \frac{1}{s_1^2} + \frac{1}{s_2^2} = \frac{s_1^2 + s_2^2}{s_1^2 s_2^2}, \\ q &= \frac{\mu_1}{s_1^2} + \frac{r}{s_2^2} = \frac{\mu_1 s_2^2 + r s_1^2}{s_1^2 s_2^2}, \\ c &= \frac{\mu_1^2}{s_1^2} + \frac{r^2}{s_2^2}. \end{aligned}

Step 4. Complete the square in aa (Lemma 1 in one dimension):

p a2−2q a=p(a−qp)2−q2p.p\,a^2 - 2q\,a = p\Big(a - \frac{q}{p}\Big)^2 - \frac{q^2}{p} .

Step 5. Integrate over aa. Only the first term depends on aa, and it is an unnormalised Gaussian with variance 1/p1/p:

∫exp⁡ ⁣(−p2(a−qp)2) da=2πp.\int \exp\!\Big(-\frac{p}{2}\Big(a - \frac{q}{p}\Big)^2\Big)\,da = \sqrt{\frac{2\pi}{p}} .

So p(z)=12πs1s22πp exp⁡ ⁣(12(q2p−c))p(z) = \frac{1}{2\pi s_1 s_2}\sqrt{\frac{2\pi}{p}}\,\exp\!\big(\tfrac12(\tfrac{q^2}{p} - c)\big).

Step 6. Put q2p−c\tfrac{q^2}{p} - c over one denominator. The common denominator is s12s22(s12+s22)s_1^2 s_2^2(s_1^2 + s_2^2):

q2p=(μ1s22+rs12)2s12s22(s12+s22),c=(μ12s22+r2s12)(s12+s22)s12s22(s12+s22).\frac{q^2}{p} = \frac{(\mu_1 s_2^2 + r s_1^2)^2}{s_1^2 s_2^2(s_1^2 + s_2^2)}, \qquad c = \frac{(\mu_1^2 s_2^2 + r^2 s_1^2)(s_1^2 + s_2^2)}{s_1^2 s_2^2(s_1^2 + s_2^2)} .

Step 7. Expand the two numerators.

(μ1s22+rs12)2=μ12s24+2μ1r s12s22+r2s14,(μ12s22+r2s12)(s12+s22)=μ12s12s22+μ12s24+r2s14+r2s12s22.\begin{aligned} (\mu_1 s_2^2 + r s_1^2)^2 &= \mu_1^2 s_2^4 + 2\mu_1 r\,s_1^2 s_2^2 + r^2 s_1^4, \\ (\mu_1^2 s_2^2 + r^2 s_1^2)(s_1^2 + s_2^2) &= \mu_1^2 s_1^2 s_2^2 + \mu_1^2 s_2^4 + r^2 s_1^4 + r^2 s_1^2 s_2^2 . \end{aligned}

Step 8. Subtract. The terms μ12s24\mu_1^2 s_2^4 and r2s14r^2 s_1^4 cancel:

2μ1r s12s22−μ12s12s22−r2s12s22=− s12s22 (r−μ1)2.2\mu_1 r\,s_1^2 s_2^2 - \mu_1^2 s_1^2 s_2^2 - r^2 s_1^2 s_2^2 = -\,s_1^2 s_2^2\,(r - \mu_1)^2 .

Dividing by the common denominator, 12(q2p−c)=−(r−μ1)22(s12+s22)=−(z−μ1−μ2)22(s12+s22)\tfrac12\big(\tfrac{q^2}{p} - c\big) = -\dfrac{(r - \mu_1)^2}{2(s_1^2 + s_2^2)} = -\dfrac{(z - \mu_1 - \mu_2)^2}{2(s_1^2 + s_2^2)}.

Step 9. The constant. With 1/p=s12s22/(s12+s22)1/p = s_1^2 s_2^2/(s_1^2 + s_2^2),

12πs1s22πs12s22s12+s22=12π s12+s22=12π(s12+s22).\frac{1}{2\pi s_1 s_2}\sqrt{\frac{2\pi s_1^2 s_2^2}{s_1^2 + s_2^2}} = \frac{1}{\sqrt{2\pi}\,\sqrt{s_1^2 + s_2^2}} = \frac{1}{\sqrt{2\pi(s_1^2 + s_2^2)}} .

Step 10. Combine Steps 8 and 9. p(z)=N(z∣μ1+μ2,  s12+s22)p(z) = \mathcal{N}(z \mid \mu_1 + \mu_2,\; s_1^2 + s_2^2), as Lemma 3 says. Convolution adds variances: it blurs.

Multiplying densities: product

Lemma 4 (Product of two Gaussian densities in the same variable)

For any a\boldsymbol{a},

N(a∣μ1,Σ1)  N(a∣μ2,Σ2)=Z  N(a∣μc,Σc),\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1)\;\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2) = Z\;\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_c, \boldsymbol{\Sigma}_c),

with

Σc=(Σ1−1+Σ2−1)−1,μc=Σc(Σ1−1μ1+Σ2−1μ2),Z=N(μ1∣μ2,  Σ1+Σ2).\begin{aligned} \boldsymbol{\Sigma}_c &= \big(\boldsymbol{\Sigma}_1^{-1} + \boldsymbol{\Sigma}_2^{-1}\big)^{-1}, \\ \boldsymbol{\mu}_c &= \boldsymbol{\Sigma}_c\big(\boldsymbol{\Sigma}_1^{-1}\boldsymbol{\mu}_1 + \boldsymbol{\Sigma}_2^{-1}\boldsymbol{\mu}_2\big), \\ Z &= \mathcal{N}(\boldsymbol{\mu}_1 \mid \boldsymbol{\mu}_2,\; \boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2). \end{aligned}
Proof

Step 1. Add the exponents. A product of exponentials is the exponential of the sum. Expanding each exponent as in “Reading a Gaussian off its exponent”,

log⁡[N(a∣μ1,Σ1) N(a∣μ2,Σ2)]=(−12a⊤Σ1−1a+a⊤Σ1−1μ1)+(−12a⊤Σ2−1a+a⊤Σ2−1μ2)+const=−12 a⊤(Σ1−1+Σ2−1)a+a⊤(Σ1−1μ1+Σ2−1μ2)+const,\begin{aligned} &\log\big[\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1)\,\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2)\big] \\ &= \big(-\tfrac12\boldsymbol{a}^\top\boldsymbol{\Sigma}_1^{-1}\boldsymbol{a} + \boldsymbol{a}^\top\boldsymbol{\Sigma}_1^{-1}\boldsymbol{\mu}_1\big) \\ &\quad + \big(-\tfrac12\boldsymbol{a}^\top\boldsymbol{\Sigma}_2^{-1}\boldsymbol{a} + \boldsymbol{a}^\top\boldsymbol{\Sigma}_2^{-1}\boldsymbol{\mu}_2\big) + \text{const} \\ &= -\tfrac12\,\boldsymbol{a}^\top\big(\boldsymbol{\Sigma}_1^{-1} + \boldsymbol{\Sigma}_2^{-1}\big)\boldsymbol{a} + \boldsymbol{a}^\top\big(\boldsymbol{\Sigma}_1^{-1}\boldsymbol{\mu}_1 + \boldsymbol{\Sigma}_2^{-1}\boldsymbol{\mu}_2\big) + \text{const}, \end{aligned}

where “const” collects every term without a\boldsymbol{a}.

Step 2. Read off the information form. Step 1 is −12a⊤Pa+a⊤h+const-\tfrac12\boldsymbol{a}^\top\mathbf{P}\boldsymbol{a} + \boldsymbol{a}^\top \boldsymbol{h} + \text{const} with

P=Σ1−1+Σ2−1,h=Σ1−1μ1+Σ2−1μ2.\mathbf{P} = \boldsymbol{\Sigma}_1^{-1} + \boldsymbol{\Sigma}_2^{-1}, \qquad \boldsymbol{h} = \boldsymbol{\Sigma}_1^{-1}\boldsymbol{\mu}_1 + \boldsymbol{\Sigma}_2^{-1}\boldsymbol{\mu}_2 .

Step 3. Complete the square (Lemma 1). As a function of a\boldsymbol{a}, the product is proportional to N(a∣P−1h,P−1)=N(a∣μc,Σc)\mathcal{N}(\boldsymbol{a} \mid \mathbf{P}^{-1}\boldsymbol{h}, \mathbf{P}^{-1}) = \mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_c, \boldsymbol{\Sigma}_c). Call the constant of proportionality ZZ.

Step 4. Find ZZ by integrating both sides over a\boldsymbol{a}. The right-hand side integrates to ZZ, since a density integrates to one:

Z=∫N(a∣μ1,Σ1)  N(a∣μ2,Σ2)  da.Z = \int \mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1)\;\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2)\;da .

Step 5. Swap the arguments of the second factor. The Gaussian density depends on its two arguments only through (a−μ2)⊤Σ2−1(a−μ2)(\boldsymbol{a} - \boldsymbol{\mu}_2)^\top\boldsymbol{\Sigma}_2^{-1}(\boldsymbol{a} - \boldsymbol{\mu}_2), which is unchanged when a\boldsymbol{a} and μ2\boldsymbol{\mu}_2 swap. So N(a∣μ2,Σ2)=N(μ2∣a,Σ2)\mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2) = \mathcal{N}(\boldsymbol{\mu}_2 \mid \boldsymbol{a}, \boldsymbol{\Sigma}_2) and

Z=∫N(a∣μ1,Σ1)  N(μ2∣a,Σ2)  da.Z = \int \mathcal{N}(\boldsymbol{a} \mid \boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1)\;\mathcal{N}(\boldsymbol{\mu}_2 \mid \boldsymbol{a}, \boldsymbol{\Sigma}_2)\;da .

Step 6. Recognise a convolution. Consider the two-stage experiment a∼N(μ1,Σ1)\boldsymbol{a} \sim \mathcal{N}(\boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1), then v=a+u\boldsymbol{v} = \boldsymbol{a} + \boldsymbol{u} with independent u∼N(0,Σ2)\boldsymbol{u} \sim \mathcal{N}(\mathbf{0}, \boldsymbol{\Sigma}_2). Given a\boldsymbol{a}, v∼N(a,Σ2)\boldsymbol{v} \sim \mathcal{N}(\boldsymbol{a}, \boldsymbol{\Sigma}_2), so the integral in Step 5 is ∫p(a) p(v∣a) da=p(v)\int p(\boldsymbol{a})\,p(\boldsymbol{v} \mid \boldsymbol{a})\,da = p(\boldsymbol{v}) evaluated at v=μ2\boldsymbol{v} = \boldsymbol{\mu}_2.

Step 7. Apply Lemma 3. v=a+u∼N(μ1,Σ1+Σ2)\boldsymbol{v} = \boldsymbol{a} + \boldsymbol{u} \sim \mathcal{N}(\boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2). Therefore

Z=N(μ2∣μ1,  Σ1+Σ2)=N(μ1∣μ2,  Σ1+Σ2),Z = \mathcal{N}(\boldsymbol{\mu}_2 \mid \boldsymbol{\mu}_1,\; \boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2) = \mathcal{N}(\boldsymbol{\mu}_1 \mid \boldsymbol{\mu}_2,\; \boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2),

using the symmetry of Step 5 once more.

Note (Product, convolution and mixture are different operations)
  • Product of two densities in the same variable (Lemma 4): precisions add, so the result is narrower than either factor. This is Bayes’ rule combining two sources of information. It sharpens.
  • Convolution, the density of a sum of independent variables (Lemma 3): covariances add, so the result is wider. This is adding noise. It blurs.
  • Mixture π1N(μ1,Σ1)+π2N(μ2,Σ2)\pi_1\mathcal{N}(\boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1) + \pi_2\mathcal{N}(\boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2), a weighted sum of densities: not Gaussian in general (it can have two modes).

Steps 4 to 7 of Lemma 4 link the first two: the normaliser of a product is a convolution evaluated at a point.

Conditioning

Lemma 5 (Gaussian conditioning)

If

[ab]∼N ⁣([μaμb],[ΣaaΣabΣbaΣbb])\begin{bmatrix} \boldsymbol{a} \\ \boldsymbol{b} \end{bmatrix} \sim \mathcal{N}\!\left(\begin{bmatrix}\boldsymbol{\mu}_a\\\boldsymbol{\mu}_b\end{bmatrix}, \begin{bmatrix}\boldsymbol{\Sigma}_{aa} & \boldsymbol{\Sigma}_{ab}\\\boldsymbol{\Sigma}_{ba} & \boldsymbol{\Sigma}_{bb}\end{bmatrix}\right)

with Σaa\boldsymbol{\Sigma}_{aa} invertible, then

b∣a∼N(μb+ΣbaΣaa−1(a−μa),    Σbb−ΣbaΣaa−1Σab).\boldsymbol{b} \mid \boldsymbol{a} \sim \mathcal{N}\big(\boldsymbol{\mu}_b + \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}(\boldsymbol{a} - \boldsymbol{\mu}_a),\;\; \boldsymbol{\Sigma}_{bb} - \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab}\big).
Proof

Step 1. Remove from b\boldsymbol{b} the part predictable from a\boldsymbol{a}. Define

G=ΣbaΣaa−1,r=b−Ga.\mathbf{G} = \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}, \qquad \boldsymbol{r} = \boldsymbol{b} - \mathbf{G}\boldsymbol{a} .

Step 2. The residual is uncorrelated with a\boldsymbol{a}. Covariance is linear in each argument:

Cov⁡[r,a]=Cov⁡[b,a]−GCov⁡[a,a]=Σba−ΣbaΣaa−1Σaa=0.\operatorname{Cov}[\boldsymbol{r}, \boldsymbol{a}] = \operatorname{Cov}[\boldsymbol{b}, \boldsymbol{a}] - \mathbf{G}\operatorname{Cov}[\boldsymbol{a}, \boldsymbol{a}] = \boldsymbol{\Sigma}_{ba} - \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{aa} = \mathbf{0} .

Step 3. Uncorrelated implies independent here. The pair (r,a)(\boldsymbol{r}, \boldsymbol{a}) is a linear map of (a,b)(\boldsymbol{a}, \boldsymbol{b}):

[ra]=[−GII0][ab],\begin{bmatrix} \boldsymbol{r} \\ \boldsymbol{a} \end{bmatrix} = \begin{bmatrix} -\mathbf{G} & \mathbf{I} \\ \mathbf{I} & \mathbf{0} \end{bmatrix}\begin{bmatrix} \boldsymbol{a} \\ \boldsymbol{b} \end{bmatrix},

so it is jointly Gaussian (Lemma 2). By Step 2 its covariance is block diagonal, so its density factorises into a function of r\boldsymbol{r} times a function of a\boldsymbol{a}. Hence r\boldsymbol{r} and a\boldsymbol{a} are independent, and conditioning on a\boldsymbol{a} does not change the distribution of r\boldsymbol{r}.

Step 4. The mean of r\boldsymbol{r}. E[r]=μb−Gμa\mathbb{E}[\boldsymbol{r}] = \boldsymbol{\mu}_b - \mathbf{G}\boldsymbol{\mu}_a.

Step 5. The covariance of r\boldsymbol{r}. Expand Cov⁡[b−Ga]\operatorname{Cov}[\boldsymbol{b} - \mathbf{G}\boldsymbol{a}] by bilinearity, then substitute G\mathbf{G}:

Cov⁡[r]=Σbb−GΣab−ΣbaG⊤+GΣaaG⊤bilinearity=Σbb−ΣbaΣaa−1Σab−ΣbaΣaa−1Σab+ΣbaΣaa−1ΣaaΣaa−1ΣabG⊤=Σaa−1Σab=Σbb−ΣbaΣaa−1ΣabΣaaΣaa−1=I.\begin{aligned} \operatorname{Cov}[\boldsymbol{r}] &= \boldsymbol{\Sigma}_{bb} - \mathbf{G}\boldsymbol{\Sigma}_{ab} - \boldsymbol{\Sigma}_{ba}\mathbf{G}^\top + \mathbf{G}\boldsymbol{\Sigma}_{aa}\mathbf{G}^\top && \text{bilinearity} \\ &= \boldsymbol{\Sigma}_{bb} - \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab} - \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab} + \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{aa}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab} && \mathbf{G}^\top = \boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab} \\ &= \boldsymbol{\Sigma}_{bb} - \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab} && \boldsymbol{\Sigma}_{aa}\boldsymbol{\Sigma}_{aa}^{-1} = \mathbf{I} . \end{aligned}

Here G⊤=(Σaa−1)⊤Σba⊤=Σaa−1Σab\mathbf{G}^\top = (\boldsymbol{\Sigma}_{aa}^{-1})^\top\boldsymbol{\Sigma}_{ba}^\top = \boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab}, because a covariance matrix is symmetric, which makes Σaa−1\boldsymbol{\Sigma}_{aa}^{-1} symmetric and Σba⊤=Σab\boldsymbol{\Sigma}_{ba}^\top = \boldsymbol{\Sigma}_{ab}.

Step 6. Put b\boldsymbol{b} back. b=r+Ga\boldsymbol{b} = \boldsymbol{r} + \mathbf{G}\boldsymbol{a}. Given a\boldsymbol{a}, the term Ga\mathbf{G}\boldsymbol{a} is a constant, and adding a constant to a Gaussian shifts its mean only. So b∣a\boldsymbol{b} \mid \boldsymbol{a} is Gaussian with the covariance of Step 5 and mean

μb−Gμa+Ga=μb+ΣbaΣaa−1(a−μa).\boldsymbol{\mu}_b - \mathbf{G}\boldsymbol{\mu}_a + \mathbf{G}\boldsymbol{a} = \boldsymbol{\mu}_b + \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}(\boldsymbol{a} - \boldsymbol{\mu}_a).

Matrix identities

Lemma 6 (Push-through and Woodbury)

Let Σw\boldsymbol{\Sigma}_w be M×MM\times M and positive definite, Φ\boldsymbol{\Phi} be N×MN\times M, σn2>0\sigma_n^2 > 0, and write

A=σn−2Φ⊤Φ+Σw−1    (M×M),G=ΦΣwΦ⊤+σn2I    (N×N).\mathbf{A} = \sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi} + \boldsymbol{\Sigma}_w^{-1} \;\;(M\times M), \qquad \mathbf{G} = \boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top + \sigma_n^2\mathbf{I} \;\;(N\times N).

Then

(push-through)σn−2 A−1Φ⊤=ΣwΦ⊤G−1,(Woodbury)A−1=Σw−ΣwΦ⊤G−1ΦΣw.\begin{aligned} \text{(push-through)}\quad & \sigma_n^{-2}\,\mathbf{A}^{-1}\boldsymbol{\Phi}^\top = \boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\mathbf{G}^{-1}, \\ \text{(Woodbury)}\quad & \mathbf{A}^{-1} = \boldsymbol{\Sigma}_w - \boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\mathbf{G}^{-1}\boldsymbol{\Phi}\boldsymbol{\Sigma}_w . \end{aligned}
Proof

Push-through.

Step 1. Multiply Φ⊤\boldsymbol{\Phi}^\top by G\mathbf{G} and expand.

σn−2Φ⊤G=σn−2Φ⊤ΦΣwΦ⊤+Φ⊤.\sigma_n^{-2}\boldsymbol{\Phi}^\top\mathbf{G} = \sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top + \boldsymbol{\Phi}^\top .

Step 2. Insert an identity. Write Φ⊤=Σw−1ΣwΦ⊤\boldsymbol{\Phi}^\top = \boldsymbol{\Sigma}_w^{-1}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top in the second term, then factor ΣwΦ⊤\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top out on the right:

σn−2Φ⊤G=(σn−2Φ⊤Φ+Σw−1)ΣwΦ⊤=AΣwΦ⊤.\sigma_n^{-2}\boldsymbol{\Phi}^\top\mathbf{G} = \big(\sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi} + \boldsymbol{\Sigma}_w^{-1}\big)\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top = \mathbf{A}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top .

Step 3. Multiply by the inverses. Both A\mathbf{A} and G\mathbf{G} are positive definite, hence invertible. Multiply Step 2 by A−1\mathbf{A}^{-1} on the left and G−1\mathbf{G}^{-1} on the right:

σn−2A−1Φ⊤=ΣwΦ⊤G−1.\sigma_n^{-2}\mathbf{A}^{-1}\boldsymbol{\Phi}^\top = \boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\mathbf{G}^{-1} .

Woodbury.

Step 4. Name the candidate. Let R=Σw−ΣwΦ⊤G−1ΦΣw\mathbf{R} = \boldsymbol{\Sigma}_w - \boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\mathbf{G}^{-1}\boldsymbol{\Phi}\boldsymbol{\Sigma}_w. For square matrices, AR=I\mathbf{A}\mathbf{R} = \mathbf{I} implies R=A−1\mathbf{R} = \mathbf{A}^{-1}, so it suffices to compute AR\mathbf{A}\mathbf{R}.

Step 5. Multiply out.

AR=AΣw−AΣwΦ⊤G−1ΦΣw=(σn−2Φ⊤ΦΣw+I)−AΣwΦ⊤G−1ΦΣwdefinition of A=σn−2Φ⊤ΦΣw+I−σn−2Φ⊤GG−1ΦΣwStep 2: AΣwΦ⊤=σn−2Φ⊤G=σn−2Φ⊤ΦΣw+I−σn−2Φ⊤ΦΣwGG−1=I=I.\begin{aligned} \mathbf{A}\mathbf{R} &= \mathbf{A}\boldsymbol{\Sigma}_w - \mathbf{A}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\mathbf{G}^{-1}\boldsymbol{\Phi}\boldsymbol{\Sigma}_w \\ &= \big(\sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi}\boldsymbol{\Sigma}_w + \mathbf{I}\big) - \mathbf{A}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\mathbf{G}^{-1}\boldsymbol{\Phi}\boldsymbol{\Sigma}_w && \text{definition of } \mathbf{A} \\ &= \sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi}\boldsymbol{\Sigma}_w + \mathbf{I} - \sigma_n^{-2}\boldsymbol{\Phi}^\top\mathbf{G}\mathbf{G}^{-1}\boldsymbol{\Phi}\boldsymbol{\Sigma}_w && \text{Step 2: } \mathbf{A}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top = \sigma_n^{-2}\boldsymbol{\Phi}^\top\mathbf{G} \\ &= \sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi}\boldsymbol{\Sigma}_w + \mathbf{I} - \sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi}\boldsymbol{\Sigma}_w && \mathbf{G}\mathbf{G}^{-1} = \mathbf{I} \\ &= \mathbf{I} . \end{aligned}

Lemma 7 (Derivatives of the inverse and the log-determinant)

For a symmetric positive definite K(θ)\mathbf{K}(\theta) that depends smoothly on a scalar θ\theta,

∂K−1∂θ=−K−1∂K∂θK−1,∂∂θlog⁡∣K∣=tr⁡ ⁣(K−1∂K∂θ).\frac{\partial\mathbf{K}^{-1}}{\partial\theta} = -\mathbf{K}^{-1}\frac{\partial\mathbf{K}}{\partial\theta}\mathbf{K}^{-1}, \qquad \frac{\partial}{\partial\theta}\log\lvert\mathbf{K}\rvert = \operatorname{tr}\!\Big(\mathbf{K}^{-1}\frac{\partial\mathbf{K}}{\partial\theta}\Big).
Proof

Write ∂\partial for ∂/∂θ\partial/\partial\theta.

Inverse.

Step 1. Differentiate the identity KK−1=I\mathbf{K}\mathbf{K}^{-1} = \mathbf{I} with the product rule; the right-hand side is constant:

(∂K) K−1+K (∂K−1)=0.(\partial\mathbf{K})\,\mathbf{K}^{-1} + \mathbf{K}\,(\partial\mathbf{K}^{-1}) = \mathbf{0} .

Step 2. Solve for ∂K−1\partial\mathbf{K}^{-1}. Move the first term across and multiply on the left by K−1\mathbf{K}^{-1}:

∂K−1=−K−1(∂K) K−1.\partial\mathbf{K}^{-1} = -\mathbf{K}^{-1}(\partial\mathbf{K})\,\mathbf{K}^{-1} .

Log-determinant.

Step 3. Perturb and factor. For a small ϵ\epsilon and E=∂K\mathbf{E} = \partial\mathbf{K},   K(θ+ϵ)=K+ϵE+O(ϵ2)=K(I+ϵK−1E)+O(ϵ2)\;\mathbf{K}(\theta + \epsilon) = \mathbf{K} + \epsilon\mathbf{E} + O(\epsilon^2) = \mathbf{K}\big(\mathbf{I} + \epsilon\mathbf{K}^{-1}\mathbf{E}\big) + O(\epsilon^2).

Step 4. Take the log-determinant. The determinant of a product is the product of determinants:

log⁡∣K+ϵE∣=log⁡∣K∣+log⁡∣I+ϵK−1E∣.\log\lvert\mathbf{K} + \epsilon\mathbf{E}\rvert = \log\lvert\mathbf{K}\rvert + \log\lvert\mathbf{I} + \epsilon\mathbf{K}^{-1}\mathbf{E}\rvert .

Step 5. Use eigenvalues. Let λ1,…,λN\lambda_1, \dots, \lambda_N be the eigenvalues of K−1E\mathbf{K}^{-1}\mathbf{E} (they are real: the matrix is similar to the symmetric K−1/2EK−1/2\mathbf{K}^{-1/2}\mathbf{E}\mathbf{K}^{-1/2}). The eigenvalues of I+ϵK−1E\mathbf{I} + \epsilon\mathbf{K}^{-1}\mathbf{E} are 1+ϵλi1 + \epsilon\lambda_i, and a determinant is the product of the eigenvalues:

log⁡∣I+ϵK−1E∣=∑i=1Nlog⁡(1+ϵλi)=ϵ∑i=1Nλi+O(ϵ2)=ϵtr⁡(K−1E)+O(ϵ2),\log\lvert\mathbf{I} + \epsilon\mathbf{K}^{-1}\mathbf{E}\rvert = \sum_{i=1}^N \log(1 + \epsilon\lambda_i) = \epsilon\sum_{i=1}^N \lambda_i + O(\epsilon^2) = \epsilon\operatorname{tr}\big(\mathbf{K}^{-1}\mathbf{E}\big) + O(\epsilon^2),

using log⁡(1+u)=u+O(u2)\log(1 + u) = u + O(u^2) and that the trace is the sum of the eigenvalues.

Step 6. Take the limit. Subtract log⁡∣K∣\log\lvert\mathbf{K}\rvert, divide by ϵ\epsilon and let ϵ→0\epsilon \to 0:   ∂log⁡∣K∣=tr⁡(K−1∂K)\;\partial\log\lvert\mathbf{K}\rvert = \operatorname{tr}(\mathbf{K}^{-1}\partial\mathbf{K}).

Putting it together: the linear-Gaussian model

Lemma 4 multiplies two densities in the same variable. The product that appears most often in practice has two different variables: a prior over a hidden x\boldsymbol{x}, times a likelihood for an observation y\boldsymbol{y} whose mean depends linearly on x\boldsymbol{x}. Bayesian linear regression, the GP and every step of a Kalman filter are this product.

Lemma 8 (Linear-Gaussian model)

Let x∼N(μx,Σx)\boldsymbol{x} \sim \mathcal{N}(\boldsymbol{\mu}_x, \boldsymbol{\Sigma}_x) and y∣x∼N(Cx+d, S)\boldsymbol{y} \mid \boldsymbol{x} \sim \mathcal{N}(\mathbf{C}\boldsymbol{x} + \boldsymbol{d},\, \mathbf{S}). Then

N(y∣Cx+d,S)  N(x∣μx,Σx)=N(y∣Cμx+d,  CΣxC⊤+S)⏟p(y)  N(x∣μx∣y,Σx∣y)⏟p(x∣y),\begin{aligned} &\mathcal{N}(\boldsymbol{y} \mid \mathbf{C}\boldsymbol{x} + \boldsymbol{d}, \mathbf{S})\;\mathcal{N}(\boldsymbol{x} \mid \boldsymbol{\mu}_x, \boldsymbol{\Sigma}_x) \\ &\quad= \underbrace{\mathcal{N}(\boldsymbol{y} \mid \mathbf{C}\boldsymbol{\mu}_x + \boldsymbol{d},\; \mathbf{C}\boldsymbol{\Sigma}_x\mathbf{C}^\top + \mathbf{S})}_{p(\boldsymbol{y})}\; \underbrace{\mathcal{N}(\boldsymbol{x} \mid \boldsymbol{\mu}_{x \mid y}, \boldsymbol{\Sigma}_{x \mid y})}_{p(\boldsymbol{x} \mid \boldsymbol{y})}, \end{aligned}

where the posterior can be written in two equivalent forms:

(moment form)Σx∣y=Σx−ΣxC⊤(CΣxC⊤+S)−1CΣx,μx∣y=μx+ΣxC⊤(CΣxC⊤+S)−1(y−Cμx−d);(information form)Σx∣y=(Σx−1+C⊤S−1C)−1,μx∣y=Σx∣y(Σx−1μx+C⊤S−1(y−d)).\begin{aligned} \text{(moment form)}\quad \boldsymbol{\Sigma}_{x \mid y} &= \boldsymbol{\Sigma}_x - \boldsymbol{\Sigma}_x\mathbf{C}^\top(\mathbf{C}\boldsymbol{\Sigma}_x\mathbf{C}^\top + \mathbf{S})^{-1}\mathbf{C}\boldsymbol{\Sigma}_x, \\ \boldsymbol{\mu}_{x \mid y} &= \boldsymbol{\mu}_x + \boldsymbol{\Sigma}_x\mathbf{C}^\top(\mathbf{C}\boldsymbol{\Sigma}_x\mathbf{C}^\top + \mathbf{S})^{-1}(\boldsymbol{y} - \mathbf{C}\boldsymbol{\mu}_x - \boldsymbol{d}); \\[4pt] \text{(information form)}\quad \boldsymbol{\Sigma}_{x \mid y} &= \big(\boldsymbol{\Sigma}_x^{-1} + \mathbf{C}^\top\mathbf{S}^{-1}\mathbf{C}\big)^{-1}, \\ \boldsymbol{\mu}_{x \mid y} &= \boldsymbol{\Sigma}_{x \mid y}\big(\boldsymbol{\Sigma}_x^{-1}\boldsymbol{\mu}_x + \mathbf{C}^\top\mathbf{S}^{-1}(\boldsymbol{y} - \boldsymbol{d})\big). \end{aligned}
Proof

Moment form: build the joint, then factor it the other way.

Step 1. Write the model as a sum. Draw x\boldsymbol{x}, then add independent noise:

y=Cx+d+ε,ε∼N(0,S) independent of x.\boldsymbol{y} = \mathbf{C}\boldsymbol{x} + \boldsymbol{d} + \boldsymbol{\varepsilon}, \qquad \boldsymbol{\varepsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{S}) \text{ independent of } \boldsymbol{x}.

Step 2. Stack. The pair (x,y)(\boldsymbol{x}, \boldsymbol{y}) is a linear map of the independent pair (x,ε)(\boldsymbol{x}, \boldsymbol{\varepsilon}), plus a constant:

[xy]=[I0CI]⏟M[xε]+[0d],[xε]∼N ⁣([μx0],[Σx00S]).\begin{bmatrix} \boldsymbol{x} \\ \boldsymbol{y} \end{bmatrix} = \underbrace{\begin{bmatrix} \mathbf{I} & \mathbf{0} \\ \mathbf{C} & \mathbf{I} \end{bmatrix}}_{\mathbf{M}} \begin{bmatrix} \boldsymbol{x} \\ \boldsymbol{\varepsilon} \end{bmatrix} + \begin{bmatrix} \mathbf{0} \\ \boldsymbol{d} \end{bmatrix}, \qquad \begin{bmatrix} \boldsymbol{x} \\ \boldsymbol{\varepsilon} \end{bmatrix} \sim \mathcal{N}\!\left(\begin{bmatrix} \boldsymbol{\mu}_x \\ \mathbf{0} \end{bmatrix}, \begin{bmatrix} \boldsymbol{\Sigma}_x & \mathbf{0} \\ \mathbf{0} & \mathbf{S} \end{bmatrix}\right).

Step 3. The joint mean (Lemma 2): M[μx0]+[0d]=[μxCμx+d]\mathbf{M}\begin{bmatrix} \boldsymbol{\mu}_x \\ \mathbf{0} \end{bmatrix} + \begin{bmatrix} \mathbf{0} \\ \boldsymbol{d} \end{bmatrix} = \begin{bmatrix} \boldsymbol{\mu}_x \\ \mathbf{C}\boldsymbol{\mu}_x + \boldsymbol{d} \end{bmatrix}.

Step 4. The joint covariance (Lemma 2), one block product at a time:

M[Σx00S]M⊤=[Σx0CΣxS][IC⊤0I]=[ΣxΣxC⊤CΣxCΣxC⊤+S].\begin{aligned} \mathbf{M}\begin{bmatrix} \boldsymbol{\Sigma}_x & \mathbf{0} \\ \mathbf{0} & \mathbf{S} \end{bmatrix}\mathbf{M}^\top &= \begin{bmatrix} \boldsymbol{\Sigma}_x & \mathbf{0} \\ \mathbf{C}\boldsymbol{\Sigma}_x & \mathbf{S} \end{bmatrix} \begin{bmatrix} \mathbf{I} & \mathbf{C}^\top \\ \mathbf{0} & \mathbf{I} \end{bmatrix} \\ &= \begin{bmatrix} \boldsymbol{\Sigma}_x & \boldsymbol{\Sigma}_x\mathbf{C}^\top \\ \mathbf{C}\boldsymbol{\Sigma}_x & \mathbf{C}\boldsymbol{\Sigma}_x\mathbf{C}^\top + \mathbf{S} \end{bmatrix}. \end{aligned}

Step 5. The marginal of y\boldsymbol{y}. Selecting a block of a Gaussian vector is the linear map [ 0    I ][\,\mathbf{0}\;\;\mathbf{I}\,], so by Lemma 2 the marginal keeps the bottom block of the mean and the bottom-right block of the covariance: p(y)=N(Cμx+d,  CΣxC⊤+S)p(\boldsymbol{y}) = \mathcal{N}(\mathbf{C}\boldsymbol{\mu}_x + \boldsymbol{d},\; \mathbf{C}\boldsymbol{\Sigma}_x\mathbf{C}^\top + \mathbf{S}).

Step 6. The conditional of x\boldsymbol{x}. Apply Lemma 5 with a=y\boldsymbol{a} = \boldsymbol{y} and b=x\boldsymbol{b} = \boldsymbol{x}, reading the blocks from Step 4:

Σaa=CΣxC⊤+S,Σba=ΣxC⊤,Σab=CΣx,Σbb=Σx.\boldsymbol{\Sigma}_{aa} = \mathbf{C}\boldsymbol{\Sigma}_x\mathbf{C}^\top + \mathbf{S}, \qquad \boldsymbol{\Sigma}_{ba} = \boldsymbol{\Sigma}_x\mathbf{C}^\top, \qquad \boldsymbol{\Sigma}_{ab} = \mathbf{C}\boldsymbol{\Sigma}_x, \qquad \boldsymbol{\Sigma}_{bb} = \boldsymbol{\Sigma}_x .

This gives the moment form.

Step 7. Joint equals marginal times conditional. p(y∣x) p(x)=p(x,y)=p(y) p(x∣y)p(\boldsymbol{y} \mid \boldsymbol{x})\,p(\boldsymbol{x}) = p(\boldsymbol{x}, \boldsymbol{y}) = p(\boldsymbol{y})\,p(\boldsymbol{x} \mid \boldsymbol{y}), which is the identity in the statement.

Information form: treat the likelihood as a function of x\boldsymbol{x}.

Step 8. Expand the likelihood exponent in x\boldsymbol{x}. Write e=y−d\boldsymbol{e} = \boldsymbol{y} - \boldsymbol{d}. Exactly as in Step 3 of Proposition 1 below,

−12(e−Cx)⊤S−1(e−Cx)=−12 x⊤C⊤S−1C x+x⊤C⊤S−1e+const.-\tfrac12(\boldsymbol{e} - \mathbf{C}\boldsymbol{x})^\top\mathbf{S}^{-1}(\boldsymbol{e} - \mathbf{C}\boldsymbol{x}) = -\tfrac12\,\boldsymbol{x}^\top\mathbf{C}^\top\mathbf{S}^{-1}\mathbf{C}\,\boldsymbol{x} + \boldsymbol{x}^\top\mathbf{C}^\top\mathbf{S}^{-1}\boldsymbol{e} + \text{const}.

As a function of x\boldsymbol{x}, the likelihood is a Gaussian in information form with precision C⊤S−1C\mathbf{C}^\top\mathbf{S}^{-1}\mathbf{C} and linear coefficient C⊤S−1e\mathbf{C}^\top\mathbf{S}^{-1}\boldsymbol{e}.

Step 9. Expand the prior exponent (“Reading a Gaussian off its exponent”): −12 x⊤Σx−1x+x⊤Σx−1μx+const-\tfrac12\,\boldsymbol{x}^\top\boldsymbol{\Sigma}_x^{-1}\boldsymbol{x} + \boldsymbol{x}^\top\boldsymbol{\Sigma}_x^{-1}\boldsymbol{\mu}_x + \text{const}.

Step 10. Add and complete the square. The log of the product is −12x⊤Px+x⊤h+const-\tfrac12\boldsymbol{x}^\top\mathbf{P}\boldsymbol{x} + \boldsymbol{x}^\top\boldsymbol{h} + \text{const} with P=Σx−1+C⊤S−1C\mathbf{P} = \boldsymbol{\Sigma}_x^{-1} + \mathbf{C}^\top\mathbf{S}^{-1}\mathbf{C} and h=Σx−1μx+C⊤S−1e\boldsymbol{h} = \boldsymbol{\Sigma}_x^{-1}\boldsymbol{\mu}_x + \mathbf{C}^\top\mathbf{S}^{-1}\boldsymbol{e}. Lemma 1 gives the information form.

Step 11. The two forms agree. Steps 6 and 10 describe the same density p(x∣y)p(\boldsymbol{x} \mid \boldsymbol{y}), so their means and covariances are equal. (Equating the two covariances is the Woodbury identity in general form; Lemma 6 is the case Σx=Σw\boldsymbol{\Sigma}_x = \boldsymbol{\Sigma}_w, C=Φ\mathbf{C} = \boldsymbol{\Phi}, S=σn2I\mathbf{S} = \sigma_n^2\mathbf{I}.)

Two special cases are worth seeing at once. With C=I\mathbf{C} = \mathbf{I} and d=0\boldsymbol{d} = \mathbf{0}, Lemma 8 is Lemma 4: the same-variable product is the case where the observation is a noisy copy of the hidden variable. With C=Φ\mathbf{C} = \boldsymbol{\Phi}, it is Bayesian linear regression (Section 2).

Remark (Two coordinate systems for one Gaussian)

A Gaussian can be stored in moment form (μ,Σ)(\boldsymbol{\mu}, \boldsymbol{\Sigma}) or in information form (h,P)=(Σ−1μ,Σ−1)(\boldsymbol{h}, \mathbf{P}) = (\boldsymbol{\Sigma}^{-1}\boldsymbol{\mu}, \boldsymbol{\Sigma}^{-1}). Each makes a different half of the toolkit trivial:

  • Moment form. Marginalising is reading off blocks (Step 5 above). Sums of independent variables add covariances (Lemma 3). Conditioning needs an inverse (the Schur complement of Lemma 5).
  • Information form. Products in the same variable add h\boldsymbol{h} and P\mathbf{P} (Lemma 4). Conditioning is reading off blocks: fix a\boldsymbol{a} in the joint exponent −12[a;b]⊤P[a;b]-\tfrac12[\boldsymbol{a}; \boldsymbol{b}]^\top\mathbf{P}[\boldsymbol{a}; \boldsymbol{b}], and the term quadratic in b\boldsymbol{b} is −12b⊤Pbbb-\tfrac12\boldsymbol{b}^\top\mathbf{P}_{bb}\boldsymbol{b}, so the conditional precision is the block Pbb\mathbf{P}_{bb}. Marginalising needs the inverse.

The same split explains why zeros mean different things: a zero in Σ\boldsymbol{\Sigma} is marginal independence, a zero in P\mathbf{P} is conditional independence given all other variables. Sparse precisions are what make Gaussian Markov random fields, and information filters for large spatial fields, cheap.

The characteristic function gives a third view. For independent a\boldsymbol{a} and b\boldsymbol{b}, φa+b(t)=E[eit⊤a] E[eit⊤b]=φa(t) φb(t)\varphi_{\boldsymbol{a}+\boldsymbol{b}}(\boldsymbol{t}) = \mathbb{E}[e^{i\boldsymbol{t}^\top\boldsymbol{a}}]\,\mathbb{E}[e^{i\boldsymbol{t}^\top\boldsymbol{b}}] = \varphi_{\boldsymbol{a}}(\boldsymbol{t})\,\varphi_{\boldsymbol{b}}(\boldsymbol{t}), and multiplying two Gaussian characteristic functions adds the exponents: exp⁡ ⁣(it⊤(μ1+μ2)−12t⊤(Σ1+Σ2)t)\exp\!\big(i\boldsymbol{t}^\top(\boldsymbol{\mu}_1 + \boldsymbol{\mu}_2) - \tfrac12\boldsymbol{t}^\top(\boldsymbol{\Sigma}_1 + \boldsymbol{\Sigma}_2)\boldsymbol{t}\big). That is Lemma 3 in one line. Convolution of densities is multiplication of their Fourier transforms, and a Gaussian keeps its shape in both domains, which is why both operations stay Gaussian.

Note (When to think in precision, when in covariance)

Covariance answers a global question; precision answers a local one. Σij\Sigma_{ij} is how xix_i and xjx_j co-vary when every other variable is unknown (marginalised out). PijP_{ij} describes how xix_i and xjx_j relate when every other variable is known (held fixed). Concretely, 1/Pii1/P_{ii} is the variance of xix_i given all the others (the conditional precision of one variable is its diagonal block, by the remark above), and −Pij/PiiPjj-P_{ij}/\sqrt{P_{ii}P_{jj}} is their partial correlation.

A worked example. Take a random walk: x1∼N(0,1)x_1 \sim \mathcal{N}(0, 1), x2=x1+e2x_2 = x_1 + e_2, x3=x2+e3x_3 = x_2 + e_3, with independent e2,e3∼N(0,1)e_2, e_3 \sim \mathcal{N}(0, 1). Then

Σ=[111122123],P=Σ−1=[2−10−12−10−11].\boldsymbol{\Sigma} = \begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 2 \\ 1 & 2 & 3 \end{bmatrix}, \qquad \mathbf{P} = \boldsymbol{\Sigma}^{-1} = \begin{bmatrix} 2 & -1 & 0 \\ -1 & 2 & -1 \\ 0 & -1 & 1 \end{bmatrix}.

(Check: multiply them; every row of Σ\boldsymbol{\Sigma} times every column of P\mathbf{P} gives 11 on the diagonal and 00 off it.) The covariance is dense: x1x_1 and x3x_3 are correlated, because x3x_3 inherits x1x_1. The precision has P13=0P_{13} = 0: once x2x_2 is known, x1x_1 tells you nothing more about x3x_3. The precision shows the model’s wiring (each variable talks only to its neighbours); the covariance shows its consequences.

Rules of thumb.

  • Information adds in precision; noise adds in covariance. Combining evidence (multiplying likelihoods, Bayes’ rule, Lemma 4) adds precisions. Adding independent noise or propagating forward in time (sums, Lemma 3) adds covariances. A Kalman filter does both: the predict step adds process noise to Σ\boldsymbol{\Sigma}, the update step adds measurement information to P\mathbf{P}.
  • Local models are sparse in precision. Markov chains, PDE stencils, spatial grids and graphs couple each variable to a few neighbours, so P\mathbf{P} is sparse even though Σ\boldsymbol{\Sigma} is dense. Matérn-12\tfrac12 GPs on sorted 1-D inputs have tridiagonal precision, as in the example; Matérn fields in two and three dimensions have sparse precision through the SPDE link (Lindgren, Rue and Lindström, 2011). Kernels, in contrast, specify Σ\boldsymbol{\Sigma} directly, which is why GP code works in covariance form.
  • The extremes live on opposite sides. “Know nothing” is P=0\mathbf{P} = \mathbf{0}, which has no covariance (Σ=∞\boldsymbol{\Sigma} = \infty); “know exactly” is Σ=0\boldsymbol{\Sigma} = \mathbf{0}, which has no precision. A flat prior or a likelihood that constrains only some directions of x\boldsymbol{x} (a singular C⊤S−1C\mathbf{C}^\top\mathbf{S}^{-1}\mathbf{C} in Lemma 8) is natural in precision form; noise-free interpolation and hard constraints are natural in covariance form.
  • Invert the smaller matrix. The moment form of Lemma 8 inverts a matrix the size of y\boldsymbol{y}, the information form one the size of x\boldsymbol{x}. Section 3 is this choice: function space (N×NN\times N) versus weight space (M×MM\times M).

2. Bayesian linear regression

We observe {(xi,yi)}i=1N\{(\boldsymbol{x}_i, y_i)\}_{i=1}^N with

yi=f(xi)+εi,εi∼iidN(0,σn2),f(x)=ϕ(x)⊤w.y_i = f(\boldsymbol{x}_i) + \varepsilon_i, \qquad \varepsilon_i \overset{\text{iid}}{\sim} \mathcal{N}(0, \sigma_n^2), \qquad f(\boldsymbol{x}) = \boldsymbol{\phi}(\boldsymbol{x})^\top \boldsymbol{w} .

Stacking the NN equations, y=Φw+ε\boldsymbol{y} = \boldsymbol{\Phi}\boldsymbol{w} + \boldsymbol{\varepsilon} with ε∼N(0,σn2I)\boldsymbol{\varepsilon} \sim \mathcal{N}(\mathbf{0}, \sigma_n^2\mathbf{I}), so the likelihood is p(y∣X,w)=N(y∣Φw,σn2I)p(\boldsymbol{y} \mid \mathbf{X}, \boldsymbol{w}) = \mathcal{N}(\boldsymbol{y} \mid \boldsymbol{\Phi}\boldsymbol{w}, \sigma_n^2\mathbf{I}). Least squares returns one w\boldsymbol{w} and hides how many others fit almost as well; the Bayesian treatment keeps a distribution over w\boldsymbol{w}, starting from the prior w∼N(0,Σw)\boldsymbol{w} \sim \mathcal{N}(\mathbf{0}, \boldsymbol{\Sigma}_w).

Proposition 1 (Posterior over weights)

p(w∣X,y)=N(w∣σn−2A−1Φ⊤y,  A−1),A=σn−2Φ⊤Φ+Σw−1.p(\boldsymbol{w} \mid \mathbf{X}, \boldsymbol{y}) = \mathcal{N}\big(\boldsymbol{w} \mid \sigma_n^{-2}\mathbf{A}^{-1}\boldsymbol{\Phi}^\top \boldsymbol{y},\; \mathbf{A}^{-1}\big), \qquad \mathbf{A} = \sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi} + \boldsymbol{\Sigma}_w^{-1}.
Proof

Step 1. Bayes’ rule. The evidence p(y∣X)p(\boldsymbol{y} \mid \mathbf{X}) does not depend on w\boldsymbol{w}:

p(w∣X,y)=p(y∣X,w) p(w)p(y∣X)  ∝  p(y∣X,w) p(w).p(\boldsymbol{w} \mid \mathbf{X}, \boldsymbol{y}) = \frac{p(\boldsymbol{y} \mid \mathbf{X}, \boldsymbol{w})\,p(\boldsymbol{w})}{p(\boldsymbol{y} \mid \mathbf{X})} \;\propto\; p(\boldsymbol{y} \mid \mathbf{X}, \boldsymbol{w})\,p(\boldsymbol{w}) .

Step 2. Take logs. Keep only the terms that depend on w\boldsymbol{w}:

log⁡p(w∣X,y)=−12σn2(y−Φw)⊤(y−Φw)−12 w⊤Σw−1w+const.\log p(\boldsymbol{w} \mid \mathbf{X}, \boldsymbol{y}) = -\frac{1}{2\sigma_n^2}(\boldsymbol{y} - \boldsymbol{\Phi}\boldsymbol{w})^\top(\boldsymbol{y} - \boldsymbol{\Phi}\boldsymbol{w}) - \tfrac12\,\boldsymbol{w}^\top\boldsymbol{\Sigma}_w^{-1}\boldsymbol{w} + \text{const}.

Step 3. Expand the residual square.

(y−Φw)⊤(y−Φw)=y⊤y−y⊤Φw−w⊤Φ⊤y+w⊤Φ⊤Φw=y⊤y−2 w⊤Φ⊤y+w⊤Φ⊤Φw.\begin{aligned} (\boldsymbol{y} - \boldsymbol{\Phi}\boldsymbol{w})^\top(\boldsymbol{y} - \boldsymbol{\Phi}\boldsymbol{w}) &= \boldsymbol{y}^\top \boldsymbol{y} - \boldsymbol{y}^\top\boldsymbol{\Phi}\boldsymbol{w} - \boldsymbol{w}^\top\boldsymbol{\Phi}^\top \boldsymbol{y} + \boldsymbol{w}^\top\boldsymbol{\Phi}^\top\boldsymbol{\Phi}\boldsymbol{w} \\ &= \boldsymbol{y}^\top \boldsymbol{y} - 2\,\boldsymbol{w}^\top\boldsymbol{\Phi}^\top \boldsymbol{y} + \boldsymbol{w}^\top\boldsymbol{\Phi}^\top\boldsymbol{\Phi}\boldsymbol{w} . \end{aligned}

Reasons, line by line: (1) multiply out; (2) y⊤Φw=w⊤Φ⊤y\boldsymbol{y}^\top\boldsymbol{\Phi}\boldsymbol{w} = \boldsymbol{w}^\top\boldsymbol{\Phi}^\top \boldsymbol{y}, the same scalar.

Step 4. Substitute and collect. y⊤y\boldsymbol{y}^\top \boldsymbol{y} joins the constant. Grouping the quadratic and linear terms in w\boldsymbol{w},

log⁡p(w∣X,y)=−12 w⊤(σn−2Φ⊤Φ+Σw−1)⏟A w  +  w⊤(σn−2Φ⊤y)⏟h+const.\log p(\boldsymbol{w} \mid \mathbf{X}, \boldsymbol{y}) = -\tfrac12\,\boldsymbol{w}^\top\underbrace{\big(\sigma_n^{-2}\boldsymbol{\Phi}^\top\boldsymbol{\Phi} + \boldsymbol{\Sigma}_w^{-1}\big)}_{\mathbf{A}}\,\boldsymbol{w} \;+\; \boldsymbol{w}^\top\underbrace{\big(\sigma_n^{-2}\boldsymbol{\Phi}^\top \boldsymbol{y}\big)}_{h} + \text{const}.

Step 5. Complete the square (Lemma 1 with P=A\mathbf{P} = \mathbf{A}): the posterior is N(A−1h,A−1)=N(σn−2A−1Φ⊤y,A−1)\mathcal{N}(\mathbf{A}^{-1}\boldsymbol{h}, \mathbf{A}^{-1}) = \mathcal{N}(\sigma_n^{-2}\mathbf{A}^{-1}\boldsymbol{\Phi}^\top \boldsymbol{y}, \mathbf{A}^{-1}).

Proposition 2 (Predictive distribution)

For f∗=ϕ∗⊤wf_* = \boldsymbol{\phi}_*^\top \boldsymbol{w} with ϕ∗=ϕ(x∗)\boldsymbol{\phi}_* = \boldsymbol{\phi}(\boldsymbol{x}_*),

p(f∗∣x∗,X,y)=N(σn−2ϕ∗⊤A−1Φ⊤y,    ϕ∗⊤A−1ϕ∗).p(f_* \mid \boldsymbol{x}_*, \mathbf{X}, \boldsymbol{y}) = \mathcal{N}\big(\sigma_n^{-2}\boldsymbol{\phi}_*^\top\mathbf{A}^{-1}\boldsymbol{\Phi}^\top \boldsymbol{y},\;\; \boldsymbol{\phi}_*^\top\mathbf{A}^{-1}\boldsymbol{\phi}_*\big).
Proof

Step 1. The posterior over w\boldsymbol{w} is N(wˉ,A−1)\mathcal{N}(\bar{\boldsymbol{w}}, \mathbf{A}^{-1}) with wˉ=σn−2A−1Φ⊤y\bar{\boldsymbol{w}} = \sigma_n^{-2}\mathbf{A}^{-1}\boldsymbol{\Phi}^\top \boldsymbol{y} (Proposition 1).

Step 2. f∗f_* is a linear map of w\boldsymbol{w}. f∗=ϕ∗⊤wf_* = \boldsymbol{\phi}_*^\top \boldsymbol{w} is Lemma 2 with the 1×M1\times M matrix C=ϕ∗⊤\mathbf{C} = \boldsymbol{\phi}_*^\top and d=0\boldsymbol{d} = 0.

Step 3. The mean. Cwˉ=ϕ∗⊤wˉ=σn−2ϕ∗⊤A−1Φ⊤y\mathbf{C}\bar{\boldsymbol{w}} = \boldsymbol{\phi}_*^\top\bar{\boldsymbol{w}} = \sigma_n^{-2}\boldsymbol{\phi}_*^\top\mathbf{A}^{-1}\boldsymbol{\Phi}^\top \boldsymbol{y}.

Step 4. The variance. CA−1C⊤=ϕ∗⊤A−1ϕ∗\mathbf{C}\mathbf{A}^{-1}\mathbf{C}^\top = \boldsymbol{\phi}_*^\top\mathbf{A}^{-1}\boldsymbol{\phi}_*.

Remark (One lemma does it all)

Propositions 1 and 2 are Lemma 8 with x=w\boldsymbol{x} = \boldsymbol{w}, μx=0\boldsymbol{\mu}_x = \mathbf{0}, Σx=Σw\boldsymbol{\Sigma}_x = \boldsymbol{\Sigma}_w, C=Φ\mathbf{C} = \boldsymbol{\Phi}, d=0\boldsymbol{d} = \mathbf{0} and S=σn2I\mathbf{S} = \sigma_n^2\mathbf{I}. Its information form is Proposition 1 (A\mathbf{A} is the posterior precision). Its marginal gives, at no extra cost, the evidence p(y∣X)=N(0, ΦΣwΦ⊤+σn2I)p(\boldsymbol{y} \mid \mathbf{X}) = \mathcal{N}(\mathbf{0},\, \boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top + \sigma_n^2\mathbf{I}), which Section 7 will recognise as N(0,Ky)\mathcal{N}(\mathbf{0}, \mathbf{K}_y).

A\mathbf{A} is a precision: the prior contributes Σw−1\boldsymbol{\Sigma}_w^{-1} and each observation adds σn−2ϕ(xi)ϕ(xi)⊤\sigma_n^{-2}\boldsymbol{\phi}(\boldsymbol{x}_i)\boldsymbol{\phi}(\boldsymbol{x}_i)^\top, so uncertainty can only shrink as data arrive. In the figure the input is a scalar xx and ϕ(x)=[1,x]⊤\boldsymbol{\phi}(x) = [1, x]^\top; each line is a posterior sample of w\boldsymbol{w} and the band is ±2\pm 2 predictive standard deviations.

N = 0 (prior) · Click to add observations.

The samples agree near the data and disagree away from it, which is what we want. The limitation is the model class: no amount of data makes a line fit a curve. The next section removes that limitation without losing tractability.

3. From weight space to function space

Rewrite the predictive distribution so that the features appear only through inner products.

Proposition 3 (Kernel form of the predictive)

Define k(x,x′)=ϕ(x)⊤Σwϕ(x′)k(\boldsymbol{x}, \boldsymbol{x}') = \boldsymbol{\phi}(\boldsymbol{x})^\top\boldsymbol{\Sigma}_w\boldsymbol{\phi}(\boldsymbol{x}'). Then the predictive mean and variance of Proposition 2 are

μ∗=k∗⊤(K+σn2I)−1y,σ∗2=k∗∗−k∗⊤(K+σn2I)−1k∗.\mu_* = \boldsymbol{k}_*^\top(\mathbf{K} + \sigma_n^2\mathbf{I})^{-1}\boldsymbol{y}, \qquad \sigma_*^2 = k_{**} - \boldsymbol{k}_*^\top(\mathbf{K} + \sigma_n^2\mathbf{I})^{-1}\boldsymbol{k}_* .
Proof

Step 1. Translate features into kernels. Row ii of Φ\boldsymbol{\Phi} is ϕ(xi)⊤\boldsymbol{\phi}(\boldsymbol{x}_i)^\top, so entry by entry:

(ΦΣwΦ⊤)ij=ϕ(xi)⊤Σwϕ(xj)=k(xi,xj),(ΦΣwϕ∗)i=ϕ(xi)⊤Σwϕ(x∗)=k(xi,x∗),ϕ∗⊤Σwϕ∗=k(x∗,x∗)=k∗∗.\begin{aligned} \big(\boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\big)_{ij} &= \boldsymbol{\phi}(\boldsymbol{x}_i)^\top\boldsymbol{\Sigma}_w\boldsymbol{\phi}(\boldsymbol{x}_j) = k(\boldsymbol{x}_i, \boldsymbol{x}_j), \\ \big(\boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\phi}_*\big)_i &= \boldsymbol{\phi}(\boldsymbol{x}_i)^\top\boldsymbol{\Sigma}_w\boldsymbol{\phi}(\boldsymbol{x}_*) = k(\boldsymbol{x}_i, \boldsymbol{x}_*), \\ \boldsymbol{\phi}_*^\top\boldsymbol{\Sigma}_w\boldsymbol{\phi}_* &= k(\boldsymbol{x}_*, \boldsymbol{x}_*) = k_{**}. \end{aligned}

That is, ΦΣwΦ⊤=K\boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top = \mathbf{K}, ΦΣwϕ∗=k∗\boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\phi}_* = \boldsymbol{k}_*, and the matrix G\mathbf{G} of Lemma 6 is K+σn2I\mathbf{K} + \sigma_n^2\mathbf{I}.

Step 2. One transpose we need. Because Σw\boldsymbol{\Sigma}_w is symmetric,

ϕ∗⊤ΣwΦ⊤=(ΦΣw⊤ϕ∗)⊤=(ΦΣwϕ∗)⊤=k∗⊤.\boldsymbol{\phi}_*^\top\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top = \big(\boldsymbol{\Phi}\boldsymbol{\Sigma}_w^\top\boldsymbol{\phi}_*\big)^\top = \big(\boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\phi}_*\big)^\top = \boldsymbol{k}_*^\top .

Step 3. The mean.

μ∗=ϕ∗⊤(σn−2A−1Φ⊤)yProposition 2=ϕ∗⊤ΣwΦ⊤G−1ypush-through (Lemma 6)=k∗⊤(K+σn2I)−1ySteps 1 and 2.\begin{aligned} \mu_* &= \boldsymbol{\phi}_*^\top\big(\sigma_n^{-2}\mathbf{A}^{-1}\boldsymbol{\Phi}^\top\big)\boldsymbol{y} && \text{Proposition 2} \\ &= \boldsymbol{\phi}_*^\top\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\mathbf{G}^{-1}\boldsymbol{y} && \text{push-through (Lemma 6)} \\ &= \boldsymbol{k}_*^\top(\mathbf{K} + \sigma_n^2\mathbf{I})^{-1}\boldsymbol{y} && \text{Steps 1 and 2}. \end{aligned}

Step 4. The variance.

σ∗2=ϕ∗⊤A−1ϕ∗Proposition 2=ϕ∗⊤Σwϕ∗−ϕ∗⊤ΣwΦ⊤ G−1 ΦΣwϕ∗Woodbury (Lemma 6)=k∗∗−k∗⊤(K+σn2I)−1k∗Steps 1 and 2.\begin{aligned} \sigma_*^2 &= \boldsymbol{\phi}_*^\top\mathbf{A}^{-1}\boldsymbol{\phi}_* && \text{Proposition 2} \\ &= \boldsymbol{\phi}_*^\top\boldsymbol{\Sigma}_w\boldsymbol{\phi}_* - \boldsymbol{\phi}_*^\top\boldsymbol{\Sigma}_w\boldsymbol{\Phi}^\top\,\mathbf{G}^{-1}\,\boldsymbol{\Phi}\boldsymbol{\Sigma}_w\boldsymbol{\phi}_* && \text{Woodbury (Lemma 6)} \\ &= k_{**} - \boldsymbol{k}_*^\top(\mathbf{K} + \sigma_n^2\mathbf{I})^{-1}\boldsymbol{k}_* && \text{Steps 1 and 2}. \end{aligned}

The features no longer appear; only kk does. This is the kernel trick: any feature space, including an infinite-dimensional one, can be used as long as kk can be evaluated. Notice also the size of the matrix being inverted: A\mathbf{A} is M×MM\times M (number of features), K+σn2I\mathbf{K} + \sigma_n^2\mathbf{I} is N×NN\times N (number of data points). The function-space form is the one to use when features outnumber data.

Example 2 (Infinitely many Gaussian features give the RBF kernel)

In one dimension, let ϕc(x)=exp⁡ ⁣(−(x−c)22s2)\phi_c(x) = \exp\!\big(-\tfrac{(x - c)^2}{2s^2}\big) for every centre c∈Rc \in \mathbb{R}, with independent weights of equal prior variance. A sum over features becomes an integral over centres:

k(x,x′)∝∫−∞∞exp⁡ ⁣(−(x−c)22s2)exp⁡ ⁣(−(x′−c)22s2) dc=π s exp⁡ ⁣(−(x−x′)24s2).\begin{aligned} k(x, x') &\propto \int_{-\infty}^{\infty} \exp\!\Big(-\frac{(x - c)^2}{2s^2}\Big)\exp\!\Big(-\frac{(x' - c)^2}{2s^2}\Big)\,dc \\ &= \sqrt{\pi}\,s\,\exp\!\Big(-\frac{(x - x')^2}{4s^2}\Big). \end{aligned}

This is the squared-exponential (RBF) kernel with length-scale ℓ=2 s\ell = \sqrt{2}\,s.

Proof: of the integral

Step 1. Combine the exponents.

exp⁡ ⁣(−(x−c)22s2)exp⁡ ⁣(−(x′−c)22s2)=exp⁡ ⁣(−(x−c)2+(x′−c)22s2).\exp\!\Big(-\frac{(x - c)^2}{2s^2}\Big)\exp\!\Big(-\frac{(x' - c)^2}{2s^2}\Big) = \exp\!\Big(-\frac{(x - c)^2 + (x' - c)^2}{2s^2}\Big).

Step 2. Expand the numerator.

(x−c)2+(x′−c)2=2c2−2c(x+x′)+x2+x′2.(x - c)^2 + (x' - c)^2 = 2c^2 - 2c(x + x') + x^2 + x'^2 .

Step 3. Complete the square in cc. Let cˉ=x+x′2\bar{c} = \tfrac{x + x'}{2}, so 2c(x+x′)=4ccˉ2c(x + x') = 4c\bar{c}, and

2c2−4ccˉ=2(c−cˉ)2−2cˉ2=2(c−cˉ)2−12(x+x′)2.2c^2 - 4c\bar{c} = 2(c - \bar{c})^2 - 2\bar{c}^2 = 2(c - \bar{c})^2 - \tfrac12(x + x')^2 .

Step 4. Simplify the leftover terms.

x2+x′2−12(x2+2xx′+x′2)=12(x2−2xx′+x′2)=12(x−x′)2.x^2 + x'^2 - \tfrac12(x^2 + 2xx' + x'^2) = \tfrac12(x^2 - 2xx' + x'^2) = \tfrac12(x - x')^2 .

So (x−c)2+(x′−c)2=2(c−cˉ)2+12(x−x′)2(x - c)^2 + (x' - c)^2 = 2(c - \bar{c})^2 + \tfrac12(x - x')^2.

Step 5. Divide by 2s22s^2 and split the exponential.

exp⁡ ⁣(−(x−c)2+(x′−c)22s2)=exp⁡ ⁣(−(x−x′)24s2)  exp⁡ ⁣(−(c−cˉ)2s2).\exp\!\Big(-\frac{(x - c)^2 + (x' - c)^2}{2s^2}\Big) = \exp\!\Big(-\frac{(x - x')^2}{4s^2}\Big)\;\exp\!\Big(-\frac{(c - \bar{c})^2}{s^2}\Big).

The first factor does not depend on cc and moves outside the integral.

Step 6. Do the remaining integral. Substitute c=cˉ+s tc = \bar{c} + s\,t, so dc=s dtdc = s\,dt, and use ∫−∞∞e−t2dt=π\int_{-\infty}^{\infty} e^{-t^2}dt = \sqrt{\pi}:

∫−∞∞exp⁡ ⁣(−(c−cˉ)2s2)dc=s∫−∞∞e−t2 dt=sπ.\int_{-\infty}^{\infty} \exp\!\Big(-\frac{(c - \bar{c})^2}{s^2}\Big)dc = s\int_{-\infty}^{\infty} e^{-t^2}\,dt = s\sqrt{\pi} .

Step 7. Match the RBF form. (x−x′)24s2=(x−x′)22ℓ2\tfrac{(x - x')^2}{4s^2} = \tfrac{(x - x')^2}{2\ell^2} exactly when ℓ2=2s2\ell^2 = 2s^2.

Increase the number of features MM; the random functions converge to samples from the RBF GP.

Faint curves (bottom) are the individual features, shown up to $M = 16$.

4. Gaussian processes

The kernel form suggests placing the prior directly on functions.

Definition 1 (Gaussian process)

A Gaussian process is a collection of random variables, any finite subset of which is jointly Gaussian. A GP over f:X→Rf : \mathcal{X} \to \mathbb{R} is specified by a mean function and a kernel, f∼GP(m,k)f \sim \mathcal{GP}(m, k), where m(x)=E[f(x)]m(\boldsymbol{x}) = \mathbb{E}[f(\boldsymbol{x})] and k(x,x′)=Cov⁡[f(x),f(x′)]k(\boldsymbol{x}, \boldsymbol{x}') = \operatorname{Cov}[f(\boldsymbol{x}), f(\boldsymbol{x}')]. For any inputs X\mathbf{X},

fX=f(X)∼N(m(X),K).\boldsymbol{f}_{\mathbf{X}} = f(\mathbf{X}) \sim \mathcal{N}\big(m(\mathbf{X}), \mathbf{K}\big).

Definition 2 (Positive-definite kernel)

A symmetric function k:X×X→Rk : \mathcal{X} \times \mathcal{X} \to \mathbb{R} is positive definite if, for every finite set {xi}i=1N\{\boldsymbol{x}_i\}_{i=1}^N and every c∈RN\boldsymbol{c} \in \mathbb{R}^N, ∑i,jci cj k(xi,xj)≥0\sum_{i,j} c_i\,c_j\,k(\boldsymbol{x}_i, \boldsymbol{x}_j) \ge 0; that is, every Gram matrix is positive semidefinite. Exactly these functions are covariance functions of GPs.

We take m≡0m \equiv 0 from now on; a known trend can be subtracted first.

Sampling a function on a grid takes three steps:

  1. Evaluate the Gram matrix K\mathbf{K} on the grid and factor it as K=LL⊤\mathbf{K} = \mathbf{L}\mathbf{L}^\top (Cholesky; L\mathbf{L} is lower triangular).
  2. Draw z∼N(0,I)\boldsymbol{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}), one independent standard normal per grid point.
  3. Set fX=Lz\boldsymbol{f}_{\mathbf{X}} = \mathbf{L}\boldsymbol{z}. By Lemma 2 it is Gaussian with mean L0=0\mathbf{L}\mathbf{0} = \mathbf{0} and covariance LIL⊤=K\mathbf{L}\mathbf{I}\mathbf{L}^\top = \mathbf{K}, as required.
Note

Smooth kernels make K\mathbf{K} numerically singular on fine grids (nearby columns are almost identical). Add a small jitter, K+ϵI\mathbf{K} + \epsilon\mathbf{I} with ϵ≈10−6\epsilon \approx 10^{-6}, before factoring.

5. Kernels encode assumptions

The kernel determines which functions are plausible a priori. Select a kernel to see three prior samples; the inset plots k(0.5,x)k(0.5, \boldsymbol{x}).

The squared-exponential (RBF) kernel is

kRBF(x,x′)=σf2exp⁡ ⁣(−∥x−x′∥22ℓ2).k_{\text{RBF}}(\boldsymbol{x}, \boldsymbol{x}') = \sigma_f^2\exp\!\left(-\frac{\lVert \boldsymbol{x} - \boldsymbol{x}'\rVert^2}{2\ell^2}\right).

Its sample paths are infinitely differentiable. The length-scale ℓ\ell sets the distance over which values decorrelate; σf2\sigma_f^2 is the prior variance at every input.

The Matérn family adds a smoothness parameter ν>0\nu > 0. With r=∥x−x′∥r = \lVert \boldsymbol{x} - \boldsymbol{x}'\rVert, the three common members are

k1/2(r)=σf2 e−r/ℓ,k3/2(r)=σf2(1+3 rℓ)e−3 r/ℓ,k5/2(r)=σf2(1+5 rℓ+5r23ℓ2)e−5 r/ℓ.\begin{aligned} k_{1/2}(r) &= \sigma_f^2\, e^{-r/\ell}, \\ k_{3/2}(r) &= \sigma_f^2\Big(1 + \frac{\sqrt{3}\,r}{\ell}\Big)e^{-\sqrt{3}\,r/\ell}, \\ k_{5/2}(r) &= \sigma_f^2\Big(1 + \frac{\sqrt{5}\,r}{\ell} + \frac{5r^2}{3\ell^2}\Big)e^{-\sqrt{5}\,r/\ell}. \end{aligned}

Sample paths are ⌈ν⌉−1\lceil\nu\rceil - 1 times mean-square differentiable, and ν→∞\nu \to \infty recovers the RBF. Physical signals are rarely infinitely smooth, so Matérn is the usual default. The periodic kernel encodes exact repetition, the linear kernel k(x,x′)=x⊤x′k(\boldsymbol{x}, \boldsymbol{x}') = \boldsymbol{x}^\top \boldsymbol{x}' recovers Bayesian linear regression (Section 2), and sums and products build structure: a sum models superposed effects (a trend plus variation), a product modulates one structure by another (a slowly changing periodic pattern).

Proposition 4 (Closure under sums and products)

If k1k_1 and k2k_2 are positive definite, so are k1+k2k_1 + k_2 and k1k2k_1 k_2.

Proof

Step 1. Reduce to matrices. Fix any inputs x1,…,xN\boldsymbol{x}_1, \dots, \boldsymbol{x}_N and let K1\mathbf{K}_1, K2\mathbf{K}_2 be the Gram matrices of k1k_1, k2k_2. Both are positive semidefinite by assumption. We must show that the Gram matrices of k1+k2k_1 + k_2 and k1k2k_1 k_2 are too.

Sum.

Step 2. The Gram matrix of k1+k2k_1 + k_2 is K1+K2\mathbf{K}_1 + \mathbf{K}_2, and for any c\boldsymbol{c},

c⊤(K1+K2)c=c⊤K1c⏟≥0+c⊤K2c⏟≥0≥0.\boldsymbol{c}^\top(\mathbf{K}_1 + \mathbf{K}_2)\boldsymbol{c} = \underbrace{\boldsymbol{c}^\top\mathbf{K}_1\boldsymbol{c}}_{\ge 0} + \underbrace{\boldsymbol{c}^\top\mathbf{K}_2\boldsymbol{c}}_{\ge 0} \ge 0 .

Product.

Step 3. The Gram matrix of k1k2k_1 k_2 has entries k1(xp,xq) k2(xp,xq)k_1(\boldsymbol{x}_p, \boldsymbol{x}_q)\,k_2(\boldsymbol{x}_p, \boldsymbol{x}_q): it is the elementwise (Hadamard) product K1∘K2\mathbf{K}_1 \circ \mathbf{K}_2.

Step 4. Decompose each factor. Symmetric positive semidefinite matrices have eigendecompositions with non-negative eigenvalues:

K1=∑iλi uiui⊤,K2=∑jκj vjvj⊤,λi,κj≥0.\mathbf{K}_1 = \sum_i \lambda_i\,\boldsymbol{u}_iu_i^\top, \qquad \mathbf{K}_2 = \sum_j \kappa_j\,\boldsymbol{v}_jv_j^\top, \qquad \lambda_i, \kappa_j \ge 0 .

Step 5. Hadamard product of two rank-one matrices. Entry (p,q)(p, q) of (uu⊤)∘(vv⊤)(uu^\top) \circ (vv^\top) is upuq vpvq=(upvp)(uqvq)u_pu_q\,v_pv_q = (u_pv_p)(u_qv_q), which is entry (p,q)(p, q) of (u∘v)(u∘v)⊤(\boldsymbol{u} \circ \boldsymbol{v})(\boldsymbol{u} \circ \boldsymbol{v})^\top. So

(uu⊤)∘(vv⊤)=(u∘v)(u∘v)⊤.(uu^\top) \circ (vv^\top) = (\boldsymbol{u} \circ \boldsymbol{v})(\boldsymbol{u} \circ \boldsymbol{v})^\top .

Step 6. Expand. The Hadamard product is linear in each factor, so by Steps 4 and 5

K1∘K2=∑i,jλiκj (ui∘vj)(ui∘vj)⊤.\mathbf{K}_1 \circ \mathbf{K}_2 = \sum_{i,j}\lambda_i\kappa_j\,(\boldsymbol{u}_i \circ \boldsymbol{v}_j)(\boldsymbol{u}_i \circ \boldsymbol{v}_j)^\top .

Step 7. Conclude. For any c\boldsymbol{c}, c⊤(K1∘K2)c=∑i,jλiκj((ui∘vj)⊤c)2≥0\boldsymbol{c}^\top(\mathbf{K}_1 \circ \mathbf{K}_2)\boldsymbol{c} = \sum_{i,j}\lambda_i\kappa_j\big((\boldsymbol{u}_i \circ \boldsymbol{v}_j)^\top \boldsymbol{c}\big)^2 \ge 0, a sum of non-negative terms. (This is the Schur product theorem.)

A kernel is stationary if k(x,x′)=κ(x−x′)k(\boldsymbol{x}, \boldsymbol{x}') = \kappa(\boldsymbol{x} - \boldsymbol{x}'); every example above except the linear kernel is stationary.

Theorem 1 (Bochner)

A continuous function κ:RD→R\kappa : \mathbb{R}^D \to \mathbb{R} defines a positive-definite stationary kernel if and only if it is the Fourier transform of a finite non-negative measure μ\mu:

κ(τ)=∫RDei ω⊤τ μ(dω).\kappa(\boldsymbol{\tau}) = \int_{\mathbb{R}^D} e^{i\,\boldsymbol{\omega}^\top\boldsymbol{\tau}}\,\mu(d\boldsymbol{\omega}).

A stationary kernel is therefore a spectrum. The RBF has a Gaussian spectral density; Matérn has heavier, Student-t\boldsymbol{t}-like tails. Sampling frequencies from μ\mu gives random Fourier features, and the stationarity assumption is what From Fourier to wavelets relaxes.

6. Conditioning

We now derive the posterior directly in function space, with no features.

Step 1. The joint prior. By Definition 1, fX\boldsymbol{f}_{\mathbf{X}} and f∗f_* are jointly Gaussian, with every covariance given by the kernel:

[fXf∗]∼N ⁣(0,  [Kk∗k∗⊤k∗∗]).\begin{bmatrix} \boldsymbol{f}_{\mathbf{X}} \\ f_* \end{bmatrix} \sim \mathcal{N}\!\left(\mathbf{0},\; \begin{bmatrix} \mathbf{K} & \boldsymbol{k}_* \\ \boldsymbol{k}_*^\top & k_{**} \end{bmatrix}\right).

Step 2. Add the noise. The observations are y=fX+ε\boldsymbol{y} = \boldsymbol{f}_{\mathbf{X}} + \boldsymbol{\varepsilon}, with ε∼N(0,σn2I)\boldsymbol{\varepsilon} \sim \mathcal{N}(\mathbf{0}, \sigma_n^2\mathbf{I}) independent of ff. The test value receives no noise, so

[yf∗]=[fXf∗]+[ε0],[ε0]∼N ⁣(0,  [σn2I00⊤0]).\begin{bmatrix} \boldsymbol{y} \\ f_* \end{bmatrix} = \begin{bmatrix} \boldsymbol{f}_{\mathbf{X}} \\ f_* \end{bmatrix} + \begin{bmatrix} \boldsymbol{\varepsilon} \\ 0 \end{bmatrix}, \qquad \begin{bmatrix} \boldsymbol{\varepsilon} \\ 0 \end{bmatrix} \sim \mathcal{N}\!\left(\mathbf{0},\; \begin{bmatrix} \sigma_n^2\mathbf{I} & 0 \\ 0^\top & 0 \end{bmatrix}\right).

Step 3. Sum of independent Gaussians (Lemma 3): the covariances add.

[yf∗]∼N ⁣(0,  [K+σn2Ik∗k∗⊤k∗∗])=N ⁣(0,  [Kyk∗k∗⊤k∗∗]).\begin{bmatrix} \boldsymbol{y} \\ f_* \end{bmatrix} \sim \mathcal{N}\!\left(\mathbf{0},\; \begin{bmatrix} \mathbf{K} + \sigma_n^2\mathbf{I} & \boldsymbol{k}_* \\ \boldsymbol{k}_*^\top & k_{**} \end{bmatrix}\right) = \mathcal{N}\!\left(0,\; \begin{bmatrix} \mathbf{K}_y & \boldsymbol{k}_* \\ \boldsymbol{k}_*^\top & k_{**} \end{bmatrix}\right).

Only the top-left block changes. The cross-covariance is unchanged because Cov⁡[fX+ε, f∗]=Cov⁡[fX,f∗]+Cov⁡[ε,f∗]=k∗+0\operatorname{Cov}[\boldsymbol{f}_{\mathbf{X}} + \boldsymbol{\varepsilon},\, f_*] = \operatorname{Cov}[\boldsymbol{f}_{\mathbf{X}}, f_*] + \operatorname{Cov}[\boldsymbol{\varepsilon}, f_*] = \boldsymbol{k}_* + \mathbf{0}.

Step 4. Condition (Lemma 5) with

a=y,b=f∗,μa=0,μb=0,Σaa=Ky,Σab=k∗,Σba=k∗⊤,Σbb=k∗∗.\begin{aligned} &\boldsymbol{a} = \boldsymbol{y}, && \boldsymbol{b} = f_*, && \boldsymbol{\mu}_a = \mathbf{0}, && \mu_b = 0, \\ &\boldsymbol{\Sigma}_{aa} = \mathbf{K}_y, && \boldsymbol{\Sigma}_{ab} = \boldsymbol{k}_*, && \boldsymbol{\Sigma}_{ba} = \boldsymbol{k}_*^\top, && \boldsymbol{\Sigma}_{bb} = k_{**} . \end{aligned}

Proposition 5 (GP posterior predictive)

f∗∣X,y,x∗∼N(μ∗,σ∗2),μ∗=k∗⊤Ky−1y,σ∗2=k∗∗−k∗⊤Ky−1k∗.f_* \mid \mathbf{X}, \boldsymbol{y}, \boldsymbol{x}_* \sim \mathcal{N}(\mu_*, \sigma_*^2), \qquad \mu_* = \boldsymbol{k}_*^\top\mathbf{K}_y^{-1}\boldsymbol{y}, \qquad \sigma_*^2 = k_{**} - \boldsymbol{k}_*^\top\mathbf{K}_y^{-1}\boldsymbol{k}_* .

For a noisy test observation y∗=f∗+ε∗y_* = f_* + \varepsilon_*, Lemma 3 adds σn2\sigma_n^2 to the variance.

These are the formulas of Proposition 3, obtained here without any reference to features. The two derivations are two views of one model:

WEIGHT SPACEFUNCTION SPACEa prior over parametersa prior over functionsprior: w ~ N(0, Σ_w)observe yposterior: p(w | X, y)f* = φ(x*)ᵀ wprior: f ~ GP(0, k)condition (Lemma 5)posterior: GP(μ, k_post)evaluate at x*kernel trickk(x, x′) = φ(x)ᵀ Σ_w φ(x′)the same predictiveμ* and σ*²
Weight space and function space meet through the kernel.
Remark (The mean is a weighted sum of kernels)

With α=Ky−1y\boldsymbol{\alpha} = \mathbf{K}_y^{-1}\boldsymbol{y}, the posterior mean at any x\boldsymbol{x} is μ(x)=k(X,x)⊤α=∑i=1Nαi k(xi,x)\mu(\boldsymbol{x}) = k(\mathbf{X}, \boldsymbol{x})^\top\boldsymbol{\alpha} = \sum_{i=1}^N \alpha_i\,k(\boldsymbol{x}_i, \boldsymbol{x}): one kernel-shaped bump per observation. This is the kernel ridge regression predictor with regulariser σn2\sigma_n^2, an instance of the representer theorem.

Remark (The variance does not depend on y)

σ∗2\sigma_*^2 involves only X\mathbf{X} and x∗\boldsymbol{x}_*, never y\boldsymbol{y}. The uncertainty after an experiment is known before the experiment is run, which is why GPs are used to decide where to measure next (active learning, Bayesian optimisation).

Remark (The posterior is a GP)

Applying Step 4 to any finite set of test inputs at once shows f∣y∼GP(μ,kpost)f \mid \boldsymbol{y} \sim \mathcal{GP}(\mu, k_{\mathrm{post}}) with kpost(x,x′)=k(x,x′)−k(X,x)⊤Ky−1 k(X,x′)k_{\mathrm{post}}(\boldsymbol{x}, \boldsymbol{x}') = k(\boldsymbol{x}, \boldsymbol{x}') - k(\mathbf{X}, \boldsymbol{x})^\top\mathbf{K}_y^{-1}\,k(\mathbf{X}, \boldsymbol{x}'). Conditioning can be repeated as data arrive.

Sampling the posterior: Matheron’s rule

Proposition 5 describes the posterior as a distribution. To draw a posterior sample at N∗N_* test inputs from it, the obvious route is to compute the N∗×N∗N_*\times N_* posterior covariance and factor it, at cost O(N∗3)O(N_*^3); the result is a vector of values, not a function. Matheron’s rule conditions samples instead.

Proposition 6 (Matheron’s rule (pathwise conditioning))

Let f∼GP(0,k)f \sim \mathcal{GP}(0, k) and ε∼N(0,σn2I)\boldsymbol{\varepsilon} \sim \mathcal{N}(\mathbf{0}, \sigma_n^2\mathbf{I}) be independent. For observed data y\boldsymbol{y}, the random function

(f∣y)(⋅)  =  f(⋅)+k(⋅,X) Ky−1(y−fX−ε)\big(f \mid \boldsymbol{y}\big)(\cdot) \;=\; f(\cdot) + k(\cdot, \mathbf{X})\,\mathbf{K}_y^{-1}\big(\boldsymbol{y} - \boldsymbol{f}_{\mathbf{X}} - \boldsymbol{\varepsilon}\big)

is distributed as the GP posterior. In words: draw a prior function, simulate the observations it would have produced, and correct the function by the kernel-weighted mismatch with the real data.

Proof

Step 1. Fix test inputs. A GP is determined by its finite-dimensional distributions, so it suffices to check any finite set of test inputs X∗\mathbf{X}_*. Write K∗X=k(X∗,X)\mathbf{K}_{*\mathbf{X}} = k(\mathbf{X}_*, \mathbf{X}), K∗∗=k(X∗,X∗)\mathbf{K}_{**} = k(\mathbf{X}_*, \mathbf{X}_*), and let b=f(X∗)\boldsymbol{b} = f(\mathbf{X}_*).

Step 2. Name the simulated observation. Let a=fX+ε\boldsymbol{a} = \boldsymbol{f}_{\mathbf{X}} + \boldsymbol{\varepsilon}: what the prior sample would have produced as data. By Steps 1 to 3 of this section (with many test inputs instead of one), (a,b)(\boldsymbol{a}, \boldsymbol{b}) is jointly Gaussian with zero mean and

Σaa=Ky,Σba=K∗X,Σbb=K∗∗.\boldsymbol{\Sigma}_{aa} = \mathbf{K}_y, \qquad \boldsymbol{\Sigma}_{ba} = \mathbf{K}_{*\mathbf{X}}, \qquad \boldsymbol{\Sigma}_{bb} = \mathbf{K}_{**}.

Step 3. Write the rule at the test inputs. With G=ΣbaΣaa−1=K∗XKy−1\mathbf{G} = \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1} = \mathbf{K}_{*\mathbf{X}}\mathbf{K}_y^{-1}, the rule evaluated at X∗\mathbf{X}_* is

b~=b+G(y−a).\tilde{\boldsymbol{b}} = \boldsymbol{b} + \mathbf{G}(\boldsymbol{y} - \boldsymbol{a}).

Here y\boldsymbol{y} is the real data, a fixed vector; a\boldsymbol{a} and b\boldsymbol{b} are random.

Step 4. Regroup. Collect the random terms:

b~=(b−Ga)⏟r+Gy.\tilde{\boldsymbol{b}} = \underbrace{(\boldsymbol{b} - \mathbf{G}\boldsymbol{a})}_{\boldsymbol{r}} + \mathbf{G}\boldsymbol{y}.

r\boldsymbol{r} is exactly the residual of Lemma 5, Step 1.

Step 5. The law of the residual. By Lemma 5, Steps 4 and 5 (with zero means), r∼N(0,  K∗∗−K∗XKy−1K∗X⊤)\boldsymbol{r} \sim \mathcal{N}\big(\mathbf{0},\; \mathbf{K}_{**} - \mathbf{K}_{*\mathbf{X}}\mathbf{K}_y^{-1}\mathbf{K}_{*\mathbf{X}}^\top\big).

Step 6. Add the constant. Gy\mathbf{G}\boldsymbol{y} is fixed, so it shifts the mean only:

b~∼N(K∗XKy−1y,    K∗∗−K∗XKy−1K∗X⊤).\tilde{\boldsymbol{b}} \sim \mathcal{N}\big(\mathbf{K}_{*\mathbf{X}}\mathbf{K}_y^{-1}\boldsymbol{y},\;\; \mathbf{K}_{**} - \mathbf{K}_{*\mathbf{X}}\mathbf{K}_y^{-1}\mathbf{K}_{*\mathbf{X}}^\top\big).

Step 7. Compare. This is the posterior at X∗\mathbf{X}_* (Proposition 5, applied to all test inputs at once). Since X∗\mathbf{X}_* was arbitrary, the two processes have the same finite-dimensional distributions, hence the same law.

The proof is the conditioning proof of Lemma 5 read backwards. There, the residual r\boldsymbol{r} is independent of the data, so conditioning only moves its mean; here, we build r\boldsymbol{r} from a prior sample and add the data’s contribution Gy\mathbf{G}\boldsymbol{y} by hand.

Remark (Why this matters)
  • The sample is a function. The prior sample f(⋅)f(\cdot) can be drawn once as an explicit function, for example with random Fourier features from Bochner’s theorem (Section 5), and the correction k(⋅,X)vk(\cdot, \mathbf{X})\boldsymbol{v} with v=Ky−1(y−fX−ε)\boldsymbol{v} = \mathbf{K}_y^{-1}(\boldsymbol{y} - \boldsymbol{f}_{\mathbf{X}} - \boldsymbol{\varepsilon}) is a fixed weighted sum of kernels. The result can be evaluated anywhere, at cost linear in the number of test points, and differentiated. Thompson sampling in Bayesian optimisation needs exactly this: maximise one posterior sample.
  • Prior and data are decoupled. The prior term can use a cheap approximation (random features), while the update uses the exact kernel or inducing points. Wilson et al. (2020) call this decoupled sampling; it keeps the error where the data are, and stays accurate far from them, where random-feature posteriors alone break down.
  • Uncertainty through a simulator. Push each pathwise sample through any downstream computation (a PDE solver, a planner, a cost function) and the outputs are samples of the propagated uncertainty, with no further Gaussian algebra.

The rule comes from geostatistics, where Matheron used it for conditional simulation of ore deposits. Cosmology rediscovered it for constrained realisations of the early universe (Hoffman and Ribak, 1991). James T. Wilson, Viacheslav Borovitskiy, Alexander Terenin, Peter Mostowsky and Marc Deisenroth brought it to machine learning (ICML 2020; JMLR 2021).

The animation uses this rule. Six prior functions are drawn once. As each observation arrives, every curve is updated by Matheron’s rule, so you can follow each one as it bends towards the data; the band is the exact posterior of Proposition 5.

7. The marginal likelihood

The hyperparameters θ=(σf,ℓ,σn)\boldsymbol{\theta} = (\sigma_f, \ell, \sigma_n) are chosen by maximising the probability of the data with ff integrated out.

Proposition 7 (Log marginal likelihood)

log⁡p(y∣X,θ)=−12 y⊤Ky−1y⏟data fit  −  12log⁡∣Ky∣⏟complexity  −  N2log⁡2π.\log p(\boldsymbol{y} \mid \mathbf{X}, \boldsymbol{\theta}) = \underbrace{-\tfrac12\,\boldsymbol{y}^\top\mathbf{K}_y^{-1}\boldsymbol{y}}_{\text{data fit}} \;\underbrace{-\;\tfrac12\log\lvert\mathbf{K}_y\rvert}_{\text{complexity}} \;-\;\tfrac{N}{2}\log 2\pi .
Proof

Route 1: as a convolution.

Step 1. Marginalise. Integrate the latent values out of the joint density:

p(y∣X)=∫p(y∣fX) p(fX∣X) dfX=∫N(y∣fX,σn2I)  N(fX∣0,K)  dfX.p(\boldsymbol{y} \mid \mathbf{X}) = \int p(\boldsymbol{y} \mid \boldsymbol{f}_{\mathbf{X}})\,p(\boldsymbol{f}_{\mathbf{X}} \mid \mathbf{X})\,d\boldsymbol{f}_{\mathbf{X}} = \int \mathcal{N}(\boldsymbol{y} \mid \boldsymbol{f}_{\mathbf{X}}, \sigma_n^2\mathbf{I})\;\mathcal{N}(\boldsymbol{f}_{\mathbf{X}} \mid \mathbf{0}, \mathbf{K})\;d\boldsymbol{f}_{\mathbf{X}} .

Step 2. Recognise a sum. This is the density of y=fX+ε\boldsymbol{y} = \boldsymbol{f}_{\mathbf{X}} + \boldsymbol{\varepsilon} with independent fX∼N(0,K)\boldsymbol{f}_{\mathbf{X}} \sim \mathcal{N}(\mathbf{0}, \mathbf{K}) and ε∼N(0,σn2I)\boldsymbol{\varepsilon} \sim \mathcal{N}(\mathbf{0}, \sigma_n^2\mathbf{I}), the convolution of prior and noise.

Step 3. Apply Lemma 3. y∼N(0,K+σn2I)=N(0,Ky)\boldsymbol{y} \sim \mathcal{N}(\mathbf{0}, \mathbf{K} + \sigma_n^2\mathbf{I}) = \mathcal{N}(\mathbf{0}, \mathbf{K}_y).

Step 4. Take the log of the density in Notation with D=ND = N:

log⁡N(y∣0,Ky)=−N2log⁡2π−12log⁡∣Ky∣−12 y⊤Ky−1y.\log\mathcal{N}(\boldsymbol{y} \mid \mathbf{0}, \mathbf{K}_y) = -\tfrac{N}{2}\log 2\pi - \tfrac12\log\lvert\mathbf{K}_y\rvert - \tfrac12\,\boldsymbol{y}^\top\mathbf{K}_y^{-1}\boldsymbol{y} .

Route 2: as a product (a check, and a bonus).

Step 5. Swap arguments in the likelihood. By the symmetry in Step 5 of Lemma 4, N(y∣fX,σn2I)=N(fX∣y,σn2I)\mathcal{N}(\boldsymbol{y} \mid \boldsymbol{f}_{\mathbf{X}}, \sigma_n^2\mathbf{I}) = \mathcal{N}(\boldsymbol{f}_{\mathbf{X}} \mid \boldsymbol{y}, \sigma_n^2\mathbf{I}), so the integrand of Step 1 is a product of two Gaussian densities in the same variable fX\boldsymbol{f}_{\mathbf{X}}.

Step 6. Apply Lemma 4 with (μ1,Σ1)=(y,σn2I)(\boldsymbol{\mu}_1, \boldsymbol{\Sigma}_1) = (\boldsymbol{y}, \sigma_n^2\mathbf{I}) and (μ2,Σ2)=(0,K)(\boldsymbol{\mu}_2, \boldsymbol{\Sigma}_2) = (0, \mathbf{K}):

N(fX∣y,σn2I)  N(fX∣0,K)=N(y∣0, σn2I+K)  N(fX∣μc,Σc).\mathcal{N}(\boldsymbol{f}_{\mathbf{X}} \mid \boldsymbol{y}, \sigma_n^2\mathbf{I})\;\mathcal{N}(\boldsymbol{f}_{\mathbf{X}} \mid \mathbf{0}, \mathbf{K}) = \mathcal{N}(\boldsymbol{y} \mid \mathbf{0},\, \sigma_n^2\mathbf{I} + \mathbf{K})\;\mathcal{N}(\boldsymbol{f}_{\mathbf{X}} \mid \boldsymbol{\mu}_c, \boldsymbol{\Sigma}_c).

Step 7. Integrate over fX\boldsymbol{f}_{\mathbf{X}}. The second factor integrates to one, leaving p(y∣X)=N(y∣0,Ky)p(\boldsymbol{y} \mid \mathbf{X}) = \mathcal{N}(\boldsymbol{y} \mid \mathbf{0}, \mathbf{K}_y): the same answer as Step 3. (Route 2 is Lemma 8 with x=fX\boldsymbol{x} = \boldsymbol{f}_{\mathbf{X}}, C=I\mathbf{C} = \mathbf{I}, d=0\boldsymbol{d} = \mathbf{0}.)

Remark (Bayes’ rule in one identity)

Route 2 delivers more than the evidence. Prior times likelihood equals evidence times posterior, so the factor N(fX∣μc,Σc)\mathcal{N}(\boldsymbol{f}_{\mathbf{X}} \mid \boldsymbol{\mu}_c, \boldsymbol{\Sigma}_c) in Step 6 is the posterior over the latent values at the training inputs, with Σc=(K−1+σn−2I)−1\boldsymbol{\Sigma}_c = (\mathbf{K}^{-1} + \sigma_n^{-2}\mathbf{I})^{-1} and μc=σn−2Σc y\boldsymbol{\mu}_c = \sigma_n^{-2}\boldsymbol{\Sigma}_c\,\boldsymbol{y}. The product of Gaussians is Bayes’ rule: its shape is the posterior and its normaliser is the marginal likelihood.

The two terms pull in opposite directions. A short length-scale fits the data closely (large data-fit term), but makes Ky\mathbf{K}_y close to diagonal with a large determinant (large penalty). The maximiser balances them. Move the slider:

Remark (Why the marginal likelihood generalises)

By the chain rule of probability,

log⁡p(y∣X,θ)=∑n=1Nlog⁡p(yn∣y1,…,yn−1,X,θ).\log p(\boldsymbol{y} \mid \mathbf{X}, \boldsymbol{\theta}) = \sum_{n=1}^{N}\log p(y_n \mid y_1, \dots, y_{n-1}, \mathbf{X}, \boldsymbol{\theta}).

Each term scores the prediction of an observation that has not yet been conditioned on. The marginal likelihood is therefore a sum of one-step-ahead predictive scores, a built-in measure of generalisation: an overly flexible kernel fits each point once it is seen, but predicts the next one poorly. (This reading follows Marc Deisenroth’s GP lectures.)

Proposition 8 (Gradient)

With α=Ky−1y\boldsymbol{\alpha} = \mathbf{K}_y^{-1}\boldsymbol{y},

∂∂θjlog⁡p(y∣X,θ)=12tr⁡ ⁣((αα⊤−Ky−1)∂Ky∂θj).\frac{\partial}{\partial\theta_j}\log p(\boldsymbol{y} \mid \mathbf{X}, \boldsymbol{\theta}) = \tfrac12\operatorname{tr}\!\Big(\big(\boldsymbol{\alpha}\boldsymbol{\alpha}^\top - \mathbf{K}_y^{-1}\big)\frac{\partial\mathbf{K}_y}{\partial\theta_j}\Big).
Proof

Write ∂\partial for ∂/∂θj\partial/\partial\theta_j. The constant −N2log⁡2π-\tfrac N2\log 2\pi has zero derivative.

Step 1. Differentiate the data-fit term (Lemma 7, inverse):

∂(−12 y⊤Ky−1y)=−12 y⊤(∂Ky−1) y=12 y⊤Ky−1(∂Ky)Ky−1y=12 α⊤(∂Ky) α,\partial\big(-\tfrac12\,\boldsymbol{y}^\top\mathbf{K}_y^{-1}\boldsymbol{y}\big) = -\tfrac12\,\boldsymbol{y}^\top(\partial\mathbf{K}_y^{-1})\,\boldsymbol{y} = \tfrac12\,\boldsymbol{y}^\top\mathbf{K}_y^{-1}(\partial\mathbf{K}_y)\mathbf{K}_y^{-1}\boldsymbol{y} = \tfrac12\,\boldsymbol{\alpha}^\top(\partial\mathbf{K}_y)\,\boldsymbol{\alpha} ,

using y⊤Ky−1=(Ky−1y)⊤=α⊤\boldsymbol{y}^\top\mathbf{K}_y^{-1} = (\mathbf{K}_y^{-1}\boldsymbol{y})^\top = \boldsymbol{\alpha}^\top (symmetry of Ky−1\mathbf{K}_y^{-1}).

Step 2. Write it as a trace. A scalar equals its own trace, and tr⁡(UV)=tr⁡(VU)\operatorname{tr}(\mathbf{U}\mathbf{V}) = \operatorname{tr}(\mathbf{V}\mathbf{U}):

α⊤(∂Ky) α=tr⁡(α⊤(∂Ky) α)=tr⁡(αα⊤ ∂Ky).\boldsymbol{\alpha}^\top(\partial\mathbf{K}_y)\,\boldsymbol{\alpha} = \operatorname{tr}\big(\boldsymbol{\alpha}^\top(\partial\mathbf{K}_y)\,\boldsymbol{\alpha}\big) = \operatorname{tr}\big(\boldsymbol{\alpha}\boldsymbol{\alpha}^\top\,\partial\mathbf{K}_y\big).

Step 3. Differentiate the complexity term (Lemma 7, log-determinant):

∂(−12log⁡∣Ky∣)=−12tr⁡(Ky−1 ∂Ky).\partial\big(-\tfrac12\log\lvert\mathbf{K}_y\rvert\big) = -\tfrac12\operatorname{tr}\big(\mathbf{K}_y^{-1}\,\partial\mathbf{K}_y\big).

Step 4. Add Steps 2 and 3. The trace is linear:

12tr⁡(αα⊤∂Ky)−12tr⁡(Ky−1∂Ky)=12tr⁡((αα⊤−Ky−1) ∂Ky).\tfrac12\operatorname{tr}\big(\boldsymbol{\alpha}\boldsymbol{\alpha}^\top\partial\mathbf{K}_y\big) - \tfrac12\operatorname{tr}\big(\mathbf{K}_y^{-1}\partial\mathbf{K}_y\big) = \tfrac12\operatorname{tr}\big((\boldsymbol{\alpha}\boldsymbol{\alpha}^\top - \mathbf{K}_y^{-1})\,\partial\mathbf{K}_y\big).

Optimise over log⁡ℓ\log\ell, log⁡σf\log\sigma_f and log⁡σn\log\sigma_n to keep them positive. The objective is non-convex; distinct optima correspond to distinct explanations of the data (a short length-scale with low noise, or a long length-scale with high noise), so use several restarts.

8. Implementation

Never form Ky−1\mathbf{K}_y^{-1} explicitly. Factor Ky=LL⊤\mathbf{K}_y = \mathbf{L}\mathbf{L}^\top once, then every quantity is a triangular solve:

  1. α=L−⊤(L−1y)\boldsymbol{\alpha} = \mathbf{L}^{-\top}(\mathbf{L}^{-1}\boldsymbol{y}), since Ky−1=(LL⊤)−1=L−⊤L−1\mathbf{K}_y^{-1} = (\mathbf{L}\mathbf{L}^\top)^{-1} = \mathbf{L}^{-\top}\mathbf{L}^{-1}.
  2. μ∗=k∗⊤α\mu_* = \boldsymbol{k}_*^\top\boldsymbol{\alpha}.
  3. v=L−1k∗\boldsymbol{v} = \mathbf{L}^{-1}\boldsymbol{k}_*, so that k∗⊤Ky−1k∗=k∗⊤L−⊤L−1k∗=v⊤v\boldsymbol{k}_*^\top\mathbf{K}_y^{-1}\boldsymbol{k}_* = \boldsymbol{k}_*^\top\mathbf{L}^{-\top}\mathbf{L}^{-1}\boldsymbol{k}_* = \boldsymbol{v}^\top \boldsymbol{v} and σ∗2=k∗∗−v⊤v\sigma_*^2 = k_{**} - \boldsymbol{v}^\top \boldsymbol{v}.
  4. log⁡∣Ky∣=2∑ilog⁡Lii\log\lvert\mathbf{K}_y\rvert = 2\sum_i \log L_{ii}, since ∣LL⊤∣=∣L∣2\lvert\mathbf{L}\mathbf{L}^\top\rvert = \lvert\mathbf{L}\rvert^2 and the determinant of a triangular matrix is the product of its diagonal.
import numpy as np
from scipy.linalg import cho_solve, solve_triangular

def rbf(A, B, ell=0.2, sf=1.0):
    d2 = ((A[:, None, :] - B[None, :, :]) ** 2).sum(-1)
    return sf**2 * np.exp(-0.5 * d2 / ell**2)

def gp_predict(X, y, Xs, ell=0.2, sf=1.0, sn=0.1, jitter=1e-8):
    N = len(X)
    L = np.linalg.cholesky(rbf(X, X, ell, sf) + (sn**2 + jitter) * np.eye(N))
    alpha = cho_solve((L, True), y)
    Ks = rbf(X, Xs, ell, sf)                     # N x N*
    mu = Ks.T @ alpha
    V = solve_triangular(L, Ks, lower=True)
    var = sf**2 - (V**2).sum(0)                  # diagonal of the posterior covariance
    lml = -0.5 * y @ alpha - np.log(np.diag(L)).sum() - 0.5 * N * np.log(2 * np.pi)
    return mu, var, lml
Note (Practical defaults)

Adapted from Marc Deisenroth’s lectures:

  • Standardise inputs and targets; initialise ℓ≈0.5\ell \approx 0.5 and σf≈1\sigma_f \approx 1.
  • Initialise the noise high, σn≈0.5 σf\sigma_n \approx 0.5\,\sigma_f, even for clean data; the optimiser moves more easily and can reduce it.
  • Use random restarts.
  • If Cholesky fails repeatedly, penalise large signal-to-noise ratios σf/σn\sigma_f/\sigma_n rather than only increasing the jitter.

Cost. O(N3)O(N^3) time and O(N2)O(N^2) memory. Standard routes to scale:

  • Inducing points: M≪NM \ll N pseudo-inputs, O(NM2)O(NM^2) (sparse variational GPs).
  • Random features: an explicit DD-dimensional approximation of kk, returning to the weight view at O(ND2)O(ND^2) (From Fourier to wavelets).
  • Structure and iterative solvers: Kronecker or Toeplitz structure on grids; conjugate gradients with fast matrix–vector products.

9. Limitations

  • Non-Gaussian likelihoods (classification, counts) break conjugacy; the posterior requires approximation (Laplace, expectation propagation, variational inference).
  • Stationarity. A single length-scale is often wrong for real signals; non-stationary kernels, input warping and multi-scale features address this.
  • High-dimensional inputs. Euclidean distances between raw inputs are rarely meaningful; deep kernels and deep GPs learn a representation.
  • Function-valued inputs and outputs. Mapping one function to another (for example, PDE coefficients to solutions) requires operator-valued kernels, the subject of the next post in this series.

Questions

Question 1

Why does the posterior variance not depend on the observed values y\boldsymbol{y}?

Answer

In Lemma 5 the conditional covariance Σbb−ΣbaΣaa−1Σab\boldsymbol{\Sigma}_{bb} - \boldsymbol{\Sigma}_{ba}\boldsymbol{\Sigma}_{aa}^{-1}\boldsymbol{\Sigma}_{ab} involves only covariances, and for a GP these depend only on the inputs.

Question 2

You multiply two Gaussian densities in the same variable and, separately, add two independent Gaussian variables. Which result is narrower, and why?

Answer

The product is narrower: its precision is the sum of the two precisions (Lemma 4), so its covariance is smaller than either. The sum is wider: its covariance is the sum of the two covariances (Lemma 3).

Question 3

Which kernel reduces a GP to Bayesian linear regression with features ϕ\boldsymbol{\phi}?

Answer

k(x,x′)=ϕ(x)⊤Σwϕ(x′)k(\boldsymbol{x}, \boldsymbol{x}') = \boldsymbol{\phi}(\boldsymbol{x})^\top\boldsymbol{\Sigma}_w\boldsymbol{\phi}(\boldsymbol{x}') (Proposition 3). With ϕ(x)=x\boldsymbol{\phi}(\boldsymbol{x}) = \boldsymbol{x} and Σw=I\boldsymbol{\Sigma}_w = \mathbf{I} this is the linear kernel.

Question 4

The optimiser returns a very short length-scale and near-zero noise. What has happened, and what should you check?

Answer

The model interpolates the data and treats noise as signal. Compare with the optimum at a longer length-scale and larger noise, place a prior on σn\sigma_n, and use several initialisations.

Question 5

How do you multiply N(y∣Cx+d,S)\mathcal{N}(\boldsymbol{y} \mid \mathbf{C}\boldsymbol{x} + \boldsymbol{d}, \mathbf{S}) by N(x∣μx,Σx)\mathcal{N}(\boldsymbol{x} \mid \boldsymbol{\mu}_x, \boldsymbol{\Sigma}_x), when the two densities are in different variables?

Answer

Read the product as a joint p(x,y)p(\boldsymbol{x}, \boldsymbol{y}), compute the joint by stacking (Lemma 2), then factor it the other way as p(y) p(x∣y)p(\boldsymbol{y})\,p(\boldsymbol{x} \mid \boldsymbol{y}) (Lemma 5). Alternatively, read the likelihood as an information-form Gaussian in x\boldsymbol{x} and add precisions. Both routes are Lemma 8.

Question 6

Matheron’s rule never computes the posterior covariance. Where does it come from?

Answer

From the prior sample itself. The residual b−Ga\boldsymbol{b} - \mathbf{G}\boldsymbol{a} of a prior draw already has the posterior covariance (Lemma 5, Step 5); the rule only adds the deterministic shift Gy\mathbf{G}\boldsymbol{y}.

References

  • C. E. Rasmussen, C. K. I. Williams. Gaussian Processes for Machine Learning. MIT Press, 2006. Chapters 2 and 5, and Appendix A for the Gaussian identities.
  • M. P. Deisenroth, A. A. Faisal, C. S. Ong. Mathematics for Machine Learning. Cambridge University Press, 2020. Chapters 6 and 9.
  • M. P. Deisenroth. Gaussian processes. Lecture slides, UCL, 2023/24.
  • K. B. Petersen, M. S. Pedersen. The Matrix Cookbook. 2012. For matrix identities and derivatives.
  • C. M. Bishop. Pattern Recognition and Machine Learning. Springer, 2006. Chapters 2.3 and 6.
  • D. J. C. MacKay. Introduction to Gaussian processes. 1998.
  • J. Görtler, R. Kehlbeck, O. Deussen. A Visual Exploration of Gaussian Processes. Distill, 2019.
  • J. T. Wilson, V. Borovitskiy, A. Terenin, P. Mostowsky, M. P. Deisenroth. Efficiently sampling functions from Gaussian process posteriors. ICML 2020.
  • J. T. Wilson, V. Borovitskiy, A. Terenin, P. Mostowsky, M. P. Deisenroth. Pathwise conditioning of Gaussian processes. JMLR, 2021.
  • Y. Hoffman, E. Ribak. Constrained realizations of Gaussian fields: a simple algorithm. The Astrophysical Journal Letters, 1991.
  • A. Doucet. A note on efficient conditional simulation of Gaussian distributions. Technical note, 2010.
  • F. Lindgren, H. Rue, J. Lindström. An explicit link between Gaussian fields and Gaussian Markov random fields: the stochastic partial differential equation approach. Journal of the Royal Statistical Society B, 2011.