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 were only a means to an end: what we predicted was the function , and the prior on mattered only through the uncertainty it induced on .
So let us put the prior on the function directly. A function is an infinite-dimensional object, one value 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 such that for any finite set of inputs the values are jointly Gaussian:
We write . The mean function is the prior guess; the kernel, or covariance function, says how strongly the values at two inputs move together. Throughout this topic : 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 matrix and sample one vector from : draw and take , where . 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 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:
It has two hyperparameters.
- is the prior variance of each value: , the typical size of .
- is the length scale: how far you must move before the function forgets its value. The covariance depends only on the distance , so the kernel is stationary: the prior looks the same everywhere.
The correlation between and at distance is . At it is , at it is , at about . Short gives wiggly draws; long gives slowly bending ones.
Not every function of two arguments is a kernel. Since must be a covariance matrix for every choice of points, has to be symmetric and every Gram matrix positive semidefinite: the variance of any combination, , 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:
- is a kernel for any feature map ;
- 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 ).
Bayesian linear regression is a Gaussian process too
Now the promised link to Bayesian linear regression. Take features , the model and the prior . Each is linear in , so any finite set of values is jointly Gaussian: is a Gaussian process. Its mean is zero, and its kernel is
In the running example of that topic, and , so
Its prior variance grows with : a random line is pinned near the origin and fans out away from it. The RBF prior, by contrast, has the same variance everywhere.
Two descriptions of one model give two ways to compute. In weight space we invert the matrix ; in function space, as the next block shows, we invert an matrix built from kernel values. The answers are identical. The function-space formulas touch only through inner products , so we may drop 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 placed ever more densely at centres 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 pairs , with targets observed through noise of precision , as in Bayesian linear regression:
Since and the noise is independent of it, the targets are Gaussian too:
This is the matrix of the linear-regression evidence, with a general in place of .
Add a test input and write and . The pair with is jointly Gaussian:
Prediction is now just a Gaussian conditional. For a Gaussian vector , the conditional of given has mean and covariance . With and :
The variance is the prior variance minus what the data explained. For a new noisy target add the noise, exactly as in topic 4:
No integral over weights, no approximation: the whole posterior is linear algebra on an matrix.
Reading the posterior
Take the three points of Bayesian linear regression, and , with , and compare two kernels: the linear from the previous block and the RBF with , .
| linear: mean | linear: | RBF: mean | RBF: | |
|---|---|---|---|---|
The linear-kernel column reproduces topic 4 exactly: at the mean and the predictive variance 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 and returns to its prior, mean and variance .
A few things to read off the formulas.
- The mean is a weighted sum of kernels. With , : one coefficient per training point, which is what "nonparametric" means. This mean coincides with kernel ridge regression with penalty ; the GP adds the variance.
- Noisy targets are not interpolated. At the RBF mean is , not : with noise variance as large as the prior variance, the model meets the observation roughly halfway.
- The variance does not look at . It depends only on where the inputs are, not on what was observed there.
Kernel hyperparameters from the evidence
The kernel has hyperparameters , 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 :
It is the evidence formula of Bayesian linear regression, with the kernel matrix inside .
The two terms pull in opposite directions. A short lets the model explain almost any targets; its spreads variance over many directions, so is large and the complexity term punishes it. A long allows only smooth functions; is small, but targets that wiggle get a poor data fit. On our three points with , :
| data fit | complexity | ||
|---|---|---|---|
The optimum is near with . The linear kernel with , scores : on three nearly collinear points, the line is the better explanation. Kernels are models, and the evidence compares them.
Maximizing the evidence over is type-II maximum likelihood, or empirical Bayes: one point estimate of the hyperparameters, not an average over them. It uses gradients,
The objective is not concave and can have several local optima; a typical bad one explains all variation as noise with a very long . Restart from several initial values and keep the best.
The $O(n^3)$ price and what comes next
The price of exactness is the matrix . One never forms ; the standard route is the Cholesky factorization with lower-triangular , followed by triangular solves, which are cheap and numerically stable. The factorization costs time and memory, once. After it, each test point costs for the mean and for the variance. A few thousand points are routine; a million are out of reach. The usual escape is a set of inducing points that summarize the function, which brings the cost down to .
Classification breaks the closed form for the same reason as in The Laplace approximation and Bayesian classification: a logistic likelihood on is not Gaussian. The Laplace approximation from that topic applies to the latent values 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.
Answers are saved on the server and tied to this browser until you sign in.