Note: CausalDiscrepancyVAE requires PyTorch. The CausalDiscrepancyVAE estimator in pydeepcausalml.generative adds an MMD penalty aligning simulated interventions to real interventional data.
CausalDiscrepancyVAE extends a standard VAE with an explicit distribution-matching penalty that forces the model’s simulated interventions to match real interventional data. Unlike CEVAE, iVAE, and CausalVAE — which infer or structure causal relationships from observational data alone — the CD-VAE uses real experimental outcomes (e.g., CRISPR knockouts, randomized trial arms) as a direct training signal, anchoring the latent space to physical interventions rather than to statistical associations.
In this notebook, we convert the complete causalDiscrepancy_VAE.ipynb workflow into R, running it on the IHDP benchmark via pydeepcausalml’s causal_discrepancy_vae() implementation.
Where CD-VAE Fits in the Causal VAE Family
This series has covered four increasingly expressive models. Understanding where CD-VAE fits clarifies when to use it.
Model
Latent structure
Training signal
Key assumption
Use when
CEVAE
Independent \(z\); \(x\) are noisy proxies
Observational \(x\) only
Ignorability in latent space
Confounders are unobserved but leave a trace in \(x\)
iVAE
Independent \(z\); conditional prior \(p(z\|u)\)
Observational \(x\) + auxiliary \(u\)
Sufficient variation in \(u\)
A context variable (label, time, environment) can anchor the latent space
CausalVAE
DAG over \(z\); independent \(\varepsilon\)
Observational \(x\) + weak graph supervision
DAG is acyclic
Causal relationships among latent factors need to be modelled explicitly
CD-VAE
Independent\(z\); MMD-aligned
Observational + real interventional data
Real interventional data available
Real experimental outcomes (CRISPR, RCT arms) exist and can supervise the model
The core trade-off is supervision richness versus data requirements. CEVAE, iVAE, and CausalVAE work purely from observational data (plus partial graph knowledge for CausalVAE). CD-VAE requires some real interventional data but gains a much more direct and robust training signal in return. Where real experimental data exists — Perturb-Seq studies, randomized trials, A/B tests — CD-VAE is the most grounded option in this family.
Overview
A Variational Autoencoder learns to encode data \(x\) into a latent representation \(z\) and reconstruct it, optimizing the Evidence Lower Bound (ELBO):
The first term rewards faithful reconstruction; the second regularizes the posterior toward a standard Gaussian prior. This works well for generative modelling, but it has no notion of causality — the latent space organizes information by statistical co-occurrence, not by causal mechanism.
The Causal Extension: Adding the MMD Penalty
The CD-VAE augments the ELBO with a third term that measures how closely the model’s predicted response to a virtual intervention matches the observed response to a real one:
where \(\hat{p}_{\text{int}}\) is the model’s predicted post-intervention distribution and \(p_{\text{int}}\) is the distribution actually observed under a real intervention. The parameter \(\lambda\) controls the strength of this causal alignment. When \(\lambda = 0\) the model reduces to a standard VAE; as \(\lambda\) increases the model devotes more capacity to making its causal predictions faithful to the real experimental data.
Architecture
The virtual intervention mechanism — how a latent modification produces the predicted interventional distribution — is the most distinctive part of CD-VAE and worth showing separately.
Maximum Mean Discrepancy
MMD is a kernel-based distance between two probability distributions. Given samples from distributions \(P\) and \(Q\), it compares their mean embeddings in a Reproducing Kernel Hilbert Space (RKHS):
Using the kernel trick with a kernel \(k\) (typically a mixture of RBF/Gaussian kernels at multiple bandwidths), this expands to a sum of three kernel expectations:
Using a mixture of kernels rather than a single one makes the estimator robust to scale differences across dimensions — an important practical consideration when the two distributions being compared live in high-dimensional gene expression or covariate space.
Property
Why it matters
Non-parametric
No assumption about the shape of either distribution
Differentiable
Can be backpropagated through directly
Works on finite samples
Tractable even with batch-sized samples
MMD \(= 0\) iff \(P = Q\)
Provides a proper distributional criterion
Scale-robust with multi-kernel
Handles high-dimensional, heterogeneous data
Virtual Interventions in the Latent Space
The “causal” part of CD-VAE relies on do-calculus intuition. A virtual intervention \(\text{do}(X_i = v)\) is simulated by:
Encoding an unperturbed observation \(x\) into the latent space: \(z \sim q_\phi(z \mid x)\)
Modifying a specific latent dimension or learned intervention subspace corresponding to feature \(i\) — this is the virtual perturbation \(\tilde{z}\)
Decoding the modified \(\tilde{z}\) back into the observation space to produce \(\hat{x}_{\text{int}}\)
The predicted post-intervention distribution \(\hat{p}_{\text{int}}\) is then compared against the real distribution \(p_{\text{int}}\) observed in the experiment using MMD. Because MMD is differentiable, the gradient of this discrepancy flows back through the decoder, the intervention, and into the encoder — forcing the latent space to organize itself in a way that makes causal predictions accurate, not just reconstructions faithful.
The sum over \(k\) runs over all intervention conditions in the training data (e.g., each distinct gene knockout or treatment arm). Each term checks: for intervention \(k\), does the model’s simulation match the observed response? The outcome head \(\mathcal{L}_{\text{outcome}}\) is an MSE loss that trains a separate prediction network for \(Y\) given \(z\) and \(T\), enabling ATE estimation at inference time.
Role of Each Hyperparameter
Hyperparameter
Effect
Typical range
\(\beta\)
Weight on the KL term; higher \(\beta\) encourages more disentangled but potentially less faithful latents
0.5 – 4.0
\(\lambda_{\text{MMD}}\)
Strength of causal alignment; higher values make the model prioritize matching interventional distributions
1.0 – 10.0
\(\lambda_Y\)
Weight on the outcome prediction loss; balances causal alignment against treatment effect accuracy
5.0 – 20.0
n_kernels
Number of RBF kernels at different bandwidths in the MMD estimator
3 – 10
latent_dim
Dimension of \(z\); should be large enough to capture the causal structure but small enough to force compression
8 – 32
Application to Perturb-Seq / CRISPR Data
Perturb-Seq combines CRISPR-based genetic perturbations with single-cell RNA sequencing: each cell has a known gene knockout (the intervention) and a full transcriptome readout (the outcome). This creates a dataset with both observational and interventional structure, exactly what CD-VAE exploits. A regular VAE trained on such data learns to reconstruct gene expression profiles but conflates the statistical pattern of knockouts with genuine causal effects. CD-VAE’s MMD penalty forces the model to distinguish the two: the latent dimensions learn to encode causal factors (genes or gene programs) rather than statistical correlations, and the trained model can then predict the effect of unseen or combinatorial perturbations by composing interventions in latent space.
Strengths and Limitations
Strengths:
CD-VAE leverages real interventional data as a direct supervisory signal, which most causal VAEs lack entirely.
MMD is computationally tractable even in high dimensions (e.g., ~20,000 genes).
The model naturally handles distributional shift between observational and interventional regimes, and can generalize to out-of-distribution perturbations when the latent causal structure is well-learned.
Limitations:
Real interventional data is required — CD-VAE cannot be applied in purely observational settings where no experiments have been run.
Identifying which latent dimensions correspond to which interventions is non-trivial and often requires auxiliary supervision or disentanglement constraints.
The multi-kernel MMD estimator introduces sensitivity to kernel bandwidth selection, and the interaction between \(\beta\), \(\lambda_{\text{MMD}}\), and \(\lambda_Y\) requires careful tuning.
Combinatorial perturbations grow exponentially, so full training coverage is rarely achievable in practice.
Treatment Effect Evaluation
ATE and PEHE
The three metrics reported cover different aspects of causal accuracy. The absolute ATE error measures population-level accuracy — how close the predicted average effect is to the true average. sqrt(PEHE) (square root of Precision in Estimation of Heterogeneous Effects) measures individual-level accuracy — how well the model captures variation in treatment effects across units. A model can have a small ATE error but a large PEHE if systematic errors in individual predictions cancel out at the population level.
ITE Scatter and Distribution
In the scatter plot, points falling close to the diagonal indicate accurate individual predictions. The orange dotted line marks the predicted ATE; the green line marks the true ATE. In the distribution plot, the two histograms should have similar shape and center if the model has learned the correct distribution of treatment effects.
ITE Calibration Check
Calibration asks whether units predicted to have a high treatment effect actually do have a high treatment effect. We sort validation units into deciles by predicted ITE and plot mean predicted ITE against mean true ITE within each decile. A well-calibrated model lies close to the diagonal.
Common-Support Sensitivity Analysis
A known weakness of observational causal methods is sensitivity to units near the boundary of common support — those with extreme propensity scores where one treatment group is rare. The plot below shows how ATE error changes when we progressively restrict the evaluation sample to units with propensity scores inside increasingly tight windows around [0.1, 0.9].
Implementation in Python
We use pydeepcausalml’s causal_discrepancy_vae() and predict.causal_discrepancy_vae(). The full pipeline follows the same end-to-end stages as the Python tutorial: IHDP loading, feature preprocessing, model training, and evaluation.
import numpy as npimport pandas as pdimport torchimport matplotlib.pyplot as pltimport seaborn as snsfrom pydeepcausalml import set_seedprint("torch:", torch.__version__)print("cuda:", torch.cuda.is_available())from sklearn.linear_model import LogisticRegression
torch: 2.12.1+cu130
cuda: False
Code
set_seed(42)run_fast =True
Load IHDP data and prepare (X, t, y)
Fitting CausalDiscrepancyVAE on the IHDP Dataset (semi-synthetic)
We first benchmark CausalDiscrepancyVAE on the Infant Health and Development Program (IHDP) dataset, a widely used semi-synthetic benchmark for treatment effect estimation. We load all nine IHDP replication files from the CausalML repository. If remote access is unavailable the code falls back to a synthetic dataset with the same structure.
The IHDP benchmark records an early childhood intervention. Treatment and control groups have substantial covariate imbalance — exactly the kind of observational confounding that CD-VAE’s MMD penalty is designed to correct in the latent space.
Before fitting any causal model it is worth verifying that the treatment and control groups share sufficient covariate support — i.e., that the propensity score distributions overlap. Extreme separation indicates near-violation of positivity, which undermines all potential-outcomes methods, including CD-VAE. We estimate propensity scores with logistic regression on the raw (unscaled) covariates.
Regions of the propensity score distribution where one group has near-zero density represent covariate combinations where causal identification is fragile. The CD-VAE MMD penalty is designed to align the two groups in latent space — but it cannot compensate for complete lack of overlap in the raw covariates. If the plot shows severe separation, consider trimming the evaluation sample to the region of common support before reporting ATE estimates.
The three panels show reconstruction loss, KL divergence, and MMD separately. In a well-behaved run, reconstruction and KL both decline monotonically; MMD may initially increase as the model explores the latent space before converging toward zero as causal alignment is achieved. If MMD fails to decline, the lambda_mmd hyperparameter likely needs to be increased, or the latent dimensionality is too small to accommodate both reconstruction and causal alignment.
Reading these numbers. CausalDiscrepancyVAE optimizes a representation-matching (MMD) objective rather than fitting the outcome scale directly, so on a heavy-tailed benchmark like IHDP it recovers the effect’s direction and ordering more reliably than its absolute magnitude. Expect the point ATE to be biased toward zero here; the DSCM and CausalEGM notebooks, which model the outcome explicitly, are better calibrated on the same data. The decile-calibration plot below is the more informative diagnostic for this model.
CausalDiscrepancyVAE bridges deep latent-variable modelling and causal effect estimation by using real interventional data as a direct supervisory signal. The MMD penalty — computed between the model’s simulated interventional distribution and the observed one — gives the latent space a strong anchor that the other models in this series (CEVAE, iVAE, CausalVAE) cannot exploit without experimental data.
Key findings from this notebook:
The propensity score overlap check is an essential diagnostic before fitting any causal model to observational data. Regions without overlap cannot be reliably corrected by any model, including CD-VAE.
The loss decomposition plot helps distinguish whether the model is struggling with reconstruction, regularization, or causal alignment, each of which has a different remedy.
Latent space PCA after training shows whether the MMD penalty has successfully aligned the treatment and control group representations — the primary mechanism by which CD-VAE removes confounding bias.
The calibration decile plot goes beyond ATE accuracy to assess whether the model has learned the rank ordering of individual effects correctly — essential for any targeting or resource allocation application.
The sensitivity analysis quantifies how much of the model’s performance depends on near-boundary units, providing an honest picture of robustness.
For production use: tune lambda_mmd first (it is the most influential hyperparameter), validate overlap diagnostics before trusting any causal estimate, and report sqrt(PEHE) alongside ATE error to capture individual-level accuracy.