From kernels to kernel PCA

Lecture 5 (High-Dimensional Data and Kernel Methods) and Lecture 6 (Kernel PCA), rebuilt as one storyline. Lecture 6 restates most of Lecture 5 with more examples, so each idea appears once here with both sources.

How to read this page. Ordered by concept, not by slide. Lecture 5 introduces kernels; Lecture 6 recaps them (its slides 2–4) and then builds kernel PCA on top. Every card says which slides it came from, what it needs, and what uses it. This page assumes the Lectures 3–4 page: covariance, eigen-decomposition, projection and the PCA algorithm.

Teal = explanation added by Claude, not on the slides. Amber = watch out. Dropdowns hide worked examples, practice questions, and their answers.

The road to kernel PCA

The “curse of dimensionality” (Part 1) is a side branch: it motivates kernels and explains HW1 Q7, but kernel PCA does not use its formulas.

Lecture map: which slides each card covers

Lecture 5 prints two slides as “6”. The second one (Nonlinear PCA) is called s6b here.

Part 1

Why linear PCA is not enough

Side branch. Read it once for the ideas; the formulas are not used later.

Data in high dimensions: hypercube and hyperball

With many attributes, the data lives in a huge box, but the points fill only a tiny part of it. Distances and angles stop behaving the way they do in 2-D.

Take the \(n \times m\) data matrix, centred so the mean is \(\mathbf 0\). Each point \(\mathbf x_i\) is in \(\mathbb R^m\); each attribute \(\boldsymbol x_j\) is in \(\mathbb R^n\).

The radius of the \(i\)-th attribute is its largest absolute value:

\[ r_i = \max_j \{ |x_{ji}| : j = 1, \dots, n \} \]

The data hyperspace as an \(m\)-dimensional hypercube of side \(2L\):

\[ \mathcal H_m(2L) = \{ \mathbf x = (x_1, \dots, x_m)^T : \forall i,\ x_i \in [-L, L] \} \] \[ L = \max_{1 \le i \le m} \{ r_i \} \]

Or as a hyperball centred at \(\mathbf 0\):

\[ \mathcal B_m(r) = \{ \mathbf x : \|\mathbf x\| \le r \} \] \[ r = \max_j \{ \|\mathbf x_j\| : 1 \le j \le n \} \]

A hypersphere is the surface of a hyperball:

\[ \mathcal S_m(r) = \{ \mathbf x : \|\mathbf x\| = r \} \]

Iris with 3 attributes (petal length, petal width, sepal length): the points occupy only a small space in the cube. This is even more pronounced in higher dimensions.

\(m\)
number of attributes = the dimension
\(L\)
half the side of the box: the biggest radius over all attributes
\(r\)
radius of the ball: the length of the longest point
ball vs sphere
the ball is the solid; the sphere is only its skin

The ball inside the cube shrinks

In 2-D a circle fills most of its square. As dimensions grow, the ball fills almost none of the cube, so almost all the room is in the corners.

Two dimensions:

\[ \frac{\operatorname{Vol}(\mathcal S_2(r))}{\operatorname{Vol}(\mathcal H_2(2r))} = \frac{\pi r^2}{4 r^2} = \frac{\pi}{4} = 78.5\% \]

Three dimensions:

\[ \frac{\operatorname{Vol}(\mathcal S_3(r))}{\operatorname{Vol}(\mathcal H_3(2r))} = \frac{4\pi r^3}{3 \times 8 r^3} = \frac{\pi}{6} = 52.4\% \]

The ratio keeps falling:

\[ \frac{\operatorname{Vol}(\mathcal S_m(r))}{\operatorname{Vol}(\mathcal H_m(2r))} \longrightarrow 0 \quad \text{as } m \longrightarrow \infty \]
  • The volume of a high-dimensional hypercube is concentrated in its corners.
  • The volume of the unit hypersphere decays to zero as \(m \to \infty\).
  • All the volume of a hypersphere is concentrated in a thin shell as \(m \to \infty\).
  • So the data tends to scatter on the corners of the cube and on the boundary of the sphere.
A square with a circle inside, then star shapes with 8 and 16 spikes and a smaller circle in the middle
L5 s5: the cube drawn as a star with one spike per corner. The inscribed ball (the circle) gets relatively smaller as the dimension grows.

A random unit vector in \(\mathbb R^{30}\) almost never lands near one chosen direction, because the sphere's surface is spread over a huge number of “corners”. That is why 100,000 random tries still lose to the eigenvector, which the eigen-decomposition finds directly.

Diagonals become orthogonal to every axis

In high dimensions the diagonal of the cube is almost at right angles to every axis. A cluster sitting near a diagonal collapses to the origin when you project it on any axis.

Hypercube centred at the origin, each coordinate in \([-1, 1]\). The \(i\)-th standard basis vector and a diagonal vector:

\[ \mathbf e_i = [0, 0, \dots, 1, \dots, 0, 0]^T \] \[ \mathbf d = [1, 1, \dots, 1, \dots, 1, 1]^T \]

The angle between them:

\[ \cos\theta = \frac{\mathbf d^T \mathbf e_i}{\|\mathbf e_i\| \cdot \|\mathbf d\|} = \frac{1}{\sqrt m} \] \[ \lim_{m \to \infty} \theta = \frac{\pi}{2} = 90^\circ \]

So all diagonal vectors are orthogonal to all coordinate axes, and a cluster of points near the diagonal gets projected to the origin.

How fast the angle grows
  1. \(m = 2\): \[ \cos\theta = \frac{1}{\sqrt 2} = 0.707 \] so \(\theta = 45^\circ\).
  2. \(m = 30\) (HW1 Q7): \[ \cos\theta = \frac{1}{\sqrt{30}} = 0.183 \] so \(\theta = 79.5^\circ\).
  3. \(m = 784\) (one MNIST image): \[ \cos\theta = \frac{1}{\sqrt{784}} = 0.036 \] so \(\theta = 88^\circ\).

Linear PCA cannot see a curve

PCA only rotates the axes, so it cannot straighten a curved pattern. If you first transform the data with a non-linear function, PCA works again.

For the arch-shaped dataset below, the covariance matrix has

\[ \boldsymbol\lambda = [3.41,\ 1.73]^T, \quad \mathbf u_1 = [0.15,\ 0.99]^T, \quad \mathbf u_2 = [0.99,\ -0.15]^T \]

Neither eigenvector captures the nonlinear relationship between \(x_1\) and \(x_2\).

Now transform the data:

