Skip to content

Commit 53840cf

Browse files
heimengqikbattocchi
authored andcommitted
add notebook for DRIV
1 parent 1592c7a commit 53840cf

14 files changed

+1916
-368
lines changed

README.md

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,23 +319,81 @@ lb, ub = est.effect_interval(X_test, alpha=0.05)
319319
</details>
320320

321321
<details>
322-
<summary>Orthogonal Instrumental Variables (click to expand)</summary>
322+
<summary>Double Machine Learning with Instrumental Variables (click to expand)</summary>
323323

324-
* Intent to Treat Doubly Robust Learner (discrete instrument, discrete treatment)
324+
* Orthogonal instrumental variable learner
325+
326+
```Python
327+
from econml.iv.dml import OrthoIV
328+
329+
est = OrthoIV(projection=False,
330+
discrete_treatment=True,
331+
discrete_instrument=True)
332+
est.fit(Y, T, Z=Z, X=X, W=W)
333+
treatment_effects = est.effect(X_test)
334+
lb, ub = est.effect_interval(X_test, alpha=0.05) # OLS confidence intervals
335+
```
336+
* Nonparametric double machine learning with instrumental variable
337+
338+
```Python
339+
from econml.iv.dml import NonParamDMLIV
340+
341+
est = NonParamDMLIV(projection=False,
342+
discrete_treatment=True,
343+
discrete_instrument=True)
344+
est.fit(Y, T, Z=Z, X=X, W=W) # no analytical confidence interval available
345+
treatment_effects = est.effect(X_test)
346+
```
347+
</details>
348+
349+
<details>
350+
<summary>Doubly Robust Machine Learning with Instrumental Variables (click to expand)</summary>
351+
352+
* Linear final stage
353+
```Python
354+
from econml.iv.dr import LinearDRIV
355+
356+
est = LinearDRIV(discrete_instrument=True, discrete_treatment=True)
357+
est.fit(Y, T, Z=Z, X=X, W=W)
358+
treatment_effects = est.effect(X_test)
359+
lb, ub = est.effect_interval(X_test, alpha=0.05) # OLS confidence intervals
360+
```
361+
362+
* Sparse linear final stage
363+
364+
```Python
365+
from econml.iv.dr import SparseLinearDRIV
366+
367+
est = SparseLinearDRIV(discrete_instrument=True, discrete_treatment=True)
368+
est.fit(Y, T, Z=Z, X=X, W=W)
369+
treatment_effects = est.effect(X_test)
370+
lb, ub = est.effect_interval(X_test, alpha=0.05) # Debiased lasso confidence intervals
371+
```
372+
373+
* Nonparametric final stage
374+
```Python
375+
from econml.iv.dr import ForestDRIV
376+
377+
est = ForestDRIV(discrete_instrument=True, discrete_treatment=True)
378+
est.fit(Y, T, Z=Z, X=X, W=W)
379+
treatment_effects = est.effect(X_test)
380+
# Confidence intervals via Bootstrap-of-Little-Bags for forests
381+
lb, ub = est.effect_interval(X_test, alpha=0.05)
382+
```
383+
384+
* Linear intent-to-treat (discrete instrument, discrete treatment)
325385

326386
```Python
327387
from econml.iv.dr import LinearIntentToTreatDRIV
328388
from sklearn.ensemble import GradientBoostingRegressor, GradientBoostingClassifier
329-
from sklearn.linear_model import LinearRegression
330389

331390
est = LinearIntentToTreatDRIV(model_y_xw=GradientBoostingRegressor(),
332391
model_t_xwz=GradientBoostingClassifier(),
333392
flexible_model_effect=GradientBoostingRegressor())
334-
est.fit(Y, T, Z=Z, X=X) # OLS inference by default
393+
est.fit(Y, T, Z=Z, X=X, W=W)
335394
treatment_effects = est.effect(X_test)
336395
lb, ub = est.effect_interval(X_test, alpha=0.05) # OLS confidence intervals
337396
```
338-
339397
</details>
340398

341399
<details>

doc/spec/comparison.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,24 @@ Detailed estimator comparison
3535
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
3636
| :class:`.NonParamDML` | 1-d/Binary | | | Yes | | Yes | | Yes |
3737
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
38-
38+
| :class:`.OrthoIV` | Any | Yes | Yes | Yes | Assumed | Yes | Yes | |
39+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
40+
| :class:`.DMLIV` | Any | Yes | | Yes | Assumed | Yes | Yes | Yes |
41+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
42+
| :class:`.NonParamDMLIV` | 1-d/Binary | Yes | | Yes | | Yes | | Yes |
43+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
44+
| :class:`.DRIV` | 1-d/Binary | Yes | Yes | Yes | | | | Yes |
45+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
46+
| :class:`.LinearDRIV` | 1-d/Binary | Yes | Yes | Yes | Projected | | | |
47+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
48+
| :class:`.SparseLinearDRIV` | 1-d/Binary | Yes | Yes | Yes | Projected | | | Yes |
49+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
50+
| :class:`.ForestDRIV` | 1-d/Binary | Yes | Yes | Yes | | | | Yes |
51+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
52+
| :class:`.IntentToTreatDRIV` | Binary | Yes | Ye | Yes | | | | Yes |
53+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
54+
| :class:`.LinearIntentToTreatDRIV` | Binary | Yes | Yes | Yes | Projected | | | |
55+
+---------------------------------------------+--------------+--------------+------------------+-------------+-----------------+------------+--------------+--------------------+
3956

