CausalML Documentation

repository·master·Indexed 26 days ago

https://github.com/uber/causalml

A Python package for uplift modeling and causal inference using machine learning algorithms. CausalML provides tools to estimate the Conditional Average Treatment Effect (CATE) to optimize targeting and personalization. It includes modules for propensity score estimation, causal matching, feature engineering, and various inference methods including meta-learners, tree-based models, and a JAX/Flax implementation of the Causal Effect Variational Autoencoder (CEVAE).

Tokens
12.2K
Snippets
17
Records
63
Agent score
88%

What's inside CausalML

  1. Overview of CausalML

    master

    CausalML is a Python package designed for uplift modeling and causal inference using machine learning. It provides a standard interface to estimate the Conditional Average Treatment Effect (CATE), which measures the causal impact of an intervention T on an outcome Y for users with observed features X.

    Key use cases include:

    • Campaign targeting optimization: Identifying customers who will respond favorably to an ad exposure to increase ROI.
    • Personalized engagement: Estimating heterogeneous treatment effects for different customer/treatment combinations to build optimal recommendation systems.
  2. Overview of Supported Algorithms in CausalML

    master

    CausalML provides several categories of algorithms for causal inference and uplift modeling:

    • Tree-based algorithms: Includes Uplift Random Forests (with various criteria like KL divergence, Euclidean Distance, Chi-Square, or Contextual Treatment Selection), Interaction Trees (IT), and Causal Inference Trees (CIT).
    • Meta-learner algorithms: Frameworks to estimate Conditional Average Treatment Effect (CATE) using base learners, including S-learner, T-learner, X-learner, R-learner, and Doubly Robust (DR) learner.
    • Instrumental variables algorithms: Includes 2-Stage Least Squares (2SLS) and Doubly Robust Instrumental Variable (DRIV) learner.
    • Neural network based algorithms: CEVAE and DragonNet.
    • Treatment optimization algorithms: Counterfactual Unit Selection and Counterfactual Value Estimator.
  3. Use causalml utility and evaluation modules

    master

    CausalML provides several utility modules for data preparation, matching, and evaluation:

    • causalml.dataset: Utilities for handling causal datasets.
    • causalml.match: Methods for causal matching (e.g., propensity score matching).
    • causalml.propensity: Tools for estimating propensity scores.
    • causalml.metrics: Metrics for evaluating causal models and uplift.
    • causalml.optimize: Optimization routines for causal inference.
    • causalml.features: Feature engineering and transformation tools.
    • causalml.feature_selection: Methods for selecting relevant features for causal models.
  4. Explore causalml inference submodules

    master

    The causalml.inference package provides various methods for estimating causal effects. Depending on your data and modeling requirements, you can use the following submodules:

    • causalml.inference.tree: Tree-based causal inference methods.
    • causalml.inference.meta: Meta-learners (e.g., S-Learner, T-Learner, X-Learner).
    • causalml.inference.iv: Instrumental Variable (IV) methods.
    • causalml.inference.nn: Neural Network-based causal inference.
    • causalml.inference.tf: TensorFlow-based causal inference implementations.
  5. Install CausalML from source

    master

    To install from source, clone the repository and use pip install -e ..

    Prerequisite: If you need Graphviz, install it via conda install python-graphviz, brew install graphviz (macOS), or sudo apt-get install graphviz (Linux).

    Use the following commands to install with specific backends:

    • .[tf] for tensorflow
    • .[torch] for torch
    • .[jax] for jax
    git clone https://github.com/uber/causalml.git
    cd causalml
    
    # Base source install
    pip install -e .
    
    # With TensorFlow
    pip install -e ".[tf]"
    
    # With PyTorch
    pip install -e ".[torch]"
    
    # With JAX
    pip install -e ".[jax]"
  6. Estimate treatment effects using Matching

    master

    Matching methods aim to mimic randomized controlled trials by finding treated and non-treated units that are as similar as possible based on relevant characteristics.

    Commonly, this is achieved using the propensity score $e_i(X_i) = P(W_i = 1 | X_i)$. Units are then matched using a distance criterion, such as $k:1$ nearest neighbors. This method typically estimates the Average Treatment Effect on the Treated (ATT): $$\mathbb{E}[Y(1) | W = 1] - \mathbb{E}[Y(0) | W = 1]$$

  7. Understand Meta-Learner Algorithms (S, T, X, R, and DR Learners)

    master

    Meta-learners estimate the Conditional Average Treatment Effect (CATE) by using machine learning estimators as base learners. They are categorized by how they utilize treatment indicators and base learners:

    • S-Learner: Uses a single machine learning model where the treatment indicator is included as a feature. It estimates $\mu(x, w)$ and calculates CATE as $\hat{\tau}(x) = \hat{\mu}(x, W=1) - \hat{\mu}(x, W=0)$.
    • T-Learner: Uses two separate models, one for the control group ($\mu_0(x)$) and one for the treatment group ($\mu_1(x)$). CATE is the difference: $\hat{\tau}(x) = \hat{\mu}_1(x) - \hat{\mu}_0(x)$.
    • X-Learner: An extension of the T-learner that uses three stages: estimating group outcomes, imputing user-level treatment effects ($D^1_i$ and $D^0_j$), and finally estimating CATE as a weighted average of those imputed effects.
    • R-Learner: Uses cross-validation out-of-fold estimates of outcomes and propensity scores to minimize an R-loss function.
    • Doubly Robust (DR) Learner: Uses cross-fitting to estimate CATE via a doubly-robust score function $\phi$ across multiple data partitions to ensure unbiasedness under correct propensity or outcome-model specification.
  8. Validate methodology using synthetic data sets

    master

    You can test causal methodologies using simulations that generate data with known causal and non-causal links. causalml implements several synthetic data generation mechanisms based on Nie (2017):

    • Mechanism 1: Complex outcome regression with an easy treatment effect.
    • Mechanism 2: Simulates a randomized trial (fair coin flip for treatment).
    • Mechanism 3: Easy propensity score but a difficult control outcome.
    • Mechanism 4: Unrelated treatment and control arms.
  9. Evaluate CATE Models using Surrogate Metrics

    master

    Since ground-truth treatment effects are never observed, CausalML provides surrogate metrics to evaluate the effect-magnitude accuracy of fitted CATE models using only observed data:

    • DR (Doubly Robust) pseudo-outcome loss: Constructs a doubly-robust pseudo-outcome $\phi$ using cross-fitted propensity and outcome models. The loss is calculated as the mean squared error between the model's CATE estimate and the pseudo-outcome: $\text{DR loss}(\hat{\tau}) = \frac{1}{n}\sum_i (\hat{\tau}(X_i) - \phi_i)^2$. Lower is better.
    • Plug-in T-learner loss: A simpler baseline that uses a cross-fit T-learner's held-out estimate as a proxy for the true CATE. The loss is $\text{T-loss}(\hat{\tau}) = \frac{1}{n}\sum_i (\hat{\tau}(X_i) - (\hat{\mu}_1(X_i) - \hat{\mu}_0(X_i)))^2$.