Quant Memo
Advanced

Kernel Ridge Regression

Kernel ridge regression takes plain ridge regression and, using the kernel trick, lets it fit curved, nonlinear relationships while keeping the exact same closed-form solve and the exact same regularization logic that makes ordinary ridge regression reliable.

Prerequisites: Ridge and LASSO Regularization, The Representer Theorem

Plain ridge regression fits a straight line, penalizing large coefficients to avoid overfitting — reliable, fast, closed-form, but limited to linear relationships. Kernel ridge regression keeps every one of ridge regression's good properties — the same closed-form solution, the same well-understood regularization — while letting the fitted relationship curve arbitrarily, by applying the kernel trick: replace every dot product in ridge regression's math with a kernel evaluation, and the whole machine now fits nonlinear functions without ever explicitly constructing the (possibly infinite-dimensional) features the kernel implicitly represents.

The analogy: the same recipe, richer ingredients

Ordinary ridge regression is a recipe that combines a small set of ingredients (the raw features) in fixed, linear proportions, with a penalty discouraging any one ingredient from dominating. Kernel ridge regression uses the exact same recipe — the same "combine and penalize dominance" structure — but swaps in a vastly richer, effectively unlimited pantry of implicit ingredients (all the nonlinear feature combinations the kernel represents), while the recipe's logic never has to change. You get the flexibility of a much bigger ingredient list without ever needing to write out what all those ingredients actually are.

The model

Standard ridge regression solves β^=(XTX+λI)1XTy\hat\beta = (X^TX + \lambda I)^{-1}X^Ty. The representer theorem guarantees the same optimization, written entirely in terms of a kernel kk, has the equivalent closed form

α=(K+λI)1y,f^(x)=i=1nαik(x,xi)\alpha = (K + \lambda I)^{-1} y, \qquad \hat f(x) = \sum_{i=1}^n \alpha_i\, k(x, x_i)

Plain English: instead of solving for a coefficient per feature (as in plain ridge), kernel ridge regression solves for a coefficient per training point, using the Gram matrix KK (all pairwise kernel evaluations) in place of XTXX^TX. This is still a single matrix inversion — no iterative optimization, no local minima — just performed in a different, nn-dimensional coordinate system instead of the original feature space.

Worked example 1: linear kernel recovers plain ridge

With k(x,x)=xxk(x,x')=x\cdot x' (the linear kernel), kernel ridge regression is mathematically identical to plain ridge regression — just computed via the dual formula instead of the primal one. On 2 points x1=1,x2=2x_1=1,x_2=2, y=(3,5)y=(3,5), λ=0.5\lambda=0.5: K=(1224)K=\begin{pmatrix}1&2\\2&4\end{pmatrix}, K+0.5I=(1.5224.5)K+0.5I=\begin{pmatrix}1.5&2\\2&4.5\end{pmatrix}, determinant =1.5(4.5)2(2)=6.754=2.75=1.5(4.5)-2(2)=6.75-4=2.75. Inverting and multiplying by yy gives α(0.36,0.945)\alpha \approx (0.36, 0.945) (solving the 2×22\times2 system directly). Predicting at x=1.5x^*=1.5: f^(1.5)=0.36k(1.5,1)+0.945k(1.5,2)=0.36(1.5)+0.945(3)=0.54+2.835=3.375\hat f(1.5) = 0.36\,k(1.5,1)+0.945\,k(1.5,2) = 0.36(1.5)+0.945(3)=0.54+2.835=3.375 — the same answer plain ridge regression would give solving directly for a single slope coefficient, just reached via the nn-dimensional dual route.

Worked example 2: RBF kernel fits a curve plain ridge cannot

Now use the RBF kernel k(x,x)=eγ(xx)2k(x,x')=e^{-\gamma(x-x')^2} with γ=1\gamma=1 on the same setup but a nonlinear target: x=(0,1,2)x=(0,1,2), y=(0,1,0)y=(0, 1, 0) — a bump shape no straight line can fit well. Plain linear ridge regression would fit a compromise line with substantial error at every point (predicting something like a flat-ish line near 0.33 everywhere, missing the bump entirely). Kernel ridge regression with the RBF kernel, by contrast, can place weight α\alpha concentrated near x2=1x_2=1 so predictions rise near x=1x=1 and fall back toward zero away from it — reproducing the bump shape, something achievable only because the RBF kernel implicitly represents infinitely many nonlinear basis functions, none of which needed to be written down explicitly.

Function explorer
-2260.1
x = 1.00f(x) = 2.718

The RBF kernel's shape is itself a decaying exponential in squared distance — dragging this curve's parameters mirrors how a wider or narrower kernel bandwidth γ\gamma changes how sharply kernel ridge regression's fit can bend around individual training points.

plain ridge (linear) kernel ridge (RBF)
A straight line can't follow a bump-shaped relationship, but the RBF kernel lets kernel ridge regression bend to fit it — same closed-form solve, richer implicit features.

What this means in practice

Kernel ridge regression is a natural choice when the true relationship is smoothly nonlinear, the dataset is small to moderate (its cost scales with n3n^3 for the matrix inversion, so it becomes impractical much past a few thousand points without approximations), and you want a fully closed-form, deterministic fit rather than an iteratively-optimized one like a neural network or gradient-boosted trees. It shows up in fitting smooth yield curves, implied volatility surfaces, and other settings where a smooth, kernel-controlled curve is preferable to a rigid parametric form, and where you have few enough points that the cubic cost of the matrix solve stays manageable.

Kernel ridge regression is exactly ridge regression's closed-form solve and regularization logic, rewritten in terms of pairwise kernel evaluations instead of raw features — it inherits ridge's reliability and exact solvability while gaining, through the kernel, the ability to fit arbitrarily nonlinear relationships.

Kernel ridge regression's cost scales with the number of training points cubed (for the n×nn\times n matrix inversion), not with the number of features — the opposite of what intuition from plain linear models suggests. A dataset with only 10 features but 50,000 training rows can make kernel ridge regression computationally infeasible, while the same 10-feature dataset with only 500 rows fits instantly; always check nn, not feature count, before reaching for it, and consider Nyström or random-feature approximations once nn climbs into the tens of thousands.

Related concepts

Practice in interviews

Further reading

  • Saunders, Gammerman & Vovk, Ridge Regression Learning Algorithm in Dual Variables (1998)
  • Murphy, Machine Learning: A Probabilistic Perspective, Ch. 14 (2012)
ShareTwitterLinkedIn