Kernels, alpha, predictions
Open the interactive lessons →Kernel regression past questions
Contents
- Orientation: what you will learn and why
- Big picture: concept map, three regressions compared, and the recipe
- Start here: inner products, expanding squares, a $3\times3$ system
- The idea: feature maps, kernels and the prediction formula
- The kernel matrix $K$ and solving for $\alpha$
- Computing predictions when $\alpha$ is given
- 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
- State the prediction formula $\hat y(x)=\sum_i\alpha_i\,k(x_i,x)$ and use it.
- Build a kernel matrix $K$ for a polynomial kernel and solve $\alpha=K^{-1}y$.
- Predict at a test point when $\alpha$ is given, including the shortcut when many kernel values are equal.
- Say what a kernel does (similarity in a transformed space, non-linear fits) and what it does not do.
- Decide whether kernel regression is parametric or non-parametric, and how its cost compares with linear regression.
Why this topic matters for Quiz 2
Counted only from the PYQs in this project (not a prediction):
| What the PYQs ask | PYQs | Marks |
|---|---|---|
| Kernel idea and prediction formula | 2 | 5 |
| Kernel matrix and solving for alpha | 2 | 7 |
| Computing predictions | 4 | 14.5 |
| Parametric or not; cost | 1 | 4 |
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.
Read each section, do the quick checks, then attempt each PYQ before opening its solution.
What is new in this upgrade [ADDED]
- A big picture section (next) with a concept map, a three-way comparison table and a five-line exam recipe.
- Figures: a curve becoming a line after lifting, the kernel value against the dot product, the prediction as a sum of pieces, and zero training error with a singular-$K$ warning.
- A complete worked example, the size of the lifted space, which functions are valid kernels, when $K^{-1}$ exists, cost tables, mistake tables, recall questions and a flowchart. Everything new is tagged [ADDED].
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
Three regressions compared
| Plain linear regression | Linear regression on lifted features (explicit $\phi$) | Kernel regression | |
|---|---|---|---|
| Fits | a 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? | no | yes | no |
| Cost grows with | $d$ | $\dim\phi$ (can be enormous) | $n$ (number of points) |
| Parametric (PYQ definition)? | yes | yes | no: needs all training points |
The exam recipe, in five lines
- 1. Write the kernel with its numbers, e.g. $k(a,b)=(1+a^Tb)^2$.
- 2. If the question gives $\alpha$, skip to step 4. Otherwise build $K$ ($n\times n$) from all pairs of training points.
- 3. Solve $K\alpha=y$.
- 4. For the test point $x$, compute every dot product $x_i^Tx$, then every $k(x_i,x)$.
- 5. Prediction $=\sum_i\alpha_ik(x_i,x)$.
Mnemonics and recognition cues [ADDED]
- "Dot, Add, Power": for a polynomial kernel always do the dot product, then add $c$, then raise to $p$. Missing either of the last two is the most common error.
- "K is n by n": $K$ has one row and column per training point, never per feature.
- "Kernel = Similarity": a large $k(x_i,x)$ means $x_i$ is very similar to $x$ in the lifted space.
- "Alpha weights the points": $\alpha_i$ is the weight of training point $i$. $w^*=\phi(X)\alpha$ means "$w^*$ is a mix of the lifted data points".
- Cue "optimal $w^*=\phi(X)[\dots]^T$": the bracket is $\alpha$. Go straight to $\sum\alpha_ik$.
- Cue "zero vector" or "$[0,0,1]^T$ is orthogonal to the data": every dot product is $0$, every $k=c^p$.
Analogy and its limit [ADDED]
| Analogy | What it explains well | Where it breaks |
|---|---|---|
| Lift the points onto a table where they form a line | why 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 similarity | the 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$.
- 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)
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]
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.
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$.
- 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.
4. The idea: feature maps, kernels and the prediction formula
From a bent pattern to a straight fit
Seeing the lift [ADDED]
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.
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.
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')$.
$$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 |
|---|---|---|---|
| 2 | 2 | 6 | $d$ multiplications, one addition, one power |
| 2 | 3 | 10 | $d$ multiplications, one addition, one power |
| 10 | 3 | 286 | $d$ multiplications, one addition, one power |
| 100 | 3 | 176851 | $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.
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.
$$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$.
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)$.
"The kernel minimises the loss" or "reduces variance" are wrong options. The kernel defines similarity; the loss is minimised when solving for $\alpha$.
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.
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$.
| Candidate | Valid? | Reason |
|---|---|---|
| $k=x^Tx'$ (linear) | yes | $\phi(x)=x$ |
| $k=(x^Tx'+c)^p$, $c\ge0$, integer $p\ge1$ | yes | products and sums of kernels are kernels; this is the PYQ family |
| $k=-x^Tx'$ | no | on 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
- 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.
Prediction is a weighted sum of similarities.
5. The kernel matrix $K$ and solving for $\alpha$
A table of how alike every pair of students is.
Definition
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$).
$$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
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$).
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)$.
Problem. Same $K$, $y=(6,6,2)^T$.
What is asked. $\alpha$.
Idea / formula used. Subtract equations so that most terms cancel.
$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$.
- Build $K$. $K_{11}=(1+1)^2=4$, $K_{12}=(2+1)^2=9$, $K_{22}=(4+1)^2=25$. So $K=\begin{bmatrix}4&9\\9&25\end{bmatrix}$.
- Determinant. $\det K=100-81=19$. $K^{-1}=\tfrac1{19}\begin{bmatrix}25&-9\\-9&4\end{bmatrix}$.
- Solve. $\alpha=K^{-1}y=\tfrac1{19}(25-27,\ -9+12)=(-\tfrac2{19},\ \tfrac3{19})\approx(-0.1053,\ 0.1579)$.
- Check. $K\alpha=(4\cdot(-\tfrac2{19})+9\cdot\tfrac3{19},\ 9\cdot(-\tfrac2{19})+25\cdot\tfrac3{19})=(\tfrac{19}{19},\ \tfrac{57}{19})=(1,3)=y$ ✓.
- Predict at $x=3$. $k(1,3)=(3+1)^2=16$, $k(2,3)=(6+1)^2=49$. $\hat y=-\tfrac2{19}\cdot16+\tfrac3{19}\cdot49=\tfrac{-32+147}{19}=\tfrac{115}{19}\approx6.0526$.
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$.)
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 = 1 | p = 2 | p = 3 | p = 4 |
|---|---|---|---|---|
| 3 | 2 (singular) | 3 (invertible) | 3 (invertible) | 3 (invertible) |
| 4 | 2 (singular) | 3 (singular) | 4 (invertible) | 4 (invertible) |
| 5 | 2 (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]
| Check | What you should see | Why |
|---|---|---|
| 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 |
| PSD | trace $\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]
| Mistake | Fix |
|---|---|
| Building $K$ of size $d\times d$ | it is $n\times n$ |
| Using $x_i^Tx_j$ without adding $c$ and without the power | every entry is $(x_i^Tx_j+c)^p$ |
| Filling only the upper triangle and then forgetting the lower triangle | copy 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.
- $K$ is symmetric, so compute one triangle and mirror it.
- Check your $\alpha$ by multiplying $K\alpha$ and comparing with $y$.
Build $K$, solve for $\alpha$, check $K\alpha$.
6. Computing predictions when $\alpha$ is given
Asking each past example for its opinion, weighted by how similar it is to the new case.
Procedure
- Write the kernel: $k(x_i,x)=(x_i^Tx+c)^p$.
- For each training point compute $x_i^Tx_{test}$.
- Turn that into $k(x_i,x_{test})$ (add $c$, raise to $p$).
- Multiply each by $\alpha_i$ and add.
Worked examples
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.
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.
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.
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]
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$ | 1 | 0 | 1 | 4 | 9 | 49 | 121 | 225 |
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 point | Prediction at $x=0$ | Seen in |
|---|---|---|---|---|
| 1 | 3 | 1 | $1\cdot\sum\alpha_i$ | exam pattern |
| 2 | 2 | 4 | $4\sum\alpha_i$ | exam pattern (test vector orthogonal to all points) |
| 2 | 3 | 8 | $8\sum\alpha_i$ | same idea, other numbers |
Two more worked examples [ADDED]
- Missing $\alpha$ with a different test point. $x_1=(1,0)$, $x_2=(0,1)$, $x_3=(-1,0)$, $x_4=(0,-1)$, $k=(1+x^Tx')^2$, $\alpha=(\alpha_1,0.5,\alpha_3,0.25)$, and the prediction at $(1,-1)$ is $2$. Dot products: $1,\ -1,\ -1,\ 1$, so $k=4,\ 0,\ 0,\ 4$. Then $4\alpha_1+0+0+4(0.25)=2\Rightarrow4\alpha_1=1\Rightarrow\alpha_1=0.25$. Observe that $x_2$ and $x_3$ vanish because their dot product is $-1$.
- Test point equal to a training point. In the tiny example of the previous section, predicting at $x=2$ gives $-\tfrac2{19}\cdot9+\tfrac3{19}\cdot25=\tfrac{57}{19}=3=y_2$. When the test point is a training point, the answer is simply its label (if $K$ is invertible).
Common mistakes, as a table [ADDED]
| Mistake | Symptom | Fix |
|---|---|---|
| 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 power | the prediction comes out far too small | apply the exponent |
| Using the labels $y$ instead of $\alpha$ | wrong numbers | the weights are $\alpha$; $y$ is only used to find $\alpha$ |
| Searching for unused $\alpha$s | extra work | points with $k=0$ drop out; solve for the one that matters |
| Rounding early | off by a few hundredths | keep fractions until the last step |
Variations to practise [ADDED] (not PYQs)
- Change the constant: same data as the worked prediction, but $k=(2+x^Tx')^2$.
- Change the degree: $k=(1+x^Tx')^3$ with the same $\alpha$.
- A test point that is orthogonal to some training points but not others.
- A two-feature $X$ where you must first compute $\alpha$ from a $3\times3$ $K$ (as in kr08), then predict.
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$.
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.
Kernel values with the new point, times $\alpha$, summed.
7. Parametric or not, and the cost of kernel regression
A reference book that grows with every case seen.
Definitions used in the exam
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
| Model | Training | Prediction |
|---|---|---|
| Linear regression | solve a $d\times d$ system | one dot product of size $d$ |
| Kernel regression | form 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.
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.
| Step | Linear regression | Kernel 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 prediction | about $d$ | $n$ kernel values: about $nd$ |
| Example size | Linear $d^3$ | Kernel $n^3$ | Which is cheaper to solve? |
|---|---|---|---|
| $d=10,\ n=100$ | 1,000 | 1,000,000 | linear |
| $d=100,\ n=100$ | 1,000,000 | 1,000,000 | same order |
| $d=1000,\ n=100$ | 1,000,000,000 | 1,000,000 | kernel (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 ask | Linear regression | Kernel regression |
|---|---|---|
| Do I need the training points to predict? | no, $w$ is enough | yes, 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 label | parametric | non-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]
| Mistake | Fix |
|---|---|
| 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$.
$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.
- 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.
Coefficients grow with the number of data points.