Learning Kernel Hyperparameters in Gaussian Processes
A Gaussian process's kernel has knobs — lengthscale, amplitude, noise level — and rather than guessing them, they are tuned by climbing the marginal likelihood, a scorecard that automatically penalizes both bad fit and needless complexity.
Prerequisites: Designing Gaussian Process Kernels, Gaussian Process Regression
A Gaussian process kernel like the squared-exponential has knobs — a lengthscale , an amplitude , and a noise level — and the fitted model can look wildly different depending on where those knobs are set. Set too small and the GP snakes through every noisy wiggle in the training data; set it too large and it flattens into a nearly straight line. Neither extreme is chosen by hand-tuning in practice — it is chosen by directly maximizing a number the data itself hands you: the marginal likelihood.
The analogy: a scorecard that punishes both underfitting and showing off
Imagine judging two witnesses describing the same event. One gives an almost too-perfect account that fits every tiny detail — plausible, but suspiciously overfit to what actually happened, unlikely to generalize to a slightly different retelling. The other gives a vague, generic account that fits nothing specifically. A good judge wants the account that fits the specific evidence well without being needlessly elaborate to do it. The marginal likelihood is exactly that judge for a GP: it rewards hyperparameters that fit the observed data closely, but it also automatically penalizes settings that only fit by being needlessly flexible — no separate validation set required, because the penalty is built into the formula itself.
The formula, term by term
The log marginal likelihood for a GP with data , covariance matrix (built from the kernel), and noise variance is:
The first term is the data-fit term — how well the current covariance explains the observed values; it gets better as the model fits the data more tightly. The second term is the complexity penalty — the log-determinant grows when the kernel is very flexible (small produces a covariance matrix closer to a scaled identity, which has a larger determinant), automatically punishing over-flexible settings. The third term is a constant that does not depend on the hyperparameters. Maximizing this sum, by gradient ascent over , , and , finds the setting that best balances "fits the data" against "isn't needlessly wiggly to do it."
Worked example 1: two lengthscales, two scores
Two observations at and , comparing against , both with , . At : , giving a covariance matrix with small off-diagonal terms — the model treats these two points as nearly independent, so the data-fit term is unremarkable and the complexity penalty (log-determinant) is large, since the matrix is close to -scaled and flexible. At : — near-perfect correlation, matching how close and actually are, giving a much better data-fit term with only a small complexity cost. Here the smoother, larger-lengthscale setting wins the marginal likelihood comparison because it explains this particular pair of nearby, similar values more efficiently.
Worked example 2: overfitting made concrete
With noisy training data at evenly spaced and a held-out point at with true value near : a too-small lengthscale () fits the training points almost exactly (data-fit term looks great) but predicts wildly at — say , an error of — because it has learned the noise, not the trend. A moderate lengthscale () fits training points slightly less perfectly but predicts around at , an error of only . The marginal likelihood favors the moderate setting even without ever seeing the held-out point, because its complexity penalty already flags the tiny-lengthscale fit as suspiciously flexible.
This is literally the optimizer used here — the hyperparameters , , are climbed via gradient ascent on the marginal likelihood surface exactly like a loss surface, complete with the risk of getting stuck at a poor local optimum.
Gaussian process hyperparameters are tuned by maximizing the log marginal likelihood, which automatically trades off data fit against model complexity through its log-determinant term — no separate validation set is needed, because Occam's razor is built into the formula itself.
What this means in practice
This is why GPs are prized for small-to-medium datasets in finance — volatility surface interpolation, sparse alternative-data series — where a held-out validation set would be too small to trust, but the marginal likelihood can still be computed exactly from all available points. The optimization is non-convex, though, so different starting values for can converge to different local optima with meaningfully different fitted behavior.
The common mistake is assuming the marginal likelihood surface is well-behaved and has one obvious best answer. It routinely has multiple local optima — one favoring a short lengthscale that explains data as mostly noise with a tiny fast-varying signal, another favoring a long lengthscale that explains the same data as mostly smooth signal with more noise. Both can be local maxima of the same marginal likelihood; which one gradient ascent finds depends on where it started, so restarting from several initial hyperparameter values is standard practice, not paranoia.
Related concepts
Practice in interviews
Further reading
- Rasmussen & Williams, Gaussian Processes for Machine Learning (2006), ch. 5