Foundations to Frontier
Tests
B. Statistical Learning Theory › B3 Kernel methods

Kernel Ridge Regression and Kernel PCA

Two everyday kernel algorithms. Kernel ridge regression (KRR) fits a nonlinear function in closed form with (K + λmI)α = y. It is the posterior mean of a Gaussian process, its leave-one-out error is available without refitting, and its effective degrees of freedom tr(H) quantify model complexity. Kernel PCA runs PCA in feature space by eigen-decomposing the centred Gram matrix, finding nonlinear structure that linear PCA misses. In the exercises you verify the exact LOO shortcut and the KRR–GP equivalence, tune λ along a U-shaped test curve, and show that RBF kernel PCA's first component separates concentric rings.

Level 0: Foundation

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

Two classic tools, now curved

Ridge regression fits a straight line (or plane) while discouraging extreme slopes. PCA finds the directions along which data varies most. Both are linear. Swap their inner products for a kernel and they become kernel ridge regression, which fits smooth curves in one matrix solve, and kernel PCA, which finds curved structure such as “distance from the centre”. Same algorithms, new geometry.

Kernel ridge regression and kernel PCA
leave-one-out MSE 0.122 (noise variance 0.09) · test MSE vs truth 0.0210 · effective degrees of freedom tr(H) = 9.5 of 40
Kernel ridge regression solves (K + λmI)α = y. Small λ chases the noise (many effective parameters, low training error, high test error); large λ flattens the curve. The leave-one-out error comes from one formula, (yᵢ − ŷᵢ)/(1 − Hᵢᵢ), without refitting, so it is cheap to tune λ and γ with it. With σ² = λm this is exactly the Gaussian-process posterior mean.
Why engineers care

KRR, often under the name Gaussian-process regression, is a go-to model for learning residual dynamics of a robot or drone from a few hundred samples, for calibrating sensors, and for surrogate models of expensive CFD or thermal simulations. Kernel PCA is used for nonlinear dimensionality reduction and anomaly detection on telemetry.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Kernel ridge regression fits…
  2. 2. Kernel PCA finds…
  3. 3. Increasing λ in KRR makes the fit…

Level 1: Beginner

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

Kernel ridge regression

min⁡f∈H 1m∑i(f(xi)−yi)2+λ∥f∥2 ⇒ f(x)=∑iαik(xi,x),  α=(K+λmI)−1y.\min_{f\in\mathcal H}\ \frac1m\sum_i(f(x_i) - y_i)^2 + \lambda\|f\|^2\ \Rightarrow\ f(x) = \sum_i\alpha_ik(x_i, x),\ \ \alpha = (K + \lambda mI)^{-1}y.

The fitted values are y^=Hy\hat y = Hy with the hat matrix H=K(K+λmI)−1H = K(K + \lambda mI)^{-1}.

Effective degrees of freedom

df(λ)=tr(H)=∑iμiμi+λm,\mathrm{df}(\lambda) = \mathrm{tr}(H) = \sum_i\frac{\mu_i}{\mu_i + \lambda m},

where μi\mu_i are the eigenvalues of K. Each eigen-direction counts fully (μ much larger than λm) or not at all. So df runs from m (as λ → 0) down to 0.

Cost

Training is an m×m solve, O(m³) time and O(m²) memory. Prediction is O(m) per point.

📝 Quick check (Level 1)
0/3 answered
  1. 1. KRR coefficients:
  2. 2. df for K = I (4×4), λ = 0.25, m = 4:
  3. 3. As λ → 0, df → …

Level 2: Intermediate

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

Leave-one-out for free

For any linear smoother y^=Hy\hat y = Hy where deleting point i amounts to re-solving with the same H structure (true for KRR), the leave-one-out residual is

yi−f^−i(xi)=yi−y^i1−Hii,y_i - \hat f_{-i}(x_i) = \frac{y_i - \hat y_i}{1 - H_{ii}},

so you can tune λ and γ by exact LOO at the cost of one fit.

KRR = Gaussian-process mean

With GP prior f∼GP(0,k)f\sim\mathcal{GP}(0, k) and noise N(0,σ2)\mathcal N(0, \sigma^2), the posterior mean is k∗⊤(K+σ2I)−1yk_\ast^\top(K + \sigma^2I)^{-1}y, which is KRR with λm=σ2\lambda m = \sigma^2. The GP adds a variance, k(x,x)−k∗⊤(K+σ2I)−1k∗k(x, x) - k_\ast^\top(K + \sigma^2I)^{-1}k_\ast, for uncertainty.

Kernel PCA

Centre in feature space: Kc=HKHK_c = HKH with H=I−1m11⊤H = I - \tfrac1m\mathbf 1\mathbf 1^\top. Eigen-decompose Kc/m=∑λjvjvj⊤K_c/m = \sum\lambda_jv_jv_j^\top. The projection of xix_i on component j is mλj vj[i]\sqrt{m\lambda_j}\,v_j[i], and λj\lambda_j is the variance along it. A linear kernel recovers ordinary PCA.

