Kernel Design for Gaussian Processes
Composing kernels and choosing hyperparameters.
The kernel is a Gaussian process's entire prior. Choosing it decides how smooth the function is, whether it repeats, which inputs matter and what it does far from the data. This lesson builds kernels on purpose: the Matérn family and the smoothness it controls, periodic and rational quadratic kernels, sums and products that compose structure, automatic searches over kernel grammars, and the ways the marginal likelihood can mislead you while you choose.
What you’ll learn
- Relate a stationary kernel's spectral density to the smoothness of its sample functions, and use the Matérn family to set the number of mean-square derivatives.
- Compose kernels for structure (periodic, linear, rational quadratic, sums, products, ARD) and read a composite kernel as a description of the function.
- Describe automatic kernel construction: compositional search, spectral mixture kernels and deep kernels.
- Recognize the pitfalls of marginal-likelihood optimization (multiple optima, degenerate fits from little data, overfitting with many hyperparameters, misalignment with generalization, non-identifiable parameters) and their remedies.
Smoothness lives in the spectrum
A stationary kernel depends only on the difference of its inputs, . By Bochner's theorem (Scaling Kernel Methods) a continuous one is the Fourier transform of a finite nonnegative measure on frequencies, usually with a density , the spectral density: the power spectrum of the process, how much variance it puts at each frequency. A spectrum with heavy tails means rough samples; one that decays fast means smooth ones. Precisely, a stationary GP is times mean-square differentiable if and only if has derivatives at the origin.
| Kernel () | Sample functions | |
|---|---|---|
| (exponential; Ornstein–Uhlenbeck in one dimension) | continuous, nowhere differentiable, jagged | |
| once mean-square differentiable | ||
| twice mean-square differentiable | ||
| (squared exponential, RBF) | infinitely differentiable |
Which Matérn kernel gives sample functions that are once, but not twice, mean-square differentiable?
Show the answer
B.
A Matérn process is times mean-square differentiable exactly when : allows but not . Equivalently, its spectrum decays like , fast enough for one derivative to have finite variance but not two.
Composing structure
| Base kernel | Formula | Encodes |
|---|---|---|
| Squared exponential | smooth local variation on scale | |
| Periodic | a pattern that repeats exactly with period ; it is an RBF kernel applied to | |
| Linear | straight lines (Bayesian linear regression); non-stationary, so it extrapolates trends | |
| Rational quadratic | variation on many scales at once: a mixture of RBF kernels whose inverse squared length scales are Gamma distributed; it tends to the RBF as | |
| White noise | independent noise |
Two operations build almost everything else:
- Sum. If and are independent, then : a sum of kernels is a sum of independent components, such as trend plus seasonality plus noise. Summing one-dimensional kernels over the inputs, , gives an additive model.
- Product. lets one structure modulate another. Periodic times a long-scale RBF is locally periodic, a cycle whose shape drifts; linear times linear is quadratic; linear times periodic is an oscillation of growing amplitude.
Automatic relevance determination (ARD) gives each input its own length scale, . Fitting the by marginal likelihood stretches the length scales of irrelevant inputs until the function barely changes along them, a soft form of feature selection (MacKay, 1994; Neal, 1996).
You want a GP for daily electricity demand with a weekly pattern whose shape changes slowly over the years. Which kernel fits that description?
- A periodic kernel with a one-week period, alone
- A periodic kernel plus an RBF kernel with a length scale of years
- A periodic kernel times an RBF kernel with a length scale of years
- An RBF kernel with a short length scale
Show the answer
C. A periodic kernel times an RBF kernel with a length scale of years
Under the product, two days a whole number of weeks apart are strongly correlated only if they are also within a few years of each other, so the weekly shape is free to evolve slowly. A sum would add an independent slow component to a fixed weekly shape.
Letting the data choose the kernel
Composition turns kernel design into a search problem. Duvenaud, Lloyd, Grosse, Tenenbaum & Ghahramani (2013) defined a grammar whose sentences are kernels: start from base kernels (squared exponential, periodic, linear, rational quadratic), and grow an expression by adding or multiplying a base kernel, or by replacing one. A greedy search expands the best structure found so far, fitting the hyperparameters of each candidate and scoring it by the marginal likelihood, with the integral over the hyperparameters approximated by the Bayesian information criterion. On time series the selected kernels decompose the data into interpretable additive parts (a trend, a periodic component, growing oscillations) that extrapolate sensibly.
A different route makes the kernel itself flexible. By Bochner's theorem a stationary kernel is equivalent to its spectral density, and Gaussian mixtures can approximate any density, so modeling the spectrum as a Gaussian mixture yields a family that can approximate any stationary kernel: the spectral mixture kernels of Wilson & Adams (2013).
Take a symmetrized Gaussian mixture as the spectral density over frequencies , with weights , means and variances :
The Fourier transform of is (a Gaussian's characteristic function), and the symmetric pair turns the complex exponential into a cosine:
Each component is a quasi-periodic pattern with frequency that decorrelates over a distance of order ; with it reduces to an RBF kernel. Fitting the parameters by marginal likelihood lets the model discover periodic structure and extrapolate it, at the price of a likelihood surface with many local optima that needs careful initialization.
Deep kernel learning (Wilson, Hu, Salakhutdinov & Xing, 2016) goes furthest: it applies a base kernel to the outputs of a neural network, , and fits the network weights together with the kernel hyperparameters by maximizing the marginal likelihood. Millions of hyperparameters push type-II maximum likelihood far outside the regime where it can be trusted, which is the subject of the next step.
When the marginal likelihood misleads
Maximizing the marginal likelihood over hyperparameters (type-II maximum likelihood) is the default, and it usually works with a handful of hyperparameters and enough data. Its failure modes:
- Several local optima. A short length scale with little noise and a long one with much noise can explain the same data about equally well (Rasmussen & Williams, 2006, §5.4.1). Restart from several initial values, and look at more than the winner.
- Too little data. With very few points, the global maximum can be degenerate, for instance "these values are unrelated" (the shortest allowed length scale) or "this is all noise".
- Too many hyperparameters. Type-II maximum likelihood is itself a fit and can overfit. Deep kernel learning can overfit severely even on a simple toy problem, although marginal-likelihood training is often assumed to protect against it (Ober, Rasmussen & van der Wilk, 2021).
- The wrong question. The marginal likelihood measures how probable the training data were before seeing any of them; it can even be negatively correlated with how well the posterior generalizes. The conditional marginal likelihood, which scores later data given earlier data, is better aligned with prediction (Lotfi, Izmailov, Benton, Goldblum & Wilson, 2022).
For a zero-mean Gaussian vector with precision matrix , conditioning one coordinate on all the others is read straight off (the conditioning theorem of GP Regression in Depth, written in precision form):
With , the mean simplifies, so one factorization yields all leave-one-out predictions (Rasmussen & Williams, 2006, §5.4.2), the GP analogue of the one-fit leave-one-out formula for linear smoothers:
Summing the leave-one-out log predictive densities gives a criterion that measures prediction rather than prior plausibility, at the same cost as the marginal likelihood.
Key takeaways
- A stationary kernel's spectrum sets sample smoothness: Matérn- processes are times mean-square differentiable exactly when ; is jagged, and are the workhorses, is the RBF.
- Sums of kernels are sums of independent components; products modulate one structure by another (locally periodic = periodic × RBF). ARD length scales switch irrelevant inputs off.
- Kernels can be searched (compositional grammars scored by marginal likelihood), made flexible (spectral mixtures from Gaussian-mixture spectra, ) or learned with networks (deep kernels).
- Type-II maximum likelihood can mislead: several optima, degenerate fits from few points, overfitting with many hyperparameters, misalignment with generalization, and parameters such as the Matérn variance and length scale that are not separately identifiable. Priors, closed-form leave-one-out and full Bayesian averaging are the remedies.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
After fitting an ARD squared-exponential kernel to standardized inputs, the length scale of input 3 is 500 while the others are near 1. What does that say?
- Input 3 is the most important, since its length scale is largest
- The function barely changes along input 3 over the range of the data: the model has effectively switched it off
- The fit failed, because length scales must be comparable
- Input 3 must be rescaled by 500 and the model refitted
Show the answer
B. The function barely changes along input 3 over the range of the data: the model has effectively switched it off
A length scale far beyond the data's spread (here 1, after standardization) makes the kernel nearly constant along that input, so the posterior ignores it. ARD "determines relevance" by stretching the scales of irrelevant inputs.
and are independent. Which kernel describes ?
Show the answer
A.
, because the cross-covariances vanish by independence. This is why sums of kernels describe additive components.
You fit an RBF GP's hyperparameters to three observations and the length scale hits its lower bound, declaring the values unrelated. What is the sound response?
- Trust it: the marginal likelihood is an automatic Occam's razor
- Lower the bound further, since the optimum lies beyond it
- Add a prior on the hyperparameters or use domain knowledge, collect more data, or average over hyperparameters, because three points cannot pin down three hyperparameters
- Switch to the periodic kernel, which fits small datasets better
Show the answer
C. Add a prior on the hyperparameters or use domain knowledge, collect more data, or average over hyperparameters, because three points cannot pin down three hyperparameters
With very few points, type-II maximum likelihood can land on degenerate explanations that fit the few numbers well but generalize badly; in the widget this happens with 3 or 4 points and not with 5 or more. Priors, more data or integrating out the hyperparameters are the standard fixes.
Data from a Matérn GP (fixed ) become ever denser in a fixed region of . Which quantity can be estimated consistently?
- The length scale
- The signal variance
- Both and , as for any parametric model
- The ratio
Show the answer
D. The ratio
Zhang (2004): models with equal define equivalent Gaussian measures on a bounded domain in dimension at most 3, so no data from that domain can separate them, but the ratio itself is estimable, and it is what interpolation mostly depends on.
Further reading
- C. E. Rasmussen & C. K. I. Williams, Gaussian Processes for Machine Learning (MIT Press, 2006), chapters 4 and 5.
- M. L. Stein, Interpolation of Spatial Data: Some Theory for Kriging (Springer, 1999).
- D. Duvenaud, J. R. Lloyd, R. Grosse, J. B. Tenenbaum & Z. Ghahramani, "Structure discovery in nonparametric regression through compositional kernel search," Proceedings of ICML, PMLR 28(3), 1166–1174 (2013).
- A. G. Wilson & R. P. Adams, "Gaussian process kernels for pattern discovery and extrapolation," Proceedings of ICML, PMLR 28(3), 1067–1075 (2013).
- H. Zhang, "Inconsistent estimation and asymptotically equal interpolations in model-based geostatistics," Journal of the American Statistical Association 99(465), 250–261 (2004).