Conditional Average Treatment Effects (CATE) with DoWhy and EconML#

This is an experimental feature where we use EconML methods from DoWhy. Using EconML allows CATE estimation using different methods.

All four steps of causal inference in DoWhy remain the same: model, identify, estimate, and refute. The key difference is that we now call econml methods in the estimation step. There is also a simpler example using linear regression to understand the intuition behind CATE estimators.

All datasets are generated using linear structural equations.

[1]:
%load_ext autoreload
%autoreload 2
[2]:
import numpy as np
import pandas as pd
import logging

import dowhy
from dowhy import CausalModel
import dowhy.datasets

import econml
import warnings
warnings.filterwarnings('ignore')

BETA = 10
[3]:
data = dowhy.datasets.linear_dataset(BETA, num_common_causes=4, num_samples=10000,
                                    num_instruments=2, num_effect_modifiers=2,
                                     num_treatments=1,
                                    treatment_is_binary=False,
                                    num_discrete_common_causes=2,
                                    num_discrete_effect_modifiers=0,
                                    one_hot_encode=False)
df=data['df']
print(df.head())
print("True causal estimate is", data["ate"])
         X0        X1   Z0        Z1        W0        W1 W2 W3         v0  \
0 -0.115786  0.886347  0.0  0.273524  1.165535 -0.439911  3  2  18.697869
1 -0.407948 -0.562698  1.0  0.707057 -0.843281 -0.472963  1  3  29.503430
2  0.466475 -1.323343  0.0  0.415082 -0.496067  1.010772  1  3  19.885066
3 -1.145593 -0.895917  0.0  0.546935  0.345397  1.469954  2  3  27.750126
4  0.099621 -2.134127  0.0  0.734060 -0.753740  1.062521  2  3  22.753793

            y
0  233.263821
1  235.113830
2  189.040352
3  156.430687
4  158.983834
True causal estimate is 6.814138302852309
[4]:
model = CausalModel(data=data["df"],
                    treatment=data["treatment_name"], outcome=data["outcome_name"],
                    graph=data["gml_graph"])
[5]:
model.view_model()
from IPython.display import Image, display
display(Image(filename="causal_model.png"))
../_images/example_notebooks_dowhy-conditional-treatment-effects_5_0.png
../_images/example_notebooks_dowhy-conditional-treatment-effects_5_1.png
[6]:
identified_estimand= model.identify_effect(proceed_when_unidentifiable=True)
print(identified_estimand)
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d
─────(E[y|W0,W3,W2,W1])
d[v₀]
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W0,W3,W2,W1,U) = P(y|v0,W0,W3,W2,W1)

### Estimand : 2
Estimand name: iv
Estimand expression:
 ⎡                              -1⎤
 ⎢    d        ⎛    d          ⎞  ⎥
E⎢─────────(y)⋅⎜─────────([v₀])⎟  ⎥
 ⎣d[Z₁  Z₀]    ⎝d[Z₁  Z₀]      ⎠  ⎦
Estimand assumption 1, As-if-random: If U→→y then ¬(U →→{Z1,Z0})
Estimand assumption 2, Exclusion: If we remove {Z1,Z0}→{v0}, then ¬({Z1,Z0}→y)

### Estimand : 3
Estimand name: frontdoor
No such variable(s) found!

Linear Model#

First, let us build some intuition using a linear model for estimating CATE. The effect modifiers (that lead to a heterogeneous treatment effect) can be modeled as interaction terms with the treatment. Thus, their value modulates the effect of treatment.

Below the estimated effect of changing treatment from 0 to 1.

[7]:
linear_estimate = model.estimate_effect(identified_estimand,
                                        method_name="backdoor.linear_regression",
                                       control_value=0,
                                       treatment_value=1)
print(linear_estimate)
*** Causal Estimate ***

## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d
─────(E[y|W0,W3,W2,W1])
d[v₀]
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W0,W3,W2,W1,U) = P(y|v0,W0,W3,W2,W1)

## Realized estimand
b: y~v0+W0+W3+W2+W1+v0*X1+v0*X0
Target units:

## Estimate
Mean value: 6.814086652020999
### Conditional Estimates
__categorical__X1  __categorical__X0
(-4.638, -1.709]   (-4.064, -1.359]      0.019109
                   (-1.359, -0.764]      2.618473
                   (-0.764, -0.269]      4.158167
                   (-0.269, 0.318]       5.813293
                   (0.318, 3.098]        8.356599
(-1.709, -1.116]   (-4.064, -1.359]      1.633835
                   (-1.359, -0.764]      4.258786
                   (-0.764, -0.269]      5.806603
                   (-0.269, 0.318]       7.370769
                   (0.318, 3.098]        9.986723
