Skip to content
BIASED

Gaussian processes

A prior placed directly on functions: the kernel as the model, Bayesian linear regression as a Gaussian process with a linear kernel, the posterior mean and variance in closed form, kernel hyperparameters chosen by the evidence, and the $O(n^3)$ price.

A prior over functions

The topic Gibbs sampling and Metropolis–Hastings finished our toolkit for posteriors over parameters. But every model so far had a parameter vector of a fixed size, chosen before seeing the data. In regression this is often the wrong thing to be uncertain about. In Bayesian linear regression the weights ww were only a means to an end: what we predicted was the function f(x)=ϕ(x)⊤wf(x) = \phi(x)^\top w, and the prior on ww mattered only through the uncertainty it induced on ff.

So let us put the prior on the function directly. A function is an infinite-dimensional object, one value f(x)f(x) for each input. We cannot write a density over all of them, but we never need to: we only ever look at finitely many values.

Definition. A Gaussian process is a random function ff such that for any finite set of inputs x1,…,xnx_1, \dots, x_n the values are jointly Gaussian:

(f(x1),…,f(xn))∼N(m,K),mi=m(xi),Kij=k(xi,xj).\big(f(x_1), \dots, f(x_n)\big) \sim \mathcal N(m, K), \qquad m_i = m(x_i), \quad K_{ij} = k(x_i, x_j).

We write f∼GP(m,k)f \sim \mathcal{GP}(m, k). The mean function m(x)=Ef(x)m(x) = \mathbb E f(x) is the prior guess; the kernel, or covariance function, k(x,x′)=Cov⁡(f(x),f(x′))k(x, x') = \operatorname{Cov}(f(x), f(x')) says how strongly the values at two inputs move together. Throughout this topic m=0m = 0: before the data, the function is as likely to go up as down.

To "draw a function" from the prior, pick a fine grid of, say, 200 inputs, build the 200×200200 \times 200 matrix KK and sample one vector from N(0,K)\mathcal N(0, K): draw ε∼N(0,I)\varepsilon \sim \mathcal N(0, I) and take LεL\varepsilon, where K=LL⊤K = LL^\top. Plotted against the grid, it looks like a curve.

A Gaussian process is nonparametric: there is no fixed list of parameters. As we will see, the prediction uses one coefficient per training point, so the model grows with the data.

The kernel is the model

Everything the model assumes about ff sits in the kernel: how smooth it is, how fast it varies, how large it is. The workhorse is the squared exponential, or RBF, kernel:

k(x,x′)=sf2exp⁡(−(x−x′)22ℓ2).k(x, x') = s_f^2 \exp\left(-\frac{(x - x')^2}{2\ell^2}\right).

It has two hyperparameters.

  • sf2s_f^2 is the prior variance of each value: Var⁡f(x)=k(x,x)=sf2\operatorname{Var} f(x) = k(x, x) = s_f^2, the typical size of ff.
  • ℓ\ell is the length scale: how far you must move before the function forgets its value. The covariance depends only on the distance x−x′x - x', so the kernel is stationary: the prior looks the same everywhere.

