EconML

repository·main·Indexed 26 days ago

https://github.com/py-why/econml

A Python library for estimating heterogeneous treatment effects from observational data by integrating machine learning with econometric methods. It provides tools for calculating Conditional Average Treatment Effects (CATE), supporting both observational datasets and instrumental variable approaches. Key implementations include DMLIV, DRIV, Dynamic DML, and Orthogonal Random Forests (ORF), with a unified API based on the BaseCateEstimator class.

Tokens
36.8K
Snippets
69
Records
182
Agent score
88%

What's inside econml

  1. Overview of Forest Based Estimators in EconML

    main

    EconML provides several forest-based estimation methods designed to model treatment effect heterogeneity using flexible, non-linear, and data-adaptive models. These methods are suitable for high-dimensional feature spaces and provide valid confidence intervals because their estimates are asymptotically normal.

    Key Estimator Classes:

    • DMLOrthoForest: Orthogonal Random Forest Estimator.
    • DROrthoForest: Orthogonal Random Forest Estimator.
    • CausalForestDML: Forest Double Machine Learning Estimator (also known as Causal Forest).
    • ForestDRLearner: Forest Doubly Robust Estimator.

    Core Assumption: All these estimators require the unconfoundedness assumption: all potential variables that could simultaneously affect both the treatment and the outcome must be observed.

  2. Overview of DMLIV and DRIV implementations

    main

    The prototypes/dml_iv/ directory contains implementations of orthogonal machine learning approaches for estimating heterogeneous treatment effects when dealing with endogenous treatment and an instrument.

    Key implementation files include:

    • dml_iv.py: Classes implementing the DMLIV algorithm and its variants.
    • dr_iv.py: Classes implementing the DRIV algorithm and its variants.
    • deep_dml_iv.py: DMLIV child classes using Keras neural network models as treatment effect models.
    • deep_dr_iv.py: DRIV child classes using Keras neural network models as treatment effect models.
    • dml_ate_iv.py: Implementation of DMLATEIV for Average Treatment Effect (ATE) estimation, assuming no compliance heterogeneity or effect heterogeneity.
  3. Overview of EconML

    main

    EconML is a Python package designed for estimating heterogeneous treatment effects from observational data using machine learning. It combines state-of-the-art machine learning techniques (such as random forests, boosting, lasso, and neural networks) with econometrics to automate complex causal inference problems.

    Key capabilities include:

    • Estimating the causal effect of treatment variable(s) T on an outcome variable Y, controlling for features X and W.
    • Modeling how effects vary as a function of features X (heterogeneity).
    • Providing valid confidence intervals and inference results.
    • Supporting both observational datasets (assuming no unobserved confounders) and instrumental variable approaches (using an instrument Z).
    • Utilizing a unified API built on standard Python machine learning and data analysis packages.
  4. Overview of Meta-Learners in EconML

    main

    Meta-Learners are discrete Conditional Average Treatment Effect (CATE) estimators that combine existing machine learning methods in a black-box manner to estimate response surfaces ($Y(0)$ and $Y(1)$). They provide high flexibility by allowing any ML algorithm to be used at each stage of estimation and support cross-validation for model selection of both nuisance quantities and the final CATE model.

    When to use Meta-Learners:

    • When you need maximum flexibility in choosing estimation methods at different stages.
    • When you want to perform automatic model selection via cross-validation.
    • Note: Because they use unrestricted ML methods, they typically do not offer valid confidence intervals due to the complex trade-offs between bias and variance.
  5. Understand Double Machine Learning (DML) in EconML

    main

    Double Machine Learning (DML) is used to estimate heterogeneous treatment effects when confounders/controls are high-dimensional or have non-parametric effects on the treatment and outcome.

    The method works in two stages:

    1. Predictive Tasks: Use machine learning to predict the outcome from controls and the treatment from controls.
    2. Final Estimation: Combine these predictive models to estimate the heterogeneous treatment effect.

    This approach allows for arbitrary machine learning algorithms in the first stage while maintaining statistical properties like small mean squared error and asymptotic normality in the final model.

  6. Understand Heterogeneous Treatment Effect Estimation with EconML

    main

    EconML is used to estimate heterogeneous treatment effects: determining how an intervention (treatment $T$) affects an outcome ($Y$) as a function of observable characteristics (features $X$).

    This is useful in scenarios where:

    • You have observational data where treatments were chosen via an unknown policy.
    • You cannot run direct A/B tests (e.g., you cannot force users to join a membership program).
    • You need to model complex, non-linear relationships using machine learning (Random Forests, Boosting, Lasso, Neural Nets) while maintaining causal interpretability and statistical validity (confidence intervals).

    Key techniques implemented include:

    • Double Machine Learning (DML)
    • Causal Forests
    • Deep Instrumental Variables (DeepIV)
    • Non-parametric Instrumental Variables
    • Meta-learners
  7. Use Orthogonal Instrumental Variables (OrthoIV) for heterogeneous treatment effects

    main

    Orthogonal instrumental variables methods allow you to estimate heterogeneous treatment effects using arbitrary machine learning models (e.g., random forests, boosting, neural networks) even when unobserved confounders are present, provided you have a valid instrument.

    This approach reduces the estimation problem to minimizing a loss function based on auxiliary models. The resulting effect model is robust to estimation errors in those auxiliary models due to the Neyman orthogonality criterion. It can also be used to estimate projections of the true effect model onto simpler hypothesis spaces (e.g., parametric models) to construct confidence intervals.

  8. Use Subsampled Honest Forest (BLB) Inference

    main

    For non-parametric models based on Random Forests, such as CausalForestDML and ForestDRLearner, you can use the 'bootstrap-of-little-bags' (BLB) approach to estimate uncertainty. This is enabled by default (inference='auto') or by explicitly setting inference='blb'.

    from econml.dml import CausalForestDML
    from sklearn.ensemble import RandomForestRegressor
    
    est = CausalForestDML(
        model_y=RandomForestRegressor(n_estimators=10, min_samples_leaf=10),
        model_t=RandomForestRegressor(n_estimators=10, min_samples_leaf=10)
    )
    
    # Enabled by default or explicitly:
    est.fit(y, t, X=X, W=W, inference='blb')
    
    point = est.const_marginal_effect(X)
    lb, ub = est.const_marginal_effect_interval(X, alpha=0.05)
  9. Use Dynamic Double Machine Learning (DynamicDML) for sequential treatments

    main

    Dynamic Double Machine Learning is used to estimate heterogeneous treatment effects when treatments are offered sequentially over time via an adaptive dynamic policy. It is suitable when you have high-dimensional or non-parametric confounders/controls that affect both the treatment decisions and the outcomes.

    Use DynamicDML when your data follows a Markov decision process structure where states ($X_t, W_t$), treatments ($T_t$), and outcomes ($Y_t$) are recorded over multiple time periods. The estimator adjusts for treatments that have causal effects on future outcomes.

    Key Inputs:

    • y: The observed outcome(s).
    • T: The treatment(s)/intervention(s) offered over time.
    • X: Observable characteristics used to understand treatment effects as a function of these features (heterogeneity).
    • W: Confounders/controls that influence both treatment and outcome.
    • groups: An array identifying the units (e.g., which rows belong to which subject/group).
  10. Configure first-stage model hyperparameters in DRLearner

    main

    You can select hyperparameters for first-stage models in two ways:

    1. In-framework: Pass GridSearchCV objects directly as model_regression or model_propensity. This performs tuning within the cross-fitting structure.
    2. Pre-fitted (Recommended for efficiency): Fit the best models outside of EconML using your full dataset, then pass the .best_estimator_ to the DRLearner. This is often more computationally efficient and statistically stable.
  11. Use OLS Inference for linear estimators

    main
    For estimators where the final stage CATE estimate uses Ordinary Least Squares (OLS), such as LinearDML and LinearDRLearner, you can use normality-based confidence intervals. By default, inference='auto' provides these. You can also explicitly set inference='statsmodels' or customize the covariance type using StatsModelsInference or StatsModelsInferenceDiscrete.
  12. Install prerequisites for Orthogonal Random Forests

    main

    To use the Orthogonal Random Forest (ORF) algorithm, ensure you have the following installed:

    • Python: 3.6 or higher
    • scikit-learn: > 0.19
    • numpy: > 1.14

    If you intend to run the Monte Carlo simulations and plotting scripts, you also require:

    • matplotlib: > 2.1
    • R: 3.3 or above
    • R CRAN packages: optparse, grf