(-1.116, -0.603]   (-4.064, -1.359]      2.637305
                   (-1.359, -0.764]      5.212477
                   (-0.764, -0.269]      6.806407
                   (-0.269, 0.318]       8.416347
                   (0.318, 3.098]       10.901608
(-0.603, -0.0157]  (-4.064, -1.359]      3.720069
                   (-1.359, -0.764]      6.258866
                   (-0.764, -0.269]      7.811525
                   (-0.269, 0.318]       9.378979
                   (0.318, 3.098]       11.845948
(-0.0157, 3.058]   (-4.064, -1.359]      5.244623
                   (-1.359, -0.764]      7.897146
                   (-0.764, -0.269]      9.502085
                   (-0.269, 0.318]      11.032859
                   (0.318, 3.098]       13.649525
dtype: float64

EconML methods#

We now move to the more advanced methods from the EconML package for estimating CATE.

First, let us look at the double machine learning estimator. Method_name corresponds to the fully qualified name of the class that we want to use. For double ML, it is “econml.dml.DML”.

Target units defines the units over which the causal estimate is to be computed. This can be a lambda function filter on the original dataframe, a new Pandas dataframe, or a string corresponding to the three main kinds of target units (“ate”, “att” and “atc”). Below we show an example of a lambda function.

Method_params are passed directly to EconML. For details on allowed parameters, refer to the EconML documentation.

[8]:
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LassoCV
from sklearn.ensemble import GradientBoostingRegressor
dml_estimate = model.estimate_effect(identified_estimand, method_name="backdoor.econml.dml.DML",
                                     control_value = 0,
                                     treatment_value = 1,
                                 target_units = lambda df: df["X0"]>1,  # condition used for CATE
                                 confidence_intervals=False,
                                method_params={"init_params":{'model_y':GradientBoostingRegressor(),
                                                              'model_t': GradientBoostingRegressor(),
                                                              "model_final":LassoCV(fit_intercept=False),
                                                              'featurizer':PolynomialFeatures(degree=1, include_bias=False)},
                                               "fit_params":{}})
print(dml_estimate)
*** Causal Estimate ***

## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d
─────(E[y|W0,W3,W2,W1])
d[v₀]
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W0,W3,W2,W1,U) = P(y|v0,W0,W3,W2,W1)

## Realized estimand
b: y~v0+W0+W3+W2+W1 | X1,X0
Target units: Data subset defined by a function

