Mercer's Condition and Valid Kernels
Not every function you can write down that takes two inputs and returns a number is a valid kernel — Mercer's condition is the exact mathematical test for whether a similarity function secretly corresponds to a dot product in some higher-dimensional feature space.
Prerequisites: The Kernel Trick and Kernel Methods, Vector Spaces and Bases
The kernel trick lets an SVM or kernel ridge regression operate as if it had mapped every data point into some enormous, possibly infinite-dimensional feature space and taken dot products there — without ever computing that mapping, just by plugging a kernel function directly into the algorithm wherever a dot product would go. That substitution is only mathematically valid if really does correspond to some dot product in some feature space, for some mapping . Not every two-input function has that property. Mercer's condition is the precise test for which functions qualify as legitimate kernels, and which are just similarity scores that happen to look kernel-shaped.
The analogy: a shortcut that only works for real distances
Suppose someone claims "I can compute the straight-line distance between any two cities without ever drawing a map, using this formula." That claim is only trustworthy if the formula genuinely corresponds to some consistent map — if it violates basic distance properties (like giving a city zero distance from a different city), no map could produce those numbers, and the "shortcut" is made up. Mercer's condition plays the same role for kernels: it tests whether genuinely corresponds to "dot product after some feature mapping ," or is just an arbitrary similarity function with no consistent geometric picture behind it — in which case algorithms relying on the kernel trick can silently break.
The condition
A symmetric function satisfies Mercer's condition if, for every finite set of points , the Gram matrix with entries is positive semi-definite — meaning for every real vector ,
Plain English: no matter which points you pick and how you weight them, this particular weighted double-sum of kernel values can never come out negative. If that holds for every possible finite set of points, Mercer's theorem guarantees a feature mapping exists (possibly infinite-dimensional) with — the kernel trick is then rigorously justified, and algorithms like the SVM dual and kernel ridge regression are guaranteed to have a well-posed (typically convex) optimization problem.
Worked example 1: verifying the linear kernel
Take (ordinary dot product) — obviously valid, since it's already literally a dot product with . Check with two points and weights : Gram matrix . The sum is . Trying other weights always gives a non-negative result here because this Gram matrix is for , which is positive semi-definite by construction — any dot-product kernel automatically passes.
Worked example 2: a function that fails Mercer's condition
Consider (negative squared distance) as a candidate "similarity." Take one point , weight : the sum is just , fine so far. But take two points with weights : . Sum . The condition fails — negative squared distance is not a valid kernel, because no feature mapping can produce dot products that go negative for a point against itself and combine this way; plugging it into an SVM's dual optimization would hand the solver a non-convex problem with no guarantee of a sensible solution.
A positive semi-definite matrix corresponds to a transformation that never flips space inside-out — watch how the eigenvalues here stay non-negative for a valid transform; a Gram matrix with a negative eigenvalue is the algebraic signature of a kernel that fails Mercer's condition.
What this means in practice
Every kernel used routinely in practice — linear, polynomial , RBF — is chosen because it's known to satisfy Mercer's condition, which is why practitioners rarely derive the condition from scratch: they pick from this pre-verified library, or combine existing valid kernels (sums and products of valid kernels are themselves valid). The condition matters most when someone invents a custom "similarity function" for a domain-specific application and plugs it directly into an SVM or Gaussian process — without checking Mercer's condition, the resulting optimization may not even be convex.
A function qualifies as a valid kernel — safely usable anywhere the kernel trick is used — only if its Gram matrix is positive semi-definite for every possible finite set of input points; this is Mercer's condition, and it's what guarantees the kernel secretly corresponds to a genuine dot product in some feature space, keeping kernel-based optimization problems well-posed.
It's a common and costly mistake to build a custom "domain similarity score" — clamped, weighted, or otherwise hand-engineered to feel intuitively reasonable — and plug it straight into an SVM or Gaussian process as if any symmetric function works. Symmetry alone is not sufficient; a function can be perfectly symmetric and still fail positive semi-definiteness, as the negative-squared-distance example shows. Using an invalid kernel doesn't always throw an error — it can silently produce a non-convex dual problem that solvers still return an answer for, just not one with any of the guarantees (uniqueness, global optimality) that make kernel methods trustworthy in the first place.
Related concepts
Practice in interviews
Further reading
- Mercer, Functions of Positive and Negative Type (1909)
- Schölkopf & Smola, Learning with Kernels (2002)