Foundations to Frontier
Tests
D. Classical Machine Learning › D5 Support Vector Machines and Kernel Methods

Kernel Ridge Regression

Kernel ridge regression (KRR) is ridge regression with the kernel trick. It is squared loss plus an RKHS-norm penalty, and its closed-form solution α = (K + λI)⁻¹y gives a smooth non-linear regressor with one linear solve. The same formula appears as the Gaussian-process posterior mean, as regularisation networks and as kriging. This page covers the representer-theorem derivation, the smoother matrix and effective degrees of freedom, exact leave-one-out cross-validation for free, the link to Gaussian processes, and scaling with Nyström and random Fourier features. In the exercises you implement KRR and verify its equivalences; then you measure how many approximate features it takes to match exact KRR.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Ridge regression, bent by a kernel

Ridge regression fits a straight line (or plane) with a penalty that keeps the weights small. Kernel ridge regression keeps the same squared-error-plus-penalty recipe, but measures similarity with a kernel, so the fit can bend. The whole model is one linear solve, α = (K + λI)⁻¹y, followed by a weighted sum of kernel bumps. Unlike SVR, every training point gets a weight, there is no tube, and there is no iterative solver.

Kernel ridge regression: λ, γ and random features
effective degrees of freedom 8.5 of 40 · leave-one-out RMSE 0.236
Solid: exact kernel ridge; dotted: true curve; dashed: random-Fourier-feature approximation with m features. Small λ and large γ give many degrees of freedom and a wiggly fit (LOO error rises); large λ or small γ flatten it. With a few dozen random features the approximation already tracks the exact fit closely.
Why engineers care

KRR is the fastest way to get a good smooth non-linear regressor on a few thousand points. It is exactly the mean prediction of a Gaussian process, which is widely used for surrogate models in aerospace design, calibration maps and robot dynamics. Exact leave-one-out error is available in closed form, so tuning is cheap.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Kernel ridge regression uses which loss?
  2. 2. Training KRR requires…
  3. 3. Compared with SVR, KRR is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Solving it by hand

Two points, λ = 1. Training inputs x₁ and x₂ with targets y = (2, 4) and kernel values K = [[1, 0.5], [0.5, 1]].

  • K + λI = [[2, 0.5], [0.5, 2]], with determinant 4 − 0.25 = 3.75.
  • α = (K + I)⁻¹y = (1/3.75)·[[2, −0.5], [−0.5, 2]]·(2, 4) = (1/3.75)·(2, 7) = (0.533, 1.867).
  • Fitted value at x₁: f(x₁) = 1·0.533 + 0.5·1.867 = 1.467, shrunk toward 0 from y₁ = 2.
  • A new point with kernel values (0.2, 0.8): f = 0.2·0.533 + 0.8·1.867 = 1.6.

Degrees of freedom. The eigenvalues of K are 1.5 and 0.5. Then trace(H) = 1.5/2.5 + 0.5/1.5 = 0.6 + 0.333 = 0.933, less than 2 because of shrinkage.

The steps in plain English:

  1. Standardise the inputs and choose a kernel (RBF by default) and its width.
  2. Build K, solve (K + λI)α = y with a Cholesky factorisation, and predict with f(x) = Σαᵢk(xᵢ, x).
  3. Tune λ and γ with the exact leave-one-out formula, or with k-fold CV.
  4. Centre y first (or add a constant to the kernel), since the penalty also shrinks the mean.
📝 Quick check (Level 1)
0/3 answered
  1. 1. For K = I and λ = 1, α equals…
  2. 2. Effective degrees of freedom as λ → 0 (K invertible)?
  3. 3. Why centre y?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Derivation, smoother and LOO

Objective. Minimise Σ(yᵢ − f(xᵢ))² + λ‖f‖²_𝓗. By the representer theorem f = Σαᵢk(xᵢ, ·), so the objective becomes ‖y − Kα‖² + λαᵀKα. Setting the gradient to zero gives K((K + λI)α − y) = 0, solved by α = (K + λI)⁻¹y.

Linear kernel. K = XXᵀ gives w = Xᵀα = (XᵀX + λI)⁻¹Xᵀy, which is ordinary ridge (the push-through identity). Solve in the primal when n ≫ p and in the dual when p ≫ n.

Smoother. Fitted values ŷ = Hy with H = K(K + λI)⁻¹. With K = UΛUᵀ, H = U·diag(λᵢ/(λᵢ + λ))·Uᵀ: it shrinks each eigen-direction of the data. Effective degrees of freedom df(λ) = trace(H) = Σλᵢ/(λᵢ + λ).

Exact leave-one-out. The LOO residual is eᵢ = (yᵢ − ŷᵢ)/(1 − Hᵢᵢ), with no refitting. Generalised CV replaces Hᵢᵢ by trace(H)/n.

Gaussian processes. With prior f ~ GP(0, k) and noise variance σ², the posterior mean is k(x, X)(K + σ²I)⁻¹y, which is KRR with λ = σ². The GP adds a predictive variance and marginal-likelihood tuning.

Scaling.

Method Time Memory
Exact (Cholesky) O(n³) O(n²)
Nyström, m landmarks O(nm²) O(nm)
Random Fourier features, m O(nm²) O(nm)
Conjugate gradients O(n² · iterations) O(n²) or on the fly
from sklearn.kernel_ridge import KernelRidge
model = KernelRidge(kernel="rbf", alpha=0.1, gamma=0.5).fit(X, y)   # 'alpha' here is λ
🐍 Exercise 1: kernel ridge and its equivalences
Implement KRR, prediction, exact leave-one-out residuals and effective degrees of freedom; verify that the linear kernel reproduces ridge, that the LOO formula matches 50 refits, that KRR equals the GP posterior mean, and that degrees of freedom fall as λ grows.
📝 Quick check (Level 2)
0/3 answered
  1. 1. LOO residual in closed form?
  2. 2. KRR equals the GP posterior mean when λ equals…
  3. 3. Effective degrees of freedom are…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory

