Double machine learning in Python (econml)
Follow the analysis.
Select a node to explore the method
Build the data
Data preparation — shapes the raw inputs into what the estimator expects.
Provide outcome, a (discrete) treatment, and effect-modifier features.
# Install: pip install econml
import numpy as np
from econml.dml import LinearDML
from sklearn.ensemble import GradientBoostingRegressor
n = 1000
X = np.random.normal(size=(n, 5))
T = np.random.binomial(1, 0.5, size=n)
Y = (X[:, 0] > 0) * T + X[:, 1] + np.random.normal(size=n)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Fit a Linear Double-ML model
The core estimate — where the causal quantity itself is computed.
LinearDML partials out flexible ML models for Y and T (cross-fitted) and recovers the effect.
est = LinearDML(model_y=GradientBoostingRegressor(),
model_t=GradientBoostingRegressor(),
discrete_treatment=True)
est.fit(Y, T, X=X)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Read the average effect
Uncertainty quantification — standard errors, intervals, and aggregation.
const_marginal_ate averages the CATE over the sample.
print(est.const_marginal_ate(X))
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Inputs & data
Outcome Y, treatment T, effect-modifier features X, and controls W (arrays or DataFrames).
Show data format & exampleHide example
| Y | T | X1 | X2 |
|---|---|---|---|
| 1.0 | 1 | 0.5 | -0.3 |
| 0.2 | 0 | -0.8 | 0.1 |
| 1.7 | 1 | 0.9 | 1.2 |
| 0.0 | 0 | 0.1 | -0.6 |
Result figure rendered by StatsOtter from the package's documented example — unofficial community showcase; all credit to the original authors.
The estimate
\tau(x)=\mathbb E[Y(1)-Y(0)\mid X=x];\quad \tilde Y=\tilde T\,\theta(x)+\varepsilon\ \ (\text{double ML residuals})
⚠️ Unofficial community showcase of econml (docs). Not affiliated with the authors — all credit to Microsoft Research / PyWhy (Lewis, Syrgkanis & collaborators); this summarizes public documentation.
What it does. econml brings the Athey–Imbens-style econometrics-meets-ML estimators to Python: it estimates conditional average treatment effects with machine-learning nuisance models while keeping valid inference on the causal parameter.
How it works. Estimators like LinearDML fit flexible models for the outcome and the treatment, partial both out (Neyman-orthogonal/double ML), and regress the residuals to recover the effect — optionally as a function of effect-modifiers X for a CATE. Cross-fitting removes overfitting bias.
Assumptions. Unconfoundedness given the controls and overlap; orthogonalization makes the estimate robust to first-stage ML error.
Packages the double-ML / heterogeneous-effects methods from the causal-ML agenda Imbens helped build; authored by the PyWhy/Microsoft team.
What you get — A CATE model you can evaluate at any X, and the average treatment effect implied by it.
Example output
0.5123847716
Discuss the workflow (0)
Log in to join the discussion.