DEV Community

Umesh Tharuka Malaviarachchi
Umesh Tharuka Malaviarachchi

Posted on

Kernel Mean Embeddings: Turning Distributions into Points You Can Compute With

Most of machine learning is built around points. A data point is a vector, a model maps vectors to predictions, and distance between vectors tells you how similar two examples are. Distributions are harder. A distribution is not a point, it is a whole object: a function, a measure, a cloud of possible outcomes. Comparing two distributions, testing whether they differ, or conditioning one on another usually means integrals, density estimates, or parametric assumptions that may not hold.

Kernel mean embeddings offer a different move. Instead of summarising a distribution with a few numbers, embed the entire distribution as a single point in a high-dimensional function space. Then operations on distributions become operations on points: distance becomes a norm, a two-sample test becomes a distance check, and conditional distributions become linear operators. The trick is that the space is chosen so that this embedding loses no information, at least for the kernels that matter.

This article explains what a kernel mean embedding is, why it works, where it is genuinely useful, and where it breaks down.

The intuition: average the features, not the data

Start with the familiar kernel trick. A kernel function

k(x,y)k(x, y)

measures similarity between two points, and it corresponds to an inner product in some feature space:

k(x,y)=⟨ϕ(x),ϕ(y)⟩H k(x, y) = \langle \phi(x), \phi(y) \rangle_{\mathcal{H}}

Here ϕ(x)\phi(x) is the feature map and H\mathcal{H} is a reproducing kernel Hilbert space, or RKHS. You never need to write ϕ(x)\phi(x) down explicitly. The kernel does the work.

Now suppose you do not have one point, but a distribution PP . What is the natural analogue of ϕ(x)\phi(x) ? Average it:

μP=EX∼P[ϕ(X)] \mu_P = \mathbb{E}_{X \sim P}[\phi(X)]

That expectation is the kernel mean embedding of PP . It is a single element of H\mathcal{H} . Intuitively, μP\mu_P is the "centre of mass" of the distribution after it has been mapped into feature space. If the kernel is rich enough, different distributions land at different points, so the embedding is a faithful representation rather than a lossy summary like a mean and variance.

This is already useful because expectations of functions in the RKHS become inner products. For any function ff in H\mathcal{H} , the reproducing property gives:

EX∼P[f(X)]=⟨f,μP⟩H \mathbb{E}{X \sim P}[f(X)] = \langle f, \mu_P \rangle{\mathcal{H}}

So evaluating an expectation, normally an integral over PP , becomes a single inner product with μP\mu_P . That identity is the engine behind almost every application below.

In practice you do not know PP . You have samples x1,…,xnx_1, \dots, x_n . The empirical embedding is just the average of their feature maps:

μ^P=1n∑i=1nϕ(xi) \hat{\mu}P = \frac{1}{n} \sum{i=1}^{n} \phi(x_i)

You never compute ϕ\phi explicitly here either. You only need kernel evaluations between samples, because inner products between empirical embeddings expand into sums of kernel values.

When is the embedding injective? Characteristic kernels

Not every kernel gives a faithful embedding. If the kernel is too simple, two different distributions can map to the same point. The linear kernel is the extreme case: μP\mu_P then encodes only the ordinary mean of PP , so any two distributions with the same mean collide.

A kernel is called characteristic when the embedding map P↦μPP \mapsto \mu_P is injective: μP=μQ\mu_P = \mu_Q implies P=QP = Q . The Gaussian radial basis function kernel is the standard example. With a Gaussian kernel, the embedding captures all moments of the distribution in a certain sense, not just the first few. That is why Gaussian kernels dominate this literature.

This matters because it turns a hard question, "are these two distributions the same?", into a geometric question, "are these two points the same?". The distance between embeddings is called the maximum mean discrepancy, or MMD:

MMD(P,Q)=∣μP−μQ∣H \text{MMD}(P, Q) = | \mu_P - \mu_Q |_{\mathcal{H}}

Equivalently, MMD is the largest difference in expectation, over all functions ff in the unit ball of H\mathcal{H} , between PP and QQ :

MMD(P,Q)=sup⁡∣f∣H≤1(EX∼P[f(X)]−EY∼Q[f(Y)]) \text{MMD}(P, Q) = \sup_{|f|{\mathcal{H}} \leq 1} \left( \mathbb{E}{X \sim P}[f(X)] - \mathbb{E}_{Y \sim Q}[f(Y)] \right)

With a characteristic kernel, MMD is zero exactly when P=QP = Q . With samples from both distributions, the squared MMD has a simple unbiased estimator built only from kernel values within and across the two samples. That gives a two-sample test that makes no parametric assumption about the distributions and works in high dimensions, on structured objects like graphs or strings, wherever a kernel is defined.

A practical example: a two-sample test in a few lines

Suppose you have two samples and want to know whether they came from the same distribution. The MMD estimator below uses a Gaussian kernel. It is not production code, but it shows how little machinery is needed once the embedding idea is in place.

import numpy as np