Spectral filtering view. KRR applies the filter λᵢ/(λᵢ + λ) to the kernel’s eigen-directions. Directions with eigenvalues much larger than λ pass, and those much smaller are suppressed. Other filters give other methods:

  • truncation gives kernel PCA regression;
  • an early-stopped gradient filter gives Landweber iteration (early stopping as regularisation; Yao, Rosasco and Caponnetto, 2007).

Statistical rates. With a well-chosen λ, KRR attains minimax-optimal rates over Sobolev-type function classes. The bias-variance trade-off is governed by df(λ) (Caponnetto and De Vito, 2007).

Approximations.

  • Random Fourier features use Bochner’s theorem: a shift-invariant kernel is the Fourier transform of a probability measure, so k(x, z) ≈ z(x)·z(z) with random cosines (Rahimi and Recht, 2007).
  • Nyström projects onto the span of m landmark points.
  • Both need m on the order of the effective degrees of freedom for little loss (Rudi, Camoriano and Rosasco, 2015; Rudi and Rosasco, 2017). Exercise 2: both matched exact KRR to within 7% at m = 200 with n = 1500.

KRR versus SVR versus GP.

  • KRR: squared loss, dense, closed form.
  • SVR: ε-insensitive loss, sparse, robust to outliers.
  • GP: same mean as KRR, plus uncertainty and evidence-based tuning.

Interview questions.

  • Why can LOO-CV be computed for free in KRR? Predictions are linear in y (ŷ = Hy), and the Sherman–Morrison identity gives the LOO residual from Hᵢᵢ.
  • When does KRR break down? Large n (n³ time, n² memory), heavy-tailed noise (squared loss), and high-dimensional inputs with an isotropic kernel.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Random Fourier features rely on…
  2. 2. Nyström approximates K using…
  3. 3. KRR's main weakness on large data is…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

How many features does approximate KRR need?

History.

  • Kernel ridge regression goes back to kriging in geostatistics (Krige, 1951; Matheron, 1963).
  • Smoothing splines (Wahba, 1990) and regularisation networks (Poggio and Girosi, 1990) are close relatives.
  • Saunders, Gammerman and Vovk (1998) gave the dual “kernel ridge” form.
  • Williams and Seeger (2001) introduced Nyström for kernel machines, and Rahimi and Recht (2007) introduced random features (NeurIPS test-of-time award, 2017).
  • FALKON (Rudi, Carratino and Rosasco, 2017) scaled KRR to billions of points.

Exercise 1.

  • The linear kernel reproduces ridge, and the closed-form LOO residuals match 50 refits exactly.
  • KRR equals the GP posterior mean.
  • Effective degrees of freedom are 10.4, 7.1 and 2.3 (of 50) for λ = 0.001, 0.1 and 10. Even with almost no regularisation the RBF kernel’s fast-decaying spectrum keeps df far below n.

Exercise 2. Setup: a smooth 3-D target, σ = 0.1 noise, n = 1500, γ = 0.5, λ = 0.1. The table shows test RMSE against the noise-free target, averaged over 3 seeds. Exact KRR: 0.0307, about 25 ms natively.

m 25 50 100 200
Nyström 0.204 0.122 0.044 0.031
Random Fourier features 0.276 0.062 0.038 0.033
  • Both approximations converge to exact KRR as m grows. At m = 200, Nyström was within 2% and RFF within 7%, in about 4 ms against 25 ms.
  • There was no consistent winner at small m. Nyström led at m = 25 and RFF at m = 50 and 100. Uniform landmark sampling makes Nyström high-variance. Leverage-score sampling reduces this (Alaoui and Mahoney, 2015).
  • Expect the time gap to widen with n, since exact KRR costs O(n³) and the approximations O(nm²). In the browser (single-threaded) the exact solve is relatively slower still.
🐍 Exercise 2 (research): Nyström and random features versus exact KRR
Fit exact KRR, Nyström KRR and random-Fourier-feature KRR (m in {25, 50, 100, 200}) to a 3-D regression problem with n = 1500; report test RMSE against the noise-free target and fit time, averaged over 3 seeds.

Kernel ridge in AI, robotics and aerospace

  • Surrogate models for aerodynamic and structural design (often as GP or kriging).
  • Calibration maps for sensors and actuators with smooth non-linearities.
  • Learning robot dynamics residuals from a few thousand samples.
  • Kernel methods for neural networks: the neural tangent kernel makes wide-network training equivalent to kernel ridge-like regression.

References

  1. C. Saunders, A. Gammerman, V. Vovk (1998). “Ridge regression learning algorithm in dual variables.” ICML.
  2. C. E. Rasmussen, C. K. I. Williams (2006). Gaussian Processes for Machine Learning. MIT Press.
  3. C. K. I. Williams, M. Seeger (2001). “Using the Nyström method to speed up kernel machines.” NeurIPS.
  4. A. Rahimi, B. Recht (2007). “Random features for large-scale kernel machines.” NeurIPS.
  5. A. Rudi, L. Carratino, L. Rosasco (2017). “FALKON: an optimal large scale kernel method.” NeurIPS.