Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Kernel Regression: From Linear to Nonlinear Modeling

1. Linear Regression Foundation

The goal of regression is to predict a continuous output yy from input features x=(x1,x2,...,xd)T\mathbf{x} = (x_1, x_2, ..., x_d)^T using a learned function ff:

y=f(x)+εy = f(\mathbf{x}) + \varepsilon

where ε\varepsilon represents random noise.

In linear regression, we assume ff is linear:

f(x)=wTxf(\mathbf{x}) = \mathbf{w}^T \mathbf{x}

(The bias term can be absorbed into w\mathbf{w} by adding a constant 1 to x\mathbf{x}.)

The parameters w\mathbf{w} minimize the sum of squared errors:

min⁡w∑i=1n(yi−wTxi)2\min_{\mathbf{w}} \sum_{i=1}^n (y_i - \mathbf{w}^T \mathbf{x}_i)^2

2. Kernel Regression: The Core Idea

2.1 Why Kernels?

Real-world data often has nonlinear patterns that linear models cannot capture. The solution: map each input x\mathbf{x} to a higher-dimensional feature space ϕ(x)\phi(\mathbf{x}) where the relationship becomes linear:

f(x)=wTϕ(x)f(\mathbf{x}) = \mathbf{w}^T \phi(\mathbf{x})

The Challenge: Explicitly computing ϕ(x)\phi(\mathbf{x}) may be computationally prohibitive or even infinite-dimensional.

The Kernel Trick: Use a kernel function KK that computes dot products in the feature space without ever constructing ϕ(x)\phi(\mathbf{x}) explicitly:

K(xi,xj)=⟨ϕ(xi),ϕ(xj)⟩K(\mathbf{x}_i, \mathbf{x}_j) = \langle \phi(\mathbf{x}_i), \phi(\mathbf{x}_j) \rangle

2.2 The Dual Formulation

Using the kernel trick, the regression function becomes a weighted sum of kernel similarities to each training point:

y^(x)=∑i=1nαi K(x,xi)\hat{y}(\mathbf{x}) = \sum_{i=1}^n \alpha_i \, K(\mathbf{x}, \mathbf{x}_i)

where:

  • αi\alpha_i are weights learned from data

  • K(x,xi)K(\mathbf{x}, \mathbf{x}_i) measures similarity between the test point x\mathbf{x} and training point xi\mathbf{x}_i

Interpretation: The prediction is a similarity-weighted average of training outputs, where the weights αi\alpha_i are learned rather than being simple averages.


3. Training: Solving for α\boldsymbol{\alpha}

3.1 The Optimization Problem

We learn αi\alpha_i by fitting the training data:

min⁡α∑j=1n(yj−∑i=1nαiK(xj,xi))2\min_{\boldsymbol{\alpha}} \sum_{j=1}^n \left( y_j - \sum_{i=1}^n \alpha_i K(\mathbf{x}_j, \mathbf{x}_i) \right)^2

3.2 Matrix Formulation

Define:

  • Kernel matrix K\mathbf{K} (size n×nn \times n): Kij=K(xi,xj)\mathbf{K}_{ij} = K(\mathbf{x}_i, \mathbf{x}_j)

  • Weight vector α=[α1,...,αn]T\boldsymbol{\alpha} = [\alpha_1, ..., \alpha_n]^T

  • Output vector y=[y1,...,yn]T\mathbf{y} = [y_1, ..., y_n]^T

For all training points, predictions are:

y^=Kα\hat{\mathbf{y}} = \mathbf{K} \boldsymbol{\alpha}

The sum of squared errors becomes:

∥y−Kα∥2\|\mathbf{y} - \mathbf{K} \boldsymbol{\alpha}\|^2

3.3 The Solution

Taking the derivative and setting to zero:

∂∂α∥y−Kα∥2=−2KT(y−Kα)=0\frac{\partial}{\partial \boldsymbol{\alpha}} \|\mathbf{y} - \mathbf{K} \boldsymbol{\alpha}\|^2 = -2\mathbf{K}^T(\mathbf{y} - \mathbf{K}\boldsymbol{\alpha}) = 0

Since K\mathbf{K} is symmetric (KT=K\mathbf{K}^T = \mathbf{K}):

K(y−Kα)=0\mathbf{K}(\mathbf{y} - \mathbf{K}\boldsymbol{\alpha}) = 0
Ky=K2α\mathbf{K}\mathbf{y} = \mathbf{K}^2\boldsymbol{\alpha}

If K\mathbf{K} is invertible (positive definite kernel with distinct points), multiply by K−1\mathbf{K}^{-1}:

y=Kα\mathbf{y} = \mathbf{K}\boldsymbol{\alpha}

Thus:

α=K−1y\boxed{\boldsymbol{\alpha} = \mathbf{K}^{-1} \mathbf{y}}

