Gaussian Processes
Distributions over functions with honest error bars.
Kernel ridge regression returns one curve. A Gaussian process returns a whole distribution over curves: put a prior on functions, observe a few noisy values, and condition. Out comes the same best-guess curve plus an error bar at every input, tight near the data and widening honestly where nobody has looked. The same machinery tunes its own hyperparameters, and it is the engine of Bayesian optimization.
What you’ll learn
- Define a Gaussian process by its mean and covariance functions, and draw sample functions from a GP prior.
- Derive the posterior mean and variance by conditioning a joint Gaussian, with and without observation noise.
- Explain what the length scale, signal variance and noise do, and fit them by maximizing the log marginal likelihood, reading its data-fit and complexity terms.
- Count the cost, relate the posterior mean to kernel ridge regression, and judge when the error bars can be trusted.
A prior over functions
A Gaussian process is a probability distribution over functions such that, for every finite set of inputs , the vector is multivariate Gaussian. It is specified by a mean function and a covariance function, a kernel, . One writes , and then
The kernel must be positive semidefinite precisely because every such has to be a covariance matrix.
To draw a function, choose a fine grid of inputs, factor its covariance by Cholesky (adding a tiny "jitter" to the diagonal for numerical safety), draw and set . Then , and plotting against the grid shows one random function.
Rice's formula says a stationary zero-mean Gaussian process with kernel crosses zero on average times per unit length; for the RBF kernel . About how many times does a prior sample with cross zero on ?
Show the answer
6.4 crossings
crossings per unit length times a length of 10 gives . At the same formula gives , matching the widget's samples. Halving the length scale doubles the wiggles.
Conditioning on data
Observations are noisy, with independent . Under the prior (mean 0), the training targets and the value at a test input are jointly Gaussian, with :
For jointly Gaussian vectors with zero means, conditioning one block on another gives a Gaussian whose mean is a linear function of the observed block, and whose covariance is a Schur complement (derived in GP Regression in Depth):
With and , the posterior at any test input is
The mean is a weighted sum of kernel functions centered on the data, with : exactly kernel ridge regression with (The Kernel Trick). The variance never mentions : it is the prior variance minus what the inputs alone explain, so it depends only on where you have looked.
A new noisy observation has the same mean and the noise added to the variance, the posterior predictive distribution:
One observation at , with the RBF kernel (, ) and noise . What is the posterior mean at ?
Show the answer
0.5
With one point, . The prior says is about , the observation says , and with equal variances the posterior splits the difference. With the mean would be .
Hyperparameters and the marginal likelihood
The kernel's hyperparameters decide everything: how fast the function may wiggle, how far it may stray, how closely it must honor each observation. A GP chooses them by maximizing the marginal likelihood, the probability of the observed targets with the function integrated out. Since is Gaussian, , and its logarithm splits into three readable terms:
In practice is maximized by gradient ascent over (the gradient has a closed form, derived in GP Regression in Depth), from several starting points, because the surface can have more than one local maximum: a short length scale with little noise and a long one with much noise can both explain the same data. Choosing hyperparameters this way is called type-II maximum likelihood or empirical Bayes; with only a handful of points it can return degenerate answers, a pitfall examined in Kernel Design for Gaussian Processes.
A very short length scale lets the posterior mean pass through every observation. Why does maximizing the marginal likelihood usually not choose it?
- It does choose it; that is why GPs overfit
- Because the noise is held fixed during the fit
- Because the log-determinant term penalizes a model flexible enough to fit almost any dataset: it gives each particular dataset low probability
- Because the optimizer always starts at
Show the answer
C. Because the log-determinant term penalizes a model flexible enough to fit almost any dataset: it gives each particular dataset low probability
The marginal likelihood is a probability over datasets and must integrate to one. A model that can produce almost anything gives low density to what it actually saw; the term measures that spread. A short length scale also tells the model that neighboring values are unrelated, which these data contradict.
Cost, connections and honest error bars
| Step | Cost |
|---|---|
| Cholesky factorization of | time ( multiply–adds), memory |
| Weights and , from the factor | |
| Posterior mean at a new input | |
| Posterior variance at a new input | (one triangular solve) |
| Gradient of | once, then per hyperparameter |
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import ConstantKernel, RBF, WhiteKernel
kernel = ConstantKernel(1.0) * RBF(length_scale=1.0) + WhiteKernel(noise_level=0.01)
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=5)
gp.fit(X_train, y_train) # maximizes the log marginal likelihood
print(gp.kernel_) # fitted signal variance, length scale and noise level
mean, std = gp.predict(X_test, return_std=True)
lower, upper = mean - 2 * std, mean + 2 * stdConstantKernel plays and WhiteKernel plays . Because the noise is part of the kernel here, the returned std describes a new noisy observation , not : subtract the fitted noise variance to get the band of .
Key takeaways
- A Gaussian process makes every finite vector of function values Gaussian with covariance . Prior samples are with ; the length scale sets their wiggle rate.
- Conditioning the joint Gaussian gives and . The mean is kernel ridge regression with ; the variance depends only on where the inputs are.
- The hyperparameters are fitted by maximizing = data fit + complexity penalty + constant, an automatic Occam's razor; the surface can have several local maxima.
- Exact GP regression costs time and memory. Its error bars are honest only up to the kernel: far from the data it reverts to the prior, and a wrong kernel gives confident mistakes.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which property defines a Gaussian process?
- At every single input, has a Gaussian distribution
- For every finite set of inputs, the vector of function values is jointly Gaussian
- The function is a weighted sum of Gaussian bumps
- The observation noise is Gaussian
Show the answer
B. For every finite set of inputs, the vector of function values is jointly Gaussian
A GP is defined by its finite-dimensional distributions: every finite collection of values is multivariate Gaussian, with mean and covariance . That joint structure is what lets an observation at one input inform the value at another.
You double every observed value and refit with the same hyperparameters. What happens to the posterior standard deviation at a test input?
- It doubles
- It is unchanged: the posterior variance depends on the inputs and hyperparameters, not on the observed values
- It halves, because the signal is stronger
- It grows by
Show the answer
B. It is unchanged: the posterior variance depends on the inputs and hyperparameters, not on the observed values
contains no . The mean doubles, the band does not. (Refitting the hyperparameters would change it, through and .)
A zero-mean GP with an RBF kernel has observations only on . What are its posterior mean and variance at ?
- The last observed value, with variance
- A linear extrapolation of the trend in the data, with growing variance
- Mean 0 and variance : the prior, untouched by data that far away
- Undefined, because the kernel matrix becomes singular
Show the answer
C. Mean 0 and variance : the prior, untouched by data that far away
At every is essentially 0, so and . The RBF kernel encodes no trend, so the GP reverts to its prior instead of extrapolating.
With an RBF kernel, distinct inputs and , what does the GP posterior do at the training inputs?
- The mean interpolates the data exactly and the variance there goes to 0
- The mean is the average of the observations
- The variance there stays at
- The posterior no longer exists
Show the answer
A. The mean interpolates the data exactly and the variance there goes to 0
With , because is the -th column of , and . The GP becomes an interpolator, the minimum-RKHS-norm one (Reproducing Kernel Hilbert Spaces).
On some data, the log marginal likelihood peaks at and is far lower at . Which term is mainly responsible?
- The complexity term , which grows more negative
- The constant
- The noise variance, which the length scale changes
- The data-fit term , which collapses because very smooth functions explain wiggly data badly
Show the answer
D. The data-fit term , which collapses because very smooth functions explain wiggly data badly
A long length scale makes nearly singular along wiggly directions, so explodes: in the widget the data fit goes from at to at , while the complexity term actually improves, from 1.96 to 10.72.
Go deeper
- GP Regression in Depth · Posterior equations, the marginal likelihood and Cholesky.
- Kernel Design for Gaussian Processes · Composing kernels and choosing hyperparameters.
- Bayesian Optimization · Acquisition functions, GP-UCB and optimizing expensive black boxes.
Further reading
- C. E. Rasmussen & C. K. I. Williams, Gaussian Processes for Machine Learning (MIT Press, 2006), chapters 2 and 5.
- C. K. I. Williams & C. E. Rasmussen, "Gaussian processes for regression," Advances in Neural Information Processing Systems 8 (1996).
- A. O'Hagan, "Curve fitting and optimal design for prediction," Journal of the Royal Statistical Society B 40(1), 1–24 (1978).
- G. Matheron, "Principles of geostatistics," Economic Geology 58(8), 1246–1266 (1963).