Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Letting the data choose the settings
Before a GP can predict, it needs its settings: how far apart two points can be and still be related (the lengthscale), how big the function’s swings are, and how noisy the measurements are. Guessing them badly ruins the error bars, as the previous topic showed. The usual fix is to pick the settings under which the observed data would have been most probable. That score automatically balances fitting the data against being too flexible. The demo plots the score against the lengthscale and shows its two competing parts: one rewards wiggly fits, the other punishes them.
Every GP library and every Bayesian optimization tool learns hyperparameters this way, many times per run. Knowing its traps (local optima, noise collapsing to zero, irrelevant inputs) is the difference between a surrogate model you can trust and one that silently interpolates noise.
- 1. GP hyperparameters are usually chosen by maximizing…
- 2. The evidence balances data fit against…
- 3. A noise variance that collapses to almost zero usually means…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.The evidence and its gradient
- Log marginal likelihood: log p(y | X, θ) = −½ yᵀC⁻¹y − ½ log|C| − (n/2) log 2π with C = K_θ + σ_n²I.
- Data fit −½ yᵀC⁻¹y: larger (closer to 0) for flexible models (short ℓ).
- Complexity −½ log|C|: more negative for flexible models, whose prior spreads over many datasets.
- Diagonal example: y = (1, 2) with variances (1, 4) gives data fit −1.0 and complexity −log 2 = −0.693.
- Gradient: ∂/∂θ_j log p = ½ tr((ααᵀ − C⁻¹) ∂C/∂θ_j), α = C⁻¹y. One O(n³) factorization gives all gradients; each extra hyperparameter costs O(n²).
- RBF in log-parameters: ∂C/∂log ℓ_d = K ∘ D_d² / ℓ_d², ∂C/∂log σ_f = 2K, ∂C/∂log σ_n = 2σ_n²I.
- Profiling the scale: writing C = σ_f²(R + λI), the optimal σ_f² = yᵀ(R + λI)⁻¹y / n in closed form; search only over ℓ and the noise ratio λ. For y = (2, 2) and R = I, σ_f² = 4.
- Optimizers: L-BFGS on log-parameters with several restarts (scikit-learn’s n_restarts_optimizer, GPyTorch with Adam or L-BFGS). A grid of 20 values per hyperparameter costs 20^d evaluations: 8000 for d = 3 but 2.56 × 10¹⁰ for d = 8.
- Alternatives:
- LOO-CV: maximize Σ log p(y_i | y₋ᵢ) using the closed-form LOO (I2-1); more robust when the model is misspecified (Sundararajan and Keerthi, 2001).
- MAP-II: add hyperpriors, e.g. log ℓ ~ N(0, 1.5²); this charges 4.71 nats for ℓ = 100 compared with ℓ = 1, keeping ARD lengthscales finite.
- Fully Bayesian: sample θ with slice sampling or HMC and average predictions (Murray and Adams, 2010); best for small n and Bayesian optimization.
- 1. The −½ log|C| term…
- 2. All hyperparameter gradients need…
- 3. The signal variance can be…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Learning hyperparameters in practice
Exercise 1.
- The data-fit and complexity terms of the evidence.
- The analytic gradient, checked against finite differences.
- The closed-form optimal signal variance.
Practice.
- Standardize inputs and outputs first so that ℓ ≈ 1 and σ_f ≈ 1 are sensible starts.
- Optimize log-parameters with L-BFGS from 5–20 random restarts; keep the best evidence, but inspect the runners-up.
- Put a lower bound or prior on the noise (e.g. σ_n ≥ 1e-3 of the output sd) so the model cannot interpolate noise; BoTorch uses Gamma/log-normal hyperpriors by default.
- With ARD, expect irrelevant lengthscales to drift to large values: use priors or report them as ‘off’.
- With fewer than about 20 points per dimension, or for Bayesian optimization, prefer MCMC over hyperparameters or at least compare a few optima.
- Check the result on held-out data or LOO; a high evidence is not a guarantee.
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import ConstantKernel as C, Matern, WhiteKernel
k = C(1.0, (1e-2, 1e2)) * Matern(length_scale=[1.0] * 6, length_scale_bounds=(1e-2, 1e3), nu=2.5) \
+ WhiteKernel(1e-2, (1e-5, 1e1)) # ARD lengthscales, bounded noise
gp = GaussianProcessRegressor(k, normalize_y=True, n_restarts_optimizer=10, random_state=0).fit(X, y)
print(gp.kernel_, gp.log_marginal_likelihood_value_) # inspect learned lengthscales and noise- 1. Restarts in hyperparameter optimization guard against…
- 2. A lower bound on the noise prevents…
- 3. For Bayesian optimization with few points, hyperparameters are best…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why the gradient formula holds. With C depending on θ_j, ∂(yᵀC⁻¹y)/∂θ_j = −yᵀC⁻¹(∂C/∂θ_j)C⁻¹y = −αᵀ(∂C)α and ∂ log|C|/∂θ_j = tr(C⁻¹ ∂C). So the gradient is ½αᵀ(∂C)α − ½tr(C⁻¹∂C) = ½ tr((ααᵀ − C⁻¹)∂C).
Why there are several optima. The evidence is not concave in θ. Data can be explained as a smooth signal plus moderate noise, as a wiggly signal with almost no noise, or as pure noise with an infinitely long lengthscale. Each is a local maximum (Rasmussen and Williams, 2006, §5.4.1, Fig. 5.5). Which one wins depends on the sample: with 20 points, a few chance alignments can make the interpolating solution more probable.
ARD and the boundary. For an irrelevant input the evidence keeps rising slowly as ℓ_d → ∞ (the input’s contribution to K vanishes), so the maximum is at infinity and the gradient never reaches zero. A weak prior on log ℓ_d makes the optimum finite and the problem well-posed: that is MAP-II, the default in BoTorch and GPyTorch practice.
Consistency and caveats. With growing data from a fixed kernel family, type-II ML is consistent for some parameters but not others: for Matérn kernels in fixed domains, only the combination σ_f²/ℓ^(2ν) (the microergodic parameter) can be estimated consistently (Zhang, 2004), and predictions are insensitive to the rest. Under misspecification, evidence-based choices can be overconfident, and LOO-CV is often more reliable for prediction (Bachoc, 2013).
- 1. The evidence as a function of hyperparameters is…
- 2. For an irrelevant ARD input, the evidence-maximizing lengthscale is…
- 3. Under misspecification, a common alternative to the evidence is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How reliable is type-II maximum likelihood: how many local optima, and does ARD find the relevant inputs?
History.
- D. MacKay, “Bayesian interpolation” (1992) and “Bayesian methods for backpropagation networks” (1994): the evidence framework and ARD.
- C. Williams and C. Rasmussen, “Gaussian processes for regression” (1996): ML-II for GP hyperparameters.
- S. Sundararajan and S. Keerthi, “Predictive approaches for choosing hyperparameters in Gaussian processes” (2001).
- I. Murray and R. Adams, “Slice sampling covariance hyperparameters of latent Gaussian models” (2010).
Exercise 2a: the evidence landscape. 20 points from sin(1.3x)·e^(−0.05x²) on [−5, 5] with noise sd 0.3, RBF kernel. With σ_f² profiled out, the evidence is computed on a 61 × 51 grid of lengthscale (0.05 to 50) and noise ratio σ_n²/σ_f² (10⁻⁴ to 10). Local maxima are found, and each grid cell is assigned to the optimum its steepest uphill path on the grid reaches (its basin).
| optimum | lengthscale | noise sd (true 0.3) | log evidence | share of starts | test RMSE |
|---|---|---|---|---|---|
| signal + noise | 1.0 | 0.186 | −8.22 | 45.3% | 0.170 |
| overfitting | 0.1 | 0.117 | −11.79 | 18.9% | 0.409 |
| all noise | 50 (edge, → ∞) | 0.518 | −15.77 | 35.8% | 0.521 |
- Local optima are real: a local optimizer started at random would miss the best optimum more often than not (55% of starts).
- Negative result (seed 1): the highest-evidence optimum was the overfitting one: ℓ = 0.14 with noise sd 0.006, log evidence −14.56 and RMSE 0.50, beating the sensible ℓ = 0.45 (noise 0.274, −15.07, RMSE 0.341). Seed 2 had only two optima, and the best (ℓ = 1.0, noise 0.317, RMSE 0.127) drew 72% of starts.
Exercise 2b: ARD. 80 points in 6-D, y = sin(1.5x₁) + 0.5x₂² + noise (sd 0.1); inputs 3–6 are irrelevant. ARD RBF vs one shared lengthscale, both fitted by BFGS on log-parameters with weak log-normal hyperpriors (log ℓ ~ N(0, 1.5²), log σ_n ~ N(log 0.1, 1.5²)), converged to gradient norm below 10⁻⁵.
| model | relevant lengthscales | irrelevant lengthscales | noise sd | test RMSE (500 points) |
|---|---|---|---|---|
| ARD | 1.8, 2.67 | ≥ 61.2 | 0.073 | 0.068 |
| shared lengthscale | 2.29 for all | – | 0.048 | 0.491 |
- ARD switched off the 4 irrelevant inputs and was 7 times more accurate; a shared lengthscale must treat all 6 inputs as equally important.
- Seeds 1 and 2: ARD RMSE 0.095 and 0.076 vs 0.504 and 0.495; the smallest irrelevant lengthscale was 16.7 and 49.1 (still at least 10 times the relevant ones).
Hyperparameter learning in AI, robotics and aerospace
- Bayesian optimization refits GP hyperparameters after every evaluation; BoTorch and Ax use MAP with priors or fully Bayesian SAASBO (Eriksson and Jankowiak, 2021) for high-dimensional problems where ARD decides which design variables matter.
- Surrogates of simulators: ARD lengthscales of a GP emulator are a cheap sensitivity analysis: which inputs of a thermal or aerodynamic model matter.
- Robot learning: GP dynamics models (PILCO) relearn hyperparameters as data arrive; noise floors prevent collapse to interpolation.
References
- D. J. C. MacKay (1992). “Bayesian interpolation.” Neural Computation, 4.
- C. K. I. Williams and C. E. Rasmussen (1996). “Gaussian processes for regression.” NeurIPS.
- S. Sundararajan and S. S. Keerthi (2001). “Predictive approaches for choosing hyperparameters in Gaussian processes.” Neural Computation, 13.
- H. Zhang (2004). “Inconsistent estimation and asymptotically equal interpolations in model-based geostatistics.” JASA, 99.
- C. E. Rasmussen and C. K. I. Williams (2006). Gaussian Processes for Machine Learning, ch. 5. MIT Press.
- I. Murray and R. P. Adams (2010). “Slice sampling covariance hyperparameters of latent Gaussian models.” NeurIPS.
- F. Bachoc (2013). “Cross validation and maximum likelihood estimations of hyper-parameters of Gaussian processes with model misspecification.” Computational Statistics and Data Analysis, 66.
- D. Eriksson and M. Jankowiak (2021). “High-dimensional Bayesian optimization with sparse axis-aligned subspaces.” UAI.