Key Insight: The weights α\boldsymbol{\alpha} are chosen so that training predictions perfectly match training outputs (y^train=y\hat{\mathbf{y}}_{\text{train}} = \mathbf{y}). This is exact interpolation.


4. Making Predictions on New Data

4.1 Single Test Point

For a new test point xtest\mathbf{x}_{\text{test}} (not in training set):

Step 1: Compute kernel similarities between xtest\mathbf{x}_{\text{test}} and all training points:

ktest=[K(xtest,x1)K(xtest,x2)⋮K(xtest,xn)](size n×1)\mathbf{k}_{\text{test}} = \begin{bmatrix} K(\mathbf{x}_{\text{test}}, \mathbf{x}_1) \\ K(\mathbf{x}_{\text{test}}, \mathbf{x}_2) \\ \vdots \\ K(\mathbf{x}_{\text{test}}, \mathbf{x}_n) \end{bmatrix} \quad \text{(size } n \times 1\text{)}

Step 2: Make prediction using the same formula:

y^test=∑i=1nαiK(xtest,xi)=ktestTα\hat{y}_{\text{test}} = \sum_{i=1}^n \alpha_i K(\mathbf{x}_{\text{test}}, \mathbf{x}_i) = \mathbf{k}_{\text{test}}^T \boldsymbol{\alpha}

4.2 Multiple Test Points

For mm test points {xtest(1),…,xtest(m)}\{\mathbf{x}_{\text{test}}^{(1)}, \dots, \mathbf{x}_{\text{test}}^{(m)}\}:

Define Ktest\mathbf{K}_{\text{test}} as an m×nm \times n matrix:

[Ktest]pj=K(xtest(p),xj)[\mathbf{K}_{\text{test}}]_{pj} = K(\mathbf{x}_{\text{test}}^{(p)}, \mathbf{x}_j)

Then predictions:

y^test=Ktest α\hat{\mathbf{y}}_{\text{test}} = \mathbf{K}_{\text{test}} \, \boldsymbol{\alpha}

4.3 Training vs. Testing Summary

PhaseInputPredictionEquals true yy?
TrainingK\mathbf{K} (n×nn \times n)y^train=Kα\hat{\mathbf{y}}_{\text{train}} = \mathbf{K}\boldsymbol{\alpha}Yes (exact fit)
Testingktest\mathbf{k}_{\text{test}} (n×1n \times 1)y^test=ktestTα\hat{y}_{\text{test}} = \mathbf{k}_{\text{test}}^T \boldsymbol{\alpha}No (generalization)

Important: The interpolation property y^train=y\hat{\mathbf{y}}_{\text{train}} = \mathbf{y} holds only for training data. For test points, predictions are determined by similarity to training points, not by exact matching.


5. The Pseudoinverse Connection

5.1 Unified View

From the matrix appendix, recall the pseudoinverse X+X^+ solves Xw=yX\mathbf{w} = \mathbf{y} optimally. For kernel regression, we solve Kα=y\mathbf{K}\boldsymbol{\alpha} = \mathbf{y}, so:

α=K+y\boldsymbol{\alpha} = \mathbf{K}^+ \mathbf{y}

When K\mathbf{K} is invertible, K+=K−1\mathbf{K}^+ = \mathbf{K}^{-1}. When K\mathbf{K} is singular or ill-conditioned, we use regularization.

5.2 Regularization (Kernel Ridge Regression)

In practice, with noisy data, exact interpolation leads to overfitting. Add a ridge penalty λ∥f∥2\lambda \|\mathbf{f}\|^2:

min⁡α∥y−Kα∥2+λαTKα\min_{\boldsymbol{\alpha}} \|\mathbf{y} - \mathbf{K}\boldsymbol{\alpha}\|^2 + \lambda \boldsymbol{\alpha}^T \mathbf{K} \boldsymbol{\alpha}

The solution becomes:

α=(K+λI)−1y\boxed{\boldsymbol{\alpha} = (\mathbf{K} + \lambda \mathbf{I})^{-1} \mathbf{y}}

This is the kernel ridge regression estimator. The regularization term:

  • Improves numerical stability (ensures invertibility)

  • Prevents overfitting by shrinking weights

  • Controls the smoothness of the prediction function


Practice

<Figure size 800x400 with 1 Axes>

6. The effects of regularization and noise levels on kernel regression

Source
<Figure size 1500x1000 with 6 Axes>

======================================================================
SUMMARY: Test MSE for different noise levels and regularization
======================================================================
Noise σ    λ = 0                λ = 1e-6             λ = 0.1             
----------------------------------------------------------------------
0.0       0.000000            0.000008            0.004277            
0.1       10490347.742265     0.368313            0.007773            
======================================================================

                       📊 INTERPRETATION GUIDE 📊                       
======================================================================
1. NOISE σ = 0 (No noise):
   - λ = 0: Perfect fit (MSE ≈ 0) - model interpolates exactly
   - λ > 0: Slight bias introduced, but still good fit