def rbf_kernel(X, Y, lengthscale=1.0):
    sq = np.sum(X ** 2, axis=1)[:, None] + np.sum(Y ** 2, axis=1)[None, :] - 2 * X @ Y.T
    return np.exp(-sq / (2 * lengthscale ** 2))

def mmd_squared(X, Y, lengthscale=1.0):
    m, n = len(X), len(Y)
    Kxx = rbf_kernel(X, X, lengthscale)
    Kyy = rbf_kernel(Y, Y, lengthscale)
    Kxy = rbf_kernel(X, Y, lengthscale)
    # Exclude diagonal terms for an unbiased estimate
    term_xx = (Kxx.sum() - np.trace(Kxx)) / (m * (m - 1))
    term_yy = (Kyy.sum() - np.trace(Kyy)) / (n * (n - 1))
    term_xy = Kxy.mean()
    return term_xx + term_yy - 2 * term_xy
Enter fullscreen mode Exit fullscreen mode

A large value relative to a permutation null distribution is evidence that the two samples differ. In practice, the lengthscale choice matters a lot, which brings us to the pitfalls section. A common heuristic is the median pairwise distance between pooled samples. It is a starting point, not a principled optimum, and a badly chosen lengthscale can make MMD nearly blind to the very difference you care about.

Beyond two samples: conditioning as a linear operator

The embedding idea extends further than comparing distributions. A joint distribution can be embedded, and a conditional distribution can be represented as an operator between RKHS spaces.

The cross-covariance operator CYXC_{YX} generalises a covariance matrix to feature spaces. Roughly, it captures how features of XX covary with features of YY . The conditional mean embedding of YY given X=xX = x can then be written, with regularisation, as:

μY∣X=x=CYX(CXX+λI)−1ϕ(x) \mu_{Y|X=x} = C_{YX} (C_{XX} + \lambda I)^{-1} \phi(x)

This looks like kernel ridge regression, and that resemblance is not an accident. Conditioning, in this framework, becomes a linear operation on embeddings. That opens the door to nonparametric graphical models, kernel Bayes rules, and state-space filtering without assuming Gaussianity or a finite-dimensional state. The price is that every operator must be estimated from data and regularised, and the theory that justifies the unregularised identity does not automatically justify the finite-sample, regularised version you actually compute.

Other applications follow the same pattern. Kernel embeddings give a way to test independence, through the Hilbert-Schmidt independence criterion, which is essentially the MMD between a joint distribution and the product of its marginals. They give a way to do distribution regression, where each training example is itself a bag of samples from a distribution, by first embedding each bag and then learning a map from embeddings to labels. They also appear in causal inference, where embeddings of interventional distributions can be estimated without modelling densities directly.

Limitations and common pitfalls

Kernel mean embeddings are elegant, but elegance is not the same as free lunch. Four problems recur.

First, computation scales badly. The estimators above need kernel matrices over all samples, so naive implementations cost quadratic time and memory. For large data sets you need approximations: random Fourier features, Nyström approximations, or incomplete Cholesky factorisation. Each approximation trades the exactness that motivated the method in the first place.

Second, kernel choice is the whole game, and there is no universal default. A Gaussian kernel with the wrong lengthscale can make MMD insensitive to differences at the scale you care about, or so sensitive to noise that it rejects everything. Characteristic kernels guarantee injectivity in the population limit, but they do not guarantee good finite-sample power. Kernel selection for testing is an active research area, and it is not solved by picking the median heuristic and moving on.

Third, high dimensions hurt in a specific way. As dimension grows, pairwise distances concentrate, Gaussian kernel values collapse toward a constant, and the geometry of the embedding flattens. MMD can still be consistent, but its power at realistic sample sizes can be poor. Embeddings do not repeal the curse of dimensionality, they repackage it.

Fourth, interpretation is limited. An embedding is a point in an infinite-dimensional space you cannot visualise directly. When MMD rejects the null, it does not tell you how the distributions differ, only that they do, in the metric induced by your kernel. If you need an interpretable answer, for example which feature shifted or which subgroup changed, you need extra work on top of the embedding, such as witness functions. The witness function, proportional to μP−μQ\mu_P - \mu_Q , can show where the two distributions differ most, but reading it still requires care.

A related pitfall is confusing the empirical embedding with the population embedding. μ^P\hat{\mu}_P converges to μP\mu_P as sample size grows, but at finite samples it carries estimation error, and downstream operators built from it amplify that error. Regularisation is not optional decoration in conditional embeddings, it is what keeps the inverse stable.

The takeaway

Kernel mean embeddings turn a distribution into a point, expectations into inner products, and distributional differences into distances. With a characteristic kernel, that point loses nothing in principle, which is why the same idea powers two-sample tests, independence tests, distribution regression, and nonparametric conditioning.

The practical lesson is narrower. The method is only as good as its kernel, its regularisation, and its sample size. Choose the kernel for the scale of difference you care about, regularise operators rather than inverting them naively, and remember that a rejection from an MMD test is a statement about your kernel's geometry as much as it is about the data. Used with those caveats, embeddings are one of the cleanest bridges between probability and geometry in modern machine learning.


Connect with me on LinkedIn: linkedin.com/in/umeshtharukaofficial

Top comments (0)