## Estimate
Mean value: 12.472487021578369
Effect estimates: [[11.35798395]
 [11.58865153]
 [ 7.75460311]
 [13.17703408]
 [14.0514222 ]
 [11.7766626 ]
 [13.17411418]
 [17.59859641]
 [13.83721905]
 [13.1590478 ]
 [11.28217162]
 [16.70946538]
 [13.84749652]
 [12.42766319]
 [12.81772244]
 [13.34389905]
 [14.03186792]
 [15.95821096]
 [17.66247577]
 [ 8.26116023]
 [15.86897457]
 [11.2539094 ]
 [13.4085242 ]
 [11.85888082]
 [13.93268361]
 [13.21608264]
 [ 8.91604995]
 [11.35565883]
 [10.1611137 ]
 [13.72682134]
 [12.12176536]
 [12.9210354 ]
 [16.11564995]
 [15.27447881]
 [11.27307472]
 [ 8.57454762]
 [17.36681785]
 [ 9.73035995]
 [14.40726779]
 [11.61295746]
 [ 8.78010473]
 [16.01786611]
 [14.32978998]
 [10.41722424]
 [13.7269995 ]
 [13.83533464]
 [14.64730893]
 [12.765736  ]
 [12.65310253]
 [15.2324996 ]
 [12.13975461]
 [14.66648134]
 [16.79474572]
 [10.62191182]
 [12.01512595]
 [11.71353939]
 [12.06271663]
 [12.9347533 ]
 [16.25348528]
 [11.74727987]
 [11.97875228]
 [14.40486457]
 [13.71219883]
 [11.58299794]
 [10.46980689]
 [18.12019098]
 [ 8.98515503]
 [13.6811615 ]
 [11.28834213]
 [10.95506828]
 [ 7.15838246]
 [14.76120193]
 [14.67465725]
 [10.32193197]
 [14.14656714]
 [12.2086769 ]
 [16.99938909]
 [13.02337954]
 [10.46322095]
 [11.90219865]
 [ 9.61515521]
 [13.49426653]
 [14.02293151]
 [14.09864049]
 [12.50934635]
 [12.02483381]
 [17.45825986]
 [12.94400919]
 [15.51237215]
 [12.0310926 ]
 [ 9.53718238]
 [10.24969001]
 [14.50599093]
 [16.39470151]
 [10.40162412]
 [12.54549931]
 [14.26965823]
 [ 9.97567687]
 [10.14938567]
 [11.73771236]
 [12.92790186]
 [10.64258837]
 [10.55099643]
 [11.8795311 ]
 [13.04958863]
 [11.53669036]
 [13.70833124]
 [12.05177725]
 [12.8805055 ]
 [16.29613077]
 [11.63259347]
 [14.00349281]
 [11.17993669]
 [13.91331922]
 [ 8.45432195]
 [ 9.73619837]
 [14.6655922 ]
 [14.50108731]
 [ 9.98291944]
 [14.53381666]
 [10.72274175]
 [13.3098202 ]
 [13.26201797]
 [11.33999371]
 [12.23417639]
 [11.32423936]
 [13.78649197]
 [15.29834654]
 [15.70532866]
 [11.69969556]
 [12.06528335]
 [ 8.38172865]
 [11.71677798]
 [13.48537237]
 [11.49098463]
 [11.44119589]
 [13.43215161]
 [15.49439525]
 [15.3670375 ]
 [ 8.1346563 ]
 [12.38531289]
 [12.02175132]
 [13.3177345 ]
 [17.59313317]
 [10.30734363]
 [10.03448752]
 [11.14270733]
 [12.6029418 ]
 [11.40265009]
 [12.58659133]
 [11.71962156]
 [12.29520348]
 [10.24110884]
 [10.62692729]
 [11.29836379]
 [ 9.29842828]
 [11.76568188]
 [ 9.23465368]
 [13.43630685]
 [13.01349548]
 [ 9.45371514]
 [10.31983061]
 [10.60532877]
 [12.48273712]
 [14.99543212]
 [10.15105025]
 [14.26430135]
 [12.83215955]
 [ 9.69727111]
 [ 8.7182288 ]
 [17.11564988]
 [11.78953753]
 [12.82614313]
 [11.30293416]
 [13.17993594]
 [11.84284756]
 [19.41446003]
 [10.05937692]
 [12.37938877]
 [ 9.39219535]
 [15.08134922]
 [11.67764191]
 [12.27269167]
 [12.49856848]
 [12.33644499]
 [13.08423155]
 [11.98673912]
 [10.92291472]
 [ 8.39542398]
 [12.988847  ]
 [15.07774653]
 [14.69071945]
 [13.88546248]
 [12.97224122]
 [10.33700523]
 [12.6897166 ]
 [11.30312156]
 [13.20707576]
 [11.18832803]
 [14.587101  ]
 [13.56175888]
 [ 9.16776107]
 [10.36432181]
 [14.40173404]
 [12.5534983 ]
 [10.0833123 ]
 [10.34164406]
 [10.87447606]
 [11.55425629]
 [13.52599996]
 [10.47982406]
 [ 8.06784908]
 [16.26931738]
 [12.84225313]
 [12.61936906]
 [11.03653617]
 [12.50318148]
 [11.66713327]
 [15.33354822]
 [13.16097211]
 [ 9.59252567]
 [12.61615535]
 [15.91797184]
 [12.42037818]
 [14.60217394]
 [12.91348564]
 [ 9.49100739]
 [14.36589249]
 [13.08646754]
 [11.57845034]
 [15.23149915]
 [10.29331103]
 [12.7036345 ]
 [14.21478675]
 [12.16083127]
 [13.2247406 ]
 [10.0859488 ]
 [12.41008498]
 [11.17939516]
 [11.84974344]
 [13.29448633]
 [ 9.3093996 ]
 [13.54325333]
 [11.4405644 ]
 [ 8.61027547]
 [10.07428283]
 [ 9.33744552]
 [16.01718796]
 [12.23999224]
 [ 9.81859977]
 [ 9.75408744]
 [ 8.48962349]
 [11.80158599]
 [10.87373339]
 [10.42154848]
 [10.73003852]
 [10.90099622]
 [13.02271695]
 [ 9.33995029]
 [ 8.96846763]
 [17.31521733]
 [17.73837552]
 [16.4230501 ]
 [ 9.72663071]
 [13.22409048]
 [15.18962196]
 [13.29245995]
 [19.5478674 ]
 [14.66020785]
 [12.98419947]
 [10.77844062]
 [ 9.92635329]
 [13.44111322]
 [12.52900967]
 [11.43772945]
 [12.5685008 ]
 [11.32471511]
 [ 9.65531111]
 [13.07228454]
 [13.68071396]
 [11.72048256]
 [11.38471919]
 [12.18798543]
 [11.33404081]
 [11.09659357]
 [12.81809145]
 [11.37459905]
 [14.6645066 ]
 [12.41941564]
 [15.78689733]
 [11.83374175]
 [10.55621294]
 [11.34952643]
 [10.8695918 ]
 [13.89322055]
 [10.59591202]
 [ 7.52234883]
 [12.59330708]
 [12.45265276]
 [10.88816194]
 [12.4097188 ]
 [13.43776465]
 [10.96113179]
 [11.96065054]
 [13.4248156 ]
 [15.80047919]
 [10.75072911]
 [13.34061759]
 [13.2438937 ]
 [13.2249325 ]
 [12.75379278]
 [11.77209166]
 [12.09988754]
 [13.62899271]
 [11.99517934]
 [14.45837276]
 [14.45909836]
 [10.26388892]
 [14.14556564]
 [13.88977938]
 [14.02338544]
 [10.97813163]
 [12.35010419]
 [16.33057233]
 [14.08984837]
 [10.51448517]
 [12.26316333]
 [14.47082745]
 [10.98577978]
 [ 6.67371438]
 [13.40081908]
 [11.40781459]
 [12.72797448]
 [14.77556574]
 [12.14971526]
 [16.144473  ]
 [11.4615527 ]
 [ 8.37976414]
 [10.1552508 ]
 [ 9.73338733]
 [10.46847279]
 [12.2128042 ]
 [10.64599123]
 [13.32335831]
 [14.58697452]
 [ 9.09783576]
 [13.69176792]
 [11.50139254]
 [11.58912609]
 [13.03172457]
 [11.83696129]
 [13.02219282]
 [11.39120894]
 [13.43573758]
 [13.64370799]
 [12.1731246 ]
 [12.71187642]
 [ 9.73571628]
 [13.99572987]
 [ 9.41386474]
 [12.59773064]
 [16.52242779]
 [12.10587874]
 [13.24209312]
 [11.46035527]
 [12.78918345]
 [ 8.81600141]
 [11.56091331]
 [11.78184789]
 [ 9.05130613]
 [12.68159251]
 [13.19927662]
 [13.07285335]
 [12.66353695]
 [11.59567807]
 [ 9.95038228]
 [ 7.40757205]
 [16.08602948]
 [14.00339861]
 [ 8.38788335]
 [13.32625647]
 [14.32706661]
 [11.53922533]
 [14.75049543]
 [14.22724491]
 [11.15953985]
 [16.09385488]
 [12.24297344]
 [15.29708367]
 [12.89460319]
 [13.05485086]
 [15.12123158]
 [11.33400777]
 [13.29196925]
 [13.12900208]
 [10.80180919]
 [12.67023504]
 [ 9.70977891]
 [10.24177743]
 [13.14430928]
 [13.08598714]
 [13.55850306]
 [13.4603694 ]
 [ 8.73510766]
 [11.69954104]
 [15.14100666]
 [14.97475693]
 [ 6.15505161]
 [14.50012835]
 [13.03207494]
 [10.94150837]
 [12.42135446]
 [13.93102817]
 [10.36924733]
 [11.59322244]
 [ 9.68895099]
 [10.46170519]
 [13.41605207]
 [10.00031867]
 [16.89053713]
 [ 9.18879538]
 [ 8.68678247]
 [ 8.11103097]
 [13.35515237]
 [12.45741229]
 [ 7.51934319]
 [16.57078355]
 [ 8.92194132]
 [15.61644491]
 [15.42312489]
 [ 8.33480804]
 [17.13210062]
 [10.2086607 ]
 [10.18562278]
 [13.84445137]
 [10.06498112]
 [18.4792854 ]
 [10.82838425]
 [10.69989711]
 [14.74631402]
 [11.73715944]
 [16.51684208]
 [11.60562395]
 [12.78655502]
 [ 8.25146111]
 [14.56552389]
 [ 9.57188932]
 [10.00110601]
 [14.12553229]
 [12.53068878]
 [10.8467476 ]
 [ 9.35390372]
 [14.97277424]
 [12.11916054]
 [12.06199875]
 [13.79718085]
 [11.92739831]
 [14.48278178]
 [10.26433614]
 [12.5935735 ]
 [10.36597858]
 [14.57973291]
 [10.5305311 ]
 [15.54322589]
 [14.5481873 ]
 [11.11991227]
 [12.07324193]
 [13.86396043]
 [11.37602935]
 [11.63552273]
 [10.86410348]
 [10.36529484]
 [17.0145809 ]
 [13.30932753]
 [12.65149822]
 [13.11614007]
 [11.10095171]
 [13.2252962 ]
 [15.82982826]
 [10.73779509]
 [12.29855948]
 [12.91326677]
 [10.11150928]
 [12.7761893 ]
 [12.07700671]
 [10.50043534]
 [ 9.7901491 ]
 [15.02949002]
 [10.3988642 ]
 [12.20671689]
 [13.42895704]
 [ 9.08865687]
 [13.29486422]
 [11.72201849]
 [11.17709414]
 [11.19736712]
 [11.85489538]
 [14.19293672]
 [13.74070324]
 [14.80855651]
 [11.74674903]
 [ 9.67894802]
 [14.76951654]
 [17.57723677]
 [14.32787077]
 [16.64316035]
 [12.8959295 ]
 [11.76251865]
 [12.23735369]
 [13.21002388]
 [16.48775327]
 [14.98702671]
 [11.59929806]
 [12.1183481 ]
 [13.5877881 ]
 [19.53353334]
 [12.24243004]
 [11.14982862]
 [ 9.76337345]
 [10.11086264]
 [17.64049602]
 [14.37351533]
 [ 9.58105524]
 [14.9778052 ]
 [13.6915256 ]
 [12.26098806]
 [14.12546701]
 [10.51187339]
 [16.62466927]
 [10.39489009]
 [13.96101544]
 [13.78669005]
 [13.61774286]
 [16.67018447]
 [11.82638695]
 [14.13061914]
 [12.45098496]
 [ 9.32209949]
 [12.07003299]
 [ 9.21484527]
 [11.15345951]
 [10.39954201]
 [11.56158776]
 [ 9.10949119]
 [13.39754317]
 [ 9.27884052]
 [11.09110093]
 [ 8.09672282]
 [13.86746718]
 [13.81667017]
 [17.63845955]
 [11.50576309]
 [15.38387275]
 [15.17860268]
 [17.21649991]
 [13.84978285]
 [13.55121572]
 [13.04351024]
 [13.09133177]
 [11.80666685]
 [13.87143112]
 [12.37067454]
 [ 9.56008999]
 [ 8.39784319]
 [12.03473674]
 [13.4427704 ]
 [12.11185471]
 [12.18556516]
 [11.42988756]
 [15.03758794]
 [13.28226131]
 [14.30846686]
 [11.50912481]
 [14.93967449]
 [12.29163344]
 [17.22000715]
 [13.31977503]
 [10.67665021]
 [13.82049278]
 [11.29902304]
 [12.45281221]
 [14.54462389]
 [11.04200658]
 [19.94446549]
 [ 9.99664169]
 [14.23206333]
 [11.94576488]
 [12.63446418]
 [11.50363537]
 [16.69715312]
 [14.91931365]
 [14.57435549]
 [10.59322694]
 [11.66747336]
 [ 8.28055937]
 [13.7262211 ]
 [15.69644251]
 [14.06742425]
 [11.92534524]
 [12.95798147]
 [11.89062359]
 [ 9.93939961]
 [11.6298759 ]
 [11.68719205]
 [14.18827508]
 [12.36104594]
 [ 8.77166603]
 [12.5800281 ]
 [12.40328378]
 [11.71516304]
 [12.50577399]
 [ 8.66563211]
 [ 9.98891259]
 [18.33728048]
 [12.83848277]
 [13.40349971]
 [13.70537907]
 [11.35993917]]