2. NOISE σ = 0.1 (Low noise):
   - λ = 0: May overfit slightly, capturing noise patterns
   - λ = 1e-6: Balances fit and smoothness
   - λ = 0.1: May underfit (too smooth)

💡 KEY INSIGHT:
   - More noise → need stronger regularization (larger λ)
   - No noise → λ = 0 gives perfect interpolation
   - Too much regularization → underfitting (bias)
   - Too little regularization → overfitting (variance)
======================================================================

8. Primal vs. Dual Perspective

8.1 Two Forms of the Solution

In standard linear regression with features X\mathbf{X}, the weight vector can be expressed in two equivalent ways:

Primal Form (feature space):

w=(XTX)−1XTy\mathbf{w} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y}

Dual Form (sample space):

w=XT(XXT)−1y\mathbf{w} = \mathbf{X}^T (\mathbf{X} \mathbf{X}^T)^{-1} \mathbf{y}

Both yield the same predictions when X\mathbf{X} has full column rank.

8.2 Why the Dual Form Matters for Kernels

The dual form reveals that w\mathbf{w} is a linear combination of training samples:

w=∑i=1nαixi,whereα=(XXT)−1y\mathbf{w} = \sum_{i=1}^n \alpha_i \mathbf{x}_i, \quad \text{where} \quad \boldsymbol{\alpha} = (\mathbf{X} \mathbf{X}^T)^{-1} \mathbf{y}

When we map to feature space ϕ(x)\phi(\mathbf{x}), this becomes:

w=∑i=1nαiϕ(xi),withα=(K)−1y\mathbf{w} = \sum_{i=1}^n \alpha_i \phi(\mathbf{x}_i), \quad \text{with} \quad \boldsymbol{\alpha} = (\mathbf{K})^{-1} \mathbf{y}

where Kij=ϕ(xi)Tϕ(xj)=K(xi,xj)\mathbf{K}_{ij} = \phi(\mathbf{x}_i)^T \phi(\mathbf{x}_j) = K(\mathbf{x}_i, \mathbf{x}_j).

Key insight: The prediction for a new point becomes:

y^(x)=wTϕ(x)=∑i=1nαiϕ(xi)Tϕ(x)=∑i=1nαiK(xi,x)\hat{y}(\mathbf{x}) = \mathbf{w}^T \phi(\mathbf{x}) = \sum_{i=1}^n \alpha_i \phi(\mathbf{x}_i)^T \phi(\mathbf{x}) = \sum_{i=1}^n \alpha_i K(\mathbf{x}_i, \mathbf{x})

We never need ϕ(x)\phi(\mathbf{x}) explicitly — only kernel evaluations K(xi,x)K(\mathbf{x}_i, \mathbf{x})!

8.3 Computational Trade-offs

ScenarioPrimal FormDual Form
n≪dn \ll d (few samples, many features)Slow (invert d×dd \times d)Fast (invert n×nn \times n)
n≫dn \gg d (many samples, few features)Fast (invert d×dd \times d)Slow (invert n×nn \times n)
Nonlinear kernels (RBF, polynomial)Not applicableRequired (kernel trick)

Example: RBF kernel with n=1000n=1000 samples effectively uses infinite-dimensional features (d=∞d = \infty):

  • Primal form impossible (cannot compute ϕ(x)\phi(\mathbf{x}) explicitly)

  • Dual form works with 1000×10001000 \times 1000 kernel matrix


9. Key Takeaways

  1. Kernel regression enables nonlinear fitting by implicitly mapping data to high-dimensional spaces

  2. The dual formulation expresses predictions as weighted sums of kernel similarities:

    y^(x)=∑i=1nαiK(x,xi)\hat{y}(\mathbf{x}) = \sum_{i=1}^n \alpha_i K(\mathbf{x}, \mathbf{x}_i)
  3. Training solves α=K−1y\boldsymbol{\alpha} = \mathbf{K}^{-1}\mathbf{y} (unregularized) or α=(K+λI)−1y\boldsymbol{\alpha} = (\mathbf{K} + \lambda\mathbf{I})^{-1}\mathbf{y} (regularized)

  4. Testing uses y^test=ktestTα\hat{y}_{\text{test}} = \mathbf{k}_{\text{test}}^T \boldsymbol{\alpha}, where ktest\mathbf{k}_{\text{test}} contains similarities between test and training points

  5. Regularization (λ>0\lambda > 0) is essential for:

    • Numerical stability (ensuring K+λI\mathbf{K} + \lambda\mathbf{I} is invertible)

    • Preventing overfitting to noise

  6. Kernel choice controls model flexibility:

    • Linear kernel →\rightarrow standard linear regression

    • RBF kernel →\rightarrow smooth, local approximation

    • Polynomial kernel →\rightarrow interaction terms up to degree dd