### Data Generation Setup Source: https://github.com/py-why/econml/blob/main/doc/spec/api.md Initializes the environment for generating synthetic data for the CATE estimation example. ```python import numpy as np ``` -------------------------------- ### Install EconML using pip Source: https://github.com/py-why/econml/blob/main/README.md Install the latest release of EconML from PyPI using pip. This is the recommended method for most users. ```bash pip install econml ``` -------------------------------- ### Setup SHAP Interpretability Source: https://github.com/py-why/econml/blob/main/notebooks/Doubly Robust Learner and Interpretability.ipynb Initializes imports for SHAP interpretability using scikit-learn models. ```python # We need to use a scikit-learn final model from econml.dr import DRLearner from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, GradientBoostingClassifier # One can replace model_y and model_t with any scikit-learn regressor and classifier correspondingly ``` -------------------------------- ### Load Sample Pricing Data with Pandas Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company.ipynb Imports the pandas library and reads a CSV file from a URL into a DataFrame. Ensure pandas is installed. ```python import pandas as pd # Import the sample pricing data file_url = "https://raw.githubusercontent.com/py-why/EconML/refs/heads/data/datasets/Pricing/pricing_sample.csv" train_data = pd.read_csv(file_url) ``` -------------------------------- ### Initialize Causal Forest simulation parameters Source: https://github.com/py-why/econml/blob/main/notebooks/Generalized Random Forests.ipynb Setup for generating synthetic data to demonstrate Causal Forest functionality. ```python np.random.seed(123) n_samples = 2000 n_features = 10 n_treatments = 1 # true_te = lambda X: np.hstack([X[:, [0]]**2 + 1, np.ones((X.shape[0], n_treatments - 1))]) ``` -------------------------------- ### Initialize DMLIV with Featurizer and Effect Model Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis_Step_CATE.ipynb Initializes a DMLIV model with a polynomial featurizer and a SelectiveLasso model for estimating CATE. This setup is for cases with instrumental variables and binary treatments. ```python from dml_iv import DMLIV, GenericDMLIV from utilities import SelectiveLasso, SeparateModel from sklearn.linear_model import LassoCV from econml.utilities import hstack np.random.seed(123) # We now specify the features to be used for heterogeneity. We will fit a CATE model of the form # theta(X) = # for some set of features phi(X). The featurizer needs to support fit_transform, that takes # X and returns phi(X). We need to include a bias if we also want a constant term. dmliv_featurizer = lambda: PolynomialFeatures(degree=1, include_bias=True) # Then we need to specify a model to be used for fitting the parameters theta in the linear form. # This model will minimize the square loss: # (Y - E[Y|X] - * (E[T|X,Z] - E[T|X]))**2 # potentially with some regularization on theta. Here we use an ell_1 penalty on theta # dmliv_model_effect = lambda: LinearRegression() # This line is commented out in the source # We could also use LassoCV to select the regularization weight in the final stage with # cross validation. # dmliv_model_effect = lambda: LassoCV(fit_intercept=False, cv=3) # This line is commented out in the source # If we also have a prior that there is no effect heterogeneity we can use a selective lasso # that does not penalize the constant term in the CATE model feature_inds = np.arange(1, X.shape[1]+1) dmliv_model_effect = lambda: SelectiveLasso(feature_inds, LassoCV(cv=5, n_jobs=-1)) cate = DMLIV(model_Y_X(), model_T_X(), model_T_XZ(), dmliv_model_effect(), dmliv_featurizer(), n_splits=10, # number of splits to use for cross-fitting binary_instrument=True, # a flag whether to stratify cross-fitting by instrument binary_treatment=True # a flag whether to stratify cross-fitting by treatment ) ``` -------------------------------- ### Initialize HeteroDynamicPanelDML Estimator Source: https://github.com/py-why/econml/blob/main/prototypes/dynamic_dml/high_dim_state_any_m_panel_hetero.ipynb Initializes the HeteroDynamicPanelDML estimator with specified models for treatment, outcome, and final estimation stages. This setup is crucial for estimating heterogeneous treatment effects in dynamic panel data settings. ```python from hetero_panel_dynamic_dml import HeteroDynamicPanelDML est = HeteroDynamicPanelDML(model_t=mlasso_model(), model_y=lasso_model(), model_final=slasso_model(), ``` -------------------------------- ### Initialize DMLIV with Selective Lasso Featurizer Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis.ipynb Initializes a DMLIV model using SelectiveLasso for effect modeling and PolynomialFeatures for featurization. This setup is suitable for estimating CATE with instrumental variables, allowing for feature selection with a non-penalized constant term. ```python from dml_iv import DMLIV, GenericDMLIV from utilities import SelectiveLasso, SeparateModel from sklearn.linear_model import LassoCV from econml.utilities import hstack np.random.seed(123) # We now specify the features to be used for heterogeneity. We will fit a CATE model of the form # theta(X) = # for some set of features phi(X). The featurizer needs to support fit_transform, that takes # X and returns phi(X). We need to include a bias if we also want a constant term. dmliv_featurizer = lambda: PolynomialFeatures(degree=1, include_bias=True) # Then we need to specify a model to be used for fitting the parameters theta in the linear form. # This model will minimize the square loss: # (Y - E[Y|X] - * (E[T|X,Z] - E[T|X]))**2 # potentially with some regularization on theta. Here we use an ell_1 penalty on theta # dmliv_model_effect = lambda: LinearRegression() # We could also use LassoCV to select the regularization weight in the final stage with # cross validation. # dmliv_model_effect = lambda: LassoCV(fit_intercept=False, cv=3) # If we also have a prior that there is no effect heterogeneity we can use a selective lasso # that does not penalize the constant term in the CATE model feature_inds = np.arange(1, X.shape[1]+1) dmliv_model_effect = lambda: SelectiveLasso(feature_inds, LassoCV(cv=5, n_jobs=-1)) cate = DMLIV(model_Y_X(), model_T_X(), model_T_XZ(), dmliv_model_effect(), dmliv_featurizer(), n_splits=10, # number of splits to use for cross-fitting binary_instrument=True, # a flag whether to stratify cross-fitting by instrument binary_treatment=True # a flag whether to stratify cross-fitting by treatment ) ``` -------------------------------- ### Federated Learning Example with Serialization Source: https://github.com/py-why/econml/blob/main/doc/spec/federated_learning.md Demonstrates a realistic federated learning scenario where models are trained in parallel, serialized, and then deserialized on a central machine to create a federated estimator. This is useful when data is too large to fit on a single machine. ```python import pickle # Assume 'model' is a trained EconML estimator with enable_federation=True # Serialize the model serialized_model = pickle.dumps(model) # On a central machine, deserialize the model central_model = pickle.loads(serialized_model) # In a real scenario, you would collect serialized_model from multiple machines # and then deserialize them to create a FederatedEstimator. # For example: # federated_est = FederatedEstimator([pickle.loads(m) for m in serialized_models_list]) ``` -------------------------------- ### Initialize Environment and Imports Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company - EconML + DoWhy.ipynb Essential imports and configuration for EconML, DoWhy, and machine learning utilities. ```python # Some imports to get us started # Utilities import numpy as np import pandas as pd from networkx.drawing.nx_pydot import to_pydot from IPython.display import Image, display # Generic ML imports from sklearn.preprocessing import PolynomialFeatures from sklearn.ensemble import GradientBoostingRegressor # EconML imports from econml.dml import LinearDML, CausalForestDML from econml.cate_interpreter import SingleTreeCateInterpreter, SingleTreePolicyInterpreter import matplotlib.pyplot as plt import warnings warnings.simplefilter('ignore') %matplotlib inline ``` -------------------------------- ### Initialize Environment and Imports Source: https://github.com/py-why/econml/blob/main/notebooks/Solutions/Causal Interpretation for Employee Attrition Dataset.ipynb Sets up necessary libraries for data processing, machine learning, and visualization. Configures pandas display options for better readability of large datasets. ```python # Some imports to get us started from sklearn.pipeline import Pipeline from sklearn.impute import SimpleImputer from sklearn.preprocessing import StandardScaler, OneHotEncoder from lightgbm import LGBMClassifier import pandas as pd import numpy as np import matplotlib.pyplot as plt pd.set_option("display.max_columns", 100) pd.set_option("display.max_rows", 100) pd.set_option("display.max_colwidth", 100) ``` -------------------------------- ### Get Decision Path Source: https://github.com/py-why/econml/blob/main/notebooks/Generalized Random Forests.ipynb Retrieve the decision path for specific samples. ```python est.decision_path(X_test[:1]) ``` -------------------------------- ### Initialize and Fit DMLIV Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/NLSYM_Linear.ipynb Sets up the DMLIV estimator for CATE estimation, including featurizers and effect models, then fits the data. ```python from dml_iv import DMLIV from utilities import SelectiveLasso, SeparateModel from sklearn.linear_model import LassoCV, LogisticRegressionCV from econml.utilities import hstack np.random.seed(random_seed) # For DMLIV we also need a model for E[T | X, Z]. To allow for heterogeneity in the compliance, i.e. # T = beta(X)*Z + gamma(X) # we train a separate model for Z=1 and Z=0. The model for Z=1 learns the # quantity beta(X) + gamma(X) and the model for Z=0 learns gamma(X). model_T_XZ = lambda: SeparateModel(model(), model()) # We now specify the features to be used for heterogeneity. We will fit a CATE model of the form # theta(X) = # for some set of features phi(X). The featurizer needs to support fit_transform, that takes # X and returns phi(X). We need to include a bias if we also want a constant term. dmliv_featurizer = lambda: PolynomialFeatures(degree=1, include_bias=True) # Then we need to specify a model to be used for fitting the parameters theta in the linear form. # This model will minimize the square loss: # (Y - E[Y|X] - * (E[T|X,Z] - E[T|X]))**2 dmliv_model_effect = lambda: LinearRegression(fit_intercept=False) # Potentially with some regularization on theta. Here we use an ell_1 penalty on theta # If we also have a prior that there is no effect heterogeneity we can use a selective lasso # that does not penalize the constant term in the CATE model #dmliv_model_effect = lambda: SelectiveLasso(np.arange(1, X.shape[1]+1), LassoCV(cv=5, fit_intercept=False)) # We initialize DMLIV with all these models and call fit cate = DMLIV(model_Y_X(), model_T_X(), model_T_XZ(), dmliv_model_effect(), dmliv_featurizer(), n_splits=N_SPLITS, # number of splits to use for cross-fitting binary_instrument=True, # a flag whether to stratify cross-fitting by instrument binary_treatment=True # a flag whether to stratify cross-fitting by treatment ) ``` ```python cate.fit(y, T, X, Z) ``` -------------------------------- ### High-dimensional DMLOrthoForest estimation Source: https://github.com/py-why/econml/blob/main/doc/spec/estimation/forest.md Advanced example using WeightedLasso for nuisance models with high-dimensional confounders. ```pycon >>> from econml.orf import DMLOrthoForest >>> from econml.orf import DMLOrthoForest >>> from econml.sklearn_extensions.linear_model import WeightedLasso >>> import matplotlib.pyplot as plt >>> np.random.seed(123) >>> X = np.random.uniform(-1, 1, size=(4000, 1)) >>> W = np.random.normal(size=(4000, 50)) >>> support = np.random.choice(50, 4, replace=False) >>> T = np.dot(W[:, support], np.random.normal(size=4)) + np.random.normal(size=4000) >>> Y = np.exp(2*X[:, 0]) * T + np.dot(W[:, support], np.random.normal(size=4)) + .5 >>> est = DMLOrthoForest(n_trees=100, ... max_depth=5, ... model_Y=WeightedLasso(alpha=0.01), ... model_T=WeightedLasso(alpha=0.01)) >>> est.fit(Y, T, X=X, W=W) >>> X_test = np.linspace(-1, 1, 30).reshape(-1, 1) >>> treatment_effects = est.effect(X_test) >>> plt.plot(X_test[:, 0], treatment_effects, label='ORF estimate') [] >>> plt.plot(X_test[:, 0], np.exp(2*X_test[:, 0]), 'b--', label='True effect') [] >>> plt.legend() >>> plt.show(block=False) ``` -------------------------------- ### Get PolicyTree Feature Importances Source: https://github.com/py-why/econml/blob/main/notebooks/Policy Learning with Trees and Forests.ipynb Retrieves and displays the feature importances calculated by the trained PolicyTree model. ```python est.feature_importances_ ``` -------------------------------- ### Generate Synthetic Data for CausalForest Source: https://github.com/py-why/econml/blob/main/notebooks/Generalized Random Forests.ipynb Setup synthetic features and treatments to simulate a causal inference problem. ```python def true_te(X): return np.hstack([(X[:, [0]] > 0) * X[:, [0]], np.ones((X.shape[0], n_treatments - 1)) * np.arange(1, n_treatments).reshape(1, -1)]) X = np.random.normal(0, 1, size=(n_samples, n_features)) T = np.random.normal(0, 1, size=(n_samples, n_treatments)) for t in range(n_treatments): T[:, t] = np.random.binomial(1, scipy.special.expit(X[:, 0])) y = np.sum(true_te(X) * T, axis=1, keepdims=True) + np.random.normal(0, .5, size=(n_samples, 1)) X_test = X[:min(100, n_samples)].copy() X_test[:, 0] = np.linspace(np.percentile(X[:, 0], 1), np.percentile(X[:, 0], 99), min(100, n_samples)) ``` -------------------------------- ### Configure Nuisance Estimators Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Recommendation AB Testing at An Online Travel Company.ipynb Sets up parameters and initializes LightGBM models for nuisance estimation (outcome-feature and treatment-instrument-feature relationships). Requires lightgbm library. ```python import lightgbm as lgb # Define nuissance estimators lgb_T_XZ_params = { 'objective' : 'binary', 'metric' : 'auc', 'learning_rate': 0.1, 'num_leaves' : 30, 'max_depth' : 5 } lgb_Y_X_params = { 'metric' : 'rmse', 'learning_rate': 0.1, 'num_leaves' : 30, 'max_depth' : 5 } model_T_XZ = lgb.LGBMClassifier(**lgb_T_XZ_params) model_Y_X = lgb.LGBMRegressor(**lgb_Y_X_params) flexible_model_effect = lgb.LGBMRegressor(**lgb_Y_X_params) ``` -------------------------------- ### True Long-Term Effect Output Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Long-Term Return-on-Investment via Short-Term Proxies.ipynb Example output displaying the calculated true long-term effect for each investment type. ```text True Long-term Effect for each investment: [0.90994672 0.709811 2.45310877] ``` -------------------------------- ### Initialize EconML Environment Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company.ipynb Imports necessary libraries for causal inference, machine learning, and data manipulation to begin the analysis. ```python # Some imports to get us started # Utilities import numpy as np import pandas as pd # Generic ML imports from sklearn.preprocessing import PolynomialFeatures from sklearn.ensemble import GradientBoostingRegressor # EconML imports from econml.dml import LinearDML, CausalForestDML from econml.cate_interpreter import SingleTreeCateInterpreter, SingleTreePolicyInterpreter import matplotlib.pyplot as plt %matplotlib inline ``` -------------------------------- ### Initialize DRIV-RW Model for CATE Estimation Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/NLSYM_Semi_Synthetic_Linear.ipynb Sets up the necessary models and wrappers for the DRIV-RW estimator, including models for E[T*Z|X], preliminary CATE estimation, and the final stage regression. Requires specific imports and random seed initialization. ```python from dml_iv import DMLIV from dr_iv import DRIV, ProjectedDRIV from utilities import SubsetWrapper, StatsModelLinearRegression, ConstantModel, WeightWrapper from sklearn.dummy import DummyRegressor np.random.seed(random_seed) # For DRIV we need a model for predicting E[T*Z | X]. We use a classifier model_TZ_X = lambda: model() # We also need a model for the final regression that will fit the function theta(X) # This model now needs to accept sample weights at fit time. const_driv_model_effect = lambda: WeightWrapper(Pipeline([('bias', PolynomialFeatures(degree=1, include_bias=True)), ('reg', SelectiveLasso(np.arange(1, X.shape[1]+1), LassoCV(cv=5, fit_intercept=False)))])) # As in OrthoDMLIV we need a perliminary estimator of the CATE. # We use a DMLIV estimator with no cross-fitting (n_splits=1) dmliv_prel_model_effect = DMLIV(model_Y_X(), model_T_X(), model_T_XZ(), dmliv_model_effect(), dmliv_featurizer(), n_splits=1, binary_instrument=True, binary_treatment=False) const_dr_cate = DRIV(model_Y_X(), model_T_X(), model_Z_X(), # same as in DMLATEIV dmliv_prel_model_effect, # preliminary model for CATE, must support fit(y, T, X, Z) and effect(X) model_TZ_X(), # model for E[T * Z | X] const_driv_model_effect(), # model for final stage of fitting theta(X) cov_clip=COV_CLIP, # covariance clipping to avoid large values in final regression from weak instruments n_splits=N_SPLITS, # number of splits to use for cross-fitting binary_instrument=True, # a flag whether to stratify cross-fitting by instrument binary_treatment=False, # a flag whether to stratify cross-fitting by treatment opt_reweighted=True # whether to optimally re-weight samples. Valid only for flexible final model ) ``` -------------------------------- ### Get Normal Effect Interval from DMLATEIV Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/NLSYM_GBM.ipynb Calculate confidence intervals for the ATE using the asymptotic normal approximation by calling `normal_effect_interval()`. ```python # We can call normal_effect_interval to get confidence intervals based # based on the asympotic normal approximation lower, upper = dmlate.normal_effect_interval(lower=2.5, upper=97.5) # Comparison with true ATE print("ATE Estimate Interval: ({:.3f}, {:.3f})".format(lower, upper)) ``` -------------------------------- ### Initialize Environment and Imports Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Multi-investment Attribution at A Software Company - EconML + DoWhy.ipynb Standard imports and configuration for running EconML and DoWhy analysis in a Jupyter environment. ```python # Some imports to get us started # Utilities import numpy as np import pandas as pd from networkx.drawing.nx_pydot import to_pydot from IPython.display import Image, display # Generic ML imports from xgboost import XGBRegressor, XGBClassifier # EconML imports from econml.dr import LinearDRLearner # DoWhy imports import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.simplefilter('ignore') %matplotlib inline ``` -------------------------------- ### Get Treatment Effect and Confidence Interval Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company - EconML + DoWhy.ipynb Retrieve the predicted treatment effect and its confidence interval for a given set of test features. ```python # Get treatment effect and its confidence interval te_pred = est_dw.effect(X_test).flatten() te_pred_interval = est_dw.effect_interval(X_test) ``` -------------------------------- ### Initialize and Fit IntentToTreatDRIV Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis.ipynb Sets up the DRIV model with GradientBoostingRegressor and fits it to the data. ```python rf_driv_model_effect = lambda: GradientBoostingRegressor(n_estimators=30, max_depth=2, min_impurity_decrease=0.001, min_samples_leaf=200) rf_dr_cate = IntentToTreatDRIV(model_Y_X(), model_T_XZ(), rf_driv_model_effect(), opt_reweighted=True, # re-weighting the final loss for variance reduction cov_clip=1e-7, n_splits=10) ``` ```python %%time rf_dr_cate.fit(y, T, X, Z, store_final=True) ``` -------------------------------- ### Get Parameter Inference Summary Source: https://github.com/py-why/econml/blob/main/README.md Provides a summary of parameter inference for the final model. This is available when the CATE model is linear and parametric. ```python from econml.dml import LinearDML # Use defaults est = LinearDML() est.fit(Y, T, X=X, W=W) # Get the parameter inference summary for the final model est.summary() ``` -------------------------------- ### Panel Groups Result Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Long-Term Return-on-Investment via Short-Term Proxies.ipynb Example output of the panelGroups array, showing group identifiers for each unit across different time periods. ```text Result: array([[0.000e+00, 0.000e+00, 0.000e+00, 0.000e+00], [1.000e+00, 1.000e+00, 1.000e+00, 1.000e+00], [2.000e+00, 2.000e+00, 2.000e+00, 2.000e+00], ..., [4.997e+03, 4.997e+03, 4.997e+03, 4.997e+03], [4.998e+03, 4.998e+03, 4.998e+03, 4.998e+03], [4.999e+03, 4.999e+03, 4.999e+03, 4.999e+03]]) ``` -------------------------------- ### Initialize Intent-to-Treat DRIV Model Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis.ipynb Sets up the necessary components for an Intent-to-Treat DRIV model. It requires a flexible CATE model that can handle sample weights, here demonstrated by wrapping a Lasso estimator with WeightWrapper. ```python from utilities import SubsetWrapper, StatsModelLinearRegression, ConstantModel from dr_iv import IntentToTreatDRIV from utilities import WeightWrapper np.random.seed(123) # For intent to treat DRIV we need a flexible model of the CATE to be used in the preliminary estimation. # This flexible model needs to accept sample weights at fit time. Here we use a weightWrapper to wrap # a lasso estimator. WeightWrapper requires a linear model with no intercept, hence the Pipeline ``` -------------------------------- ### Initialize Dynamic Panel Data Generator Source: https://github.com/py-why/econml/blob/main/prototypes/dynamic_dml/high_dim_state_any_m_panel.ipynb Sets up parameters and initializes the DynamicPanelDGP class for generating observational data. Ensure all parameters are correctly set before creating an instance. ```python import numpy as np from dynamic_panel_dgp import DynamicPanelDGP, LongRangeDynamicPanelDGP n_units = 400 n_periods = 3 n_treatments = 1 n_x = 100 s_x = 10 s_t = 10 sigma_x = .5 sigma_t = .5 sigma_y = .5 gamma = .0 autoreg = .5 state_effect = .5 conf_str = 6 hetero_strength = 0 hetero_inds = None #dgp_class = LongRangeDynamicPanelDGP dgp_class = DynamicPanelDGP dgp = dgp_class(n_periods, n_treatments, n_x).create_instance(s_x, sigma_x, sigma_y, conf_str, hetero_strength, hetero_inds, autoreg, state_effect, random_seed=39) ``` -------------------------------- ### Generated Data Shapes Output Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Long-Term Return-on-Investment via Short-Term Proxies.ipynb Example output showing the shapes of the generated panel data arrays for outcomes, treatments, and controls. ```text Outcome shape: (5000, 4) Treatment shape: (5000, 4, 3) Controls shape: (5000, 4, 71) ``` -------------------------------- ### Helper Imports for Python Source: https://github.com/py-why/econml/blob/main/notebooks/ForestLearners Basic Example.ipynb Imports necessary libraries like numpy and matplotlib for numerical operations and plotting. Ensure these libraries are installed before use. ```python # Helper imports import numpy as np import matplotlib.pyplot as plt %matplotlib inline ``` -------------------------------- ### Initialize Estimators Source: https://github.com/py-why/econml/blob/main/notebooks/CATE validation.ipynb Configures GradientBoostingRegressor and RandomForestClassifier for use in CATE models. ```python model_regression = GradientBoostingRegressor(random_state=0) model_propensity = RandomForestClassifier(random_state=0) ``` -------------------------------- ### Random Forest First Stages in EconML Source: https://github.com/py-why/econml/blob/main/doc/spec/estimation/dml.md Demonstrates using RandomForests as a non-parametric regressor for first-stage estimates in EconML. Ensure scikit-learn is installed. ```python from sklearn.ensemble import RandomForestRegressor est = DML(model_y=RandomForestRegressor(), model_t=RandomForestRegressor()) ``` -------------------------------- ### Get Shape of Outcome Variable Source: https://github.com/py-why/econml/blob/main/prototypes/sensitivity_analysis/ovb_dml.ipynb Retrieves the shape of the outcome variable 'y' from the generated data dictionary. This is a basic check to confirm data dimensions. ```python dpg_dict['y'].shape ``` -------------------------------- ### Initialize DMLIV and DRIV Imports Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis.ipynb Imports necessary classes and sets up the model for predicting E[T*Z | X]. ```python from dml_iv import DMLIV from dr_iv import DRIV, ProjectedDRIV from utilities import SubsetWrapper, StatsModelLinearRegression, ConstantModel np.random.seed(123) # For DRIV we need a model for predicting E[T*Z | X]. We use a classifier model_TZ_X = lambda: model_clf() ``` -------------------------------- ### Get CATE Estimates from DMLIV Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/NLSYM_GBM.ipynb Call the `effect(X)` method to obtain CATE estimates for each observation in X. The results are then plotted as a histogram. ```python # To get the CATE at every X we call effect(X) dml_effect = cate.effect(X) plt.hist(dml_effect, label='est') plt.legend() plt.show() ``` -------------------------------- ### Configure AutoML Workspace Source: https://github.com/py-why/econml/blob/main/notebooks/AutomatedML/Automated Machine Learning For EconML.ipynb Sets up the Azure ML workspace for AutoML. Ensure you replace placeholders with your actual Azure subscription details. ```python setAutomatedMLWorkspace(workspace_name = "", subscription_id="", resource_group="") ``` -------------------------------- ### Get ATE Estimate from DMLATEIV Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/NLSYM_GBM.ipynb Call the `effect()` method to retrieve the estimated Average Treatment Effect (ATE) from the fitted DMLATEIV model. ```python # We call effect() to get the ATE ta_effect = dmlate.effect() ``` -------------------------------- ### Get Confidence Intervals for Intercept and Coefficients Source: https://github.com/py-why/econml/blob/main/notebooks/Dynamic Double Machine Learning Examples.ipynb Retrieves the confidence intervals for the intercept and coefficients of an estimated model using a specified alpha level. ```python conf_ints_intercept = est.intercept__interval(alpha=0.05) conf_ints_coef = est.coef__interval(alpha=0.05) ``` -------------------------------- ### Initialize and Fit CausalForest Source: https://github.com/py-why/econml/blob/main/notebooks/Generalized Random Forests.ipynb Configure the CausalForest estimator and fit it to the training data. ```python est = CausalForest(criterion='het', n_estimators=400, min_samples_leaf=5, max_depth=None, min_var_fraction_leaf=None, min_var_leaf_on_val=True, min_impurity_decrease = 0.0, max_samples=0.45, min_balancedness_tol=.45, warm_start=False, inference=True, fit_intercept=True, subforest_size=4, honest=True, verbose=0, n_jobs=-1, random_state=1235) ``` ```python est.fit(X, T, y) ``` -------------------------------- ### Get CATE Model Intercept Inference Source: https://github.com/py-why/econml/blob/main/notebooks/Doubly Robust Learner and Interpretability.ipynb Retrieves the inference results for the intercept of a CATE model. Ensure the model is fitted before calling this method. ```python est.intercept__inference(T=1).summary_frame() ``` -------------------------------- ### Initialize and Fit DRLearner Source: https://github.com/py-why/econml/blob/main/notebooks/Doubly Robust Learner and Interpretability.ipynb Initializes a DRLearner with specified regression and propensity models, then fits it to the data. Ensure your models accept the sample_weight keyword argument. ```python est = DRLearner(model_regression=GradientBoostingRegressor(max_depth=3, n_estimators=100, min_samples_leaf=30), model_propensity=GradientBoostingClassifier(max_depth=3, n_estimators=100, min_samples_leaf=30), model_final=RandomForestRegressor(max_depth=3, n_estimators=100, min_samples_leaf=30)) est.fit(y, T, X=X[:, :4], W=X[:, 4:]) ``` -------------------------------- ### Generate Synthetic Data for Multiple Continuous Treatments Source: https://github.com/py-why/econml/blob/main/notebooks/Double Machine Learning Examples.ipynb Setup a DGP with high-dimensional confounders and heterogeneous treatment effects for testing CATE models. ```python # DGP constants np.random.seed(123) n = 6000 n_w = 30 support_size = 5 n_x = 5 # Outcome support support_Y = np.random.choice(np.arange(n_w), size=support_size, replace=False) coefs_Y = np.random.uniform(0, 1, size=support_size) def epsilon_sample(n): return np.random.uniform(-1, 1, size=n) # Treatment support support_T = support_Y coefs_T = np.random.uniform(0, 1, size=support_size) def eta_sample(n): return np.random.uniform(-1, 1, size=n) # Generate controls, covariates, treatments and outcomes W = np.random.normal(0, 1, size=(n, n_w)) X = np.random.uniform(0, 1, size=(n, n_x)) # Heterogeneous treatment effects TE1 = np.array([x_i[0] for x_i in X]) TE2 = np.array([x_i[0]**2 for x_i in X]).flatten() T = np.dot(W[:, support_T], coefs_T) + eta_sample(n) Y = TE1 * T + TE2 * T**2 + np.dot(W[:, support_Y], coefs_Y) + epsilon_sample(n) ``` -------------------------------- ### Initialize EconML models Source: https://github.com/py-why/econml/blob/main/notebooks/Causal Model Selection with the RScorer.ipynb Configures a list of various causal inference models for comparison. ```python models = [('ldml', LinearDML(model_y=reg(), model_t=clf(), discrete_treatment=True, cv=3)), ('sldml', SparseLinearDML(model_y=reg(), model_t=clf(), discrete_treatment=True, featurizer=PolynomialFeatures(degree=2, include_bias=False), cv=3)), ('xlearner', XLearner(models=reg(), cate_models=reg(), propensity_model=clf())), ('dalearner', DomainAdaptationLearner(models=reg(), final_models=reg(), propensity_model=clf())), ('slearner', SLearner(overall_model=reg())), ('tlearner', TLearner(models=reg())), ('drlearner', DRLearner(model_propensity=clf(), model_regression=reg(), model_final=reg(), cv=3)), ('rlearner', NonParamDML(model_y=reg(), model_t=clf(), model_final=reg(), discrete_treatment=True, cv=3)), ('dml3dlasso', DML(model_y=reg(), model_t=clf(), model_final=LassoCV(), discrete_treatment=True, featurizer=PolynomialFeatures(degree=3), cv=3)) ] ``` -------------------------------- ### Import necessary libraries in Python Source: https://github.com/py-why/econml/blob/main/notebooks/Choosing First Stage Models.ipynb Imports common libraries for data manipulation, machine learning models, and plotting in Python. Ensure these are installed before running. ```python import numpy as np from econml.dml import LinearDML from sklearn.linear_model import Lasso, LassoCV from sklearn.ensemble import GradientBoostingRegressor from sklearn.model_selection import GridSearchCV from sklearn.preprocessing import PolynomialFeatures import matplotlib.pyplot as plt %matplotlib inline ``` -------------------------------- ### Configure IPython Environment Source: https://github.com/py-why/econml/blob/main/notebooks/Causal Model Selection with the RScorer.ipynb Enable autoreload for development convenience. ```python %load_ext autoreload %autoreload 2 ``` -------------------------------- ### Get Identified Estimand Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company - EconML + DoWhy.ipynb Retrieves the identified estimand from the fitted causal model. This object contains details about the causal effect being estimated, including assumptions. ```python identified_estimand = est_dw.identified_estimand_ print(identified_estimand) ``` -------------------------------- ### Initialize and Fit CATE Estimators Source: https://github.com/py-why/econml/blob/main/notebooks/ForestLearners Basic Example.ipynb Configures and fits ForestDRLearner and DRLearner models. ```python model_regression = clone(first_stage_reg().fit(np.hstack([T.reshape(-1, 1), X]), Y).best_estimator_) model_regression ``` ```python from econml.dr import ForestDRLearner est = ForestDRLearner(model_regression=model_y, model_propensity=model_t, cv=3, n_estimators=4000, min_samples_leaf=10, verbose=0, min_weight_fraction_leaf=.005) est.fit(Y, T, X=X) ``` ```python from econml.dr import DRLearner est2 = DRLearner(model_regression=model_y, model_propensity=model_t, model_final=final_stage(), cv=3) est2.fit(Y, T.reshape((-1, 1)), X=X) ``` -------------------------------- ### Generate Synthetic Data for Treatment Effects Source: https://github.com/py-why/econml/blob/main/notebooks/AutomatedML/Automated Machine Learning For EconML.ipynb Setup synthetic data generation for treatment effects, including train-test splitting and test data creation. ```python TE = np.array([te(x_i) for x_i in X]) T = vg(np.dot(W[:, support_T], coefs_T))+ eta_sample(n) Y = TE * T + vm(np.dot(W[:, support_Y], coefs_Y))+ epsilon_sample(n) Y_train, Y_val, T_train, T_val, X_train, X_val, W_train, W_val = train_test_split(Y, T, X, W, test_size=.2) # Generate test data X_test = np.array(list(product(np.arange(0, 1, 0.01), repeat=n_x))) ``` -------------------------------- ### Initialize and Fit DMLIV Model Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/NLSYM_Semi_Synthetic_GBM.ipynb Configures the DMLIV model with specific featurizers and estimators, then fits it to the data. ```python from dml_iv import DMLIV from utilities import SelectiveLasso, SeparateModel from sklearn.linear_model import LassoCV, LogisticRegressionCV from econml.utilities import hstack np.random.seed(random_seed) # For DMLIV we also need a model for E[T | X, Z]. To allow for heterogeneity in the compliance, i.e. # T = beta(X)*Z + gamma(X) # we train a separate model for Z=1 and Z=0. The model for Z=1 learns the # quantity beta(X) + gamma(X) and the model for Z=0 learns gamma(X). model_T_XZ = lambda: SeparateModel(model(), model()) # We now specify the features to be used for heterogeneity. We will fit a CATE model of the form # theta(X) = # for some set of features phi(X). The featurizer needs to support fit_transform, that takes # X and returns phi(X). We need to include a bias if we also want a constant term. dmliv_featurizer = lambda: PolynomialFeatures(degree=1, include_bias=True) # Then we need to specify a model to be used for fitting the parameters theta in the linear form. # This model will minimize the square loss: # (Y - E[Y|X] - * (E[T|X,Z] - E[T|X]))**2 #dmliv_model_effect = lambda: LinearRegression(fit_intercept=False) # Potentially with some regularization on theta. Here we use an ell_1 penalty on theta # If we also have a prior that there is no effect heterogeneity we can use a selective lasso # that does not penalize the constant term in the CATE model dmliv_model_effect = lambda: SelectiveLasso(np.arange(1, X.shape[1]+1), LassoCV(cv=5, fit_intercept=False)) # We initialize DMLIV with all these models and call fit cate = DMLIV(model_Y_X(), model_T_X(), model_T_XZ(), dmliv_model_effect(), dmliv_featurizer(), n_splits=N_SPLITS, # number of splits to use for cross-fitting binary_instrument=True, # a flag whether to stratify cross-fitting by instrument binary_treatment=False # a flag whether to stratify cross-fitting by treatment ) ``` ```python cate.fit(y, T, X, Z) ``` -------------------------------- ### Train DROrthoForest Model Source: https://github.com/py-why/econml/blob/main/notebooks/ForestLearners Basic Example.ipynb Instantiate and fit a DROrthoForest model. This example uses Lasso and Logistic Regression as base estimators and their final versions for refinement. ```python from econml.orf import DROrthoForest from sklearn.linear_model import LogisticRegressionCV from econml.sklearn_extensions.linear_model import WeightedLassoCV est3 = DROrthoForest(model_Y=Lasso(alpha=0.01), propensity_model=LogisticRegression(C=1), model_Y_final=WeightedLassoCV(cv=3), propensity_model_final=LogisticRegressionCV(cv=3), n_trees=1000, min_leaf_size=10) est3.fit(Y, T, X=X) ``` -------------------------------- ### Configure and Initialize IntentToTreatDRIV Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis.ipynb Define the flexible model and final projection model, then initialize the IntentToTreatDRIV estimator. ```python driv_flexible_model_effect = lambda: WeightWrapper(Pipeline([('bias', PolynomialFeatures(degree=1, include_bias=True)), ('lasso', SelectiveLasso(np.arange(1, X.shape[1]+1), LassoCV(cv=5, n_jobs=-1)))])) # Then we can also define any final model to project to. Here we project to a constant model to get an ATE driv_final_model_effect = lambda: ConstantModel() dr_cate = IntentToTreatDRIV(model_Y_X(), model_T_XZ(), driv_flexible_model_effect(), final_model_effect=driv_final_model_effect(), cov_clip=0.0001, n_splits=10) ``` -------------------------------- ### Get Population Inference from Sample Data Source: https://github.com/py-why/econml/blob/main/notebooks/Doubly Robust Learner and Interpretability.ipynb Retrieves population-level inference results based on sample data. This is useful for generalizing findings from the sample to the broader population. ```python est.effect_inference(X_test[:,:4]).population_summary() ``` -------------------------------- ### Initialize and fit ForestDRLearner Source: https://github.com/py-why/econml/blob/main/notebooks/Doubly Robust Learner and Interpretability.ipynb Configures a ForestDRLearner with a GradientBoostingRegressor and fits it to the data. ```python from econml.dr import ForestDRLearner from sklearn.ensemble import GradientBoostingRegressor est = ForestDRLearner(model_regression=GradientBoostingRegressor(), model_propensity=DummyClassifier(strategy='prior'), cv=5, n_estimators=1000, min_samples_leaf=10, verbose=0, min_weight_fraction_leaf=.01) est.fit(y, T, X=X[:, :4]) ``` -------------------------------- ### Get Population Effect Inference Summary Source: https://github.com/py-why/econml/blob/main/README.md Calculates the population summary for effect inference across the entire sample X. Requires the model to be linear and parametric. ```python from econml.dml import LinearDML # Use defaults est = LinearDML() est.fit(Y, T, X=X, W=W) # Get the population summary for the entire sample X est.effect_inference(X_test).population_summary(alpha=0.1, value=0, decimals=3, tol=0.001) ``` -------------------------------- ### Initialize and fit SparseLinearDRLearner Source: https://github.com/py-why/econml/blob/main/notebooks/Doubly Robust Learner and Interpretability.ipynb Configures the SparseLinearDRLearner with regression and propensity models, then fits it to the provided data. ```python est = SparseLinearDRLearner(model_regression=WeightedLassoCV(cv=3), model_propensity=DummyClassifier(strategy='prior'), featurizer=PolynomialFeatures(degree=3, interaction_only=True, include_bias=False)) est.fit(y, T, X=X[:, :4]) ``` -------------------------------- ### Setup Lasso Models for DML Source: https://github.com/py-why/econml/blob/main/prototypes/dynamic_dml/high_dim_state_any_m_panel.ipynb Configures Lasso and MultiTaskLasso models with cross-validation for use in the DynamicPanelDML. It defines a lambda function to create the models with specified parameters. ```python from sklearn.linear_model import LinearRegression, LassoCV, Lasso, MultiTaskLasso, MultiTaskLassoCV from sklearn.model_selection import GroupKFold import warnings warnings.simplefilter('ignore') np.random.seed(123) alpha_regs = [1e-4, 1e-3, 1e-2, 5e-2, .1, 1] lasso_model = lambda : LassoCV(cv=3, alphas=alpha_regs, max_iter=500) mlasso_model = lambda : MultiTaskLassoCV(cv=3, alphas=alpha_regs, max_iter=500) ``` -------------------------------- ### Initialize Random Forest CATE Model Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis.ipynb Sets up a Random Forest regressor for the final CATE estimation stage. ```python from dml_iv import DMLIV from dr_iv import DRIV, ProjectedDRIV from utilities import SubsetWrapper from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor np.random.seed(123) # We need a model for the final regression that will fit the function theta(X) # Now we use a linear model and a lasso. rf_driv_model_effect = lambda: RandomForestRegressor(n_estimators=100, max_depth=3, min_impurity_decrease=0.1, min_samples_leaf=500, bootstrap=True) ``` -------------------------------- ### Get Linear CATE Model Coefficients Source: https://github.com/py-why/econml/blob/main/notebooks/Weighted Double Machine Learning Examples.ipynb Retrieves and prints the coefficients of the linear CATE model along with their corresponding feature names, aliased as 'A', 'B', 'C', 'D'. ```python # Getting the coefficients of the linear CATE model together with the corresponding feature names print(np.array(list(zip(est.cate_feature_names(['A', 'B', 'C', 'D']), est.coef_)))) ``` -------------------------------- ### Initialize IntentToTreatDRIV Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis_Step_CATE.ipynb Sets up the IntentToTreatDRIV model with a random forest effect model. ```python rf_driv_model_effect = lambda: HonestForest(RandomForestRegressor(n_estimators=100, max_depth=6, min_impurity_decrease=0.001, min_samples_leaf=100, bootstrap=True)) rf_dr_cate = IntentToTreatDRIV(model_Y_X(), model_T_XZ(), rf_driv_model_effect(), opt_reweighted=True, # re-weighting the final loss for variance reduction cov_clip=1e-4, n_splits=10) ``` -------------------------------- ### Configure Intent to Treat DRIV Source: https://github.com/py-why/econml/blob/main/prototypes/dml_iv/TA_DGP_analysis_Step_CATE.ipynb Sets up imports and configuration for Intent to Treat DRIV, including a weight wrapper for the preliminary estimator. ```python from utilities import SubsetWrapper, StatsModelLinearRegression, ConstantModel, HonestForest from dr_iv import IntentToTreatDRIV from utilities import WeightWrapper np.random.seed(123) # For intent to treat DRIV we need a flexible model of the CATE to be used in the preliminary estimation. # This flexible model needs to accept sample weights at fit time. Here we use a weightWrapper to wrap # a lasso estimator. WeightWrapper requires a linear model with no intercept, hence the Pipeline ``` -------------------------------- ### Get Confidence Intervals with SparseLinearDML Source: https://github.com/py-why/econml/blob/main/doc/spec/estimation/dml.md Use SparseLinearDML for L1-regularized final models to obtain confidence intervals. This class leverages the DebiasedLasso algorithm and its asymptotic normality properties. ```python model = SparseLinearDML(model_y=..., model_t=..., ..., msm=True) model.fit(Y, T) # To get confidence intervals on the effect of going from T0 to T1 model.effect(X=..., T0=..., T1=..., interval=True) ``` -------------------------------- ### Initialize LightGBM Models Source: https://github.com/py-why/econml/blob/main/notebooks/CustomerScenarios/Case Study - Recommendation AB Testing at An Online Travel Company - EconML + DoWhy.ipynb Initializes LightGBM classifier and regressor models using predefined parameters. These models will serve as nuisance estimators in the EconML framework. ```python model_T_XZ = lgb.LGBMClassifier(**lgb_T_XZ_params) model_Y_X = lgb.LGBMRegressor(**lgb_Y_X_params) flexible_model_effect = lgb.LGBMRegressor(**lgb_Y_X_params) ``` -------------------------------- ### Get Confidence Intervals with LinearDML Source: https://github.com/py-why/econml/blob/main/doc/spec/estimation/dml.md Use LinearDML to obtain confidence intervals for treatment effects. This method assumes a linear effect model and uses StatsModelsLinearRegression for inference. ```python model = LinearDML(model_y=..., model_t=..., ..., msm=True) model.fit(Y, T) # To get confidence intervals on the effect of going from T0 to T1 model.effect(X=..., T0=..., T1=..., interval=True) ``` -------------------------------- ### Instantiate SLearner with Grid Search Regressor Source: https://github.com/py-why/econml/blob/main/notebooks/AutomatedML/Automated Machine Learning For EconML.ipynb Initializes an S-Learner using a grid search regressor as the overall model for estimating treatment effects. ```python # Instantiate S learner overall_model = grid_search_reg() S_learner_rf = SLearner(overall_model) ``` -------------------------------- ### Predict Treatment Effects with NonParamDMLIV Source: https://github.com/py-why/econml/blob/main/notebooks/OrthoIV and DRIV Examples.ipynb Predicts the conditional average treatment effect (CATE) using a fitted NonParamDMLIV estimator. Confidence intervals are not computed in this specific example. ```python te_pred3 = est3.effect(X_test) res_pred.append(te_pred2) res_lb.append(np.array([])) res_ub.append(np.array([])) ``` -------------------------------- ### Initialize and Configure GridSearchCV for LightGBM Source: https://github.com/py-why/econml/blob/main/notebooks/Solutions/Causal Interpretation for Ames Housing Price.ipynb Sets up a LightGBM regression model and defines a parameter grid for hyperparameter tuning using GridSearchCV. `n_jobs=-1` utilizes all available CPU cores for faster computation. ```python # train a lightGBM regression model est = LGBMRegressor() param_grid = {"learning_rate": [0.1, 0.05, 0.01], "max_depth": [3, 5, 10]} search = GridSearchCV(est, param_grid, n_jobs=-1) ```