🐍 Exercise 1: kernel ridge regression, LOO and GPs
Implement KRR, verify the closed-form leave-one-out error against refitting m times, check that KRR equals the GP posterior mean with σ² = λm, and sweep λ to see training, LOO and test error.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The LOO residual equals…
  2. 2. KRR equals the GP posterior mean when…
  3. 3. Kernel PCA needs the Gram matrix to be…

Level 3: Advanced

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

Proof of the LOO formula

Let y~\tilde y equal y except y~i=f^−i(xi)\tilde y_i = \hat f_{-i}(x_i). Then f^−i\hat f_{-i} also minimizes the full objective on y~\tilde y (point i contributes zero loss there, and it’s optimal for the rest). So f^−i(xi)=(Hy~)i=y^i−Hii(yi−f^−i(xi))\hat f_{-i}(x_i) = (H\tilde y)_i = \hat y_i - H_{ii}(y_i - \hat f_{-i}(x_i)). Solving for yi−f^−i(xi)y_i - \hat f_{-i}(x_i) gives the formula. ∎

Why kernel PCA works

Feature-space covariance C=1m∑φ~iφ~i⊤C = \tfrac1m\sum\tilde\varphi_i\tilde\varphi_i^\top. Any eigenvector with λ ≠ 0 lies in the span of the φ~i\tilde\varphi_i, so u=∑aiφ~iu = \sum a_i\tilde\varphi_i. Substituting gives Kca=mλaK_ca = m\lambda a (on the range of KcK_c). Normalizing ∥u∥=1\|u\| = 1 means a⊤Kca=1a^\top K_ca = 1, so a=v/mλa = v/\sqrt{m\lambda}, and projections are Kca=mλ vK_ca = \sqrt{m\lambda}\,v. ∎

Bias and variance in the spectrum

KRR shrinks the component of y along each eigenvector of K by μi/(μi+λm)\mu_i/(\mu_i + \lambda m). Large-μ (smooth) directions are kept and small-μ (wiggly) ones are killed: spectral filtering. Variance shrinks with df, and bias grows as λm passes the eigenvalues where the signal lives.

📝 Quick check (Level 3)
0/3 answered
  1. 1. KRR shrinks the eigen-component i of y by…
  2. 2. The kernel-PCA projection of xᵢ on component j is…
  3. 3. Feature-space PCA eigenvectors lie in…

Level 4: Expert

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

KRR and kPCA, measured

Exercise 1 (RBF γ = 2, noise σ = 0.3). The closed-form LOO equals brute-force refitting to 10⁻¹⁵ (0.155965 for both), and KRR equals the GP mean with σ² = λm exactly. Sweeping λ with m = 100, as train / LOO / test MSE:

λ train LOO test
10⁻⁶ 0.074 0.104 0.025
10⁻⁴ 0.075 0.096 0.014
10⁻³ 0.077 0.095 0.012
10⁻² 0.105 0.122 0.046
10⁻¹ 0.409 0.445 0.403
1 0.745 0.758 0.799

The test error is U-shaped, and LOO, which is about the noise variance 0.09 plus the test error, picks the same λ = 10⁻³.

Exercise 2. Linear-kernel kPCA equals PCA to 4 × 10⁻¹⁵, and its eigenvalues are the component variances. On two rings (radii 0.5 and 1.5), a threshold on the first component separates 66.5% with linear PCA, 64.5% and 61.5% with RBF γ = 0.1 and 0.5, 100% with γ = 1 and 2, then 94.5% and 78.0% with γ = 5 and 20. The kernel must match the structure’s scale.

🐍 Exercise 2 (research): kernel PCA unrolls rings
Implement kernel PCA (centring, eigen-decomposition, projections), check that the linear kernel reproduces PCA, then measure how well a single threshold on the first component separates two concentric rings for linear PCA and RBF kernels of different widths.

KRR and kPCA in AI, robotics and aerospace

  • GP and KRR dynamics models for model-based control (PILCO), and residual-dynamics learning on quadrotors.
  • Surrogate modelling and Bayesian optimization of designs such as airfoils and thermal layouts.
  • Kernel PCA for nonlinear feature extraction and novelty detection on spacecraft telemetry.
  • Scalable variants: Nyström, random features and inducing-point GPs for onboard use.

References

  1. C. Saunders, A. Gammerman, V. Vovk (1998). “Ridge regression learning algorithm in dual variables.” ICML.
  2. B. Schölkopf, A. Smola, K.-R. Müller (1998). “Nonlinear component analysis as a kernel eigenvalue problem.” Neural Computation, 10.
  3. C. E. Rasmussen, C. K. I. Williams (2006). Gaussian Processes for Machine Learning. MIT Press.
  4. G. Wahba (1990). Spline Models for Observational Data. SIAM (leave-one-out and GCV).
  5. M. P. Deisenroth, C. E. Rasmussen (2011). “PILCO: a model-based and data-efficient approach to policy search.” ICML.