[KongchangAI]
· 2 min read· 1,037 words

Sub-quadratic Time Kernel Two-sample Testing: A Fast Approximation Method for MMD

Sub-quadratic Time Kernel Two-sample Testing: A Fast Approximation Method for MMD

A new method uses Fenwick trees to achieve sub-quadratic time MMD testing while preserving minimax-optimal power.

This article presents a sub-quadratic time approximation method for kernel two-sample testing that addresses the quadratic complexity of the classic Maximum Mean Discrepancy (MMD) statistic caused by computing all pairwise kernel values. The core idea is to transform kernel sums into Fenwick tree prefix-sum queries, combined with fast kernel density estimation and the exponentially convergent trapezoidal rule, achieving sub-quadratic time complexity for product kernels (such as Laplace, Matérn, Gaussian, and inverse multiquadric kernels) in fixed dimensions. Theoretical analysis shows that the approximation error is tightly controlled, making the test power identical to the minimax-optimal exact MMD test, with minimal distributional assumptions. Sub-quadratic complexity is maintained when the dimension d=o(log N/log log N), and the method can be combined with dimensionality reduction. This work provides an efficient and statistically optimal solution for kernel two-sample testing on large-scale data.

Sub-quadratic Time Kernel Two-sample Testing: A Fast Approximation Method for MMD

Kernel two-sample testing is an important tool in statistical learning, but the classic Maximum Mean Discrepancy (MMD) statistic requires computing kernel function values for all sample pairs, resulting in quadratic time complexity that is difficult to handle for large-scale data. A recent paper proposes a sub-quadratic time approximation method that significantly reduces computational overhead while maintaining minimax-optimal test power.

Problem Background: The Computational Bottleneck of MMD Testing

MMD testing determines whether two samples come from the same distribution by comparing their mean embeddings in a reproducing kernel Hilbert space. Computing the standard MMD statistic requires evaluating pairwise kernel function values for all $N$ combined samples, with complexity $\Theta(N^2)$. As sample size grows, this computational cost becomes prohibitive.

Some existing sub-quadratic time approximate MMD methods attempt to alleviate this problem, but they either sacrifice test power or require additional distributional assumptions to achieve power comparable to quadratic-time MMD. This leaves a gap: can we achieve the same minimax-optimal power as the exact MMD test in sub-quadratic time?

MMD test illustration

Reproducing Kernel Hilbert Space (RKHS) is the theoretical foundation of MMD testing. Given a kernel function $k(\cdot,\cdot)$, one can define a Hilbert space whose elements are functions satisfying the reproducing property: $\langle f, k(x,\cdot) \rangle_{\mathcal{H}} = f(x)$. For a probability distribution $P$, its mean embedding in the RKHS is defined as $\mu_P = \mathbb{E}{X\sim P}[k(X,\cdot)]$. The core idea of MMD testing is that two distributions are identical if and only if their mean embeddings are equal, i.e., $\text{MMD}(P,Q) = |\mu_P - \mu_Q|{\mathcal{H}} = 0$. Therefore, by empirically estimating $|\mu_P - \mu_Q|_{\mathcal{H}}$, one can construct a two-sample test statistic. The test power of MMD depends on the choice of kernel: when the kernel is characteristic (such as the Gaussian kernel), MMD can detect all types of distributional differences. This property makes MMD a powerful tool for nonparametric two-sample testing, but it also requires computing kernel values for all sample pairs, leading to quadratic complexity.

Method Core: Fast Kernel Density Estimation and Fenwick Trees

The proposed method draws on fast kernel density estimation and the exponentially convergent trapezoidal rule from numerical analysis. Its core idea is to transform the kernel sum over all sample pairs into fast Fenwick tree prefix-sum queries. For product kernels (such as Laplace, Matérn, Gaussian, and inverse multiquadric kernels) on $\mathbb{R}^d$, the method achieves a sub-quadratic time algorithm.

