fenwick-mmd
Formally verified, CPU-native, sub-quadratic minimax-optimal kernel two-sample testing (MMD) in safe Rust (#![forbid(unsafe_code)]) with full Python (PyO3) parity.
Implements the compressed multidimensional Fenwick tree algorithm, orthantwise separable (OS) and almost orthantwise separable (AOS) product kernels, finite-sample level-$\alpha$ permutation testing, multi-bandwidth aggregation, and sample-splitting PCA-MMD from:
Ikjun Choi, Shourya Pandey, & Purnamrita Sarkar (2026). "Minimax-Optimal Kernel Two-sample Testing in Sub-quadratic Time." arXiv:2612.05470.
Features
- Exact sub-quadratic MMD ($O(2{d-1} N \log{(d-1) \vee 1} N)$ time, $O(2{d-1} N \log{(d-2) \vee 0} N)$ memory) for Laplace and half-integer Matérn ($\nu = 3/2, 5/2$) product kernels using compressed multidimensional Fenwick trees and auxiliary rank flips.
- Near-exact sub-quadratic MMD for Gaussian product kernels via the exponentially convergent trapezoidal rule (Lemma 2 & Lemma 3 of arXiv:2612.05470) with explicit uniform error bound $\delta$.
- Direct dense-rank 2D Fenwick fast path & deterministic parallel permutations (
v0.3.0): In 2D (dim == 2, including 2D PCA-MMD), indexes directly into flat dense-rank Fenwick arrays with zero binary searches, and parallelizes permutation sweeps across all CPU cores viastd::thread::scope(zero external dependencies, bit-for-bit identical to single-threaded execution). - Multi-channel compressed Fenwick trees (
v0.2.0): For multi-term kernels (Matérn 3/2, Matérn 5/2, Gaussian), all $R$ orthantwise separable channels are stored contiguously per Fenwick node and updated/queried in a single tree traversal, with 1D factor evaluations precomputed once per point (inspired by the multi-channel topology design note in Choi, Pandey, & Sarkar 2026). - Zero-allocation permutation reuse: Pre-sorts the pooled sample $\mathbf{Z} = \mathbf{X} \cup \mathbf{Y}$ once, computes $K_{ZZ}$ once, and resets the pre-built compressed Fenwick trees in-place across all $B$ permutations.
- High-dimensional & adaptive extensions: Sample-splitting PCA-MMD (Algorithm 7) for latent low-dimensional signals and multi-bandwidth aggregated MMD for unknown smoothness.
- Zero unsafe Rust, zero external math/BLAS dependencies, full Python parity.
Benchmarks (CPU, Release Build, v0.3.0)
Exact $O(N^2)$ quadratic MMD vs. fenwick-mmd:
| Kernel | Dim | $n = m$ ($N$) | Exact $O(N^2)$ | fenwick-mmd |
Speedup | Abs Error |
|---|---|---|---|---|---|---|
| Laplace | 1 | 5,000 (10,000) | 333.81 ms | 1.74 ms | 191.8x | 4.40e-14 |
| Laplace | 2 | 2,000 (4,000) | 121.04 ms | 2.33 ms | 51.9x | 3.33e-16 |
| Laplace | 2 | 5,000 (10,000) | 632.42 ms | 4.57 ms | 138.5x | 4.66e-15 |
| Matérn 3/2 | 2 | 2,000 (4,000) | 98.86 ms | 1.85 ms | 53.4x | 1.67e-14 |
| Matérn 5/2 | 2 | 2,000 (4,000) | 102.04 ms | 2.58 ms | 39.6x | 1.14e-14 |
Gaussian (1e-2) |
2 | 2,000 (4,000) | 96.86 ms | 11.29 ms | 8.6x | 1.51e-6 |
Gaussian (1e-4) |
2 | 2,000 (4,000) | 95.25 ms | 34.82 ms | 2.7x | 3.52e-9 |
Full $B = 99$ permutation tests at $n = m = 2000$ ($N = 4000$, 2D, deterministic parallel): 8.6 ms (Laplace), 14.8 ms (Matérn 3/2).
Formal Verification (spec/invariants.dfy)
Core theorems and invariants verified in Dafny 4 (zero assume, zero {:axiom}, zero {:verify false}):
- Exact pooled lower-triangular decomposition ($K_{ZZ} = K_{XX} + K_{YY} + K_{XY}$ and exact U-statistic identity).
- Adjacent-transposition permutation invariance of $K_{ZZ}$ (justifying the dimension-saving sort along coordinate 1).
- Exact auxiliary rank-flip equivalence converting suffix sums into single prefix queries without inclusion-exclusion.
- 1D Fenwick tree (
Lowbit,FenwickQuery,FenwickUpdateRec) exactness and invariant preservation. - Exact equivalence of the $O(N)$ sorted 1D sweep to the $O(N^2)$ lower-triangular sum.
- Theorem 1 (AOS product kernel uniform error bound $d (U + \delta_0)^{d-1} \delta_{\max}$).
- Lemma 1 ($4\delta$ uniform perturbation bound on $\widehat{\mathrm{MMD}}^2$ and exact $8\delta$ permutation-test decision sandwich).
- Exact algebraic separability of Matérn $\nu = 3/2$ and $\nu = 5/2$.
Refinement boundary: Dafny proofs hold over exact mathematical reals (real) and integers (int); the Rust implementation executes in IEEE-754 f64 with libm transcendentals and a deterministic seeded PRNG (Xoshiro256**).
Quickstart
Rust
use ;
let bws = median_bandwidths?;
let kernel = laplace?;
let res = permutation_test?;
println!;
Python
=
# High-dimensional embeddings / OOD drift gating via sample-splitting PCA-MMD:
=
License
Dual-licensed under MIT or Apache-2.0.