Skip to main content

This package contains several methods for calculating Conditional Average Treatment Effects

Project description

Build Status PyPI version PyPI wheel Supported Python versions

EconML: A Python Package for ML-Based Heterogeneous Treatment Effects Estimation

EconML is a Python package for estimating heterogeneous treatment effects from observational data via machine learning. This package was designed and built as part of the ALICE project at Microsoft Research with the goal to combine state-of-the-art machine learning techniques with econometrics to bring automation to complex causal inference problems. The promise of EconML:

  • Implement recent techniques in the literature at the intersection of econometrics and machine learning
  • Maintain flexibility in modeling the effect heterogeneity (via techniques such as random forests, boosting, lasso and neural nets), while preserving the causal interpretation of the learned model and often offering valid confidence intervals
  • Use a unified API
  • Build on standard Python packages for Machine Learning and Data Analysis

In a nutshell, this toolkit is designed to measure the causal effect of some treatment variable(s) T on an outcome variable Y, controlling for a set of features X. For detailed information about the package, consult the documentation at https://econml.azurewebsites.net/.

Table of Contents

Introduction

About Treatment Effect Estimation

One of the biggest promises of machine learning is to automate decision making in a multitude of domains. At the core of many data-driven personalized decision scenarios is the estimation of heterogeneous treatment effects: what is the causal effect of an intervention on an outcome of interest for a sample with a particular set of features?

Such questions arise frequently in customer segmentation (what is the effect of placing a customer in a tier over another tier), dynamic pricing (what is the effect of a pricing policy on demand) and medical studies (what is the effect of a treatment on a patient). In many such settings we have an abundance of observational data, where the treatment was chosen via some unknown policy, but the ability to run control A/B tests is limited.

Example Applications

Customer Targeting

Businesses offer personalized incentives to customers to increase sales and level of engagement. Any such personalized intervention corresponds to a monetary investment and the main question that business analytics are called to answer is: what is the return on investment? Analyzing the ROI is inherently a treatment effect question: what was the effect of any investment on a customer's spend? Understanding how ROI varies across customers can enable more targeted investment policies and increased ROI via better targeting.

Personalized Pricing

Personalized discounts have are widespread in the digital economy. To set the optimal personalized discount policy a business needs to understand what is the effect of a drop in price on the demand of a customer for a product as a function of customer characteristics. The estimation of such personalized demand elasticities can also be phrased in the language of heterogeneous treatment effects, where the treatment is the price on the demand as a function of observable features of the customer.

Stratification in Clinical Trials

Which patients should be selected for a clinical trial? If we want to demonstrate that a clinical treatment has an effect on at least some subset of a population then fully randomized clinical trials are inappropriate as they will solely estimate average effects. Using heterogeneous treatment effect techniques, we can use observational data to come up with estimates of these effects and identify good candidate patients for a clinical trial that our model estimates have high treatment effects.

Learning Click-Through-Rates

In the design of a page layout and ad placement, it is important to understand the click-through-rate of page components on different positions of a page. Modern approaches may be to run multiple A/B tests, but when such page component involve revenue considerations, then observational data can help guide correct A/B tests to run. Heterogeneous treatment effect estimation can provide estimates of the click-through-rate of page components from observational data. In this setting, the treatment is simply whether the component is placed on that page position and the response is whether the user clicked on it.

News

04/10/2019: Release v0.2, see release notes here.

03/06/2019: Release v0.1, welcome to have a try and provide feedback.

Getting Started

Installation

Install the latest release from PyPI:

pip install econml

To install from source, see For Developers section below.