[9]:
print("True causal estimate is", data["ate"])
True causal estimate is 6.814138302852309
[10]:
dml_estimate = model.estimate_effect(identified_estimand, method_name="backdoor.econml.dml.DML",
                                     control_value = 0,
                                     treatment_value = 1,
                                 target_units = 1,  # condition used for CATE
                                 confidence_intervals=False,
                                method_params={"init_params":{'model_y':GradientBoostingRegressor(),
                                                              'model_t': GradientBoostingRegressor(),
                                                              "model_final":LassoCV(fit_intercept=False),
                                                              'featurizer':PolynomialFeatures(degree=1, include_bias=True)},
                                               "fit_params":{}})
print(dml_estimate)
*** Causal Estimate ***

## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d
─────(E[y|W0,W3,W2,W1])
d[v₀]
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W0,W3,W2,W1,U) = P(y|v0,W0,W3,W2,W1)

## Realized estimand
b: y~v0+W0+W3+W2+W1 | X1,X0
Target units:

## Estimate
Mean value: 6.718976387968945
Effect estimates: [[11.29879019]
 [ 7.63246516]
 [ 8.77470323]
 ...
 [ 8.29831206]
 [ 7.30809026]
 [ 9.6767495 ]]

CATE Object and Confidence Intervals#

EconML provides its own methods to compute confidence intervals. Using BootstrapInference in the example below.

