Kernel regression

Kernels, alpha, predictions

7 lessons · 9 past questions · 30.5 marks in the papers collected here

Open the interactive lessons →Kernel regression past questions

Diagrams, animations, quizzes and checked solutions are on the interactive pages.

Contents

  1. Orientation: what you will learn and why
  2. Big picture: concept map, three regressions compared, and the recipe
  3. Start here: inner products, expanding squares, a $3\times3$ system
  4. The idea: feature maps, kernels and the prediction formula
  5. The kernel matrix $K$ and solving for $\alpha$
  6. Computing predictions when $\alpha$ is given
  7. Parametric or not, and the cost of kernel regression

1. Orientation: what you will learn and why

A straight line cannot fit data that bends. One fix is to first transform each input into many new features (squares, products, ...) and fit a line in that bigger space. Kernel regression gets the same power without ever writing the new features: it only needs a function $k(x,x')$ that gives the inner product in the big space.

What you will be able to do

Why this topic matters for Quiz 2

Counted only from the PYQs in this project (not a prediction):

What the PYQs askPYQsMarks
Kernel idea and prediction formula25
Kernel matrix and solving for alpha27
Computing predictions414.5
Parametric or not; cost14

In total 9 PYQs worth 30.5 marks across 7 papers (2 MCQ, 2 MSQ, 5 numeric). Most are "compute the prediction" questions that reduce to a few arithmetic steps.

How this topic connects

It builds on linear regression ($w^*=X\alpha$, the span of data points) and reuses matrix inverses from the ridge topic.

Tags: [SRC] from your files, [DERIVED] answer I worked out because the bank gives no key, [ADDED] my own example, [FLAG] disagreement between my calculation and a key.
How to use this page

Read each section, do the quick checks, then attempt each PYQ before opening its solution.

What is new in this upgrade [ADDED]

The route through this topic
feature maps and kernelsthe kernel matrixpredictionscost and properties

2. Big picture: concept map, three regressions compared, and the recipe

[ADDED] Read this page first. It shows how the pieces fit before the detail.

The concept map

Linear regressionw* = X α (in the span)Feature map φx → φ(x), a longer vectorKernel k(x, x′)= φ(x)ᵀφ(x′), no φ neededKernel matrix KKᵢⱼ = k(xᵢ, xⱼ)Solve for αK α = y → α = K⁻¹yPredict at xŷ = Σ αᵢ k(xᵢ, x)What stays hiddenφ is never written down; only dot productsWhy this worksleast-squares weights lie in the span of the data, so only inner products between data points matter
From linear regression to kernel regression. The first row is the idea (lift, then use only inner products). The right column is what you actually compute in an exam: build $K$, solve for $\alpha$, predict.

Three regressions compared

Plain linear regressionLinear regression on lifted features (explicit $\phi$)Kernel regression
Fitsa straight line / plane in $x$a straight line in $\phi(x)$, a curve in $x$the same curve as the lifted model
What you store$w$ ($d$ numbers)$w$ in $\phi$-space (can be huge)$\alpha$ ($n$ numbers) and the training points
What you compute to train$XX^T w=Xy$ ($d\times d$)$\phi\phi^T w=\phi y$ (huge)$K\alpha=y$ ($n\times n$)
Prediction$w^Tx$$w^T\phi(x)$$\sum_i\alpha_ik(x_i,x)$
Needs $\phi$ written out?noyesno
Cost grows with$d$$\dim\phi$ (can be enormous)$n$ (number of points)
Parametric (PYQ definition)?yesyesno: needs all training points

The exam recipe, in five lines

Mnemonics and recognition cues [ADDED]

Analogy and its limit [ADDED]

AnalogyWhat it explains wellWhere it breaks
Lift the points onto a table where they form a linewhy a line in the lifted space is a curve in $x$the "table" can have thousands of dimensions; you cannot draw it, and the kernel trick exists precisely so you never build it
Each training point votes with weight $\alpha_i$, scaled by similaritythe prediction formula $\sum\alpha_ik$votes can be negative ($\alpha_i<0$), and the weights come from solving a system, not from counting

Recall questions

What is $K$ for $n$ training points?

An $n\times n$ symmetric matrix with $K_{ij}=k(x_i,x_j)$.

Is kernel regression parametric (PYQ definition)?

No. Prediction needs every training point $x_i$.

What is a prediction at the zero vector for $k=(1+x^Tx')^2$?

$\sum_i\alpha_i$, because every kernel value is $(1+0)^2=1$.

Tips and tricks
  • Spot the kernel first, then the sizes: $K$ is $n\times n$, with one row per data point.
  • Prediction needs only kernel values between the new point and the training points.

3. Start here: inner products, expanding squares, a $3\times3$ system

Piece 1. Inner product (dot product)

Definition: Inner product

For $a=(a_1,\dots,a_d)$ and $b=(b_1,\dots,b_d)$: $a^Tb=a_1b_1+\dots+a_db_d$. It is large and positive when $a$ and $b$ point the same way, $0$ when they are perpendicular.

Example: $a=(1,2)$, $b=(3,-1)$: $a^Tb=3-2=1$.

Piece 2. Squaring a bracket

$(1+u)^2=1+2u+u^2$. With $u=a^Tb=a_1b_1+a_2b_2$ this is a sum of terms with $a_1,a_2$ squared and multiplied together.

Piece 3. Solving a linear system

$K\alpha=y$ means find the vector $\alpha$ that $K$ turns into $y$. If $K$ has an inverse, $\alpha=K^{-1}y$. You can also solve it by substitution; the answer is the same.

Two more tools [ADDED]

Tool 4: "symmetric" and "positive semi-definite" in plain words

A square matrix $K$ is symmetric if $K_{ij}=K_{ji}$ (it equals its transpose). A symmetric matrix is positive semi-definite (PSD) if all its eigenvalues are $\ge0$. For a $2\times2$ matrix $\begin{bmatrix}a&b\\b&d\end{bmatrix}$ the eigenvalues have sum $a+d$ and product $ad-b^2$, so PSD means $a+d\ge0$ and $ad-b^2\ge0$.

Example: $\begin{bmatrix}4&9\\9&25\end{bmatrix}$ has sum $29$ and product $100-81=19$, both positive, so it is PSD (its eigenvalues are $28.329$ and $0.671$). The matrix $\begin{bmatrix}-1&-2\\-2&-4\end{bmatrix}$ has sum $-5$ and product $4-4=0$, eigenvalues $0$ and $-5$: not PSD.

Tool 5: powers of a sum and the number $c^p$

If the dot product is $0$, the polynomial kernel $(0+c)^p$ becomes $c^p$. Memorise the cases that appear in exams: $c=1$: always $1$. $c=2,p=2$: $4$. $c=2,p=3$: $8$.

Is $\begin{bmatrix}2&3\\3&2\end{bmatrix}$ PSD?

Sum $=4$, product $=4-9=-5<0$. One eigenvalue is negative, so it is not PSD.

What is $(0+2)^3$?

$8$.

Tips and tricks
  • Expand $(x^Ty+1)^2$ term by term to read off the feature map.
  • Solve a $3\times3$ system by looking for zeros before eliminating.
Expanding a square kernel
take x and ycompute x·yadd 1 and squareread off the features

4. The idea: feature maps, kernels and the prediction formula

From a bent pattern to a straight fit

Seeing the lift [ADDED]

Original input x: a curvexy-2-10123best straight line: SSE 18.39 curve: SSE 0.10Lifted input z = x²: a straight linez = x²y14710the same data, now fitted by a line: SSE 0.10φ
Left: seven points that bend like a parabola. The best straight line (dashed red) misses badly (SSE $18.39$); the best curve (green) fits well (SSE $0.10$). Right: the same seven points after squaring the input ($z=x^2$). Now a straight line fits them with the same small SSE. The arrow $\phi$ is the feature map $x\mapsto(1,x^2)$; the curve in $x$ is the line in $z$. The code checks that least squares on $\phi(x)=(1,x^2)$ gives this line.

A straight line is $\hat y=w_0+w_1x$. The curve above is $\hat y=w_0+w_1x^2$, which is still linear in the weights (and in the new feature $x^2$). So the whole linear-regression machinery applies. The line gets the error down by a factor of about 182 in this example.

Worked example: a kernel value equals a dot product

Problem. Scalars, $k(x,x')=(2+xx')^2$, with $x=1$ and $x'=3$.

What is asked. Check that it is a dot product of longer vectors.

Idea / formula used. Expand the square and split each term between the two points.

$(2+xx')^2=4+4xx'+x^2x'^2$, so $\phi(x)=(2,\,2x,\,x^2)$.
Kernel: $(2+3)^2=25$.
Features: $\phi(1)=(2,2,1)$, $\phi(3)=(2,6,9)$, and $4+12+9=25$.
Answer: both give $25$
Common mistake: squaring each coordinate first.
Definition: Feature map and kernel

A feature map $\phi$ sends $x$ to a longer vector $\phi(x)$. The kernel is the inner product in that space: $k(x,x')=\phi(x)^T\phi(x')$.

Formula: Polynomial kernel

$$k(x,x')=(x^Tx'+c)^p$$ $c\ge0$ is a constant (often $1$ or $2$) and $p$ is the degree. It equals $\phi(x)^T\phi(x')$ for a $\phi$ containing all products of the features up to degree $p$.

Two features: the six-term map

Check for $p=2$, $c=1$ and two features: $\phi(x)=(1,\sqrt2x_1,\sqrt2x_2,x_1^2,x_2^2,\sqrt2x_1x_2)$. For $a=(1,2)$, $b=(3,-1)$: $a^Tb=1$, so $k=(1+1)^2=4$, and $\phi(a)^T\phi(b)$ also equals 4 (verified in code).

The polynomial kernel $(x^Tx'+1)^p$ corresponds to a lifted space with $\binom{d+p}{p}$ coordinates for $d$ original features.

Features $d$Degree $p$Lifted dimension $\binom{d+p}{p}$Cost of one kernel value
226$d$ multiplications, one addition, one power
2310$d$ multiplications, one addition, one power
103286$d$ multiplications, one addition, one power
1003176851$d$ multiplications, one addition, one power

For $d=100$, $p=3$ the lifted space has $176851$ coordinates. Writing each point there is expensive. The kernel value, however, needs only the $100$-number dot product. This is the kernel trick: do the cheap computation in the small space and get the answer of the huge space.

Why the weights lie in the span of the data

The squared loss is $\|\phi(X)^Tw-y\|^2$. Its gradient is $2\phi(X)(\phi(X)^Tw-y)$, which is a combination of the columns of $\phi(X)$. So every gradient step moves $w$ inside the span of the lifted data points. Starting from $w=0$ (which is in the span) we never leave it, and the solution found is $w^*=\phi(X)\alpha$. Any part of $w$ outside the span would not change any prediction on the training points, so it is never needed.

Why $w$ is a mix of the data points, on numbers

Take $x_1=1$, $x_2=2$ with $\phi(x)=(1,\sqrt2x,x^2)$ and $\alpha=(1,-1)$. Then $w=\phi(1)-\phi(2)=(0,\,-\sqrt2,\,-3)$. At $x=1$: $w^T\phi(1)=-2-3=-5$. With kernels only: $k(1,1)-k(2,1)=4-9=-5$. The two routes always agree, so $w$ never has to be built.

Only the part of $w$ made from $\phi(x_1),\phi(x_2),\dots$ changes any training prediction: a part at right angles to all of them gives dot product $0$ with each. So the least-squares fit can drop it.

Formula: Prediction

$$w^*=\phi(X)\alpha,\qquad \hat y(x)=w^{*T}\phi(x)=\sum_{i=1}^n\alpha_i\,k(x_i,x)$$ $\phi(X)$ is the matrix whose columns are $\phi(x_i)$, $\alpha_i$ is the weight of training point $i$, and $k(x_i,x)$ measures how similar the test point $x$ is to training point $i$.

Worked example: one prediction

Problem. Training points $x_1=1$, $x_2=-1$, $\alpha=(2,1)$, kernel $(1+xx')^2$.

What is asked. The prediction at $x=3$.

Idea / formula used. $\hat y(x)=\sum_i\alpha_ik(x_i,x)$.

$k(1,3)=(1+3)^2=16$ and $k(-1,3)=(1-3)^2=4$.
$2\cdot16+1\cdot4=36$.
Answer: $36$
Common mistake: adding the $\alpha_i$ without the kernel values.
Trap: kernel is not the loss

"The kernel minimises the loss" or "reduces variance" are wrong options. The kernel defines similarity; the loss is minimised when solving for $\alpha$.

Worked example: the order of operations

Problem. $k=(x^Tx'+2)^2$ with $x=(2,-1)$, $x'=(1,4)$.

What is asked. The kernel value.

Idea / formula used. Dot product first, then add, then take the power.

$x^Tx'=2-4=-2$.
$(-2+2)^2=0$.
Answer: $0$: a kernel value can be zero, but $k(x,x)$ is never negative
Common mistake: adding $2$ to each coordinate before the dot product.
Which functions are kernels?

A function $k$ is a valid kernel if it equals $\phi(x)^T\phi(x')$ for some $\phi$. Equivalent test: every Gram matrix $[k(x_i,x_j)]$ must be symmetric and PSD, and in particular $k(x,x)\ge0$.

CandidateValid?Reason
$k=x^Tx'$ (linear)yes$\phi(x)=x$
$k=(x^Tx'+c)^p$, $c\ge0$, integer $p\ge1$yesproducts and sums of kernels are kernels; this is the PYQ family
$k=-x^Tx'$noon points $1,2$ the Gram matrix is $\begin{bmatrix}-1&-2\\-2&-4\end{bmatrix}$ with eigenvalue $-5$ (and $k(x,x)<0$)
$k=x^Tx'-1$no$k(0,0)=-1<0$, but $k(x,x)=\|\phi(x)\|^2\ge0$ always
Picture: kernel value against dot product
1-20-1104192dot product xᵢᵀxkernel value (1 + xᵢᵀx)²dot = −1 gives k = 0k is never negative
For $k=(1+t)^2$ with $t=x_i^Tx$. The dots are the five values you meet in exams: $t=-2,-1,0,1,2$ give $k=1,0,1,4,9$. Note that $k=0$ exactly at $t=-1$ and that $k$ is never negative. This is why some training points can have zero influence on a prediction (a PYQ pattern, see the next sections).
Tips and tricks
  • Predict with $\sum_i\alpha_i\,k(x_i,x)$; you do not need the feature vectors.
  • Compute each $k(x_i,x)$ first, then take the weighted sum.
Memory line

Prediction is a weighted sum of similarities.

5. The kernel matrix $K$ and solving for $\alpha$

Picture it

A table of how alike every pair of students is.

Definition

Definition: Kernel matrix

For $n$ training points the kernel matrix is the $n\times n$ table $K_{ij}=k(x_i,x_j)$. It equals $\phi(X)^T\phi(X)$, so it is symmetric and positive semi-definite (all its eigenvalues are $\ge0$).

Formula: Solving for the weights

$$K\alpha=y\ \Longrightarrow\ \alpha=K^{-1}y$$ $y$ is the vector of training labels. The normal equation $XX^Tw=Xy$ with $w=X\alpha$ reduces to this when $K$ is invertible.

Worked examples

Where $K\alpha=y$ comes from

Least squares asks $\phi(X)\phi(X)^Tw=\phi(X)y$. Put $w=\phi(X)\alpha$ and use $\phi(X)^T\phi(X)=K$: this reads $\phi(X)K\alpha=\phi(X)y$. Choosing $\alpha$ with $K\alpha=y$ satisfies it (when $K$ is invertible, that $\alpha$ is $K^{-1}y$).

Worked example: Simple: build a kernel matrix

Problem. Points $x_1=(1,1)$, $x_2=(1,-1)$, $x_3=(0,0)$, kernel $k(a,b)=(1+a^Tb)^2$.

What is asked. $K$.

Idea / formula used. Fill $K_{ij}=k(x_i,x_j)$.

Diagonal: $K_{11}=(1+2)^2=9$, $K_{22}=(1+2)^2=9$, $K_{33}=(1+0)^2=1$.
$x_1^Tx_2=0$, so $K_{12}=1$. Anything with $x_3$ has dot product $0$: $K_{13}=K_{23}=1$.
Copy across the diagonal: $K$ is symmetric.
Answer: $K=\begin{bmatrix}9&1&1\\1&9&1\\1&1&1\end{bmatrix}$
Common mistake: using $a^Tb$ without adding $1$, or forgetting to square.
Worked example: Exam level: solve $K\alpha=y$

Problem. Same $K$, $y=(6,6,2)^T$.

What is asked. $\alpha$.

Idea / formula used. Subtract equations so that most terms cancel.

The equations are $9\alpha_1+\alpha_2+\alpha_3=6$, $\alpha_1+9\alpha_2+\alpha_3=6$, $\alpha_1+\alpha_2+\alpha_3=2$.
First minus third: $8\alpha_1=4$, so $\alpha_1=0.5$.
Second minus third: $8\alpha_2=4$, so $\alpha_2=0.5$.
Third: $\alpha_3=2-0.5-0.5=1$. Check row 1: $4.5+0.5+1=6$.
Answer: $\alpha=(0.5,\,0.5,\,1)^T$
Common mistake: setting $\alpha=Ky$ instead of solving $K\alpha=y$.
Trap: K is n by n

$K$ has one row and one column per training point, not per feature. The size grows with $n$.

A complete small example, start to finish [ADDED]

Two points in one dimension: $x_1=1$, $x_2=2$ with labels $y=(1,3)$. Kernel $k(a,b)=(ab+1)^2$.

Cross-check with the explicit feature map $\phi(x)=(1,\sqrt2x,x^2)$: $w^*=\phi(X)\alpha=(0.0526, 0.2977, 0.5263)$ and $w^{*T}\phi(3)=6.0526$, the same number (verified in code).

The big property: a training point is always reproduced [ADDED]

Because $K\alpha=y$, the prediction at a training point $x_j$ is $\sum_i\alpha_ik(x_i,x_j)=(K\alpha)_j=y_j$. So whenever $K$ is invertible, kernel regression has zero training error: it passes exactly through every training label. (In the example above, the model gives $1$ at $x=1$ and $3$ at $x=2$.)

-2-112Kernel regression, 4 training pointsdegree 3, c = 1degree 5, c = 1Both pass through every dot:K α = y means zero training error.Degree 2: K is singular (rank 3),so K⁻¹ does not exist.
Four training points (black dots). Kernel regression with degree $3$ (blue) and degree $5$ (red dashed) both pass exactly through all four points, so training error is $0$ for both. Between the points they disagree by up to 2.87 somewhere on the range $-2\le x\le2$ (mostly near the ends). Zero training error does not mean a good model on new data. For degree $2$ with four points $K$ is singular (rank $3$), so $K^{-1}$ does not exist.

When does $K^{-1}$ exist? A rank table [ADDED]

For one-dimensional distinct inputs, the lifted space of $(xx'+1)^p$ has $p+1$ coordinates, so $K$ can have rank at most $p+1$. It is invertible only when $n\le p+1$. The table shows the rank of $K$ (computed in code).

Number of points $n$p = 1p = 2p = 3p = 4
32 (singular)3 (invertible)3 (invertible)3 (invertible)
42 (singular)3 (singular)4 (invertible)4 (invertible)
52 (singular)3 (singular)4 (singular)5 (invertible)

Take-away: a low-degree kernel on many points gives a singular $K$. Exam questions are built with small $n$ so that $K$ is invertible. If you ever see a singular $K$, say that $K^{-1}$ does not exist; do not force a division by zero. [ADDED] (Extension beyond the PYQs: adding $\lambda I$, i.e. $\alpha=(K+\lambda I)^{-1}y$, makes it solvable; this is kernel ridge regression and uses the idea of the ridge topic. For the four-point degree-2 case above, $K+0.1I$ is invertible. Not asked in any PYQ of this project.)

Properties you can check on any $K$ [ADDED]

CheckWhat you should seeWhy
Symmetry$K_{ij}=K_{ji}$$k(a,b)=k(b,a)$ since the dot product is symmetric
Diagonal$K_{ii}=(1+\|x_i\|^2)^p\ge1$ for $c=1$$K_{ii}=\|\phi(x_i)\|^2$
Size$n\times n$one row and column per training point
PSDtrace $\ge0$ and $\det\ge0$ for $2\times2$$K=\phi(X)^T\phi(X)$ is a Gram matrix
$K\alpha=y$row $j$ gives the training prediction at $x_j$consistency check on $\alpha$

Common mistakes, as a table [ADDED]

MistakeFix
Building $K$ of size $d\times d$it is $n\times n$
Using $x_i^Tx_j$ without adding $c$ and without the powerevery entry is $(x_i^Tx_j+c)^p$
Filling only the upper triangle and then forgetting the lower trianglecopy it by symmetry
Dividing by a determinant of $0$then $K$ is singular; the question should not ask for $K^{-1}$

Recall questions

What is the training error of kernel regression when $K$ is invertible?

Zero. $K\alpha=y$ reproduces every label.

For $n=5$ distinct 1-D points, what is the smallest degree $p$ that makes $K$ invertible (with $c=1$)?

$p=4$, because the lifted space has $p+1=5$ coordinates.

Tips and tricks
  • $K$ is symmetric, so compute one triangle and mirror it.
  • Check your $\alpha$ by multiplying $K\alpha$ and comparing with $y$.
Memory line

Build $K$, solve for $\alpha$, check $K\alpha$.

6. Computing predictions when $\alpha$ is given

Picture it

Asking each past example for its opinion, weighted by how similar it is to the new case.

Procedure

  1. Write the kernel: $k(x_i,x)=(x_i^Tx+c)^p$.
  2. For each training point compute $x_i^Tx_{test}$.
  3. Turn that into $k(x_i,x_{test})$ (add $c$, raise to $p$).
  4. Multiply each by $\alpha_i$ and add.

Worked examples

Worked example: Simple: test point makes every dot product $0$

Problem. Kernel $(x^Tx'+3)^2$, training points $(2,0,0),(0,2,0),(1,1,0),(-1,3,0)$, $\alpha=(1,-0.5,2,0.5)$. Predict at $(0,0,5)$.

What is asked. The prediction.

Idea / formula used. Look at the dot products before computing anything.

Every training point has third coordinate $0$ and the test point has the others $0$, so every dot product is $0$.
Each kernel value is $(0+3)^2=9$.
Prediction $=9\sum_i\alpha_i=9\times3=27$.
Answer: $27$
Common mistake: treating a zero dot product as a zero kernel value.
Worked example: Exam level: given kernel and $\alpha$

Problem. $x_1=(1,0),x_2=(1,1),x_3=(0,2)$, $\alpha=(2,-1,0.5)$, $k=(1+x^Tx')^2$. Predict at $(2,1)$.

What is asked. The prediction.

Idea / formula used. Make a small table: dot product, kernel value, weighted value.

Dot products with $(2,1)$: $2,\;3,\;2$.
Kernel values: $9,\;16,\;9$.
Weighted sum: $2\cdot9-1\cdot16+0.5\cdot9=18-16+4.5$.
Answer: $6.5$
Common mistake: weighting the dot products instead of the kernel values.
Worked example: Tricky: find a missing $\alpha$

Problem. $x_1=(0,1),x_2=(1,1),x_3=(0,-1),x_4=(-1,-1)$, $k=(1+x^Tx')^2$, $\alpha=(\alpha_1,0.5,\alpha_3,\alpha_4)$. The prediction at $(0,1)$ is $6$. Find $\alpha_1$.

What is asked. $\alpha_1$.

Idea / formula used. Some kernel values are zero, so their weights drop out.

Dot products with $(0,1)$: $1,1,-1,-1$.
Kernel values: $4,4,0,0$.
$4\alpha_1+4(0.5)=6$, so $4\alpha_1=4$.
Answer: $\alpha_1=1$
Common mistake: trying to find $\alpha_3$ and $\alpha_4$, which do not affect this prediction.
The zero-vector trick

If the test point is the zero vector, every dot product is $0$ and $(0+c)^p=c^p$ is the same for all points. For $c=1$ the prediction is simply $\sum\alpha_i$. Example: $\alpha=(2.3,-1,0.4,-0.7)$ gives $1.0$.

The prediction as a sum of pieces [ADDED]

Prediction at (2, 2) is built from three kernel-weighted piecesα₁·k = 0.5×49 = 24.5α2·k = 1×121= 121α3·k = 1.5×225= 337.5sum = 483k values come from (1 + xᵢᵀx)² with xᵢᵀx = 6, 10, 14.The point with the largest k·α contributes most. Here x₃ gives 337.5 of 483.This is the exam recipe: dot products, then kernel values, then weights, then add.
The numbers of PYQ kr06 (kernel $(1+x^Tx')^2$, $\alpha=(0.5,1,1.5)$, test point $(2,2)$): three pieces $24.5$, $121$ and $337.5$ add to $483$. Each piece is $\alpha_i\cdot k(x_i,x)$.

Dot product to kernel value: a lookup table [ADDED]

For $k=(1+t)^2$ (the kernel of kr05, kr06 and the kernel-matrix examples) with $t=x_i^Tx$:

$t=x_i^Tx$$-2$$-1$$0$$1$$2$$6$$10$$14$
$k=(1+t)^2$1014949121225

The value $t=-1$ gives $k=0$: that training point has no influence on that prediction. This is the trick in kr05, where two of the four points have $t=-1$.

The zero-test-vector table [ADDED]

Constant $c$Degree $p$$k=c^p$ for every pointPrediction at $x=0$Seen in
131$1\cdot\sum\alpha_i$exam pattern
224$4\sum\alpha_i$exam pattern (test vector orthogonal to all points)
238$8\sum\alpha_i$same idea, other numbers

Two more worked examples [ADDED]

Common mistakes, as a table [ADDED]

MistakeSymptomFix
Forgetting the constant $c$a value of $0$ where the answer is not $0$ (the whole prediction can become $0$)add $c$ before the power
Forgetting the powerthe prediction comes out far too smallapply the exponent
Using the labels $y$ instead of $\alpha$wrong numbersthe weights are $\alpha$; $y$ is only used to find $\alpha$
Searching for unused $\alpha$sextra workpoints with $k=0$ drop out; solve for the one that matters
Rounding earlyoff by a few hundredthskeep fractions until the last step

Variations to practise [ADDED] (not PYQs)

Recall questions

A test point gives $x_i^Tx=-1$ for a training point under $(1+t)^2$. What is its contribution?

Zero, since $k=0$.

Kernel $(1+t)^3$, test vector $0$, $\alpha=(1,2,-1)$. Prediction?

Every $k=1^3=1$, so the prediction is $1+2-1=2$.

Interactive explorer

Five training points. Switch between linear, polynomial and Gaussian kernels and watch the prediction curve and the coefficients $\alpha_i$ change. Move the new point to read off a prediction.

Memory line

Kernel values with the new point, times $\alpha$, summed.

7. Parametric or not, and the cost of kernel regression

Picture it

A reference book that grows with every case seen.

Definitions used in the exam

Definition: Parametric and non-parametric (as defined in the PYQ)

Parametric: the training samples are not needed to predict a new point (a fixed set of weights is enough). Non-parametric: all training samples are needed to predict.

Kernel regression predicts with $\sum_i\alpha_ik(x_i,x)$, which uses every training point $x_i$. So by these definitions it is non-parametric.

Cost

ModelTrainingPrediction
Linear regressionsolve a $d\times d$ systemone dot product of size $d$
Kernel regressionform and invert an $n\times n$ matrix $K$compute $n$ kernel values

When $n$ is large, kernel regression is more expensive than simple linear regression. In general it is computationally more expensive.

Trap: not parametric

Do not tick "kernel regression is parametric": the stored training points are part of the model.

Compare the costs with numbers [ADDED]

Let $n$ be the number of training points and $d$ the number of features. Counts below are operation counts, not time in seconds.

StepLinear regressionKernel regression
Build the matrix$XX^T$: about $nd^2$ multiplications$K$: $n^2$ kernel values, each costing about $d$: about $n^2d$
Solve$d\times d$ system: about $d^3$$n\times n$ system: about $n^3$
Store the model$d$ weights$n$ weights and all $n$ training points
One predictionabout $d$$n$ kernel values: about $nd$
Example sizeLinear $d^3$Kernel $n^3$Which is cheaper to solve?
$d=10,\ n=100$1,0001,000,000linear
$d=100,\ n=100$1,000,0001,000,000same order
$d=1000,\ n=100$1,000,000,0001,000,000kernel (when the lifted $d$ is huge)

Kernel regression wins when the lifted feature space is far larger than $n$. It loses when $n$ is huge and the plain model is enough.

Parametric or non-parametric: a decision table [ADDED]

Question to askLinear regressionKernel regression
Do I need the training points to predict?no, $w$ is enoughyes, all $x_i$ appear in $\sum\alpha_ik(x_i,x)$
Does the number of stored values grow with $n$?no (fixed $d$)yes ($n$ weights)
PYQ-style labelparametricnon-parametric

Another non-parametric model in this course is k-NN (it also needs all the training points). Decision trees, by contrast, store only the tree and do not need the training points to predict.

Common mistakes, as a table [ADDED]

MistakeFix
Ticking "kernel regression is parametric"it needs all training points
Ticking "kernel regression is cheaper than linear regression"training is a $n\times n$ problem; generally more expensive
Ticking "a kernel reduces variance"a kernel makes the model richer; that can increase variance

Recall questions

Training cost of kernel regression in terms of $n$?

About $n^3$ for the solve (and $n^2d$ to build $K$).

Prediction cost of kernel regression?

$n$ kernel values, about $nd$.

Worked example: counting the cost

$n=1000$ training points with $d=10$ features. Linear regression solves a $10\times10$ system and predicts with $10$ multiplications. Kernel regression builds a $1000\times1000$ matrix ($10^6$ kernel values), solves a $1000\times1000$ system, and needs $1000$ kernel values for every prediction. It must also keep all $1000$ training points, which is why it is called non-parametric.

Tips and tricks
  • The number of coefficients grows with the number of points, which is why the method is called non-parametric.
  • Separate training cost (building and solving $K$) from the cost of one prediction.
Memory line

Coefficients grow with the number of data points.

Where the cost goes
compare every pair: build Ksolve the n by n systemstore the n coefficientseach prediction uses all n points

Practise this topic →

Weeks covering this topic

Other MLT topics