Kernel Regression

The least-squares weights are always a combination of the training points, w* = Xα. Substituting turns training and prediction into operations on dot products only, so a kernel can replace them: predictions become f(x) = Σ αᵢ k(xᵢ, x), with α found from the n×n kernel matrix. In practice a ridge term, α = (K + λI)⁻¹y, keeps the fit stable.

Machine Learning Techniques

Kernel PCA came from one observation: if an algorithm needs only dot products between data points, a kernel can replace them and the algorithm becomes non-linear for free. Linear regression passes the same test, and the result, kernel regression, can fit curves of almost any shape while still solving a linear system.

The weights live in the span of the data

The least-squares solution satisfies the normal equations XX⊤w∗=XyXX^\top w^* = Xy. Look at the right-hand side: Xy=∑iyixiXy = \sum_i y_i x_i is a combination of the training points. A short argument shows that the solution can always be taken as such a combination too. Split any weight vector into a part inside the span of the training points and a part orthogonal to all of them: w=Xα+w⊥w = X\alpha + w_\perp. The orthogonal part has xi⊤w⊥=0x_i^\top w_\perp = 0 for every training point, so it changes no training prediction and cannot reduce the loss; dropping it loses nothing. Hence there is an optimal solution of the form

w∗=Xα=∑i=1nαi xifor some α∈Rn.w^* = X\alpha = \sum_{i=1}^{n} \alpha_i\, x_i \qquad \text{for some } \alpha \in \mathbb{R}^n .

This is the regression version of the fact we used in kernel PCA, that the principal components are combinations of the data points, and it is a simple case of a general result called the representer theorem.

Everything in terms of dot products

Substitute w=Xαw = X\alpha.

Predictions on the training set are X⊤w=X⊤Xα=KαX^\top w = X^\top X\alpha = K\alpha, where K=X⊤XK = X^\top X is the n×nn \times n matrix of dot products, Kij=xi⊤xjK_{ij} = x_i^\top x_j. The loss becomes

L(α)=∥Kα−y∥2,L(\alpha) = \lVert K\alpha - y \rVert^2 ,

which is minimised when Kα=yK\alpha = y, so (if KK is invertible) α∗=K−1y\alpha^* = K^{-1}y.

A prediction for a new point xx is

f(x)=w⊤x=∑i=1nαi xi⊤x.f(x) = w^\top x = \sum_{i=1}^{n} \alpha_i\, x_i^\top x .

Again only dot products appear: between the new point and each training point.

Swapping in a kernel

Replace every dot product xi⊤xjx_i^\top x_j by a kernel k(xi,xj)k(x_i, x_j), which computes a dot product ϕ(xi)⊤ϕ(xj)\phi(x_i)^\top\phi(x_j) in some feature space:

Kij=k(xi,xj),α∗=K−1y,f(x)=∑i=1nαi∗ k(xi,x).K_{ij} = k(x_i, x_j), \qquad \alpha^* = K^{-1}y, \qquad f(x) = \sum_{i=1}^{n} \alpha^*_i\, k(x_i, x) .

This is linear regression in the feature space of ϕ\phi, carried out without ever computing ϕ\phi. With a polynomial kernel of degree pp it fits polynomials of degree pp; with the RBF kernel it fits smooth curves of essentially any shape.

The prediction has an appealing reading. Each training point xix_i places a bump αi k(xi,⋅)\alpha_i\,k(x_i, \cdot) centred on itself, and the prediction at xx is the sum of all the bumps there. With the RBF kernel, nearby training points dominate and distant ones contribute almost nothing: the model predicts by similarity to the examples it has seen.

Noisy points along a wavy curve; several small Gaussian bumps centred on training points, scaled by their weights alpha, and their sum drawn as a smooth fitted curve
RBF kernel regression. Each training point contributes a bump αᵢ k(xᵢ, x); the prediction is the sum of the bumps.

Making it work in practice: add a ridge

Solving Kα=yK\alpha = y exactly makes the curve pass through every training point, noise included, which is overfitting in its purest form. With the RBF kernel, KK is also often nearly singular (two close points give two nearly identical rows), so K−1K^{-1} is numerically unstable. The standard remedy adds a small multiple of the identity:

α∗=(K+λI)−1y.\alpha^* = (K + \lambda I)^{-1}y .

This is kernel ridge regression. The term λI\lambda I makes the system well conditioned and stops the fit from chasing every noisy label; the next chapters show that it corresponds to a penalty on the size of the weights (equivalently, a prior belief that they are small). Choosing λ\lambda, and the kernel's own parameters such as the RBF width σ\sigma, is done by validation.

The cost of kernels

Kernel regression trades the d×dd \times d system of ordinary least squares for an n×nn \times n one: about n3n^3 operations to solve and n2n^2 numbers to store. Prediction needs every training point (all nn kernel values for each new xx), unlike linear regression, which only needs ww. That is fine for thousands of points and painful for millions. Approximations such as random features or using a subset of points as centres keep kernel methods practical at scale. Support vector machines (Module 10) solve the prediction-cost problem differently: their solution keeps only a few training points.

EasyKernel regression

With the linear kernel k(x, x') = xᵀx', what does kernel regression reduce to?

MediumKernel regressionRegularisation

Why does unregularised kernel regression with an RBF kernel overfit, and how does the ridge term help?

MediumKernel regressionComplexity

You have 2 million training points and 20 features. Would you use linear regression with explicit features or kernel regression? Why?