Skip to content

Kernel ridge regression

Fit nonlinear response functions through a regularized kernel matrix. A two-observation example explains the coefficients, shrinkage and loss-normalization convention.

Kernel ridge regression (KRR) fits a response function by minimizing squared prediction errors plus a penalty on its size in a kernel-defined function space. It extends ridge regression to nonlinear relationships without explicitly building every transformed feature. A quant could use it to model execution cost from order size, spread and volatility; the kernel determines which combinations of those inputs the model treats as similar. Nonlinear flexibility does not itself establish predictive value. scikit-learn, §1.3

A fit expressed through training observations

Given training pairs (xi,yi)(x_i,y_i), choose a real symmetric kernel kk. It must generate a positive semidefinite matrix for every finite collection of inputs, not merely assign plausible similarity scores. Write Kij=k(xi,xj)K_{ij}=k(x_i,x_j). This is the training Gram matrix; it can be rank deficient, for example when feature representations repeat. Rasmussen and Williams, §4.1

Let Hk\mathcal H_k be the kernel's reproducing kernel Hilbert space (RKHS): the function space with a norm compatible with evaluating functions through kk. Here the objective is

f^=arg⁡min⁡f∈Hk{∑i=1n(yi−f(xi))2+λ∥f∥Hk2},λ>0.\widehat f=\arg\min_{f\in\mathcal H_k} \left\{\sum_{i=1}^{n}(y_i-f(x_i))^2+\lambda\lVert f\rVert_{\mathcal H_k}^{2}\right\}, \qquad \lambda>0.
The loss is an unaveraged sum and there is no separate unpenalized intercept. The norm measures function size according to the chosen kernel; it is not generally the squared Euclidean norm of the kernel coefficients. The representer theorem reduces the fit to
f^(x)=∑i=1nαik(xi,x),(K+λIn)α=y.\widehat f(x)=\sum_{i=1}^{n}\alpha_i k(x_i,x), \qquad (K+\lambda I_n)\alpha=y.
For this representation, ∥f∥Hk2=α⊤Kα\lVert f\rVert_{\mathcal H_k}^{2}=\alpha^\top K\alpha. Replacing that term by α⊤α\alpha^\top\alpha changes the problem. A positive λ\lambda makes K+λInK+\lambda I_n positive definite even when KK is singular. The displayed system gives a unique coefficient vector; for singular KK, other coefficient representations can describe the same fitted function. Rasmussen and Williams, §§6.1–6.2.2
Check the normalization before copying a penalty setting. If the objective instead uses n−1∑i(yi−f(xi))2+λ∥f∥Hk2n^{-1}\sum_i(y_i-f(x_i))^2+\lambda\lVert f\rVert_{\mathcal H_k}^{2}, its system is (K+nλIn)α=y(K+n\lambda I_n)\alpha=y. Keeping the same fit therefore requires dividing the unaveraged-loss penalty by nn. Multiplying the entire objective by a constant changes no minimizer; scaling only one term does.

Two observations, one coupled solution

Take fictional, unitless responses and a valid kernel matrix:

K=(10.50.51),y=(10),λ=0.5.K=\begin{pmatrix}1&0.5\\0.5&1\end{pmatrix}, \qquad y=\begin{pmatrix}1\\0\end{pmatrix}, \qquad \lambda=0.5.

Its eigenvalues are 1.51.5 and 0.50.5, both positive. One explicit feature construction is ϕ(x1)=(1,0)⊤\phi(x_1)=(1,0)^\top and ϕ(x2)=(0.5,0.75)⊤\phi(x_2)=(0.5,\sqrt{0.75})^\top, using k(x,z)=ϕ(x)⊤ϕ(z)k(x,z)=\phi(x)^\top\phi(z). Their inner products reproduce KK, so the example can also be checked by ordinary ridge regression in these two features.

The kernel system is