[11]:
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LassoCV
from sklearn.ensemble import GradientBoostingRegressor
from econml.inference import BootstrapInference
dml_estimate = model.estimate_effect(identified_estimand,
                                     method_name="backdoor.econml.dml.DML",
                                     target_units = "ate",
                                     confidence_intervals=True,
                                     method_params={"init_params":{'model_y':GradientBoostingRegressor(),
                                                              'model_t': GradientBoostingRegressor(),
                                                              "model_final": LassoCV(fit_intercept=False),
                                                              'featurizer':PolynomialFeatures(degree=1, include_bias=True)},
                                               "fit_params":{
                                                               'inference': BootstrapInference(n_bootstrap_samples=100, n_jobs=-1),
                                                            }
                                              })
print(dml_estimate)
*** Causal Estimate ***

## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d
─────(E[y|W0,W3,W2,W1])
d[v₀]
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W0,W3,W2,W1,U) = P(y|v0,W0,W3,W2,W1)

## Realized estimand
b: y~v0+W0+W3+W2+W1 | X1,X0
Target units: ate

## Estimate
Mean value: 6.734673668348556
Effect estimates: [[11.30937603]
 [ 7.64735087]
 [ 8.79336509]
 ...
 [ 8.31535902]
 [ 7.32189598]
 [ 9.68974865]]
