Separate the all-sample PSD condition for a valid kernel from Mercer's spectral expansion. Exact Gram matrices show why positive entries and one successful sample are insufficient.
Mercer's theorem and valid kernels
Mercer's theorem represents a continuous positive semidefinite kernel on a compact domain through eigenfunctions of an associated integral operator. It explains how a similarity rule can encode feature coordinates. For a quant using a custom similarity in a kernel method, the immediate safeguard is more basic: every finite kernel matrix must be positive semidefinite. A plausible-looking similarity score need not satisfy that requirement.
Validity asks about every finite sample
For real-valued inputs from a set X, a symmetric function k:X×X→R is a positive semidefinite kernel when
i=1∑nj=1∑ncicjk(xi,xj)≥0
for every sample size n, every choice of inputs x1,…,xn and every real coefficient vector c. Equivalently, every Gram matrix Kij=k(xi,xj) is positive semidefinite (PSD). Here “positive” concerns quadratic forms, not individual entries. Some kernel literature calls this a positive definite kernel even though zero quadratic forms are permitted. Rasmussen and Williams, §4.1
A feature representation k(x,z)=⟨ϕ(x),ϕ(z)⟩ proves the condition directly:
c⊤Kc=i∑ciϕ(xi)2≥0.
Conversely, the Moore–Aronszajn theorem gives every such kernel a unique reproducing kernel Hilbert space (RKHS), with canonical feature map ϕ(x)=k(⋅,x). This existence result needs no compact domain or continuity assumption. The finite-Gram criterion is often loosely called “Mercer's condition” in machine learning; Mercer’s spectral expansion adds regularity assumptions. Sejdinovic and Gretton, Theorem 42 and §6
Two positive-looking matrices, different answers
Take the linear kernel k(x,z)=xz on the real line and sample inputs 1 and 2. Its Gram matrix is
A=(1224).
Its eigenvalues are 0 and 5. More decisively, for arbitrary coefficients a,b,
(ab)A(a\b)=(a+2b)2≥0.
The zero eigenvalue is allowed: PSD does not require invertibility. The kernel is globally valid because its feature map is simply ϕ(x)=x, not because this particular sample passed.
Now suppose a proposed similarity assigns the matrix
B=(1221)
to two inputs. All entries are positive and the matrix is symmetric, yet its eigenvalues are −1 and 3. With v=(1,−1)⊤,
v⊤Bv=1−2−2+1=−2.
That single counterexample rules out the proposed rule as a PSD kernel on any domain containing those inputs. If used as a covariance matrix, it would assign a negative variance to their difference.
Two exact symmetric matrices: A has rows 1,2 and 2,4 with eigenvalues 0 and 5; B has rows 1,2 and 2,1 with eigenvalues minus 1 and 3. Unit-direction quadratic forms stay nonnegative for A but reach minus 1 for B at 135 degrees. The unnormalized contrast vector 1,minus 1 gives minus 2 for B.
Original exact example. The curves evaluate each quadratic form on unit-length vectors, so their extrema equal the matrix eigenvalues. The raw vector (1, −1) has squared length 2, explaining its value −2 rather than −1.
A successful Gram check certifies that matrix alone. Global validity requires a proof covering all finite choices, for example an explicit feature map. An eigensolver is useful for finding counterexamples; numerical tolerance should distinguish rounding near zero from a materially negative eigenvalue.
What Mercer adds
One standard real-valued version assumes a continuous PSD kernel k on a compact metric space X, and a finite Borel measure μ with full support: every nonempty open subset has positive measure. Define
(Tkf)(x)=∫Xk(x,z)f(z)dμ(z).
This is a compact, positive, self-adjoint operator on L2(X,μ). For its positive eigenvalues λj and corresponding continuous eigenfunctions ej, orthonormal in L2(X,μ), Mercer gives
k(x,z)=j∈J∑λjej(x)ej(z).
The index set is at most countable; convergence is uniform on X×X and absolute for each pair. Hence ϕj(x)=λjej(x) supplies feature coordinates. The sum may be finite. These conclusions use the stated assumptions; an arbitrary PSD kernel does not automatically have this particular continuous spectral representation. Sejdinovic and Gretton, §§6.1–6.2, Theorem 48
For a separate continuous example, restrict k(x,z)=xz to X=[0,1] and use Lebesgue measure. This domain differs from the earlier real-line sample, whose second input was 2. There is just one positive operator eigenvalue:
λ1=31,e1(x)=3x,∫01e1(x)2dx=1.
Indeed, Tke1=e1/3 and (1/3)e1(x)e1(z)=xz. The operator eigenvalue 1/3 belongs to a domain-and-measure problem; the earlier Gram eigenvalue 5 belongs to a two-observation matrix. They are different objects.