1.5α1+0.5α2=1,0.5α1+1.5α2=0,α=(0.75−0.25).\begin{aligned} 1.5\alpha_1+0.5\alpha_2&=1,\\ 0.5\alpha_1+1.5\alpha_2&=0, \end{aligned} \qquad \alpha=\begin{pmatrix}0.75\\-0.25\end{pmatrix}.
The fitted training values are not the coefficients:
y^=Kα=(0.75−0.1250.375−0.25)=(0.6250.125).\widehat y=K\alpha =\begin{pmatrix}0.75-0.125\\0.375-0.25\end{pmatrix} =\begin{pmatrix}0.625\\0.125\end{pmatrix}.

The negative second coefficient offsets the positive similarity between observations. These coefficients are not probabilities or portfolio weights. At a new input x∗x_*, prediction requires its similarities to the training inputs: f^(x∗)=k∗⊤α\widehat f(x_*)=k_*^\top\alpha, where (k∗)i=k(xi,x∗)(k_*)_i=k(x_i,x_*). The training matrix alone does not specify every future prediction.

Fictional unitless responses 1 and 0 become fitted values 0.625 and 0.125 at penalty 0.5. Kernel eigenvalues 1.5 and 0.5 retain 75 percent of the common-response component and 50 percent of the contrast component.
Fictional unitless responses 1 and 0 become fitted values 0.625 and 0.125 at penalty 0.5. Kernel eigenvalues 1.5 and 0.5 retain 75 percent of the common-response component and 50 percent of the contrast component.

Original deterministic example, with no separate intercept. The two observations are categories, not positions on a fitted curve. The right panel shows the fraction retained along each kernel eigenvector.

Why do the responses move this way? For an eigenvector of KK with eigenvalue dd, the training smoother K(K+λIn)−1K(K+\lambda I_n)^{-1} retains the fraction d/(d+λ)d/(d+\lambda) of that response component. Here the common direction (1,1)⊤(1,1)^\top retains 75%75\%, while the contrast direction (1,−1)⊤(1,-1)^\top retains 50%50\%. Thus

y=0.5(11)+0.5(1−1)⟶y^=0.375(11)+0.25(1−1).y=0.5\begin{pmatrix}1\\1\end{pmatrix} +0.5\begin{pmatrix}1\\-1\end{pmatrix} \quad\longrightarrow\quad \widehat y=0.375\begin{pmatrix}1\\1\end{pmatrix} +0.25\begin{pmatrix}1\\-1\end{pmatrix}.
This is joint regularization, rather than the normalized local average used in Nadaraya–Watson kernel regression. KRR need not have nonnegative response weights or weights summing to one. With no separate intercept, even a constant response component can shrink, as the common direction does here.

What a financial prediction still needs

For a distance-based kernel such as a radial basis function kernel, feature units and scale affect similarity. Fit preprocessing on each training fold, apply that fitted transformation to its validation data, and select kernel parameters and the penalty within the training portion of the evaluation design. Leave the final test period untouched by selection. These steps prevent the preprocessing and tuning forms of look-ahead bias. scikit-learn, §§12.1–12.2.1

For financial time series, preserve forecast chronology and ensure training labels are available at the forecast origin; overlapping future-return labels can require purging around validation boundaries. Use cross-validation that reflects the intended deployment. The small training calculation above measures neither future forecast error nor returns after trading costs.

KRR supplies a point prediction, not an uncertainty band. A zero-mean Gaussian process regression model has the same posterior mean when its covariance kernel matches kk, its independent Gaussian observation-noise variance equals this unaveraged-loss λ\lambda, and the data transformations match. That correspondence does not establish calibrated uncertainty for actual market data. Rasmussen and Williams, §2.2, equations 2.25–2.27
For a dense exact implementation, storing the Gram matrix takes O(n2)O(n^2) memory and a standard factorization takes O(n3)O(n^3) work. Solve the regularized linear system, for example by Cholesky factorization, rather than explicitly forming an inverse. A new prediction generally uses all nn kernel evaluations; this is not ordinarily a sparse model. Rasmussen and Williams, Algorithm 2.1, scikit-learn, §1.3
For the feature-map intuition behind this construction, see The Kernel Trick: Feature Maps and Properties of Kernels (public preview; full lesson requires membership).

Sources

Last updated
  • kernel methods
  • ridge regression
  • regularization
  • supervised learning
← Back to the glossaryBack to top ↑