95.0% confidence interval: [[[11.27970413  7.63547486  8.7545313  ...  8.29952879  7.31352143
    9.67211911]]

 [[11.59111253  7.78279699  8.98433123 ...  8.4815012   7.44836272
    9.90048576]]]

Can provide a new inputs as target units and estimate CATE on them.#

[12]:
test_cols= data['effect_modifier_names'] # only need effect modifiers' values
test_arr = [np.random.uniform(0,1, 10) for _ in range(len(test_cols))] # all variables are sampled uniformly, sample of 10
test_df = pd.DataFrame(np.array(test_arr).transpose(), columns=test_cols)
dml_estimate = model.estimate_effect(identified_estimand,
                                     method_name="backdoor.econml.dml.DML",
                                     target_units = test_df,
                                     confidence_intervals=False,
                                     method_params={"init_params":{'model_y':GradientBoostingRegressor(),
                                                              'model_t': GradientBoostingRegressor(),
                                                              "model_final":LassoCV(),
                                                              'featurizer':PolynomialFeatures(degree=1, include_bias=True)},
                                               "fit_params":{}
                                              })
print(dml_estimate.cate_estimates)
[[11.9642651 ]
 [12.12723688]
 [12.09880049]
 [13.1884783 ]
 [12.62759431]
 [11.5579824 ]
 [13.85803999]
 [12.7464567 ]
 [10.79390616]
 [11.89480246]]

Can also retrieve the raw EconML estimator object for any further operations#

[13]:
print(dml_estimate._estimator_object)
<econml.dml.dml.DML object at 0x7f03e296afd0>

Works with any EconML method#

In addition to double machine learning, below we example analyses using orthogonal forests, DRLearner (bug to fix), and neural network-based instrumental variables.

Binary treatment, Binary outcome#

[14]:
data_binary = dowhy.datasets.linear_dataset(BETA, num_common_causes=4, num_samples=10000,
                                    num_instruments=1, num_effect_modifiers=2,
                                    treatment_is_binary=True, outcome_is_binary=True)
# convert boolean values to {0,1} numeric
data_binary['df'].v0 = data_binary['df'].v0.astype(int)
data_binary['df'].y = data_binary['df'].y.astype(int)
print(data_binary['df'])

model_binary = CausalModel(data=data_binary["df"],
                    treatment=data_binary["treatment_name"], outcome=data_binary["outcome_name"],
                    graph=data_binary["gml_graph"])
identified_estimand_binary = model_binary.identify_effect(proceed_when_unidentifiable=True)
            X0        X1   Z0        W0        W1        W2        W3  v0  y
0    -0.274904  3.059553  0.0  0.484849  2.072388 -1.125590 -2.228188   0  0
1     0.397075  0.361155  1.0  0.220431 -0.114853 -0.442494 -1.111725   1  1
2    -1.177774 -0.396454  0.0  0.326009  0.227604  0.024575 -2.060106   0  0
3    -0.215870 -0.086596  1.0 -0.158992  1.672587  0.450781  0.729116   1  1
4     1.717884 -1.236285  1.0  1.588426  0.988368 -0.449457 -2.250651   1  1
...        ...       ...  ...       ...       ...       ...       ...  .. ..
9995 -1.060382  0.516601  1.0  0.952794 -0.122010 -0.792453 -2.270641   0  0
9996  0.243871  0.372210  0.0  0.106365  0.459577 -0.404531 -1.077417   0  0
9997 -0.595850  1.715115  0.0  1.464994  0.920318 -0.892116 -1.556017   0  0
9998 -2.245326  1.408546  1.0 -0.637026 -0.124252  0.257762  0.083195   1  1
9999 -0.272595 -0.030628  1.0  1.149912  1.615597 -0.341732 -2.831388   1  1

[10000 rows x 9 columns]

Using DRLearner estimator#

[15]:
from sklearn.linear_model import LogisticRegressionCV
#todo needs binary y
drlearner_estimate = model_binary.estimate_effect(identified_estimand_binary,
                                method_name="backdoor.econml.dr.LinearDRLearner",
                                confidence_intervals=False,
                                method_params={"init_params":{
                                                    'model_propensity': LogisticRegressionCV(cv=3, solver='lbfgs', multi_class='auto')
                                                    },
                                               "fit_params":{}
                                              })