Usage Examples

  • Double Machine Learning

    from econml.dml import DMLCateEstimator
    from sklearn.linear_model import LassoCV
    
    est = DMLCateEstimator(model_y=LassoCV(), model_t=LassoCV)
    est.fit(Y, T, X, W) # W -> high-dimensional confounders, X -> features
    treatment_effects = est.const_marginal_effect(X_test)
    
  • Orthogonal Random Forests

    from econml.ortho_forest import ContinuousTreatmentOrthoForest
    # Use defaults
    est = ContinuousTreatmentOrthoForest()
    # Or specify hyperparameters
    est = ContinuousTreatmentOrthoForest(n_trees=500, min_leaf_size=10, max_depth=10, 
                                         subsample_ratio=0.7, lambda_reg=0.01,
                                         model_T=LassoCV(cv=3), model_Y=LassoCV(cv=3)
                                         )
    est.fit(Y, T, X, W)
    treatment_effects = est.const_marginal_effect(X_test)
    
  • Deep Instrumental Variables

    import keras
    from econml.deepiv import DeepIVEstimator
    
    treatment_model = keras.Sequential([keras.layers.Dense(128, activation='relu', input_shape=(2,)),
                                       keras.layers.Dropout(0.17),
                                       keras.layers.Dense(64, activation='relu'),
                                       keras.layers.Dropout(0.17),
                                       keras.layers.Dense(32, activation='relu'),
                                       keras.layers.Dropout(0.17)])
    response_model = keras.Sequential([keras.layers.Dense(128, activation='relu', input_shape=(2,)),
                                      keras.layers.Dropout(0.17),
                                      keras.layers.Dense(64, activation='relu'),
                                      keras.layers.Dropout(0.17),
                                      keras.layers.Dense(32, activation='relu'),
                                      keras.layers.Dropout(0.17),
                                      keras.layers.Dense(1)])
    est = DeepIVEstimator(n_components=10, # Number of gaussians in the mixture density networks)
                          m=lambda z, x: treatment_model(keras.layers.concatenate([z, x])), # Treatment model
                          h=lambda t, x: response_model(keras.layers.concatenate([t, x])), # Response model
                          n_samples=1 # Number of samples used to estimate the response
                          )
    est.fit(Y, T, X, Z) # Z -> instrumental variables
    treatment_effects = est.effect(T0, T1, X_test)
    
  • Bootstrap Confidence Intervals

    from econml.bootstrap import BootstrapEstimator
    
    # Bootstrap estimator wrapper
    boot_est = BootstrapEstimator(est, n_bootstrap_samples=10)
    boot_est.fit(Y, T, X, W)
    treatment_effect_interval = boot_est.const_marginal_effect_interval(X_test, lower=1, upper=99)
    

To see more complex examples, go to the notebooks section of the repository. For a more detailed description of the treatment effect estimation algorithms, see the EconML documentation.

For Developers

You can get started by cloning this repository. We use setuptools for building and distributing our package. We rely on some recent features of setuptools, so make sure to upgrade to a recent version with pip install setuptools --upgrade. Then from your local copy of the repository you can run python setup.py develop to get started.

Running the tests

This project uses pytest for testing. To run tests locally after installing the package, you can use python setup.py pytest.

Generating the documentation

This project's documentation is generated via Sphinx. To generate a local copy of the documentation from a clone of this repository, just run python setup.py build_sphinx, which will build the documentation and place it under the build/sphinx/html path.

The reStructuredText files that make up the documentation are stored in the docs directory; module documentation is automatically generated by the Sphinx build process.

Blogs and Publications

Contributing and Feedback

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

References

M. Oprescu, V. Syrgkanis and Z. S. Wu. Orthogonal Random Forest for Causal Inference. ArXiv preprint arXiv:1806.03467, 2018.

Jason Hartford, Greg Lewis, Kevin Leyton-Brown, and Matt Taddy. Deep IV: A flexible approach for counterfactual prediction. Proceedings of the 34th International Conference on Machine Learning, 2017.

V. Chernozhukov, D. Chetverikov, M. Demirer, E. Duflo, C. Hansen, and a. W. Newey. Double Machine Learning for Treatment and Causal Parameters. ArXiv preprint arXiv:1608.00060, 2016.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

econml-0.4.tar.gz (170.8 kB view details)

Uploaded Source

Built Distributions

econml-0.4-py3.6.egg (278.7 kB view details)

Uploaded Source

econml-0.4-py3-none-any.whl (187.5 kB view details)

Uploaded Python 3

File details

Details for the file econml-0.4.tar.gz.

File metadata

  • Download URL: econml-0.4.tar.gz
  • Upload date:
  • Size: 170.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for econml-0.4.tar.gz
Algorithm Hash digest
SHA256 488a4793018bb03dabfc2ead93f9a5971964a2530bc47bc8a3f34c8b65d092f2
MD5 079500e2086dcb8c893008578a39108a
BLAKE2b-256 ff1709700d64527d43c744aafd7dad36f9dfa758c9777c38a5bfbbd57feea5c4

See more details on using hashes here.

File details

Details for the file econml-0.4-py3.6.egg.

File metadata

  • Download URL: econml-0.4-py3.6.egg
  • Upload date:
  • Size: 278.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for econml-0.4-py3.6.egg
Algorithm Hash digest
SHA256 54d194adc28d2d71c92f5e1cb9ce0969fc0eedca8948b06f2eea944c6d7bfb4f
MD5 f1995fcbd5f6e8ce915e2c042ecd4925
BLAKE2b-256 d9ad21a7b838dfbe8f2c97071ee75a175cda567d71d5c114d964c320dbde97e3

See more details on using hashes here.

File details

Details for the file econml-0.4-py3-none-any.whl.

File metadata

  • Download URL: econml-0.4-py3-none-any.whl
  • Upload date:
  • Size: 187.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for econml-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0c832308406b4ff9f90939e63825a26aa02d99821d3679b024e49599a8d3c093
MD5 f163a3c28cea423e4bb8e84e3ed4ca0e
BLAKE2b-256 919a8ce586fbacdbd558fd4cecd014f7d9a86c98b99e2f0e03e60f5006470c04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page