CausalPFN (Amortized Causal Effect Estimation via In-Context Learning)
An easy-to-use library for causal effect estimation using transformer-based in-context learning
🛠️ Installation • 🚀 Quick Start • 📊 Examples • 🔬 Reproducibility
This library is is used to produce the results in our paper CausalPFN: Amortized Causal Effect Estimation via In-Context Learning.
🌟 Overview
CausalPFN leverages the power of transformer architectures for amortized causal effect estimation, enabling fast and accurate inference across diverse causal scenarios without the need for retraining. Our approach combines the flexibility of in-context learning with the rigor of causal inference.
✨ Key Features
- 🚀 Fast Inference: Amortized learning enables rapid causal effect estimation without retraining
- 🧮 Uncertainty Quantification: Built-in calibration and confidence estimation
- ⚡ GPU Accelerated: Optimized for modern hardware with CUDA support
- 📈 Benchmarked: Competitive performance against state-of-the-art causal inference methods
- 📊 Uplift-Modelling: Supports treatment effect estimation for personalized decision-making in real-world applications
Installation
Via PyPI
pip install causalpfn
Requirements
- Python 3.10+
- PyTorch 2.3+
- NumPy
- scikit-learn
- tqdm
- huggingface_hub
Quick Start
Here's a complete example demonstrating CausalPFN for causal effect estimation:
import numpy as np
import torch
import time
from causalpfn import CATEEstimator, ATEEstimator
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 1. Generate synthetic data
np.random.seed(42)
n, d = 20000, 5
X = np.random.normal(1, 1, size=(n, d)).astype(np.float32)
# Define true causal effects
def true_cate(x):
return np.sin(x[:, 0]) + 0.5 * x[:, 1]
def true_ate():
return np.mean(true_cate(X))
# Generate treatment and outcomes
tau = true_cate(X).astype(np.float32)
T = np.random.binomial(1, p=0.5, size=n).astype(np.float32)
Y0 = X[:, 0] - X[:, 1] + np.random.normal(0, 0.1, size=n).astype(np.float32)
Y1 = Y0 + tau
Y = Y0 * (1 - T) + Y1 * T
# 2. Train/test split
train_idx = np.random.choice(n, size=int(0.7 * n), replace=False)
test_idx = np.setdiff1d(np.arange(n), train_idx)
X_train, X_test = X[train_idx], X[test_idx]
T_train, Y_train = T[train_idx], Y[train_idx]
tau_test = tau[test_idx]
# 3. CATE Estimation
start_time = time.time()
causalpfn_cate = CATEEstimator(
device=device,
verbose=True,
)
causalpfn_cate.fit(X_train, T_train, Y_train)
cate_hat = causalpfn_cate.estimate_cate(X_test)
cate_time = time.time() - start_time
# 4. ATE Estimation
causalpfn_ate = ATEEstimator(
device=device,
verbose=True,
)
causalpfn_ate.fit(X, T, Y)
ate_hat = causalpfn_ate.estimate_ate()
# 5. Evaluation
pehe = np.sqrt(np.mean((cate_hat - tau_test) ** 2))
ate_rel_error = np.abs((ate_hat - true_ate()) / true_ate())
print(f"Results:")
print(f"ATE Relative Error: {ate_rel_error:.4f}")
print(f"PEHE: {pehe:.4f}")
print(f"CATE estimation time per 1000 samples: {cate_time / (len(X) / 1000):.4f}s")
Examples
Explore our notebook collection below. Before running the notebooks, make sure to install the additional dependencies via pip install .[dev].
| Notebook | Description | Features |
|---|---|---|
| Causal Effect Estimation | Compare CausalPFN with baseline methods | CATE/ATE estimation, benchmarking |
| Hillstrom Marketing | Uplift modeling case study | Real-world marketing application |
| Targeted Experiments with CausalPFN | Reduce the cost of A/B testing | Real-world marketing application |
| Calibration Analysis | Uncertainty quantification demo | Confidence intervals, calibration |
Performance Benchmark
Time vs. Performance. Comparison across 310 causal inference tasks from IHDP, ACIC, and Lalonde. CausalPFN achieves the best average rank (by precision in estimation of heterogeneous effect) while being much faster than other baselines.
Reproducibility
To fully reproduce the paper results, see the REPRODUCE file.
Citation
If you use CausalPFN in your research, please cite our paper:
@inproceedings{balazadeh2025causalpfn,
title={CausalPFN: Amortized Causal Effect Estimation via In-Context Learning},
author={Vahid Balazadeh and Hamidreza Kamkari and Valentin Thomas and Benson Li and Junwei Ma and Jesse C. Cresswell and Rahul G. Krishnan},
booktitle = {Advances in Neural Information Processing Systems},
volume = {38},
year = {2025}
}
Contributing
We welcome contributions! Please feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
⭐ Star us on GitHub • 🐛 Report Bug • 💡 Request Feature
Made with ❤️ for better causal inference
Metadata
Release files for causalpfn 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| causalpfn-0.1.5.tar.gz | 32.8 kB | With provenance | Details for causalpfn-0.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| causalpfn-0.1.5-py3-none-any.whl | Python 3 | none | any | Details for causalpfn-0.1.5-py3-none-any.whl |
Total release size: 62.0 kB
Release files / causalpfn-0.1.5.tar.gz
| Download URL | causalpfn-0.1.5.tar.gz |
|---|---|
| Size | 32.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d0af643a819e45613b91d355f82257c7d3196e195cea0be71997760588cfd6a2
|
|
BLAKE2b-256 checksum How to use checksums |
330e0ce66b258ddb57fbec2655f9bfcc513428410d79361f61699237f3c16805
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.
Transparency logRelease files / causalpfn-0.1.5-py3-none-any.whl
| Download URL | causalpfn-0.1.5-py3-none-any.whl |
|---|---|
| Size | 29.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c129280a40bdf1b5639ec9ecdf91a308f539c80d93990a9d997b886cd9a08d8f
|
|
BLAKE2b-256 checksum How to use checksums |
c8b3fa919f219b8028a4d44c9de95832927eb87b9c8e7bf46bcc2adbdf55e1e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.
Transparency logSecurity for causalpfn 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for causalpfn-0.1.5.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.