Counterfactual Explanations for Deep Two-Sample Testing

Wei-Cheng Lai, Marco Simnacher, Christoph LippertarXiv 2026

TL;DR

While deep two-sample tests can accurately determine if two high-dimensional distributions (like medical image cohorts) differ, they fail to explain why. This paper introduces a novel framework that combines diffusion autoencoders with deep test models to generate visual counterfactual explanations, editing individual images to show exactly which features drive group-level statistical differences.

What Problem Does This Paper Address?

Two-sample testing is a bedrock statistical tool used to determine whether two groups of data come from the same underlying distribution (the null hypothesis $H_0: p = q$). While classical tests struggle with complex, high-dimensional data like images, recent "deep" two-sample tests solve this by mapping data into learned latent spaces where differences are easier to spot.

However, these deep tests operate as black boxes. They output a single test statistic or a p-value to reject or accept $H_0$, providing zero insight into which semantic features caused the rejection. In fields like medical imaging, knowing that two patient cohorts differ statistically is only half the battle; clinicians need to see how they differ to discover biomarkers or understand disease progression.

Methods

The authors bridge this interpretability gap by proposing a deep-test-guided counterfactual generation framework. Instead of explaining a single instance's classification label, this method explains group-level distribution shifts.

The architecture consists of two main components:

  1. A Pretrained Deep Two-Sample Test Model ($f$): This model measures the discrepancy between two groups using Deep Maximum Mean Discrepancy (DMMD).
  2. A Diffusion Autoencoder (DiffAE): This generative model encodes an image into a semantic latent code ($z^{\text{sem}}$) and a stochastic noise variable, allowing for highly realistic image reconstructions and edits.
Overview of the proposed deep-test-guided counterfactual generation framework.

To generate a counterfactual, a source image $X_i$ is encoded into the DiffAE's latent space. The framework then optimizes a counterfactual loss function directly via gradient descent on $z^{\text{sem}}$. The objective function balances two competing goals:

  • Statistical Alignment: Minimizing the DMMD test statistic to make the modified source group look statistically indistinguishable from the target group under the test model.
  • Minimality: An LPIPS (Learned Perceptual Image Patch Similarity) regularizer ensuring the edited image remains perceptually close to the original source image.

Datasets

The method was evaluated on both real-world clinical data and synthetic benchmarks:

  • Brain MRI Cohorts: Real T1-weighted structural brain MRIs from the ADNI (Alzheimer’s Disease Neuroimaging Initiative) dataset. The cohorts were split based on two clinically relevant criteria: Clinical Dementia Rating (CDR; healthy vs. cognitively impaired) and Age (younger vs. older). The auxiliary test model was pretrained on the massive UK Biobank (UKBB) dataset to predict ventricular volume.
  • Synthetic dSprites: A standard dataset of 2D shapes used to evaluate shape-transfer capabilities (transforming squares into ellipses). The auxiliary test model was trained on the Spaceshapes dataset.

Key Insights and Ideas

  • From Instance-Level to Group-Level Explanations: Traditional counterfactual methods ask, "How do I change this specific image to flip a classifier's prediction?" This paper shifts the paradigm to: "How do I minimally alter samples from Group A so that the entire group statistically matches Group B?"
  • Direct Statistical Supervision: By using the gradient of the actual DMMD test statistic as the optimization signal, the framework forces the generative model to alter precisely the features that the statistical test is sensitive to.
  • The Three-Way Trade-off: The paper highlights an inherent trade-off in distribution-level counterfactuals between statistical effectiveness (shifting the p-value), minimality (keeping edits subtle), and realism (ensuring images look natural). Improving one often comes at the cost of another, depending on how the cohorts are defined.

Key Results

The proposed method consistently outperformed a label-conditional baseline at shifting the source distribution toward the target distribution.

As shown in the quantitative evaluation below, the proposed method achieved a much higher mean change in p-value ($\Delta p$) and a larger reduction in the test discrepancy ($\Delta t$) for both the dementia (CDR) and age-based MRI cohorts:

Statistical evaluation of MRI counterfactuals showing significant improvements in p-value and discrepancy reduction.

Qualitatively, the generated MRIs provided highly interpretable, clinically valid visual evidence. When transforming healthy brains toward the cognitively impaired cohort, the model automatically introduced ventricular enlargement, increased cerebrospinal fluid (CSF) space, and more pronounced cortical sulci—all well-known anatomical hallmarks of dementia.

Limitations and Future Work

  • Cohort-Dependent Performance: While the method excelled in the CDR dementia setting across all metrics, the baseline performed better on realism and minimality in the Age and dSprites settings, indicating that the optimal trade-off is highly dataset-dependent.
  • Test Model Specificity: The framework is currently tightly coupled with deep MMD-based tests. Adapting it to work with other types of deep non-parametric two-sample tests remains an open avenue.
  • Error Propagation: Because the framework relies on a pretrained test model and a generative model, any biases or blind spots in either model will inevitably propagate into the visual explanations.

Summary updated

BibTeX
@misc{lai2026counterfactual,
  author = {Wei-Cheng Lai and Marco Simnacher and Christoph Lippert},
  title = {{Counterfactual Explanations for Deep Two-Sample Testing}},
  year = {2026},
  eprint = {2606.04009},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2606.04009},
}
Figure