Specifically, by expanding the kernel function into exponential form, the trapezoidal rule can be used to efficiently approximate integrals, while Fenwick trees are used to quickly compute prefix sums, thereby avoiding explicit traversal of all sample pairs. This approximation error is tightly controlled, so that the final test power is the same as that of the minimax-optimal quadratic-time unapproximated MMD test.

Fenwick tree (Binary Indexed Tree) is a data structure that supports efficient prefix-sum queries and point updates. For an array of length $N$, a Fenwick tree can perform prefix-sum queries and element updates in $O(\log N)$ time, with space complexity of only $O(N)$. In MMD testing, one needs to compute all-pairs kernel sums of the form $\sum_{i,j} k(x_i, y_j)$. By expanding the kernel function into a linear combination of exponential functions (for example, the Gaussian kernel can be expressed as a product form of $e^{-|x-y|^2/(2\sigma^2)}$), the sum can be transformed into a combination of multiple one-dimensional prefix sums. Specifically, for a product kernel $k(x,y)=\prod_{r=1}^d \phi_r(x_r, y_r)$, the all-pairs sum can be decomposed into a product of one-dimensional sums across dimensions. Using Fenwick trees, each one-dimensional prefix sum can be computed in $O(d \log N)$ time, thereby reducing the total complexity to sub-quadratic. The key to this technique is the decomposability of the kernel function, which allows high-dimensional sums to be efficiently computed through combinations of low-dimensional data structures.

Theoretical Guarantees and Applicability Conditions

The method has sub-quadratic time complexity in fixed dimensions. More generally, when the dimension $d = o(\log N / \log\log N)$, the same implementation still maintains sub-quadratic complexity. This means the method can be directly applied to medium-to-low-dimensional data. When the signal is low-dimensional, it can also be combined with dimensionality reduction algorithms to further extend its applicability.

Controlling the approximation error is key: the authors prove that the error between the approximated statistic and the exact MMD statistic is sufficiently small and does not affect the minimax optimality of the test. This resolves the power compromise issue of existing approximation methods.

Algorithm flow illustration

Minimax-optimal test power is a theoretical benchmark in statistical hypothesis testing. In two-sample testing, minimax optimality means that for a given class of alternative distributions, the worst-case power decay rate of the test method reaches the information-theoretic lower bound. Specifically, for MMD testing, when the strength of the difference between two distributions is $\epsilon$, the minimum sample size required for the minimax-optimal test to detect the difference with high probability satisfies $N \asymp \epsilon^{-4}$ (for smooth kernels). The paper proves that its sub-quadratic time approximation method preserves this optimal rate, i.e., the error introduced by approximation does not lead to an increase in the required sample size. This is much stronger than merely guaranteeing consistency (i.e., power tends to 1 as sample size tends to infinity), because many approximation methods, while consistent, require more samples to achieve the same power. Minimax optimality guarantees that at the same sample size, the approximate test has comparable detection capability to the exact MMD test, making it attractive both theoretically and practically.

Significance and Outlook

The contribution of this work lies in providing an MMD approximation method that achieves minimax-optimal power in sub-quadratic time, with minimal distributional assumptions. For kernel two-sample testing applications that need to handle large-scale data, such as generative model evaluation and two-sample homogeneity testing, this method is expected to significantly improve computational efficiency.

Future directions may include extending to more general kernel functions, optimizing constant factors in practical implementations, and exploring approximation performance in high-dimensional data. Although the paper primarily focuses on theoretical guarantees, its algorithmic ideas offer new possibilities for practical applications.

Paper title: Minimax-Optimal Kernel Two-sample Testing in Sub-quadratic Time

References:

  • Original paper: Minimax-Optimal Kernel Two-sample Testing in Sub-quadratic Time
  • Related background: MMD testing by Gretton et al. (2012)
#kernel two-sample testing#MMD#sub-quadratic time#Fenwick tree#minimax-optimal#kernel density estimation#maximum mean discrepancy
Share:

Related articles