Inferensys

Glossary

Maximum Mean Discrepancy (MMD)

A kernel-based statistical test used as a loss function in deep learning to measure the distance between two probability distributions, enabling the alignment of latent feature distributions from different batches.
Data engineer managing feature store on laptop, feature definitions visible, casual data engineering session.
DISTRIBUTIONAL DISTANCE METRIC

What is Maximum Mean Discrepancy (MMD)?

A kernel-based statistical measure quantifying the distance between two probability distributions by comparing their mean embeddings in a reproducing kernel Hilbert space (RKHS).

Maximum Mean Discrepancy (MMD) is a non-parametric test statistic that measures the distance between two distributions, P and Q, by computing the squared difference between their mean embeddings in a high-dimensional reproducing kernel Hilbert space. It answers the question: are these two samples drawn from the same distribution? An MMD value of zero occurs if and only if the two distributions are identical, making it a powerful two-sample test.

In batch effect normalization, MMD serves as a domain-invariance loss function within deep learning architectures like domain-adversarial neural networks (DANNs). By minimizing the MMD between latent feature representations from different experimental batches, the model learns a shared embedding space where technical artifacts are removed while biological signals are preserved, effectively aligning heterogeneous datasets without requiring explicit batch labels.

KERNEL MEAN EMBEDDING

Key Properties of MMD

Maximum Mean Discrepancy operates as a non-parametric distance metric in a reproducing kernel Hilbert space, providing a robust statistical test and a differentiable loss function for aligning probability distributions in high-dimensional latent spaces.

01

Kernel-Based Statistical Distance

MMD measures the distance between two probability distributions P and Q by comparing their mean embeddings in a reproducing kernel Hilbert space (RKHS). The squared MMD is defined as the squared RKHS distance between the kernel mean embeddings:

  • Definition: MMD² = ||μ_P - μ_Q||²_H
  • Key insight: If the kernel is characteristic (e.g., Gaussian RBF), MMD(P, Q) = 0 if and only if P = Q
  • Unbiased estimator: Computed from finite samples using the V-statistic or U-statistic formulation
  • Kernel choice: The Gaussian kernel k(x, y) = exp(-||x - y||² / 2σ²) is the most common characteristic kernel, with the bandwidth σ controlling the scale of distributional features captured
RKHS
Embedding Space
P=Q ↔ 0
Characteristic Property
02

Two-Sample Test Statistic

MMD serves as a non-parametric statistical test for the null hypothesis that two sets of samples are drawn from the same distribution. It is widely used in batch effect detection and correction validation:

  • Null hypothesis (H₀): P = Q (no batch effect present)
  • Test procedure: Compute the empirical MMD and compare against a null distribution estimated via permutation testing or a parametric bootstrap
  • Quadratic-time computation: The naive estimator requires O(n²) operations, but linear-time approximations exist using random Fourier features
  • Application in batch correction: MMD is used to quantitatively evaluate whether latent representations from different experimental batches have been successfully aligned, complementing metrics like kBET and LISI
O(n²)
Naive Complexity
Permutation
Null Distribution
03

Differentiable Loss Function

MMD is fully differentiable with respect to its inputs, making it an ideal domain-invariant loss for training deep neural networks. This property enables its use in domain-adversarial and disentanglement architectures:

  • MMD-AAE (Adversarial Autoencoders): Replaces the adversarial discriminator with an MMD loss to match the aggregated posterior of the latent code to a prior distribution, providing more stable training
  • Domain-Adversarial Neural Networks (DANN): MMD can substitute the domain classifier, directly minimizing the distributional discrepancy between source and target domain features
  • Batch correction via MMD: The loss L = L_task + λ · MMD²(Z_batch_A, Z_batch_B) is minimized to learn a latent representation Z that is invariant to batch identity while preserving biological signal
  • Gradient flow: Backpropagation through the MMD computation requires computing kernel derivatives, which are well-defined for smooth kernels like the Gaussian RBF
λ
Regularization Weight
Stable
Training Dynamics
04

Witness Function for Distributional Differences

The witness function f*(x) = μ_P(x) - μ_Q(x) identifies where in the input space the two distributions differ most. This provides interpretability for batch effect analysis:

  • Definition: The function that maximizes the mean discrepancy, revealing the regions of feature space where P and Q diverge
  • Interpretation: High absolute values of the witness function indicate systematic shifts between batches at specific feature coordinates
  • Batch effect localization: By evaluating the witness function on latent representations, researchers can identify which cell types or gene programs are most affected by technical variation
  • Visualization: Plotting the witness function over a low-dimensional embedding (e.g., t-SNE or UMAP) creates a heatmap of distributional discrepancy, guiding targeted correction strategies
f*(x)
Witness Function
Localized
Discrepancy Detection
05

Deep Kernel MMD for High-Dimensional Data

Standard MMD with a fixed Gaussian kernel may lose statistical power in high-dimensional spaces. Deep kernel MMD addresses this by learning an optimal kernel representation:

  • Deep kernel: k_ω(x, y) = k(φ_ω(x), φ_ω(y)), where φ_ω is a neural network feature extractor
  • Optimization: The kernel parameters ω are optimized to maximize the test power of the two-sample test, effectively learning the most discriminative features for distinguishing distributions
  • Application to scRNA-seq: Deep kernel MMD can learn a latent space where batch effects are maximally detectable, then be inverted to learn a batch-invariant representation
  • Relationship to C2ST: This approach connects to classifier two-sample tests (C2ST), where a classifier's ability to discriminate batches is used as a proxy for distributional discrepancy
φ_ω
Learned Feature Map
Max Power
Optimization Objective
06

Relationship to Optimal Transport

MMD and optimal transport (OT) are complementary frameworks for comparing distributions, each with distinct computational and theoretical properties relevant to batch correction:

  • MMD: Compares distributions via kernel mean embeddings in RKHS; provides a closed-form, differentiable metric with O(n²) complexity
  • Wasserstein distance (OT): Solves a transportation problem to find the minimal-cost mapping between distributions; respects the geometry of the underlying metric space
  • Trade-offs: MMD is computationally cheaper and easier to integrate into deep learning pipelines, while OT provides a more geometrically faithful alignment that preserves local structure
  • Hybrid approaches: Sinkhorn divergences and kernelized OT bridge the gap, combining the computational efficiency of MMD with the geometric sensitivity of optimal transport for robust batch correction
O(n²)
MMD Complexity
O(n³)
OT Complexity
MMD EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about Maximum Mean Discrepancy and its role in batch effect normalization.

Maximum Mean Discrepancy (MMD) is a kernel-based statistical test that measures the distance between two probability distributions by comparing their mean embeddings in a reproducing kernel Hilbert space (RKHS). It works by mapping samples from two distributions, P and Q, into a high-dimensional feature space using a characteristic kernel function, such as the Gaussian radial basis function (RBF) kernel. The MMD is then computed as the squared distance between the empirical mean embeddings of these two samples. A key property is that MMD equals zero if and only if the two distributions are identical, making it a powerful non-parametric two-sample test. In batch correction, MMD is used as a loss function to penalize differences between latent representations from different experimental batches, forcing a neural network to learn a batch-invariant feature space.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.