\[ \phi(\mathbf x) = (x_1^2 - 5x_1,\ x_2) = (x_1',\ x_2) \]

The eigenvalues and eigenvectors of \(\operatorname{Cov}(x_1', x_2)\):

\[ \boldsymbol\lambda = [7.04,\ 0.11]^T, \quad \mathbf u_1 = [-0.68,\ 0.73]^T, \quad \mathbf u_2 = [0.73,\ 0.68]^T \]

Lecture 6: two rings stay tangled after PCA. PCA can only rotate the data. What separates the rings is the radius \(x_1^2 + x_2^2\), a nonlinear feature.

An arch of points with two eigenvector arrows, and the same arch with a fitted parabola
L5 s6b: the two principal directions (left) say nothing about the arch (right).
The arch, and the transformed data lying along a line with two eigenvector arrows
L5 s7: after replacing \(x_1\) by \(x_1^2 - 5x_1\), the data is nearly a line and the first eigenvalue holds 98% of the variance.
Two concentric rings of points before and after PCA, still concentric
L6 s16: PCA rotates the rings but cannot separate them.

Before the transform the two eigenvalues are similar (3.41 and 1.73), so no single direction explains the data. After it, one eigenvalue dominates (7.04 vs 0.11): one direction is enough. The transform \(\phi\) is the first example of a feature map, the next card.

Part 2

Kernels: inner products in a bigger space

Feature map and the kernel trick

A feature map sends each point to a longer vector made of its squares and products. The kernel trick is that the dot product of two mapped vectors can be computed from the original dot product, without ever writing the long vectors.

A mapping \(\phi\) that sends \(\mathbf x = (x_1, x_2)^T \in \mathbb R^2\) into a quadratic feature space:

\[ \phi(\mathbf x) = (x_1^2,\ x_2^2,\ \sqrt 2\, x_1 x_2)^T \]

The inner product of two transformed vectors:

\[ \begin{aligned} \phi(\mathbf x)^T \phi(\mathbf y) &= x_1^2 y_1^2 + x_2^2 y_2^2 + 2 x_1 x_2 y_1 y_2 \\ &= (x_1 y_1 + x_2 y_2)^2 \\ &= (\mathbf x^T \mathbf y)^2 \end{aligned} \]

So the inner product in the feature space can be written with the inner product in the input space, using a kernel function:

\[ \phi(\mathbf x)^T \phi(\mathbf y) = (\mathbf x^T \mathbf y)^2 = K(\mathbf x, \mathbf y) \]

This is known as the kernel trick.

For \(\mathbf x \in \mathbb R^3\) the same kernel has a six-dimensional feature map:

\[ \phi(\mathbf x) = (x_1^2,\ x_2^2,\ x_3^2,\ \sqrt 2\, x_1 x_2,\ \sqrt 2\, x_1 x_3,\ \sqrt 2\, x_2 x_3)^T \]
\(\phi\)
the feature map: input vector in, longer feature vector out
\(\phi(\mathbf x)\)
the point in feature space (we usually never compute it)
\(K(\mathbf x, \mathbf y)\)
the kernel: a number, equal to \(\phi(\mathbf x)^T \phi(\mathbf y)\)
\(\sqrt 2\)
makes the cross term come out as \(2 x_1 x_2 y_1 y_2\) when squared

Expand the square of a two-term sum:

\[ (x_1 y_1 + x_2 y_2)^2 = x_1^2 y_1^2 + 2\, x_1 y_1 x_2 y_2 + x_2^2 y_2^2 \]

The middle term must come from one coordinate of \(\phi\). Writing that coordinate as \(\sqrt 2\, x_1 x_2\) gives \((\sqrt 2\, x_1 x_2)(\sqrt 2\, y_1 y_2) = 2 x_1 x_2 y_1 y_2\). With \(m\) inputs there is one such coordinate per pair, so \(\phi\) has \(m\) squares plus \(\binom m2\) cross terms: 3 coordinates for \(m = 2\), 6 for \(m = 3\).

Map one vector
  1. \[ \mathbf x = (-0.5,\ \sqrt 2)^T \]
  2. \[ x_1^2 = 0.25 \]
  3. \[ x_2^2 = 2 \]
  4. \[ \sqrt 2\, x_1 x_2 = \sqrt 2 \times (-0.5) \times \sqrt 2 = -1 \]
  5. \[ \phi(\mathbf x) = (0.25,\ 2,\ -1)^T \]
Both routes give 25

\(\mathbf x = [2\ \ 1]^T\), \(\mathbf y = [1\ \ 3]^T\).

  1. Kernel route: \[ \mathbf x^T \mathbf y = 2 \times 1 + 1 \times 3 = 5 \]
  2. \[ K(\mathbf x, \mathbf y) = 5^2 = 25 \]
  3. Feature route: \[ \phi(\mathbf x) = (4,\ 1,\ 2\sqrt 2)^T \]
  4. \[ \phi(\mathbf y) = (1,\ 9,\ 3\sqrt 2)^T \]
  5. \[ \phi(\mathbf x)^T \phi(\mathbf y) = 4 + 9 + 12 = 25 \] Same answer, and the kernel route never needed \(\phi\).
Six-dimensional feature vectors

\(\mathbf x = (-1, 0, 1)^T\), \(\mathbf y = (1, -2, -1)^T\), kernel \(K(\mathbf x, \mathbf y) = (\mathbf x^T \mathbf y)^2\). Find \(\phi(\mathbf x)\), \(\phi(\mathbf y)\), and check the kernel value.

  1. \[ \mathbf x^T \mathbf y = -1 + 0 - 1 = -2 \]
  2. \[ K(\mathbf x, \mathbf y) = (-2)^2 = 4 \]
  3. Order of coordinates: \(x_1^2, x_2^2, x_3^2, \sqrt2 x_1 x_2, \sqrt2 x_1 x_3, \sqrt2 x_2 x_3\). \[ \phi(\mathbf x) = (1,\ 0,\ 1,\ 0,\ -\sqrt 2,\ 0)^T \]
  4. \[ \phi(\mathbf y) = (1,\ 4,\ 1,\ -2\sqrt 2,\ -\sqrt 2,\ 2\sqrt 2)^T \]
  5. \[ \phi(\mathbf x)^T \phi(\mathbf y) = 1 + 0 + 1 + 0 + 2 + 0 = 4 \] It matches the kernel value.

Kernel functions: polynomial and RBF

A kernel is any function of two points that equals a dot product in some feature space. The two you need are the polynomial kernel and the Gaussian (RBF) kernel.

The kernel implements the inner product in a high-dimensional feature space:

\[ K(\mathbf x, \mathbf y) = \langle \phi(\mathbf x), \phi(\mathbf y) \rangle = \phi(\mathbf x)^T \phi(\mathbf y) \]

The sum of any two kernel functions, \(K = K_1 + K_2\), is a kernel function.

Polynomial kernel, with \(c \ge 0\):

\[ K_p(\mathbf x, \mathbf y) = (c + \mathbf x^T \mathbf y)^p \]

Linear kernel (\(p = 1\)):

\[ \phi(\mathbf x) = (x_1, \dots, x_m, \sqrt c)^T \]

Quadratic kernel (\(p = 2\)) for \(m = 2\):

\[ \phi(\mathbf x) = (x_1^2,\ x_2^2,\ \sqrt 2\, x_1 x_2,\ \sqrt{2c}\, x_1,\ \sqrt{2c}\, x_2,\ c)^T \]

Gaussian, or Radial Basis Function (RBF), kernel, with \(\gamma \gt 0\):

\[ K_{RBF}(\mathbf x, \mathbf y) = \exp(-\gamma \|\mathbf x - \mathbf y\|^2) \]
\(p\)
degree of the polynomial (1 = linear, 2 = quadratic)
\(c\)
constant added before raising to the power; \(c = 0\) gives the homogeneous kernel \((\mathbf x^T \mathbf y)^p\)
\(\gamma\)
width parameter of the RBF kernel; bigger \(\gamma\) makes similarity fall faster with distance
\(\|\mathbf x - \mathbf y\|^2\)
squared Euclidean distance: sum of squared coordinate differences
\[ K_{RBF}(\mathbf x, \mathbf x) = e^{0} = 1 \] \[ 0 \lt K_{RBF}(\mathbf x, \mathbf y) \le 1 \]

Every point has feature-space length 1, and far-apart points have a kernel value near 0. The feature space of the RBF kernel is infinite-dimensional, so \(\phi\) can never be written out: everything must go through \(K\).

HW2 Q3 and Quiz 2 use \((\mathbf x^T \mathbf y)^2\), with no constant. L5 s12–14 and L6 Example 3 use \((1 + \mathbf x^T \mathbf y)^2\). Read the question before squaring.

Same two points, three kernels

\(\mathbf x = (2, 3)^T\), \(\mathbf y = (-1, 1)^T\). First \(\mathbf x^T \mathbf y = -2 + 3 = 1\) and \(\|\mathbf x - \mathbf y\|^2 = 3^2 + 2^2 = 13\).

  1. Linear, \(c = 0\): \[ K = 1 \]
  2. Quadratic, \(c = 1\): \[ K = (1 + 1)^2 = 4 \]
  3. Homogeneous quadratic, \(c = 0\): \[ K = 1^2 = 1 \]
  4. RBF, \(\gamma = 0.1\): \[ K = e^{-0.1 \times 13} = e^{-1.3} = 0.2725 \]

The kernel matrix and its properties

The kernel matrix stores the kernel value for every pair of points. It is a table of similarities, and it is all that kernel methods ever look at.

For a dataset \(X = \{\mathbf x_1, \dots, \mathbf x_n\}\):

\[ \mathbf K = [K_{ij}] = \begin{bmatrix} K(\mathbf x_1, \mathbf x_1) & K(\mathbf x_1, \mathbf x_2) & \cdots & K(\mathbf x_1, \mathbf x_n) \\ K(\mathbf x_2, \mathbf x_1) & K(\mathbf x_2, \mathbf x_2) & \cdots & K(\mathbf x_2, \mathbf x_n) \\ \vdots & \vdots & & \vdots \\ K(\mathbf x_n, \mathbf x_1) & K(\mathbf x_n, \mathbf x_2) & \cdots & K(\mathbf x_n, \mathbf x_n) \end{bmatrix} \]

Each element:

\[ K_{ij} = K(\mathbf x_i, \mathbf x_j) = \langle \phi(\mathbf x_i), \phi(\mathbf x_j) \rangle \]

The kernel matrix is symmetric:

\[ K_{ij} = K_{ji} \]

Lecture 6: every entry is a dot product, so \(\mathbf K\) is a Gram matrix with four properties.

PropertyStatementReason
1. Symmetric\(K_{ij} = K_{ji}\)order of a dot product
2. Non-negative diagonal\(K_{ii} \ge 0\)\(K_{ii} = \|\phi(\mathbf x_i)\|^2\)
3. Bounded entries\(|K_{ij}| \le \sqrt{K_{ii} K_{jj}}\)Cauchy–Schwarz: \(|\cos\theta_\phi| \le 1\)
4. Positive semidefinite\(\mathbf c^T \mathbf K \mathbf c \ge 0\) for every \(\mathbf c\)\(\mathbf c^T \mathbf K \mathbf c = \|\sum_i c_i \phi(\mathbf x_i)\|^2\)

Property 4 is equivalent to: all eigenvalues of \(\mathbf K\) are non-negative.

Recipe: kernel matrix by hand

  1. Write the points as columns of \(X\) and compute the Gram matrix \(X^T X\) (every pairwise dot product). For \(n\) points that is \(n(n+1)/2\) distinct dot products.
  2. Polynomial kernel: apply \((c + \cdot)^p\) to every entry. The slide writes this with the entry-wise (Hadamard) product \(\circ\): \[ \mathbf K_2 = (1 + X^T X) \circ (1 + X^T X) \]
  3. RBF kernel: first build the squared-distance matrix \(D = [\delta_{ij}]\) with \(\delta_{ij} = \|\mathbf x_i - \mathbf x_j\|^2\), then \[ \mathbf K_{RBF} = [e^{-\gamma \delta_{ij}}] \]
  4. Check: symmetric, diagonal \(\ge 0\) (RBF diagonal is exactly 1).

\(\|\mathbf x_i - \mathbf x_j\|^2 = \mathbf x_i^T \mathbf x_i + \mathbf x_j^T \mathbf x_j - 2\, \mathbf x_i^T \mathbf x_j\). So the Gram matrix also gives you \(D\): diagonal entry plus diagonal entry minus twice the off-diagonal entry.

Quadratic (c = 1) and RBF (γ = 0.1) kernel matrices for three points

Points as columns:

\[ X = \begin{bmatrix} -1 & 1 & 2 \\ 0 & -1 & 1 \\ 1 & 0 & -2 \end{bmatrix} \]
  1. \[ X^T X = \begin{bmatrix} 2 & -1 & -4 \\ -1 & 2 & 1 \\ -4 & 1 & 9 \end{bmatrix} \]
  2. Add 1 to every entry, then square every entry: \[ \mathbf K_2 = \begin{bmatrix} 9 & 0 & 9 \\ 0 & 9 & 4 \\ 9 & 4 & 100 \end{bmatrix} \]
  3. Squared distances, e.g. \(\delta_{12} = 2 + 2 - 2(-1) = 6\): \[ D = \begin{bmatrix} 0 & 6 & 19 \\ 6 & 0 & 9 \\ 19 & 9 & 0 \end{bmatrix} \]
  4. \[ \mathbf K_{RBF} = [e^{-0.1\,\delta_{ij}}] = \begin{bmatrix} 1 & 0.5488 & 0.1496 \\ 0.5488 & 1 & 0.4066 \\ 0.1496 & 0.4066 & 1 \end{bmatrix} \]
Can a hand-made similarity table be a kernel?

An analyst scores three products:

\[ S = \begin{bmatrix} 1 & 0.9 & 0.1 \\ 0.9 & 1 & 0.9 \\ 0.1 & 0.9 & 1 \end{bmatrix} \]

(a) Treat \(S\) as a kernel and compute the three feature-space distances. Do they satisfy the triangle inequality? (b) Find the eigenvalues. Hint: try \((1, 0, -1)^T\) and \((a, b, a)^T\).

  1. Unit diagonal, so \(d_\phi = \sqrt{2 - 2 S_{ij}}\) (see distance). \[ d_{AB} = d_{BC} = \sqrt{0.2} = 0.447 \]
  2. \[ d_{AC} = \sqrt{1.8} = 1.342 \]
  3. Triangle inequality needs \(d_{AC} \le d_{AB} + d_{BC} = 0.894\). It fails, so no set of points can have these distances.
  4. Try \((1, 0, -1)^T\): \[ S \begin{bmatrix} 1 \\ 0 \\ -1 \end{bmatrix} = \begin{bmatrix} 0.9 \\ 0 \\ -0.9 \end{bmatrix} \] so \(\lambda = 0.9\).
  5. Try \((a, b, a)^T\). Row 1 and row 2 give two equations: \[ 1.1 a + 0.9 b = \lambda a \]
  6. \[ 1.8 a + b = \lambda b \]
  7. Eliminate \(a, b\): \[ \lambda^2 - 2.1 \lambda - 0.52 = 0 \]
  8. \[ \lambda = 2.324 \quad \text{or} \quad \lambda = -0.224 \]
  9. A negative eigenvalue: \(S\) is not positive semidefinite, so it is not a valid kernel. Kernel PCA on \(S\) would report a negative variance.
Kernel matrix for two points, homogeneous quadratic kernel

\(\mathbf x_1 = (1, 2)^T\), \(\mathbf x_2 = (2, 1)^T\), \(K(\mathbf x_i, \mathbf x_j) = (\mathbf x_i^T \mathbf x_j)^2\). Find \(\mathbf K\).

  1. \[ \mathbf x_1^T \mathbf x_1 = 1 + 4 = 5 \]
  2. \[ \mathbf x_2^T \mathbf x_2 = 4 + 1 = 5 \]
  3. \[ \mathbf x_1^T \mathbf x_2 = 2 + 2 = 4 \]
  4. Square each: \[ \mathbf K = \begin{bmatrix} 25 & 16 \\ 16 & 25 \end{bmatrix} \]
Kernel matrix for two points (rebuilt from the solution sheet)

\(\mathbf x_1 = (1, 0)^T\), \(\mathbf x_2 = (1, 1)^T\), \(K(\mathbf x, \mathbf y) = (\mathbf x^T \mathbf y)^2\). Find \(\mathbf K\).

  1. \[ \mathbf x_1^T \mathbf x_1 = 1, \quad \mathbf x_1^T \mathbf x_2 = 1, \quad \mathbf x_2^T \mathbf x_2 = 2 \]
  2. \[ \mathbf K = \begin{bmatrix} 1 & 1 \\ 1 & 4 \end{bmatrix} \]
RBF kernel matrix for three points

\(\mathbf x_1 = (2.5, 1)^T\), \(\mathbf x_2 = (3.5, 4)^T\), \(\mathbf x_3 = (2, 2.1)^T\), Gaussian kernel with \(\gamma = 0.1\).

  1. \[ \delta_{12} = 1^2 + 3^2 = 10 \]
  2. \[ \delta_{13} = 0.5^2 + 1.1^2 = 1.46 \]
  3. \[ \delta_{23} = 1.5^2 + 1.9^2 = 5.86 \]
  4. \[ K_{12} = e^{-1} = 0.3679, \quad K_{13} = e^{-0.146} = 0.8642, \quad K_{23} = e^{-0.586} = 0.5565 \]
  5. \[ \mathbf K = \begin{bmatrix} 1 & 0.3679 & 0.8642 \\ 0.3679 & 1 & 0.5565 \\ 0.8642 & 0.5565 & 1 \end{bmatrix} \]
A squared-distance “kernel” for four points

\(\mathbf x_1 = (4, 2.9)\), \(\mathbf x_2 = (2.5, 1)\), \(\mathbf x_3 = (3.5, 4)\), \(\mathbf x_4 = (2, 2.1)\), with \(K(\mathbf x_i, \mathbf x_j) = \|\mathbf x_i - \mathbf x_j\|^2\). Compute the matrix.

  1. \[ \delta_{12} = 1.5^2 + 1.9^2 = 5.86 \]
  2. \[ \delta_{13} = 0.5^2 + 1.1^2 = 1.46 \]
  3. \[ \delta_{14} = 2^2 + 0.8^2 = 4.64 \]
  4. \[ \delta_{23} = 1^2 + 3^2 = 10 \]
  5. \[ \delta_{24} = 0.5^2 + 1.1^2 = 1.46 \]
  6. \[ \delta_{34} = 1.5^2 + 1.9^2 = 5.86 \]
  7. \[ \mathbf K = \begin{bmatrix} 0 & 5.86 & 1.46 & 4.64 \\ 5.86 & 0 & 10 & 1.46 \\ 1.46 & 10 & 0 & 5.86 \\ 4.64 & 1.46 & 5.86 & 0 \end{bmatrix} \]

Its diagonal is 0 and its eigenvalues are 15.12, −0.48, −2.17, −12.47. Three are negative, which breaks property 4 above. A squared-distance matrix is a distance table, not a Gram matrix. The 2025 key still reports the eigenvector of the largest eigenvalue, \((0.405, 0.580, 0.580, 0.405)^T\), but a 4×4 eigenproblem is not a by-hand exam task. If a question gives you such a matrix, state that it is not positive semidefinite.

Part 3

Working in feature space with only K

Every quantity PCA needs (lengths, distances, means, centring) can be written with kernel values alone.

Norm, distance and cosine in feature space

The length of a mapped point, the distance between two mapped points, and the angle between them all come from three kernel values. You never need the mapped vectors themselves.

Norm of a point

\[ \|\phi(\mathbf x)\|^2 = \phi(\mathbf x)^T \phi(\mathbf x) = K(\mathbf x, \mathbf x) \] \[ \|\phi(\mathbf x)\| = \sqrt{K(\mathbf x, \mathbf x)} \]

Distance in feature space

\[ \|\phi(\mathbf x) - \phi(\mathbf y)\|^2 = \|\phi(\mathbf x)\|^2 + \|\phi(\mathbf y)\|^2 - 2\, \phi(\mathbf x)^T \phi(\mathbf y) \] \[ \|\phi(\mathbf x) - \phi(\mathbf y)\| = \sqrt{K(\mathbf x, \mathbf x) + K(\mathbf y, \mathbf y) - 2 K(\mathbf x, \mathbf y)} \]

The kernel value \(K(\mathbf x, \mathbf y)\) is a similarity measure in feature space.

Cosine similarity between \(\phi(\mathbf x)\) and \(\phi(\mathbf y)\)

\[ \cos\theta_\phi = \frac{\phi(\mathbf x)^T \phi(\mathbf y)}{\|\phi(\mathbf x)\|\, \|\phi(\mathbf y)\|} = \frac{K(\mathbf x, \mathbf y)}{\sqrt{K(\mathbf x, \mathbf x)\, K(\mathbf y, \mathbf y)}} \]

Each formula is the Lecture 1 formula with every dot product replaced by a kernel value: \(\mathbf a^T \mathbf b \to K(\mathbf a, \mathbf b)\). That substitution is the whole method.

The slide computes \(\sqrt{14^2 + 3^2 - 2 \times 2^2} = 16.4012\). The expression is right but the value is not: \(196 + 9 - 8 = 197\), and \(\sqrt{197} = 14.036\). The example below uses the corrected value.

Norms, distance and cosine with the quadratic kernel (c = 1)

\(\mathbf x = [2\ \ 3]^T\), \(\mathbf y = [-1\ \ 1]^T\), \(K(\mathbf x, \mathbf y) = (1 + \mathbf x^T \mathbf y)^2\).

  1. \[ \|\phi(\mathbf x)\| = \sqrt{K(\mathbf x, \mathbf x)} = 1 + \mathbf x^T \mathbf x = 1 + 13 = 14 \]
  2. \[ \|\phi(\mathbf y)\| = 1 + \mathbf y^T \mathbf y = 1 + 2 = 3 \]
  3. \[ K(\mathbf x, \mathbf y) = (1 + (-2 + 3))^2 = 4 \]
  4. \[ \|\phi(\mathbf x) - \phi(\mathbf y)\| = \sqrt{196 + 9 - 2 \times 4} = \sqrt{197} = 14.036 \]
  5. \[ \cos\theta_\phi = \frac{4}{14 \times 3} = \frac{4}{42} = 0.0952 \]
Choosing γ and what RBF does to far-apart points

Customers compared with \(K(\mathbf x, \mathbf y) = \exp(-\gamma \|\mathbf x - \mathbf y\|^2)\). Two customers at input distance \(d = 2\) should have similarity 0.5. (a) Find \(\gamma\). (b) Show \(d_\phi^2 = 2 - 2K\). (c) Compute \(d_\phi\) for \(d = 1, 2, 4, 8\). (d) What happens to far-apart points?

  1. \[ e^{-4\gamma} = \tfrac12 \]
  2. \[ \gamma = \frac{\ln 2}{4} = 0.173 \] so \(K = 2^{-d^2/4}\).
  3. \(K(\mathbf x, \mathbf x) = 1\) for every point, so \[ d_\phi^2 = 1 + 1 - 2K(\mathbf x, \mathbf y) = 2 - 2K \]
  4. \(d\)\(K = 2^{-d^2/4}\)\(d_\phi = \sqrt{2 - 2K}\)
    10.8410.564
    20.5001.000
    40.06251.369
    8≈ 01.414
  5. Input distances 1 : 2 : 4 : 8 become 0.56 : 1 : 1.37 : 1.41. All far points look equally far, \(d_\phi \to \sqrt 2\). The RBF kernel is local; \(\gamma\) decides where “far” begins.
Feature distance rising from 0 and flattening at the square root of 2 as input distance grows
L6 s7: feature distance saturates at \(\sqrt 2\).
Cosine similarity with the Gaussian kernel

Same \(\mathbf x = (2, 3)^T\), \(\mathbf y = (-1, 1)^T\). Find \(\cos\theta_\phi\) with the RBF kernel, \(\gamma = 0.1\).

  1. \[ \|\mathbf x - \mathbf y\|^2 = 3^2 + 2^2 = 13 \]
  2. \[ K(\mathbf x, \mathbf y) = e^{-0.1 \times 13} = e^{-1.3} = 0.2725 \]
  3. \[ K(\mathbf x, \mathbf x) = K(\mathbf y, \mathbf y) = 1 \]
  4. \[ \cos\theta_\phi = \frac{0.2725}{\sqrt{1 \times 1}} = 0.2725 \] For the RBF kernel the cosine always equals the kernel value, because every mapped point has length 1.

The mean in feature space and its length

The mean of the mapped points cannot be written down, but its length can: average all the entries of the kernel matrix. The distance from any point to that mean also comes from the kernel matrix.

Mean in feature space:

\[ \boldsymbol\mu_\phi = \frac1n \sum_{i=1}^n \phi(\mathbf x_i) \]

If \(\phi(X) = [\phi(\mathbf x_1), \dots, \phi(\mathbf x_n)]\) (points as columns) then

\[ \boldsymbol\mu_\phi = \frac1n\, \phi(X)\, \mathbf 1_{n \times 1} \]

\(\boldsymbol\mu_\phi\) cannot be computed explicitly if we don't have access to \(\phi(\mathbf x_i)\). The norm of the mean is given by

\[ \boldsymbol\mu_\phi^T \boldsymbol\mu_\phi = \|\boldsymbol\mu_\phi\|^2 = \frac{1}{n^2} \sum_{i=1}^n \sum_{j=1}^n K(\mathbf x_i, \mathbf x_j) = \frac{1}{n^2}\, \mathbf 1^T \mathbf K \mathbf 1 \]
\(\mathbf 1\)
a column of \(n\) ones; \(\mathbf 1^T \mathbf K \mathbf 1\) is the sum of all entries of \(\mathbf K\)
\(\mathbf 1_{n \times n}\)
the \(n \times n\) matrix of all ones (used in centring)

Expand \(\|\phi(\mathbf x_i) - \boldsymbol\mu_\phi\|^2\) like any squared distance and replace dot products by kernel values:

\[ \|\phi(\mathbf x_i) - \boldsymbol\mu_\phi\|^2 = K_{ii} - \frac2n \sum_{j=1}^n K_{ij} + \frac{1}{n^2} \sum_{k=1}^n \sum_{l=1}^n K_{kl} \]

In words: the diagonal entry, minus twice the mean of row \(i\), plus the grand mean of \(\mathbf K\). This is exactly \(\hat K_{ii}\), the diagonal of the centred kernel matrix in the next card.

Norm of the feature-space mean of two points

With \(\mathbf K = \begin{bmatrix} 25 & 16 \\ 16 & 25 \end{bmatrix}\), find \(\|\boldsymbol\mu_\phi\|\) for \(\boldsymbol\mu_\phi = \tfrac12(\phi(\mathbf x_1) + \phi(\mathbf x_2))\) using only \(\mathbf K\).

  1. Sum of all entries: \[ \mathbf 1^T \mathbf K \mathbf 1 = 25 + 16 + 16 + 25 = 82 \]
  2. \[ \|\boldsymbol\mu_\phi\|^2 = \frac{82}{2^2} = 20.5 \]
  3. \[ \|\boldsymbol\mu_\phi\| = \sqrt{20.5} = 4.528 \]
Distance of φ(x₁) from the mean, RBF kernel

Use the RBF kernel matrix from HW2 Q5 (a): \(K_{12} = 0.3679\), \(K_{13} = 0.8642\), \(K_{23} = 0.5565\), diagonal 1.

  1. Mean of row 1: \[ \frac13 (1 + 0.3679 + 0.8642) = 0.7440 \]
  2. Grand mean: \[ \frac19 \big(3 + 2(0.3679 + 0.8642 + 0.5565)\big) = \frac{6.5772}{9} = 0.7308 \]
  3. \[ \|\phi(\mathbf x_1) - \boldsymbol\mu_\phi\|^2 = 1 - 2(0.7440) + 0.7308 = 0.2428 \]
  4. \[ \|\phi(\mathbf x_1) - \boldsymbol\mu_\phi\| = \sqrt{0.2428} = 0.4927 \]

Centring in feature space: the centred kernel matrix

PCA needs the data centred at zero. You cannot subtract the mean from vectors you never computed, but you can fix the kernel matrix instead: subtract the row means and column means and add back the grand mean.

Centring in feature space:

\[ \hat\phi(\mathbf x_i) = \phi(\mathbf x_i) - \boldsymbol\mu_\phi \]

Updating the kernel matrix (matrix form):

\[ \hat{\mathbf K} = \mathbf K - \frac1n \mathbf 1_{n \times n} \mathbf K - \frac1n \mathbf K \mathbf 1_{n \times n} + \frac{1}{n^2} \mathbf 1_{n \times n} \mathbf K \mathbf 1_{n \times n} \] \[ \hat{\mathbf K} = \Big( \mathbf I - \frac1n \mathbf 1_{n \times n} \Big)\, \mathbf K\, \Big( \mathbf I - \frac1n \mathbf 1_{n \times n} \Big) \]

Lecture 6, entry-wise. With \(\phi_i = \phi(\mathbf x_i)\) and \(\hat K_{ij} = \hat\phi_i^T \hat\phi_j\), expand the dot product:

\[ \hat K_{ij} = (\phi_i - \boldsymbol\mu_\phi)^T (\phi_j - \boldsymbol\mu_\phi) = \phi_i^T \phi_j - \phi_i^T \boldsymbol\mu_\phi - \boldsymbol\mu_\phi^T \phi_j + \boldsymbol\mu_\phi^T \boldsymbol\mu_\phi \]

Write each term with \(\mathbf K\):

\[ \phi_i^T \phi_j = K_{ij} \] \[ \phi_i^T \boldsymbol\mu_\phi = \frac1n \sum_k K_{ik} = \bar r_i \quad \text{(mean of row } i\text{)} \] \[ \boldsymbol\mu_\phi^T \boldsymbol\mu_\phi = \frac{1}{n^2} \sum_k \sum_l K_{kl} = \bar g \quad \text{(grand mean)} \] \[ \hat K_{ij} = K_{ij} - \bar r_i - \bar r_j + \bar g \]
\(\hat{\mathbf K}\)
centred kernel matrix: dot products of the centred feature vectors
\(\mathbf H = \mathbf I - \frac1n \mathbf 1_{n \times n}\)
the centring matrix; \(\hat{\mathbf K} = \mathbf H \mathbf K \mathbf H\)
\(\bar r_i\)
mean of row \(i\) of \(\mathbf K\) (equal to the column mean, by symmetry)
\(\bar g\)
mean of all \(n^2\) entries of \(\mathbf K\), which is \(\|\boldsymbol\mu_\phi\|^2\)

Recipe: centre a kernel matrix by hand

  1. Compute each row mean \(\bar r_i\).
  2. Compute the grand mean \(\bar g\) (average of the row means).
  3. For every entry: \[ \hat K_{ij} = K_{ij} - \bar r_i - \bar r_j + \bar g \]
  4. Check: every row of \(\hat{\mathbf K}\) sums to 0, and \(\hat{\mathbf K}\) is symmetric. (Rows sum to 0 because the centred vectors add up to the zero vector.)

Multiplying \(\mathbf K\) on the left by \(\frac1n \mathbf 1_{n \times n}\) replaces every entry by its column mean; on the right, by its row mean; on both sides, by the grand mean. So the four-term matrix formula and the entry-wise formula are identical. For \(n = 2\): \[ \mathbf H = \begin{bmatrix} 0.5 & -0.5 \\ -0.5 & 0.5 \end{bmatrix} \]

Quiz 2 feedback: many students skipped checking whether the data was centred. In kernel PCA the check is on \(\mathbf K\): if its rows do not sum to zero, centre it first. Eigenvalues of the uncentred \(\mathbf K\) are not variances.

Three points: K, mean norm, centred K, who is the outlier, centred cosine

\(\mathbf x_1 = (1, 1)^T\), \(\mathbf x_2 = (1, -1)^T\), \(\mathbf x_3 = (-1, 0)^T\), kernel \((1 + \mathbf x^T \mathbf y)^2\).

  1. Dot products: \[ \mathbf x_1^T \mathbf x_1 = \mathbf x_2^T \mathbf x_2 = 2, \quad \mathbf x_3^T \mathbf x_3 = 1, \quad \mathbf x_1^T \mathbf x_2 = 0, \quad \mathbf x_1^T \mathbf x_3 = \mathbf x_2^T \mathbf x_3 = -1 \]
  2. Add 1 and square: \[ \mathbf K = \begin{bmatrix} 9 & 1 & 0 \\ 1 & 9 & 0 \\ 0 & 0 & 4 \end{bmatrix} \]
  3. Norm of the mean: \[ \|\boldsymbol\mu_\phi\|^2 = \frac19\, \mathbf 1^T \mathbf K \mathbf 1 = \frac{24}{9} \] so \(\|\boldsymbol\mu_\phi\| = 1.633\).
  4. Row means and grand mean: \[ \bar{\mathbf r} = \Big( \tfrac{10}{3},\ \tfrac{10}{3},\ \tfrac43 \Big), \quad \bar g = \tfrac83 \]
  5. \(\hat K_{ij} = K_{ij} - \bar r_i - \bar r_j + \bar g\), e.g. \(\hat K_{11} = 9 - \tfrac{10}3 - \tfrac{10}3 + \tfrac83 = 5\): \[ \hat{\mathbf K} = \begin{bmatrix} 5 & -3 & -2 \\ -3 & 5 & -2 \\ -2 & -2 & 4 \end{bmatrix} \]
  6. Squared distance to the mean in the input space, mean \((\tfrac13, 0)\): \[ \tfrac{13}{9},\ \tfrac{13}{9},\ \tfrac{16}{9} \]
  7. In the feature space it is \(\hat K_{ii}\): \[ 5,\ 5,\ 4 \] The ranking flips: \(\mathbf x_3\) is the outlier in the input space, \(\mathbf x_1, \mathbf x_2\) in the feature space.
  8. Centred cosine, centre first then normalise: \[ \cos\theta_{12} = \frac{\hat K_{12}}{\sqrt{\hat K_{11} \hat K_{22}}} = \frac{-3}{5} = -0.6 \]
Two ways to centre, and the centred kernel matrix

With \(\phi(X) = [\phi(\mathbf x_1)\ \phi(\mathbf x_2)]\) and \(\mathbf K = \begin{bmatrix} 25 & 16 \\ 16 & 25 \end{bmatrix}\): (c) can we centre with (i) \(\phi(X) - \boldsymbol\mu_\phi \mathbf 1_2^T\) or (ii) \(\phi(X)(\mathbf I - \tfrac12 \mathbf 1_{2 \times 2})\)? (d) Find \(\hat{\mathbf K}\).

  1. (i): \(\boldsymbol\mu_\phi \mathbf 1_2^T\) is a matrix whose two columns are both \(\boldsymbol\mu_\phi\). Subtracting it takes the mean off each column. Valid.
  2. (ii): \(\phi(X)\mathbf 1_{2 \times 2}\) has both columns equal to \(\phi(\mathbf x_1) + \phi(\mathbf x_2) = 2\boldsymbol\mu_\phi\). Times \(\tfrac12\), both columns are \(\boldsymbol\mu_\phi\). So (ii) is the same matrix as (i). Valid, and this is the form that leads to \(\hat{\mathbf K} = \mathbf H \mathbf K \mathbf H\).
  3. Row means: both \(\tfrac{41}{2} = 20.5\). Grand mean 20.5.
  4. \[ \hat K_{11} = 25 - 20.5 - 20.5 + 20.5 = 4.5 \]
  5. \[ \hat K_{12} = 16 - 20.5 - 20.5 + 20.5 = -4.5 \]
  6. \[ \hat{\mathbf K} = \begin{bmatrix} 4.5 & -4.5 \\ -4.5 & 4.5 \end{bmatrix} \] Rows sum to 0 ✓
Centre K = [[1, 1], [1, 4]]
  1. Row means: \(\bar r_1 = 1\), \(\bar r_2 = 2.5\). Grand mean \(\bar g = \tfrac14(1 + 1 + 1 + 4) = 1.75\).
  2. \[ \hat K_{11} = 1 - 1 - 1 + 1.75 = 0.75 \]
  3. \[ \hat K_{12} = 1 - 1 - 2.5 + 1.75 = -0.75 \]
  4. \[ \hat K_{22} = 4 - 2.5 - 2.5 + 1.75 = 0.75 \]
  5. \[ \hat{\mathbf K} = \begin{bmatrix} 0.75 & -0.75 \\ -0.75 & 0.75 \end{bmatrix} \] The grading scheme gave 1 point for the means, 1.5 for the three distinct entries, 0.5 for the final matrix.

Part 4

Kernel PCA

PCA recap, written with points as columns

This is the PCA you already know, but with the data stored as columns and the covariance written as a matrix product. Kernel PCA rewrites each of these lines using the kernel matrix.

Centred data \(X = [\mathbf x_1, \dots, \mathbf x_n] \in \mathbb R^{m \times n}\) (points are columns, mean \(\mathbf 0\)). Projection of \(\mathbf x_j\) onto a unit vector \(\mathbf u\):

\[ a_j = \mathbf u^T \mathbf x_j \]

Variance of the projected values:

\[ \frac1n \sum_{j=1}^n a_j^2 = \frac1n \sum_{j=1}^n \mathbf u^T \mathbf x_j \mathbf x_j^T \mathbf u = \mathbf u^T \boldsymbol\Sigma\, \mathbf u \] \[ \boldsymbol\Sigma = \frac1n X X^T \quad (m \times m) \]

PCA as an optimisation problem:

\[ \max_{\mathbf u}\ \mathbf u^T \boldsymbol\Sigma\, \mathbf u \quad \text{subject to} \quad \mathbf u^T \mathbf u = 1 \] \[ L(\mathbf u, \lambda) = \mathbf u^T \boldsymbol\Sigma\, \mathbf u - \lambda (\mathbf u^T \mathbf u - 1) \] \[ \nabla_{\mathbf u} L = 2 \boldsymbol\Sigma \mathbf u - 2 \lambda \mathbf u = 0 \implies \boldsymbol\Sigma \mathbf u = \lambda \mathbf u \]
  • The variance along an eigenvector is \(\mathbf u^T \boldsymbol\Sigma \mathbf u = \lambda\, \mathbf u^T \mathbf u = \lambda\).
  • First PC \(\mathbf u_1\): eigenvector with the largest eigenvalue \(\lambda_1\). The \(r\)-th PC is the \(r\)-th eigenvector.
  • Same answer as minimising the reconstruction error (MSE).

In practice:

\[ \boldsymbol\Sigma = U \Lambda U^T, \quad \lambda_1 \ge \lambda_2 \ge \cdots \ge \lambda_m \ge 0 \] \[ \operatorname{trace}(\boldsymbol\Sigma) = \sum_{i=1}^m \lambda_i \]

Choose \(r\) as the smallest \(r\) with

\[ \frac{\lambda_1 + \cdots + \lambda_r}{\lambda_1 + \cdots + \lambda_m} \ge \alpha \] \[ A = U_r^T X \quad (r \times n) \] \[ \tilde X = U_r A = U_r U_r^T X = P_r X \] \[ \text{MSE} = \sum_{i = r+1}^m \lambda_i \]

Rows-as-samples there, columns-as-samples here. With columns, \(\frac1n X X^T\) is the covariance; with rows it is \(\frac1n X^T X\). Everything else is identical: see PCA as an optimisation and the PCA algorithm on the Lectures 3–4 page.

Kernel PCA: the idea and the obstacle

Do ordinary PCA, but on the mapped and centred points. The problem is that the covariance matrix in feature space is huge or infinite, so every step must be rewritten using the small n×n centred kernel matrix.

  1. Map each point, \(\mathbf x_j \mapsto \phi(\mathbf x_j)\), then centre: \[ \hat\phi(\mathbf x_j) = \phi(\mathbf x_j) - \boldsymbol\mu_\phi \]
  2. Covariance in feature space: \[ \boldsymbol\Sigma_\phi = \frac1n \sum_{j=1}^n \hat\phi(\mathbf x_j)\, \hat\phi(\mathbf x_j)^T \]
  3. Solve \[ \boldsymbol\Sigma_\phi \mathbf u = \lambda \mathbf u \]

The obstacle. \(\boldsymbol\Sigma_\phi\) has the size of the feature space: huge, or infinite for RBF. We must rewrite every step in terms of \(\hat{\mathbf K}\) (\(n \times n\)).

\(\boldsymbol\Sigma_\phi\)
covariance of the centred feature vectors (size: feature dimension squared)
\(\lambda\)
variance along a principal direction in feature space
\(\mathbf u\)
a principal direction in feature space (we will never write it explicitly)

Derivation: from Σφu = λu to K̂c = ηc

Each principal direction is a weighted sum of the data points, so finding it means finding n weights. Dotting the eigen-equation with each data point turns everything into kernel values, and the weights come out as an eigenvector of the centred kernel matrix.

Step I: every direction is a weighted sum of the points

Start from \(\boldsymbol\Sigma_\phi \mathbf u = \lambda \mathbf u\) and expand \(\boldsymbol\Sigma_\phi\):

\[ \frac1n \sum_{j=1}^n \hat\phi(\mathbf x_j)\, \big( \hat\phi(\mathbf x_j)^T \mathbf u \big) = \lambda \mathbf u \]

The bracket is a scalar. Divide by \(\lambda\):

\[ \mathbf u = \sum_{j=1}^n c_j\, \hat\phi(\mathbf x_j) \] \[ c_j = \frac{\hat\phi(\mathbf x_j)^T \mathbf u}{n \lambda} \]

Every principal direction is a weighted sum of the data points. Finding \(\mathbf u\) means finding the \(n\) weights \(\mathbf c = (c_1, \dots, c_n)^T\).

Step II: substitute and meet K̂

Write \(\hat\phi_i = \hat\phi(\mathbf x_i)\). Substitute \(\mathbf u = \sum_j c_j \hat\phi_j\) into \(\boldsymbol\Sigma_\phi \mathbf u = \lambda \mathbf u\). Left side:

\[ \boldsymbol\Sigma_\phi \mathbf u = \frac1n \sum_i \hat\phi_i \hat\phi_i^T \sum_j c_j \hat\phi_j = \frac1n \sum_i \sum_j \hat\phi_i\, \big( \hat\phi_i^T \hat\phi_j \big)\, c_j = \frac1n \sum_i \sum_j \hat\phi_i\, \hat K_{ij}\, c_j \]

Right side:

\[ \lambda \mathbf u = \lambda \sum_j c_j \hat\phi_j \]

Problem. Both sides are still vectors in feature space, built from \(\hat\phi_i\), which we cannot compute.

Step III: dot with each point to get numbers

Take the dot product with \(\hat\phi_k\) to turn each side into a number made of kernel values, for \(k = 1, \dots, n\):

\[ \frac1n \sum_i \hat K_{ki} \sum_j \hat K_{ij} c_j = \lambda \sum_j c_j \hat K_{kj} \]

Matrix form. \(\sum_j \hat K_{ij} c_j = (\hat{\mathbf K} \mathbf c)_i\), so the left side is \((\hat{\mathbf K}^2 \mathbf c)_k\):

\[ \frac1n \hat{\mathbf K}^2 \mathbf c = \lambda \hat{\mathbf K} \mathbf c \]

which is implied by

\[ \hat{\mathbf K} \mathbf c = \eta \mathbf c, \quad \eta = n \lambda \]
  • Eigenvalues of \(\hat{\mathbf K}\) give the variances: \(\lambda = \eta / n\).
  • \(\phi\) has disappeared: only kernel values remain.

Cancel one \(\hat{\mathbf K}\) from both sides of \(\frac1n \hat{\mathbf K}^2 \mathbf c = \lambda \hat{\mathbf K} \mathbf c\) and multiply by \(n\). The result is a plain eigenproblem for the \(n \times n\) matrix \(\hat{\mathbf K}\). Its eigenvalue \(\eta\) is \(n\) times the variance \(\lambda\).

Step IV: unit length and the coordinates

Unit-length direction:

\[ \|\mathbf u\|^2 = \sum_{i,j} c_i c_j \hat K_{ij} = \mathbf c^T \hat{\mathbf K} \mathbf c = \eta \|\mathbf c\|^2 \]

Setting \(\|\mathbf u\| = 1\) gives \(\|\mathbf c\|^2 = 1/\eta\):

\[ \mathbf c = \frac{1}{\sqrt\eta}\, \mathbf v \]

where \(\mathbf v\) is the unit eigenvector. Coordinate of \(\mathbf x_j\) on \(\mathbf u\):

\[ a_j = \mathbf u^T \hat\phi(\mathbf x_j) = \sum_i c_i \hat K_{ij} = \mathbf c^T \hat{\mathbf K}_j \]

where \(\hat{\mathbf K}_j\) is column \(j\) of \(\hat{\mathbf K}\). For all training points at once:

\[ \mathbf a = \hat{\mathbf K} \mathbf c = \sqrt\eta\, \mathbf v \]
\(\mathbf v\)
unit eigenvector of \(\hat{\mathbf K}\) (length 1, what you get from the characteristic equation)
\(\mathbf c\)
the same eigenvector rescaled to length \(1/\sqrt\eta\), so that \(\mathbf u\) has length 1
\(\mathbf a\)
the \(n\) coordinates of the training points on this component

For the training points you never need \(\mathbf c\): the coordinates are the unit eigenvector times the square root of its eigenvalue.

\[ \mathbf a_i = \sqrt{\eta_i}\, \mathbf v_i \]

Check: the variance of the coordinates is \(\frac1n \|\mathbf a\|^2 = \frac1n \eta \|\mathbf v\|^2 = \eta / n = \lambda\), as it should be. Quiz 2 P2(d) was exactly this line.

PCA vs kernel PCA, and the linear kernel

Side by side, every PCA quantity has a kernel twin. With the linear kernel the twins are literally the same numbers, which is also a cheap way to do PCA when there are far more attributes than points.

PCAKernel PCA
Matrix\(\boldsymbol\Sigma = \frac1n X X^T\) (\(m \times m\))\(\hat{\mathbf K}\) (\(n \times n\))
Eigenproblem\(\boldsymbol\Sigma \mathbf u = \lambda \mathbf u\)\(\hat{\mathbf K} \mathbf c = \eta \mathbf c\)
Variance of component\(\lambda\)\(\eta / n\)
Direction\(\mathbf u\), explicit\(\mathbf u = \sum_j c_j \hat\phi(\mathbf x_j)\), implicit
Scaling\(\|\mathbf u\| = 1\)\(\|\mathbf c\|^2 = 1/\eta\)
Coordinate of \(\mathbf x_j\)\(\mathbf u^T \mathbf x_j\)\(\mathbf c^T \hat{\mathbf K}_j\)
Max. number of components\(m\)\(n - 1\)

Linear kernel. With \(K(\mathbf x, \mathbf y) = \mathbf x^T \mathbf y\) and centred data:

\[ \hat{\mathbf K} = X^T X \quad (n \times n) \]
  • \(X^T X\) and \(X X^T = n \boldsymbol\Sigma\) share the same nonzero eigenvalues, so \(\eta = n \lambda\).
  • The coordinates \(\mathbf c^T \hat{\mathbf K}_j\) equal \(\mathbf u^T \mathbf x_j\) (up to sign).
  • Kernel PCA is a true generalisation of PCA.
  • When \(m \gg n\) (e.g. 500 images with \(10^6\) pixels), eigen-decompose the small \(n \times n\) matrix \(X^T X\) instead of the huge \(m \times m\) matrix \(\boldsymbol\Sigma\).

Centring makes the \(n\) feature vectors add up to zero, so they span at most \(n - 1\) directions. One eigenvalue of \(\hat{\mathbf K}\) is always 0, with eigenvector \((1, 1, \dots, 1)^T\). With \(n = 2\) points there is exactly one component (HW2 Q3, Quiz 2); with \(n = 3\), two (L6 Example 3).

The kernel PCA algorithm

Build the kernel matrix, centre it, find its eigenvalues and unit eigenvectors, and scale each eigenvector by the square root of its eigenvalue to get the new coordinates.

Input: data \(\mathbf x_1, \dots, \mathbf x_n\), kernel \(K\), variance threshold \(0 \lt \alpha \lt 1\).

  1. Kernel matrix: \(K_{ij} = K(\mathbf x_i, \mathbf x_j)\)
  2. Centre: \[ \hat{\mathbf K} = \Big( \mathbf I - \tfrac1n \mathbf 1_{n \times n} \Big) \mathbf K \Big( \mathbf I - \tfrac1n \mathbf 1_{n \times n} \Big) \]
  3. Eigen-decompose \(\hat{\mathbf K}\): \(\eta_1 \ge \eta_2 \ge \dots\), unit eigenvectors \(\mathbf v_1, \mathbf v_2, \dots\)
  4. Scale: \[ \mathbf c_i = \mathbf v_i / \sqrt{\eta_i} \] (so that \(\|\mathbf u_i\| = 1\))
  5. Choose the smallest \(r\) with \[ \frac{\sum_{i=1}^r \eta_i}{\sum_i \eta_i} \ge \alpha \]
  6. Reduced data: \[ A = C_r^T \hat{\mathbf K} \quad (r \times n), \quad C_r = [\mathbf c_1, \dots, \mathbf c_r] \]

Recipe for an exam question (n = 2 or 3 points)

  1. Dot products of the points, then apply the kernel formula to each: \(\mathbf K\).
  2. Row means and grand mean, then \(\hat K_{ij} = K_{ij} - \bar r_i - \bar r_j + \bar g\). Check rows sum to 0.
  3. Eigenvalues of \(\hat{\mathbf K}\) from \(\det(\hat{\mathbf K} - \eta \mathbf I) = 0\). One of them is 0. Unit eigenvectors \(\mathbf v_i\).
  4. Variances in feature space: \(\lambda_i = \eta_i / n\). Fraction kept by the first component: \(\eta_1 / \sum \eta_i\).
  5. Coordinates of the training points on component \(i\): \(\mathbf a_i = \sqrt{\eta_i}\, \mathbf v_i\). Check: \(\frac1n \|\mathbf a_i\|^2 = \lambda_i\), and \(a_{1}^2 + a_{2}^2 + \dots = \hat K_{jj}\) for each point \(j\) when all components are kept.

Reporting \(\eta\) as the variance (it is \(\eta / n\)). Using the unit eigenvector as the coordinates (multiply by \(\sqrt\eta\)). Working with the uncentred \(\mathbf K\) (HW2 Q5c asks for the dominant eigenvector of \(\mathbf K\) itself; that is a matrix exercise, not kernel PCA).

Kernel PCA on Example 3, by hand

Start from the centred matrix of Example 3:

\[ \hat{\mathbf K} = \begin{bmatrix} 5 & -3 & -2 \\ -3 & 5 & -2 \\ -2 & -2 & 4 \end{bmatrix} \]
  1. Eigenvectors from symmetry. Swapping \(\mathbf x_1 \leftrightarrow \mathbf x_2\) leaves \(\hat{\mathbf K}\) unchanged, so try \((1, -1, 0)^T\): \[ \hat{\mathbf K} \begin{bmatrix} 1 \\ -1 \\ 0 \end{bmatrix} = \begin{bmatrix} 8 \\ -8 \\ 0 \end{bmatrix} \] so \(\eta_1 = 8\).
  2. \[ \hat{\mathbf K} \begin{bmatrix} 1 \\ 1 \\ -2 \end{bmatrix} = \begin{bmatrix} 6 \\ 6 \\ -12 \end{bmatrix} \] so \(\eta_2 = 6\).
  3. \[ \hat{\mathbf K} \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix} \] so \(\eta_3 = 0\).
  4. Variances: \[ \lambda_1 = \tfrac83, \quad \lambda_2 = 2 \]
  5. Fraction of variance: \[ \tfrac{8}{14} = 57\%, \quad \tfrac{6}{14} = 43\% \] Two components capture 100%. With \(n = 3\) points at most \(n - 1 = 2\) can be nonzero.
  6. Scaling \(\mathbf c_i = \mathbf v_i / \sqrt{\eta_i}\): \[ \mathbf c_1 = \frac{1}{\sqrt 2} \frac{1}{\sqrt 8} \begin{bmatrix} 1 \\ -1 \\ 0 \end{bmatrix} = \frac14 \begin{bmatrix} 1 \\ -1 \\ 0 \end{bmatrix} \]
  7. \[ \mathbf c_2 = \frac{1}{\sqrt 6} \frac{1}{\sqrt 6} \begin{bmatrix} 1 \\ 1 \\ -2 \end{bmatrix} = \frac16 \begin{bmatrix} 1 \\ 1 \\ -2 \end{bmatrix} \]
  8. Coordinates \(\mathbf a_i = \hat{\mathbf K} \mathbf c_i\): \[ \mathbf a_1 = \frac14 \begin{bmatrix} 8 \\ -8 \\ 0 \end{bmatrix} = \begin{bmatrix} 2 \\ -2 \\ 0 \end{bmatrix} \]
  9. \[ \mathbf a_2 = \frac16 \begin{bmatrix} 6 \\ 6 \\ -12 \end{bmatrix} = \begin{bmatrix} 1 \\ 1 \\ -2 \end{bmatrix} \]
  10. Check the variances: \[ \tfrac13 (2^2 + 2^2 + 0^2) = \tfrac83 = \lambda_1 \]
  11. \[ \tfrac13 (1^2 + 1^2 + 2^2) = 2 = \lambda_2 \]
  12. PC 1 separates \(\mathbf x_1\) from \(\mathbf x_2\); PC 2 separates \(\mathbf x_3\) from the pair. In the input space \(\mathbf x_3\) looked like the outlier; kernel PCA spreads all three points apart.
Three points in input space, and the same three points spread out in kernel PCA coordinates
L6 s28: input space (left) and kernel PCA coordinates \((a_1, a_2)\) (right).
Eigenvalues, variances and coordinates for two points

From \(\hat{\mathbf K} = \begin{bmatrix} 4.5 & -4.5 \\ -4.5 & 4.5 \end{bmatrix}\): (e) all eigenvalues and eigenvectors of \(\hat{\mathbf K}\); (f) eigenvalues of \(\boldsymbol\Sigma_\phi\); (g) coordinates of the centred \(\phi(\mathbf x_1)\), \(\phi(\mathbf x_2)\) along the eigenvectors of \(\boldsymbol\Sigma_\phi\).

  1. \[ \det(\hat{\mathbf K} - \eta \mathbf I) = (4.5 - \eta)^2 - 4.5^2 = 0 \]
  2. \[ \eta (\eta - 9) = 0 \]
  3. \[ \eta_1 = 9, \quad \eta_2 = 0 \]
  4. For \(\eta_1 = 9\): \((4.5 - 9) v_1 - 4.5 v_2 = 0\) gives \(v_2 = -v_1\). \[ \mathbf v_1 = \frac{1}{\sqrt 2} \begin{bmatrix} 1 \\ -1 \end{bmatrix} \]
  5. For \(\eta_2 = 0\): \(v_2 = v_1\). \[ \mathbf v_2 = \frac{1}{\sqrt 2} \begin{bmatrix} 1 \\ 1 \end{bmatrix} \]
  6. Eigenvalues of \(\boldsymbol\Sigma_\phi\): \[ \lambda_1 = \frac{9}{2} = 4.5, \quad \lambda_2 = 0 \] (all remaining feature-space directions also have eigenvalue 0).
  7. Coordinates on the first component: \[ \mathbf a_1 = \sqrt{\eta_1}\, \mathbf v_1 = \frac{3}{\sqrt 2} \begin{bmatrix} 1 \\ -1 \end{bmatrix} = \begin{bmatrix} 2.121 \\ -2.121 \end{bmatrix} \] On the second, both coordinates are 0.
  8. Check: \(a_{11}^2 = 4.5 = \hat K_{11}\) ✓ and \(\tfrac12(2.121^2 + 2.121^2) = 4.5 = \lambda_1\) ✓. The key reports the same values with the opposite sign, which is also correct.
Eigen-decompose the centred K and project

From \(\hat{\mathbf K} = \begin{bmatrix} 0.75 & -0.75 \\ -0.75 & 0.75 \end{bmatrix}\): (c) eigenvalues and unit eigenvectors of \(\hat{\mathbf K}\), and the eigenvalues of \(\boldsymbol\Sigma_\phi\); (d) the coordinate of each point on the first kernel principal component.

  1. \[ \det(\hat{\mathbf K} - \eta \mathbf I) = (0.75 - \eta)^2 - 0.75^2 = \eta(\eta - 1.5) = 0 \]
  2. \[ \eta_1 = 1.5, \quad \mathbf c_1 = \tfrac{1}{\sqrt 2}(1, -1)^T \]
  3. \[ \eta_2 = 0, \quad \mathbf c_2 = \tfrac{1}{\sqrt 2}(1, 1)^T \]
  4. Eigenvalues of \(\boldsymbol\Sigma_\phi\): \[ \frac{1.5}{2} = 0.75, \quad 0 \]
  5. \[ a_{11} = \sqrt{1.5} \times \tfrac{1}{\sqrt 2} = \sqrt{0.75} = 0.866, \quad a_{21} = -0.866 \]
  6. Check: \(\hat K_{11} = \|\hat\phi(\mathbf x_1)\|^2 = 0.75 = a_{11}^2\), consistent with all variance lying along one direction. Opposite signs accepted. (Grading: 1.5 for the eigenvalues of \(\hat{\mathbf K}\), 1.5 for unit eigenvectors, 1 for the eigenvalues of \(\boldsymbol\Sigma_\phi\), 1 per coordinate.)
Dominant eigenvector of the (uncentred) RBF kernel matrix

For \(\mathbf K = \begin{bmatrix} 1 & 0.3679 & 0.8642 \\ 0.3679 & 1 & 0.5565 \\ 0.8642 & 0.5565 & 1 \end{bmatrix}\), find the dominant eigenvalue and eigenvector.

  1. A 3×3 characteristic polynomial is not an exam-size task; the 2025 key used software. \[ \eta_{\max} = 2.2176, \quad \mathbf v_{\max} = (0.6002,\ 0.4754,\ 0.6433)^T \]
  2. How to verify any claimed pair: multiply. \[ \mathbf K \mathbf v_{\max} = (1.331,\ 1.054,\ 1.427)^T \] and \(2.2176 \times \mathbf v_{\max} = (1.331,\ 1.054,\ 1.427)^T\). They agree.
  3. All three entries of \(\mathbf v_{\max}\) are positive and similar in size: the dominant eigenvector of an uncentred similarity matrix mostly measures “how similar each point is to everyone”, not a principal direction. For kernel PCA you would centre \(\mathbf K\) first.

Part 5

Exam toolkit

Which formula, when

The formulas are given in the exam. The skill tested is picking the right one and showing the steps.

The question asks for…UseCard
the feature vector \(\phi(\mathbf x)\) of a quadratic kernelsquares, then \(\sqrt 2\) × each product (and \(\sqrt{2c}\, x_i\), \(c\) if \(c \ne 0\))Feature map Kernel functions
a kernel value\((c + \mathbf x^T \mathbf y)^p\) or \(\exp(-\gamma \|\mathbf x - \mathbf y\|^2)\)Kernel functions
the kernel matrixGram matrix \(X^T X\), then the kernel formula entry by entryKernel matrix
is this matrix a valid kernel?symmetric, diagonal ≥ 0, no negative eigenvalueKernel matrix
length of \(\phi(\mathbf x)\)\(\sqrt{K(\mathbf x, \mathbf x)}\)Norm, distance, cosine
distance between \(\phi(\mathbf x)\) and \(\phi(\mathbf y)\)\(\sqrt{K(\mathbf x,\mathbf x) + K(\mathbf y,\mathbf y) - 2K(\mathbf x,\mathbf y)}\)Norm, distance, cosine
cosine similarity in feature space\(K(\mathbf x,\mathbf y) / \sqrt{K(\mathbf x,\mathbf x) K(\mathbf y,\mathbf y)}\)Norm, distance, cosine
norm of the feature-space mean\(\sqrt{\tfrac{1}{n^2} \mathbf 1^T \mathbf K \mathbf 1}\) (root of the grand mean)Mean in feature space
distance of a point from the mean\(\sqrt{K_{ii} - 2 \bar r_i + \bar g} = \sqrt{\hat K_{ii}}\)Mean in feature space
the centred kernel matrix\(\hat K_{ij} = K_{ij} - \bar r_i - \bar r_j + \bar g\), or \(\mathbf H \mathbf K \mathbf H\)Centring K
variance along a kernel component\(\lambda = \eta / n\), with \(\eta\) an eigenvalue of \(\hat{\mathbf K}\)Dot with each point
coordinates of the points on a component\(\mathbf a = \sqrt\eta\, \mathbf v\) (or \(\hat{\mathbf K} \mathbf c\) with \(\mathbf c = \mathbf v / \sqrt\eta\))Scaling & coordinates
how many components to keepsmallest \(r\) with \(\sum_{i \le r} \eta_i / \sum \eta_i \ge \alpha\); at most \(n - 1\)Kernel PCA algorithm
why ordinary PCA failed on this dataPCA only rotates; a curve or a ring needs a nonlinear featureLinear PCA misses curves

Rows of \(\hat{\mathbf K}\) sum to 0. One eigenvalue of \(\hat{\mathbf K}\) is 0. The kernel variances \(\eta_i / n\) add up to the mean of the diagonal of \(\hat{\mathbf K}\). For an RBF kernel every diagonal entry of \(\mathbf K\) is 1 and every entry lies in \((0, 1]\).