Quant Memo
Advanced

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 \ell, an amplitude σ2\sigma^2, and a noise level σn2\sigma_n^2 — and the fitted model can look wildly different depending on where those knobs are set. Set \ell 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 yy, covariance matrix KK (built from the kernel), and noise variance σn2\sigma_n^2 is:

logp(y)=12y(K+σn2I)1y    12logK+σn2I    n2log(2π)\log p(y) = -\frac{1}{2} y^\top (K + \sigma_n^2 I)^{-1} y \; - \; \frac{1}{2}\log\left|K + \sigma_n^2 I\right| \; - \; \frac{n}{2}\log(2\pi)

The first term is the data-fit term — how well the current covariance explains the observed yy 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 \ell 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 \ell, σ2\sigma^2, and σn2\sigma_n^2, 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 x=0,y=1x=0, y=1 and x=1,y=1.1x=1, y=1.1, comparing =0.5\ell=0.5 against =5\ell=5, both with σ2=1\sigma^2=1, σn2=0.01\sigma_n^2 = 0.01. At =0.5\ell=0.5: k(0,1)=exp(1/(2×0.25))=0.135k(0,1)=\exp(-1/(2\times0.25)) = 0.135, 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 σn2I\sigma_n^2 I-scaled and flexible. At =5\ell = 5: k(0,1)=exp(1/50)=0.980k(0,1) = \exp(-1/50) = 0.980 — near-perfect correlation, matching how close y=1y=1 and y=1.1y=1.1 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 y={1.0,1.3,0.9,1.4,1.0}y = \{1.0, 1.3, 0.9, 1.4, 1.0\} at evenly spaced x=0,1,2,3,4x = 0,1,2,3,4 and a held-out point at x=2.5x=2.5 with true value near 1.151.15: a too-small lengthscale (=0.2\ell=0.2) fits the training points almost exactly (data-fit term looks great) but predicts wildly at x=2.5x=2.5 — say 0.60.6, an error of 0.550.55 — because it has learned the noise, not the trend. A moderate lengthscale (=1.5\ell=1.5) fits training points slightly less perfectly but predicts around 1.11.1 at x=2.5x=2.5, an error of only 0.050.05. 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.

lengthscale ℓ → complexity penalty data-fit term best ℓ
The marginal likelihood (solid) is the sum of the two dashed terms — it peaks where fitting the data well and staying simple are best balanced, not at either extreme.

Gradient descent
parameter →
position -0.623loss -0.141gradient -0.930converging

This is literally the optimizer used here — the hyperparameters \ell, σ2\sigma^2, σn2\sigma_n^2 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 \ell 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
ShareTwitterLinkedIn