Skip to contents

The maximum mean discrepancy is the largest difference in expectation over the unit ball of a reproducing-kernel Hilbert space. Its biased empirical estimate keeps the diagonal terms,

Usage

morie_mmd_test(
  x,
  y,
  kernel = "rbf",
  B = 999L,
  cdf = NULL,
  gamma = NULL,
  unbiased = FALSE
)

Arguments

x, y

The two samples, matrices with matching column counts. A vector is read as a single column.

kernel

One of "rbf", "laplacian", "linear". A characteristic kernel (rbf, laplacian) makes MMD zero only when the distributions are equal; "linear" compares means alone and is blind to any difference that leaves the mean unchanged.

B

Number of label permutations. Default 999.

cdf

Optional function giving the null CDF of the statistic.

gamma

Kernel bandwidth. Defaults to the median heuristic on the pooled sample, 1 / median squared pairwise distance. Ignored by the linear kernel.

unbiased

Use equation (3) rather than the biased estimate.

Value

Named list with statistic, p_value, kernel, gamma, B, m, n, unbiased, null_statistics, method.

Details

$$MMD_b^2 = \frac{1}{m^2}\sum_{i,j} k(x_i,x_j) + \frac{1}{n^2}\sum_{i,j} k(y_i,y_j) - \frac{2}{mn}\sum_{i,j} k(x_i,y_j)$$

while the unbiased estimate of Gretton et al.'s equation (3) drops them, dividing the within-sample sums by \(m(m-1)\) and \(n(n-1)\) over \(i \ne j\).

The null has no simple closed form, so the p-value comes from permuting the group labels. Any bias is shared by the observed and permuted statistics and cancels in the ranking, which is why the biased form is a sound test statistic even though the unbiased one is the better estimate of \(MMD^2\).

Mirrors morie.fn.otmtest on the Python side.

References

Gretton A, Borgwardt KM, Rasch MJ, Scholkopf B & Smola A (2012). A kernel two-sample test. Journal of Machine Learning Research, 13, 723-773.

Examples

set.seed(1)
morie_mmd_test(rnorm(30), rnorm(30, 2), B = 99)$p_value
#> [1] 0.01