The correlation between f(x)f(x) and f(x′)f(x') at distance dd is exp⁡(−d2/2ℓ2)\exp(-d^2 / 2\ell^2). At d=ℓd = \ell it is e−1/2≈0.61e^{-1/2} \approx 0.61, at 2ℓ2\ell it is 0.140.14, at 3ℓ3\ell about 0.010.01. Short ℓ\ell gives wiggly draws; long ℓ\ell gives slowly bending ones.

Not every function of two arguments is a kernel. Since KK must be a covariance matrix for every choice of points, kk has to be symmetric and every Gram matrix KK positive semidefinite: the variance of any combination, v⊤Kv=Var⁡∑ivif(xi)v^\top K v = \operatorname{Var}\sum_i v_i f(x_i), cannot be negative (see the topic Symmetric matrices and quadratic forms in Mathematics for ML). Checking this directly is hard, so kernels are usually built from known ones by rules that preserve positive semidefiniteness:

  • ϕ(x)⊤ϕ(x′)\phi(x)^\top \phi(x') is a kernel for any feature map ϕ\phi;
  • a positive multiple of a kernel, a sum of two kernels and a product of two kernels are kernels;
  • a positive constant is a kernel (a random offset shared by all xx).

Bayesian linear regression is a Gaussian process too

Now the promised link to Bayesian linear regression. Take features ϕ(x)∈Rd\phi(x) \in \mathbb R^d, the model f(x)=ϕ(x)⊤wf(x) = \phi(x)^\top w and the prior w∼N(0,α−1I)w \sim \mathcal N(0, \alpha^{-1} I). Each f(x)f(x) is linear in ww, so any finite set of values is jointly Gaussian: ff is a Gaussian process. Its mean is zero, and its kernel is

k(x,x′)=Cov⁡(ϕ(x)⊤w, ϕ(x′)⊤w)=ϕ(x)⊤Cov⁡(w) ϕ(x′)=α−1 ϕ(x)⊤ϕ(x′).k(x, x') = \operatorname{Cov}\big(\phi(x)^\top w,\ \phi(x')^\top w\big) = \phi(x)^\top \operatorname{Cov}(w)\, \phi(x') = \alpha^{-1}\, \phi(x)^\top \phi(x').

In the running example of that topic, ϕ(x)=(1,x)\phi(x) = (1, x) and α=2\alpha = 2, so

k(x,x′)=1+xx′2.k(x, x') = \frac{1 + x x'}{2}.

Its prior variance k(x,x)=(1+x2)/2k(x, x) = (1 + x^2)/2 grows with ∣x∣|x|: a random line is pinned near the origin and fans out away from it. The RBF prior, by contrast, has the same variance sf2s_f^2 everywhere.

Two descriptions of one model give two ways to compute. In weight space we invert the d×dd \times d matrix SN−1S_N^{-1}; in function space, as the next block shows, we invert an n×nn \times n matrix built from kernel values. The answers are identical. The function-space formulas touch ϕ\phi only through inner products ϕ(x)⊤ϕ(x′)\phi(x)^\top \phi(x'), so we may drop ϕ\phi altogether and start from a kernel. This is the kernel trick. Behind the RBF kernel stands an infinite set of features: it is the limit of Gaussian bumps exp⁡(−(x−c)2/ℓ2)\exp(-(x - c)^2/\ell^2) placed ever more densely at centres cc along the line, each with its own Gaussian weight. No weight-space computer could store them; the kernel needs one number per pair of points.

Regression is a Gaussian conditional

The data are nn pairs (xi,ti)(x_i, t_i), with targets observed through noise of precision β\beta, as in Bayesian linear regression:

ti=f(xi)+εi,εi∼N(0,β−1).t_i = f(x_i) + \varepsilon_i, \qquad \varepsilon_i \sim \mathcal N(0, \beta^{-1}).

Since f(X)=(f(x1),…,f(xn))∼N(0,K)f(X) = (f(x_1), \dots, f(x_n)) \sim \mathcal N(0, K) and the noise is independent of it, the targets are Gaussian too:

t∼N(0,C),C=K+β−1In.t \sim \mathcal N(0, C), \qquad C = K + \beta^{-1} I_n.

This is the matrix C=β−1In+α−1ΦΦ⊤C = \beta^{-1} I_n + \alpha^{-1}\Phi\Phi^\top of the linear-regression evidence, with a general KK in place of α−1ΦΦ⊤\alpha^{-1}\Phi\Phi^\top.

Add a test input x∗x_* and write k∗=(k(x∗,x1),…,k(x∗,xn))k_* = (k(x_*, x_1), \dots, k(x_*, x_n)) and k∗∗=k(x∗,x∗)k_{**} = k(x_*, x_*). The pair (t,f∗)(t, f_*) with f∗=f(x∗)f_* = f(x_*) is jointly Gaussian:

(tf∗)∼N(0, (Ck∗k∗⊤k∗∗)).\begin{pmatrix} t \\ f_* \end{pmatrix} \sim \mathcal N\left(0,\ \begin{pmatrix} C & k_* \\ k_*^\top & k_{**} \end{pmatrix}\right).

Prediction is now just a Gaussian conditional. For a Gaussian vector (a,b)(a, b), the conditional of aa given bb has mean μa+ΣabΣbb−1(b−μb)\mu_a + \Sigma_{ab}\Sigma_{bb}^{-1}(b - \mu_b) and covariance Σaa−ΣabΣbb−1Σba\Sigma_{aa} - \Sigma_{ab}\Sigma_{bb}^{-1}\Sigma_{ba}. With a=f∗a = f_* and b=tb = t:

E[f∗∣t]=k∗⊤C−1t,Var⁡[f∗∣t]=k∗∗−k∗⊤C−1k∗.\mathbb E[f_* \mid t] = k_*^\top C^{-1} t, \qquad \operatorname{Var}[f_* \mid t] = k_{**} - k_*^\top C^{-1} k_*.

The variance is the prior variance minus what the data explained. For a new noisy target t∗=f∗+ε∗t_* = f_* + \varepsilon_* add the noise, exactly as in topic 4:

Var⁡[t∗∣t]=k∗∗−k∗⊤C−1k∗+β−1.\operatorname{Var}[t_* \mid t] = k_{**} - k_*^\top C^{-1} k_* + \beta^{-1}.

No integral over weights, no approximation: the whole posterior is linear algebra on an n×nn \times n matrix.

Reading the posterior

Take the three points of Bayesian linear regression, x=(−1,0,1)x = (-1, 0, 1) and t=(−1,1,2)t = (-1, 1, 2), with β=1\beta = 1, and compare two kernels: the linear k=(1+xx′)/2k = (1 + xx')/2 from the previous block and the RBF with sf=1s_f = 1, ℓ=1\ell = 1.

x∗x_* linear: mean linear: Var⁡f∗\operatorname{Var} f_* RBF: mean RBF: Var⁡f∗\operatorname{Var} f_*
00 0.400.40 0.200.20 0.570.57 0.400.40
22 1.901.90 1.201.20 0.610.61 0.810.81
33 2.652.65 2.452.45 0.130.13 0.990.99

The linear-kernel column reproduces topic 4 exactly: at x∗=2x_* = 2 the mean 1.91.9 and the predictive variance 1.2+1=2.21.2 + 1 = 2.2 are what the weight posterior gave. The RBF column tells a different story, because the kernel is a different model. A line extrapolates the trend and grows more uncertain; the RBF process forgets the data beyond a few ℓ\ell and returns to its prior, mean 00 and variance sf2=1s_f^2 = 1.

A few things to read off the formulas.

  • The mean is a weighted sum of kernels. With a=C−1ta = C^{-1} t, E[f(x)∣t]=∑iai k(x,xi)\mathbb E[f(x) \mid t] = \sum_i a_i\, k(x, x_i): one coefficient per training point, which is what "nonparametric" means. This mean coincides with kernel ridge regression with penalty λ=β−1\lambda = \beta^{-1}; the GP adds the variance.
  • Noisy targets are not interpolated. At x=0x = 0 the RBF mean is 0.570.57, not t=1t = 1: with noise variance β−1=1\beta^{-1} = 1 as large as the prior variance, the model meets the observation roughly halfway.
  • The variance does not look at tt. It depends only on where the inputs are, not on what was observed there.

Kernel hyperparameters from the evidence

The kernel has hyperparameters θ=(sf,ℓ,β)\theta = (s_f, \ell, \beta), and the table above shows that they change the predictions a lot. We choose them as in the topic Bayesian model selection: by the evidence, the probability the model gave to the observed targets before seeing them. The function is already integrated out, since t∼N(0,Cθ)t \sim \mathcal N(0, C_\theta):

log⁡p(t∣X,θ)=−12 t⊤Cθ−1t⏟data fit − 12log⁡det⁡Cθ⏟complexity − n2log⁡2π.\log p(t \mid X, \theta) = \underbrace{-\tfrac12\, t^\top C_\theta^{-1} t}_{\text{data fit}} \ \underbrace{-\ \tfrac12 \log\det C_\theta}_{\text{complexity}} \ -\ \tfrac{n}{2}\log 2\pi.

It is the evidence formula of Bayesian linear regression, with the kernel matrix inside CC.

The two terms pull in opposite directions. A short ℓ\ell lets the model explain almost any targets; its CC spreads variance over many directions, so log⁡det⁡C\log\det C is large and the complexity term punishes it. A long ℓ\ell allows only smooth functions; log⁡det⁡C\log\det C is small, but targets that wiggle get a poor data fit. On our three points with sf=1s_f = 1, β=1\beta = 1:

ℓ\ell data fit complexity log⁡p(t∣X,θ)\log p(t \mid X, \theta)
0.50.5 −1.47-1.47 −1.04-1.04 −5.26-5.26
11 −1.48-1.48 −0.94-0.94 −5.18-5.18
22 −1.87-1.87 −0.81-0.81 −5.44-5.44

The optimum is near ℓ≈0.92\ell \approx 0.92 with log⁡p≈−5.17\log p \approx -5.17. The linear kernel with α=2\alpha = 2, β=1\beta = 1 scores −5.04-5.04: on three nearly collinear points, the line is the better explanation. Kernels are models, and the evidence compares them.

Maximizing the evidence over θ\theta is type-II maximum likelihood, or empirical Bayes: one point estimate of the hyperparameters, not an average over them. It uses gradients,

∂∂θjlog⁡p(t∣X,θ)=12 a⊤∂C∂θja−12tr⁡(C−1∂C∂θj),a=C−1t.\frac{\partial}{\partial\theta_j} \log p(t \mid X, \theta) = \tfrac12\, a^\top \frac{\partial C}{\partial\theta_j} a - \tfrac12 \operatorname{tr}\left(C^{-1}\frac{\partial C}{\partial\theta_j}\right), \qquad a = C^{-1} t.

The objective is not concave and can have several local optima; a typical bad one explains all variation as noise with a very long ℓ\ell. Restart from several initial values and keep the best.

The $O(n^3)$ price and what comes next

The price of exactness is the n×nn \times n matrix CC. One never forms C−1C^{-1}; the standard route is the Cholesky factorization C=LL⊤C = LL^\top with lower-triangular LL, followed by triangular solves, which are cheap and numerically stable. The factorization costs O(n3)O(n^3) time and O(n2)O(n^2) memory, once. After it, each test point costs O(n)O(n) for the mean and O(n2)O(n^2) for the variance. A few thousand points are routine; a million are out of reach. The usual escape is a set of m≪nm \ll n inducing points that summarize the function, which brings the cost down to O(nm2)O(nm^2).

Classification breaks the closed form for the same reason as in The Laplace approximation and Bayesian classification: a logistic likelihood on f(xi)f(x_i) is not Gaussian. The Laplace approximation from that topic applies to the latent values f(X)f(X) as it did to the weights; expectation propagation, which matches moments instead of fitting the mode, is usually more accurate.

The Gaussian process put a prior on an object that grows with the data, a whole function, and kept the posterior exact for regression. Clustering asks for a different growing object. In the two-component mixture of EM and Bayesian PCA we fixed the number of components in advance; we would rather let the data decide, with new clusters appearing as new points arrive. That needs a prior over partitions of the data into an unknown number of groups: the topic Dirichlet processes.

Check your understanding

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

Go deeper — optional

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

  • GP RBF: draw from the prioroptional

    Build K_RBF(X,X)+σ²I and sample f~N(0,K); check PSD.

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

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

Full notes

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