4057
Treatment Type
4158
Some estimators can only estimate effects of particular kinds of treatments.

doc/spec/estimation/orthoiv.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. _orthoivuserguide:
2+
3+
=================================
4+
Orthogonal instrumental variables
5+
=================================
6+
7+
8+
What is it?
9+
==================================
10+
Orthogonal instrumental variables is a suite of methods to estimate heterogeneous treatment effects with arbitrary machine
11+
learning methods in the presence of unobserved confounders with the aid of a valid instrument. We develop a statistical learning
12+
approach to the estimation of heterogeneous effects, reducing the problem to the minimization of an appropriate loss function that
13+
depends on a set of auxiliary models (each corresponding to a separate prediction task). The reduction enables the use of all
14+
recent Machine Learning models (e.g. random forest, boosting, neural nets). We show that the estimated effect model is robust to
15+
estimation errors in the auxiliary models, by showing that the loss satisfies a Neyman orthogonality criterion.
16+
Our approach can be used to estimate projections of the true effect model on simpler hypothesis spaces.
17+
When these spaces are parametric, then the parameter estimates are asymptotically normal,
18+
which enables construction of confidence intervals.
19+
For a more detailed overview of these methods, see e.g. [Syrgkanis2019]_.
20+
21+
22+
What are the relevant estimator classes?
23+
========================================
24+
25+
This section describes the methodology implemented in the classes :class:`.OrthoIV`,
26+
:class:`.DMLIV`, :class:`.NonParamDMLIV`, :class:`.LinearDRIV`, :class:`.SparseLinearDRIV`, :class:`.ForestDRIV`,
27+
:class:`.IntentToTreatDRIV`, :class:`.LinearIntentToTreatDRIV`.
28+
Click on each of these links for detailed module documentation and input parameters of each class.
29+
30+
When should you use it?
31+
==================================
32+
Suppose you have observational (or experimental from an A/B test) historical data, where some treatment(s)/intervention(s)/action(s)
33+
:math:`T` were chosen and some outcome(s) :math:`Y` were observed. However, not all of the variables :math:`W` that could have
34+
potentially gone into the choice of :math:`T`, and simultaneously could have had a direct effect on the outcome :math:`Y`
35+
(aka controls or confounders) are recorded in the dataset. At the same time if you could observe a variable :math:`Z` which
36+
will have a direct effect on treatment and an indirect effect on outcome which only goes through the treatment, we could use classes
37+
mentioned above to learn the heterogeneous treatment effect on high dimensional dataset. In other words, we learn the effect of the
38+
treatment on the outcome as a function of a set of observable characteristics :math:`X`.
39+
40+
In particular, these methods are especially useful in A/B tests with an intent-to-treat structure, where the experimenter randomizes over
41+
which user will receive a recommendation to take an action, and we are interested in the effect of the downstream action.
42+
43+
For instance call:
44+
45+
.. testsetup::
46+
47+
# LinearIntentToTreatDRIV
48+
import numpy as np
49+
X = np.random.normal(size=(100, 3))
50+
y = np.random.normal(size=(100,))
51+
T = np.random.binomial(1, 0.5, size=(100,))
52+
Z = np.random.binomial(1, 0.5, size=(100,))
53+
W = np.random.normal(size=(100, 10))
54+
55+
.. testcode::
56+
57+
from econml.iv.dr import LinearIntentToTreatDRIV
58+
est = LinearIntentToTreatDRIV()
59+
est.fit(y, T, Z=Z, X=X, W=X)
60+
est.effect(X)
61+
62+
63+
Class Hierarchy Structure
64+
==================================
65+
In this library we implement variants of several of the approaches mentioned in the last section. The hierarchy
66+
structure of the implemented CATE estimators is as follows.
67+
68+
.. inheritance-diagram:: econml.iv.dml.OrthoIV econml.iv.dml.NonParamDMLIV econml.iv.dml.DMLIV econml.iv.dr.DRIV
69+
econml.iv.dr.LinearDRIV econml.iv.dr.SparseLinearDRIV econml.iv.dr.ForestDRIV
70+
econml.iv.dr.IntentToTreatDRIV econml.iv.dr.LinearIntentToTreatDRIV
71+
:parts: 1
72+
:private-bases:
73+
:top-classes: econml._ortho_learner._OrthoLearner, econml._cate_estimator.StatsModelsCateEstimatorMixin, econml._cate_estimator.DebiasedLassoCateEstimatorMixin
74+
75+
76+
Usage Examples
77+
==================================
78+
79+
For more extensive examples check out the following notebooks:
80+
`OrthoIV and DRIV Examples Jupyter Notebook <https://github.com/microsoft/EconML/blob/master/notebooks/OrthoIV%20and%20DRIV%20Examples.ipynb>`_.

doc/spec/estimation_iv.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ of [Newey2003]_.
1515
:maxdepth: 2
1616

1717
estimation/deepiv.rst
18-
estimation/two_sls.rst
18+
estimation/two_sls.rst
19+
estimation/orthoiv.rst

doc/spec/references.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ References
124124
Hernán, Miguel A., and James M. Robins (2010).
125125
Causal inference.
126126
URL https://www.hsph.harvard.edu/miguel-hernan/causal-inference-book/
127+
128+
.. [Syrgkanis2019]
129+
Syrgkanis, V., Lei, V., Oprescu, M., Hei, M., Battocchi, K., Lewis, G. (2019)
130+
Machine Learning Estimation of Heterogeneous Treatment Effects with Instruments
131+
URL https://arxiv.org/abs/1905.10176

0 commit comments

Comments
 (0)