Skip to content

Evaluation, diagnostics, benchmarks, and additional tests · Evaluation, diagnostics, benchmarks, and additional hypothesis tests · EVA016 · Hypothesis test

Maximum mean discrepancy two-sample test

Tests equality of distributions using a kernel-space discrepancy; this extends baseline entry 169 to learned representations and generative evaluation.

Runs in CodeSplash Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through multivariate.distribution_test. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Tests equality of distributions using a kernel-space discrepancy; this extends baseline entry 169 to learned representations and generative evaluation.

Question / hypotheses

H0: P=Q; H1: P differs from Q in a way detectable by the chosen kernel.

Architecture / computational structure

Construct within-sample and cross-sample kernel matrices and combine their off-diagonal U-statistic averages. Under pooled-sample exchangeability, reassign group membership while preserving sample sizes to generate the null distribution; reject for large squared MMD.

Assumptions

Independent exchangeable observations are required for ordinary pooled permutations. A characteristic kernel is needed for broad consistency; dependence requires an adapted calibration scheme.

Formula / computational definition

MMD^u2=∑i≠jk(xi,xj)m(m−1)+∑i≠jk(yi,yj)n(n−1)−2∑ijk(xi,yj)mn.\widehat{MMD}_u^2=\frac{\sum_{i\ne j}k(x_i,x_j)}{m(m-1)}+\frac{\sum_{i\ne j}k(y_i,y_j)}{n(n-1)}-\frac{2\sum_{ij}k(x_i,y_j)}{mn}.

Parameters and notation

m,n are group sizes; kernel bandwidth and feature extractor are hyperparameters. Fit or select these independently of the test, or include their selection in calibration.

Fitting, fine-tuning, and adaptation

No distribution parameters are fitted by the basic fixed-kernel test. Fix a representation and bandwidth using independent data or a label-invariant rule justified by the permutation design. If selecting a kernel by the observed group difference, repeat selection inside each permutation or use a valid split. Report Monte Carlo resolution as well as the statistic.

How to use and calibrate

Freeze the representation, compute the statistic, permute group labels under exchangeability, and report an effect-size estimate with the p-value. Keep synthetic-generation settings fixed.

Example application

Test whether deployment embeddings differ from an independent reference sample.

Interpretation / pitfalls

Watch out

Failure to reject does not establish equivalence. A representation can hide important differences. The unbiased squared estimate can be negative; using a test to tune generators repeatedly causes adaptive leakage.

Python and R code examples

Scope: Mathematical calculation on synthetic inputs; not a complete benchmark pipeline.

Validation: Python — Executed successfully on synthetic inputs. R — reviewed but not executed in this environment. Python dependencies are explicit imports; R uses base R unless a package/bridge is explicitly named.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Mathematical calculation on synthetic inputs; not a complete benchmark pipeline. · needs numpy, sklearn

import numpy as np
from sklearn.metrics.pairwise import rbf_kernel
x=np.array([[0.],[1.],[2.]]); y=np.array([[2.],[3.],[4.]])
K=rbf_kernel(x,gamma=.5); L=rbf_kernel(y,gamma=.5); C=rbf_kernel(x,y,gamma=.5)
print((K.sum()-np.trace(K))/6+(L.sum()-np.trace(L))/6-2*C.mean())
R

Complete calculation — Mathematical calculation on synthetic inputs; not a complete benchmark pipeline.

x <- c(0,1,2); y <- c(2,3,4); ker <- function(a,b) exp(-.5*outer(a,b,"-")^2)
K <- ker(x,x); L <- ker(y,y); C <- ker(x,y)
print((sum(K)-sum(diag(K)))/6+(sum(L)-sum(diag(L)))/6-2*mean(C))

Sources

From the CodeSplash reference, entry EVA016.