print(drlearner_estimate)
print("True causal estimate is", data_binary["ate"])
*** Causal Estimate ***

## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d
─────(E[y|W0,W3,W2,W1])
d[v₀]
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W0,W3,W2,W1,U) = P(y|v0,W0,W3,W2,W1)

## Realized estimand
b: y~v0+W0+W3+W2+W1 | X1,X0
Target units: ate

## Estimate
Mean value: 0.5341623383787538
Effect estimates: [[0.56470052]
 [0.5364521 ]
 [0.52372615]
 ...
 [0.54884323]
 [0.54094153]
 [0.53026463]]

True causal estimate is 0.4485

Instrumental Variable Method#

[16]:
dmliv_estimate = model.estimate_effect(identified_estimand,
                                        method_name="iv.econml.iv.dml.DMLIV",
                                        target_units = lambda df: df["X0"]>-1,
                                        confidence_intervals=False,
                                method_params={"init_params":{
                                                              'discrete_treatment':False,
                                                              'discrete_instrument':False
                                                             },
                                               "fit_params":{}})
print(dmliv_estimate)
*** Causal Estimate ***

## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: iv
Estimand expression:
 ⎡                              -1⎤
 ⎢    d        ⎛    d          ⎞  ⎥
E⎢─────────(y)⋅⎜─────────([v₀])⎟  ⎥
 ⎣d[Z₁  Z₀]    ⎝d[Z₁  Z₀]      ⎠  ⎦
Estimand assumption 1, As-if-random: If U→→y then ¬(U →→{Z1,Z0})
Estimand assumption 2, Exclusion: If we remove {Z1,Z0}→{v0}, then ¬({Z1,Z0}→y)

## Realized estimand
b: y~v0+W0+W3+W2+W1 | X1,X0
Target units: Data subset defined by a function

## Estimate
Mean value: 8.253642788065598
Effect estimates: [[11.53162306]
 [ 7.72784209]
 [ 8.6711528 ]
 ...
 [ 8.27961792]
 [ 7.46031948]
 [ 9.82462308]]

Metalearners#

[17]:
data_experiment = dowhy.datasets.linear_dataset(BETA, num_common_causes=5, num_samples=10000,
                                    num_instruments=2, num_effect_modifiers=5,
                                    treatment_is_binary=True, outcome_is_binary=False)
# convert boolean values to {0,1} numeric
data_experiment['df'].v0 = data_experiment['df'].v0.astype(int)
print(data_experiment['df'])
model_experiment = CausalModel(data=data_experiment["df"],
                    treatment=data_experiment["treatment_name"], outcome=data_experiment["outcome_name"],
                    graph=data_experiment["gml_graph"])
identified_estimand_experiment = model_experiment.identify_effect(proceed_when_unidentifiable=True)
            X0        X1        X2        X3        X4   Z0        Z1  \
0    -1.932084  1.136512 -0.389367 -0.076252  0.759181  0.0  0.227064
1    -0.777060  0.930151  0.247036  1.497312 -0.589523  0.0  0.988054
2     0.298298  3.046355 -2.315320 -1.139840 -0.801745  0.0  0.462610
3    -1.813180  0.643101 -1.868251 -0.361255  1.499118  1.0  0.026595
4    -0.234770  0.921801 -0.613060  0.693038 -0.634193  0.0  0.100257
...        ...       ...       ...       ...       ...  ...       ...
9995 -0.846952  0.393042 -0.421911 -1.823197 -1.076180  0.0  0.922284
9996 -0.139719  0.116947 -0.545799 -0.765484  0.387768  0.0  0.424084
9997  0.791286 -0.437980 -1.458165 -1.089568 -0.424289  0.0  0.391986
9998 -1.427741 -0.933949 -0.353541 -0.233056 -0.330201  0.0  0.231693
9999 -1.444598 -0.641976 -0.135012  1.715607 -0.101838  0.0  0.946984

            W0        W1        W2        W3        W4  v0         y
0     0.428325 -0.029410 -1.427274  0.089205  0.205915   1  5.826773
1     0.408357  0.599696 -1.525233  0.299341 -1.338285   1  5.116411
2     0.386342 -0.497189 -0.119797  0.371819 -0.435458   1  1.669797
3    -1.236691 -0.833541 -0.369903  1.302128  1.790599   1  5.816574
4     0.536352 -2.790315 -0.785533  1.614928 -0.896869   1  4.684101
...        ...       ...       ...       ...       ...  ..       ...
9995 -0.030341 -0.507823  0.237217  0.009697  0.031376   1  0.144866
9996 -0.112841  0.055808 -0.849705 -0.681086 -2.115430   0 -9.429483
9997  0.380298 -0.584118  0.086391 -0.689556 -1.527852   1 -4.457761
9998  1.120929  0.074552 -1.111431 -1.217129  0.352709   1 -2.953001
9999  0.446493 -0.322832  0.048454  0.505652 -0.482340   1  8.498693

