fenwick-mmd 0.2.0

Formally verified, sub-quadratic minimax-optimal kernel two-sample testing (MMD) via compressed multidimensional Fenwick trees and orthantwise separable kernels
Documentation

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$.
  • 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.2.0)

Exact $O(N^2)$ quadratic MMD vs. fenwick-mmd multi-channel compressed Fenwick sweep:

Kernel Dim $n = m$ ($N$) Exact $O(N^2)$ fenwick-mmd Speedup Abs Error
Laplace 1 5,000 (10,000) 1205.16 ms 7.84 ms 153.6x 4.40e-14
Laplace 2 2,000 (4,000) 350.78 ms 7.35 ms 47.7x 3.33e-16
Laplace 2 5,000 (10,000) 2366.13 ms 39.18 ms 60.4x 4.66e-15
Matérn 3/2 2 2,000 (4,000) 381.88 ms 19.25 ms 19.8x 1.67e-14
Matérn 5/2 2 2,000 (4,000) 437.40 ms 19.46 ms 22.5x 1.14e-14
Gaussian (1e-2) 2 2,000 (4,000) 360.98 ms 64.00 ms 5.6x 1.51e-6
Gaussian (1e-4) 2 2,000 (4,000) 362.74 ms 202.08 ms 1.8x 3.52e-9

Full $B = 99$ permutation tests at $n = m = 2000$ ($N = 4000$, 2D): 255.2 ms (Laplace), 373.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 fenwick_mmd::{permutation_test, ProductKernel, median_bandwidths};

let bws = median_bandwidths(&x, &y, 512)?;
let kernel = ProductKernel::laplace(&bws)?;
let res = permutation_test(&x, &y, &kernel, 199, 0.05, 42)?;
println!("mmd^2={:.6e}, p={:.4}, reject={}", res.mmd_squared, res.p_value, res.reject);

Python

import fenwick_mmd as fm

res = fm.permutation_test(x, y, kernel="laplace", num_permutations=199, alpha=0.05, seed=42)
print(res.mmd_squared, res.p_value, res.reject)

# High-dimensional embeddings / OOD drift gating via sample-splitting PCA-MMD:
pca_res = fm.pca_mmd_test(x_emb, y_emb, proj_dim=2, kernel="laplace", num_permutations=199)

License

Dual-licensed under MIT or Apache-2.0.