Skip to content
Edit this page View source of this page

optora.divergences.mmd

Kernel maximum mean discrepancy between distributions on a finite support.

MMDDivergence

Bases: Divergence

Maximum mean discrepancy of a candidate distribution from a reference.

For distributions p and q on a shared finite support \(\{x_1, \dots, x_n\}\) and a positive semidefinite kernel \(k\) with Gram matrix \(K_{ij} = k(x_i, x_j)\), the maximum mean discrepancy is the distance between their kernel mean embeddings in the reproducing kernel Hilbert space \(\mathcal{H}\) of \(k\) (Gretton et al. 2012),

\[ \mathrm{MMD}_k(p, q) = \Big\lVert \sum_i (p_i - q_i)\, k(x_i, \cdot) \Big\rVert_{\mathcal{H}} = \sqrt{(p - q)^\top K (p - q)}. \]

It is symmetric and satisfies the triangle inequality, and it is a metric on distributions over the support whenever \(K\) is positive definite, as it is for a characteristic kernel such as the Laplacian \(e^{-\lVert x - y \rVert}\) on distinct points. Unlike a \(\varphi\)-divergence it stays finite between distributions with different supports. optora.dro.MMDAmbiguitySet bounds it by its radius.

Attributes:

Name Type Description
gram Tensor

Symmetric positive semidefinite Gram matrix of shape (n, n).

__init__(gram, validate=False)

Initialize the maximum mean discrepancy.

Parameters:

Name Type Description Default
gram Tensor

Symmetric positive semidefinite Gram matrix of shape (n, n) of the kernel on the support points.

required
validate bool

Whether to check that gram is finite, symmetric, and positive semidefinite up to rounding. The check computes the eigenvalues of gram and reads them on the host, so it is opt-in.

False

Raises:

Type Description
ValueError

If gram is not a square 2D tensor, or validate is set and gram is not a finite symmetric positive semidefinite matrix.

forward(p, q)

Compute the maximum mean discrepancy between p and q.

Parameters:

Name Type Description Default
p Tensor

Candidate distribution of shape (..., n).

required
q Tensor

Reference distribution of shape (..., n), broadcasting against p.

required

Returns:

Type Description
Tensor

\(\mathrm{MMD}_k(p, q)\) over the broadcast batch, computed in

Tensor

optora.core.working_dtype(p, q, gram) and returned in the

Tensor

promoted dtype of p and q. The quadratic form is clamped at

Tensor

zero before the square root, so rounding near p == q never

Tensor

produces nan.

Raises:

Type Description
ValueError

If the last dimension of p or q does not match gram.