[10000 rows x 14 columns]
[18]:
from sklearn.ensemble import RandomForestRegressor
metalearner_estimate = model_experiment.estimate_effect(identified_estimand_experiment,
                                method_name="backdoor.econml.metalearners.TLearner",
                                confidence_intervals=False,
                                method_params={"init_params":{
                                                    'models': RandomForestRegressor()
                                                    },
                                               "fit_params":{}
                                              })
print(metalearner_estimate)
print("True causal estimate is", data_experiment["ate"])
*** Causal Estimate ***

## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d
─────(E[y|W0,W3,W2,W4,W1])
d[v₀]
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W0,W3,W2,W4,W1,U) = P(y|v0,W0,W3,W2,W4,W1)

## Realized estimand
b: y~v0+X1+X0+X4+X2+X3+W0+W3+W2+W4+W1
Target units: ate

## Estimate
Mean value: 8.906765511007583
Effect estimates: [[12.9817867 ]
 [14.28140721]
 [ 2.69729158]
 ...
 [-0.77505617]
 [ 3.21768587]
 [ 9.43546237]]

True causal estimate is 5.6012223032416335

Avoiding retraining the estimator#

Once an estimator is fitted, it can be reused to estimate effect on different data points. In this case, you can pass fit_estimator=False to estimate_effect. This works for any EconML estimator. We show an example for the T-learner below.

[19]:
# For metalearners, need to provide all the features (except treatmeant and outcome)
metalearner_estimate = model_experiment.estimate_effect(identified_estimand_experiment,
                                method_name="backdoor.econml.metalearners.TLearner",
                                confidence_intervals=False,
                                fit_estimator=False,
                                target_units=data_experiment["df"].drop(["v0","y", "Z0", "Z1"], axis=1)[9995:],
                                method_params={})
print(metalearner_estimate)
print("True causal estimate is", data_experiment["ate"])
*** Causal Estimate ***

## Identified estimand
Estimand type: EstimandType.NONPARAMETRIC_ATE

### Estimand : 1
Estimand name: backdoor
Estimand expression:
  d
─────(E[y|W0,W3,W2,W4,W1])
d[v₀]
Estimand assumption 1, Unconfoundedness: If U→{v0} and U→y then P(y|v0,W0,W3,W2,W4,W1,U) = P(y|v0,W0,W3,W2,W4,W1)

## Realized estimand
b: y~v0+X1+X0+X4+X2+X3+W0+W3+W2+W4+W1
Target units: Data subset provided as a data frame

## Estimate
Mean value: 5.2927826378920795
Effect estimates: [[ 1.64637606]
 [12.93944506]
 [-0.77505617]
 [ 3.21768587]
 [ 9.43546237]]

True causal estimate is 5.6012223032416335

Refuting the estimate#

Adding a random common cause variable#

[20]:
res_random=model.refute_estimate(identified_estimand, dml_estimate, method_name="random_common_cause")
print(res_random)
Refute: Add a random common cause
Estimated effect:12.285756278272949
New effect:12.313727563951174
p value:0.6200000000000001

Adding an unobserved common cause variable#

[21]:
res_unobserved=model.refute_estimate(identified_estimand, dml_estimate, method_name="add_unobserved_common_cause",
                                     confounders_effect_on_treatment="linear", confounders_effect_on_outcome="linear",
                                    effect_strength_on_treatment=0.01, effect_strength_on_outcome=0.02)
print(res_unobserved)
Refute: Add an Unobserved Common Cause
Estimated effect:12.285756278272949
New effect:12.351316463365716

Replacing treatment with a random (placebo) variable#

[22]:
res_placebo=model.refute_estimate(identified_estimand, dml_estimate,
        method_name="placebo_treatment_refuter", placebo_type="permute",
        num_simulations=10 # at least 100 is good, setting to 10 for speed
        )
print(res_placebo)
Refute: Use a Placebo Treatment
Estimated effect:12.285756278272949
New effect:0.010059999131380318
p value:0.3417021218970768

Removing a random subset of the data#

[23]:
res_subset=model.refute_estimate(identified_estimand, dml_estimate,
        method_name="data_subset_refuter", subset_fraction=0.8,
        num_simulations=10)
print(res_subset)
Refute: Use a subset of data
Estimated effect:12.285756278272949
New effect:12.31662689446168
p value:0.29995390921191584

More refutation methods to come, especially specific to the CATE estimators.