🧮 Brain Teaser

The Kernel Trick: Why Inner Products Are All You Need

You are building a classifier. You decide to map your input xRdx \in \mathbb{R}^d to a high-dimensional (even infinite-dimensional) feature space ϕ(x)H\phi(x) \in \mathcal{H}, then run a linear classifier there.

The prediction function takes the form:

f(x)=i=1nαiϕ(xi),ϕ(x)Hf(x) = \sum_{i=1}^{n} \alpha_i \langle \phi(x_i), \phi(x) \rangle_{\mathcal{H}}

The puzzle: You never want to compute ϕ(x)\phi(x) explicitly. Instead you only ever evaluate a kernel function k(x,x)=ϕ(x),ϕ(x)Hk(x, x') = \langle \phi(x), \phi(x') \rangle_{\mathcal{H}}.

Consider the polynomial kernel k(x,x)=(1+xx)2k(x, x') = (1 + x \cdot x')^2 on R2\mathbb{R}^2, where x=(x1,x2)x = (x_1, x_2).

(a) Find an explicit feature map ϕ:R2Rm\phi: \mathbb{R}^2 \to \mathbb{R}^m such that k(x,x)=ϕ(x),ϕ(x)k(x,x') = \langle \phi(x), \phi(x') \rangle.

(b) What is the dimension mm of the feature space? Now consider the more general kernel k(x,x)=(1+xx)pk(x, x') = (1 + x \cdot x')^p on Rd\mathbb{R}^d. What is the dimension of the induced feature space as a function of dd and pp? What happens as dd and pp grow?

(c) The conceptual punch line: Suppose evaluating k(x,x)k(x, x') costs O(d)O(d) time. Evaluating ϕ(x),ϕ(x)\langle \phi(x), \phi(x') \rangle explicitly costs O(m)O(m) time, where mm grows polynomially in dd and pp. What does this say about the power of the kernel trick?

kernel trickfeature mapsSVMpolynomial kerneldimensionalityinner product

Answer: The Kernel Trick: Why Inner Products Are All You Need

Key Idea / Intuition

The kernel trick is the observation that many algorithms only need pairwise inner products between data points — never the feature vectors themselves. So if we can compute ϕ(x),ϕ(x)\langle \phi(x), \phi(x') \rangle cheaply via a kernel function k(x,x)k(x,x'), we get the power of working in a huge (or infinite) feature space at the cost of working in the original space. The puzzle here is to make this concrete: expand the polynomial kernel by the binomial theorem and read off ϕ\phi directly.


Formal Proof / Solution

Part (a): Explicit feature map for k(x,x)=(1+xx)2k(x,x') = (1 + x \cdot x')^2, xR2x \in \mathbb{R}^2

Let x=(x1,x2)x = (x_1, x_2) and x=(x1,x2)x' = (x_1', x_2'). Expand:

k(x,x)=(1+x1x1+x2x2)2k(x, x') = (1 + x_1 x_1' + x_2 x_2')^2

Apply the multinomial theorem:

=1+x12(x1)2+x22(x2)2+2x1x1+2x2x2+2x1x2x1x2= 1 + x_1^2 (x_1')^2 + x_2^2 (x_2')^2 + 2 x_1 x_1' + 2 x_2 x_2' + 2 x_1 x_2 x_1' x_2'

This is exactly ϕ(x),ϕ(x)\langle \phi(x), \phi(x') \rangle with:

ϕ(x)=(1, x12, x22, 2x1, 2x2, 2x1x2)R6\phi(x) = \bigl(1,\ x_1^2,\ x_2^2,\ \sqrt{2}\, x_1,\ \sqrt{2}\, x_2,\ \sqrt{2}\, x_1 x_2\bigr) \in \mathbb{R}^6

Verification: ϕ(x),ϕ(x)=11+x12(x1)2+x22(x2)2+2x1x1+2x2x2+2x1x2x1x2=k(x,x)\langle \phi(x), \phi(x') \rangle = 1 \cdot 1 + x_1^2 (x_1')^2 + x_2^2(x_2')^2 + 2x_1 x_1' + 2x_2 x_2' + 2 x_1 x_2 x_1' x_2' = k(x,x') \checkmark


Part (b): Dimension of the feature space

For k(x,x)=(1+xx)pk(x,x') = (1 + x \cdot x')^p on Rd\mathbb{R}^d:

The feature map ϕ(x)\phi(x) consists of all monomials in the components of xx of degree 0,1,2,,p0, 1, 2, \ldots, p, each with appropriate (pα)(αα)\sqrt{\binom{p}{|\alpha|}\binom{|\alpha|}{\alpha}} coefficients.

The number of monomials x1α1xdαdx_1^{\alpha_1} \cdots x_d^{\alpha_d} with α=k|\alpha| = k is (d+k1k)\binom{d+k-1}{k}, so the total dimension is:

m=k=0p(d+k1k)=(d+pp)m = \sum_{k=0}^{p} \binom{d+k-1}{k} = \binom{d+p}{p}

For fixed pp, this grows as O(dp)O(d^p); for fixed dd, as O(pd)O(p^d).

Concretely:

  • d=2,p=2d = 2, p = 2: m=(42)=6m = \binom{4}{2} = 6 ✓ (matches part (a))
  • d=100,p=5d = 100, p = 5: m=(1055)9.6×109m = \binom{105}{5} \approx 9.6 \times 10^9 — nearly 10 billion dimensions!

Part (c): The punch line

  • Computing k(x,x)=(1+xx)pk(x, x') = (1 + x \cdot x')^p: costs O(d)O(d) — just a dot product and a power.
  • Computing ϕ(x),ϕ(x)\langle \phi(x), \phi(x') \rangle explicitly: costs O(m)=O(dp)O(m) = O(d^p), which is astronomically larger.

The kernel trick lets you implicitly operate in an mm-dimensional feature space while paying only O(d)O(d) per inner product.

More strikingly: for the RBF (Gaussian) kernel k(x,x)=exx2/(2σ2)k(x,x') = e^{-\|x-x'\|^2/(2\sigma^2)}, the induced feature space is infinite-dimensional — yet you still evaluate kk in O(d)O(d) time. You are effectively running a linear classifier in \infty-dimensional space at finite cost.

The key insight: Any algorithm (SVM, kernel regression, PCA, etc.) that only accesses data through inner products can be "kernelized" for free — the feature map ϕ\phi need never be constructed explicitly. The computational bottleneck becomes the n×nn \times n kernel matrix, not the dimension of the feature space.

Source: The Elements of Statistical Learning, Hastie, Tibshirani, Friedman (2nd ed.), Ch. 12; also standard ML folklore

Type: ML/StatsSource: The Elements of Statistical Learning, Hastie, Tibshirani, Friedman (2nd ed.), Ch. 12; also standard ML folkloreEdit on GitHub ↗