PCA finds flat structure. This chapter shows how to make curved structure flat, and then how to do so without paying for it. The second half of that sentence is the kernel trick, one of the most reusable ideas in classical machine learning; it will return in regression and in support vector machines.
Curved data, made linear
Take points that lie on a circle of radius centred at . Every point satisfies
PCA cannot see this. After centring, every direction through a circle has about the same spread, so PCA reports two roughly equal eigenvalues and concludes that both dimensions are needed. Yet a circle is a one-dimensional curve: one number (an angle) fixes a point on it.
Expand the equation:
It is not linear in , but it is linear in the list of terms . So map each point to
and define the fixed vector . Then every data point satisfies
In the six-dimensional feature space, all the mapped points are perpendicular to : they lie in a linear subspace. Curved structure in the original space has become flat structure in the feature space, which is exactly what PCA can find.
So the plan is: choose a map that contains the kinds of non-linear terms we expect, map every point, and run PCA in the feature space. Since the feature space has more dimensions than we have points, use the route from the last chapter.
The cost of the feature space
How big does the feature space get? All terms (monomials) of degree up to in variables number
which grows roughly like . Four features with all terms up to degree 3 give coordinates, which is fine. A hundred features up to degree 3 give . A thousand features up to degree 2 give over half a million. Computing quickly becomes impractical, and for some useful maps the feature space is infinite-dimensional, so it cannot be computed at all.
Needing only dot products
Recall the observation that ended the last module: the version of PCA needs only the dot products between points. In the feature space those are
The coordinates of are never used individually. So the question becomes: can we compute without computing ?
A function that computes a dot product in disguise
Take two points in the plane, and , and evaluate
Multiply it out:
Each term is a product of something that depends only on and the matching thing for . Collect them:
So one dot product in two dimensions, plus one, squared, gives exactly the dot product of the six-dimensional feature vectors, and that contains every term up to degree two (the factors just weight them). We computed a six-dimensional dot product without building a single six-dimensional vector.
Kernels
A function is a kernel if there is some map , to some feature space, with
Two families do most of the work in practice.
- Polynomial kernel: . Its feature space holds every monomial up to degree (weighted by binomial coefficients), dimensions in all, yet each evaluation costs one -dimensional dot product.
- Radial basis function (RBF), or Gaussian, kernel: . Its feature space is infinite-dimensional: expanding the exponential as a power series produces terms of every degree, with decreasing weights. It measures similarity by distance: nearly 1 for close points, nearly 0 for far ones, with setting what counts as close.
Which functions are kernels?
Not every function of two points is a dot product somewhere. One way to prove a function is a kernel is to exhibit , as we did above. When that is hard, Mercer's theorem gives a test. Informally, is a valid kernel if and only if:
- it is symmetric: ; and
- for every finite set of points , the matrix is positive semi-definite (all its eigenvalues are ).
The necessity of both conditions is easy to see. Dot products are symmetric, so must be. And if , then for the matrix of mapped points, whose non-zero eigenvalues are those of the (scaled) covariance matrix in the feature space, which are variances and hence non-negative. The deep part of the theorem is that these conditions are also sufficient: any symmetric function whose matrices are always positive semi-definite is a dot product in some feature space.
Mercer's condition also gives a quick way to disprove a candidate. If you can find a single small set of points whose kernel matrix has a negative eigenvalue, the function is not a kernel.