Minimax-Optimal Kernel Two-sample Testing in Sub-quadratic Time

Ikjun Choi, Shourya Pandey, Purnamrita Sarkar

TL;DR

Maximum Mean Discrepancy (MMD) is a powerful tool for comparing two datasets, but its standard calculation is painfully slow ($O(N^2)$). This paper introduces a new algorithm that uses Fenwick trees and numerical approximation to compute MMD in sub-quadratic time while proving that it retains the same statistical "punch" (minimax optimality) as the full, slow version.

What Problem Does This Paper Address?

In machine learning and statistics, we often need to ask: "Do these two sets of data come from the same distribution?" This is the two-sample testing problem.

The Maximum Mean Discrepancy (MMD) is the "gold standard" for this task because it doesn't require you to estimate the density of the data and works for complex, high-dimensional distributions. However, MMD has a massive catch: to calculate it exactly, you have to compare every point in the first dataset to every point in the second. If you have $N$ total samples, this takes $O(N^2)$ time.

Researchers have tried to fix this with "Linear MMD" (fast but weak) or "Block MMD" (a middle ground), but these methods usually sacrifice statistical power—the ability of the test to actually detect a difference when one exists. The authors of this paper set out to find a way to get the speed of an approximation with the mathematical guarantees of the full $O(N^2)$ test.

Methods

The authors bridge the gap between computational efficiency and statistical power using three clever ideas:

  1. Fenwick Tree Prefix Sums: The core insight is that MMD is essentially a sum of kernel evaluations. For certain "separable" kernels (like the Laplace kernel), this sum can be reframed as a multidimensional prefix-sum problem. By using Fenwick trees (a data structure usually used for efficient array updates and sum queries), they can compute these sums in $O(N \log^d N)$ time instead of $O(N^2)$.
  2. Almost Separable Kernels: Most popular kernels (like Gaussian or Matérn) aren't perfectly separable. The authors use the exponentially convergent trapezoidal rule from numerical analysis to approximate these kernels as a sum of a few separable ones. They prove that the error from this approximation is so small that it doesn't affect the final statistical result.
  3. PCA-MMD for High Dimensions: Since the Fenwick tree approach still scales poorly as the dimension $d$ grows (it's exponential in $d$), the authors propose a hybrid approach. For high-dimensional data where the "signal" lives in a lower-dimensional space, they use Principal Component Analysis (PCA) to project the data down before running their fast MMD test.

Datasets

The authors evaluated their method using:

  • Synthetic Benchmarks:
    • Gaussian Mean Shift (GMS): A simple test where one distribution is slightly shifted from the other.
  • Perturbed Uniform (PU): A much harder test involving high-frequency "bumps" that are difficult for linear-time tests to detect.
  • MNIST: The classic handwritten digits dataset. They used it to test if the algorithm could distinguish between the distribution of "even" digits and "odd" digits after reducing the dimensionality with PCA.

Key Insights and Ideas

  • The "Aha" Moment: You don't need a perfect calculation of MMD to get a perfect statistical test. As long as your approximation error decays faster than the statistical "noise" of the test, you achieve minimax optimality—the best possible performance any test could achieve.
  • Breaking the $O(N^2)$ Barrier: By treating kernel summation as a geometric query problem, the authors move MMD from the realm of "all-pairs comparisons" to "efficient data structure lookups."
  • Dimension Matters: The paper provides a rigorous analysis of the "growing dimension" regime. They show that their test remains sub-quadratic and optimal as long as the dimension $d$ doesn't grow too fast relative to the number of samples $N$ ($d = o(\log N / \log \log N)$).

Key Results

  • Power Retention: In experiments, the "Fenwick MMD" curves almost perfectly overlap with the "Full MMD" curves. This means they achieved the speedup without losing any ability to detect differences between distributions.
  • Speedup: For a dataset of $N=8000$ samples, the Fenwick-based Laplace kernel test was orders of magnitude faster than the full $O(N^2)$ version while being significantly more powerful than Linear or Block MMD.
  • PCA Effectiveness: On MNIST, the PCA-MMD approach successfully distinguished even/odd digits with high power, showing that the method is practical for real-world high-dimensional data when paired with dimensionality reduction.

Limitations and Future Work

  • The Curse of Dimensionality: While the algorithm is sub-quadratic for small $d$, the complexity includes a $(2 \log N)^d$ term. This means for very high-dimensional data without a low-dimensional latent structure, the method still becomes slow.
  • Offline Processing: The current implementation requires knowing the "ranks" of all data points, which means it's designed for offline computation rather than streaming data.
  • Future Directions: The authors suggest expanding the framework to include more types of kernels and exploring how this "fast summation" logic could be applied to other kernel-based methods like HSIC (Hilbert-Schmidt Independence Criterion).

Summary updated

BibTeX
@inproceedings{choi2026minimax,
  author = {Ikjun Choi and Shourya Pandey and Purnamrita Sarkar},
  title = {{Minimax-Optimal Kernel Two-sample Testing in Sub-quadratic Time}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=pe7A1xPNan},
}
Figure