### RegressionForest example: setup and training Source: https://www.pywhy.org/EconML/_autosummary/econml.grf.RegressionForest.html Imports necessary libraries, generates synthetic regression data, splits into train/test sets, and initializes a RegressionForest with specified parameters. ```python import numpy as np from econml.grf import RegressionForest from sklearn.datasets import make_regression from sklearn.model_selection import train_test_split np.set_printoptions(suppress=True) np.random.seed(123) X, y = make_regression(n_samples=1000, n_features=4, n_informative=2, random_state=0, shuffle=False) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5) regr = RegressionForest(max_depth=None, random_state=0, n_estimators=1000) ``` -------------------------------- ### Example: Implementing a Simple RLearner Source: https://www.pywhy.org/EconML/_autosummary/econml.dml._rlearner.html Example code demonstrating how to subclass _RLearner to implement a simple double machine learning method with custom model selectors and final model. ```APIDOC ## Example: Implementing a Simple RLearner ### Description This example implements a very simple version of the double machine learning method on top of the _RLearner class, for expository purposes. For a more elaborate implementation, see DML and its child classes. ### Code Example ```python import numpy as np from sklearn.linear_model import LinearRegression from econml.dml._rlearner import _RLearner from econml.sklearn_extensions.model_selection import SingleModelSelector from sklearn.base import clone class ModelFirst: def __init__(self, model): self._model = clone(model, safe=False) def fit(self, X, W, Y, sample_weight=None): self._model.fit(np.hstack([X, W]), Y) return self def predict(self, X, W): return self._model.predict(np.hstack([X, W])) class ModelSelector(SingleModelSelector): def __init__(self, model): self._model = ModelFirst(model) def train(self, is_selecting, folds, X, W, Y, sample_weight=None): self._model.fit(X, W, Y, sample_weight=sample_weight) return self @property def best_model(self): return self._model @property def best_score(self): return 0 class ModelFinal: def fit(self, X, T, T_res, Y_res, sample_weight=None, freq_weight=None, sample_var=None): self.model = LinearRegression(fit_intercept=False).fit(X * T_res.reshape(-1, 1), Y_res) return self def predict(self, X): return self.model.predict(X) class RLearner(_RLearner): def _gen_model_y(self): return ModelSelector(LinearRegression()) def _gen_model_t(self): return ModelSelector(LinearRegression()) def _gen_rlearner_model_final(self): return ModelFinal() np.random.seed(123) X = np.random.normal(size=(1000, 3)) y = X[:, 0] + X[:, 1] + np.random.normal(0, 0.01, size=(1000,)) est = RLearner(cv=2, discrete_outcome=False, discrete_treatment=False, treatment_featurizer=None, categories='auto', random_state=None) est.fit(y, X[:, 0], X=np.ones((X.shape[0], 1)), W=X[:, 1:]) ``` ``` -------------------------------- ### DMLOrthoForest Usage Example Source: https://www.pywhy.org/EconML/spec/estimation/forest.html This example demonstrates how to instantiate and fit a DMLOrthoForest estimator and then use its effect method to estimate treatment effects on new data points. ```APIDOC ## DMLOrthoForest ### Description DMLOrthoForest is an orthogonal random forest estimator for heterogeneous treatment effects. It uses double machine learning with random forests to estimate conditional average treatment effects (CATE). ### Usage #### Import ```python from econml.orf import DMLOrthoForest ``` #### Constructor Parameters - `n_trees` (int) - Number of trees in the forest. - `max_depth` (int) - Maximum depth of each tree. - `subsample_ratio` (float) - Ratio of samples to use for each tree. - `model_T` (estimator) - Model for the treatment regression. - `model_Y` (estimator) - Model for the outcome regression. #### Methods ##### fit(Y, T, X=None, W=None) Fits the estimator to the data. **Parameters:** - `Y` (array-like) - Outcome vector. - `T` (array-like) - Treatment vector. - `X` (array-like, optional) - Features for heterogeneity. - `W` (array-like, optional) - Confounders. **Returns:** - The fitted estimator instance. ##### effect(X=None) Estimates the conditional average treatment effect (CATE) at the given feature points. **Parameters:** - `X` (array-like, optional) - Feature points at which to evaluate the effect. If None, uses the training features. **Returns:** - `ndarray` - Estimated treatment effects. ### Example ```python import numpy as np import sklearn from econml.orf import DMLOrthoForest np.random.seed(123) T = np.array([0, 1]*60) W = np.array([0, 1, 1, 0]*30).reshape(-1, 1) Y = (.2 * W[:, 0] + 1) * T + .5 est = DMLOrthoForest(n_trees=1, max_depth=1, subsample_ratio=1, model_T=sklearn.linear_model.LinearRegression(), model_Y=sklearn.linear_model.LinearRegression()) est.fit(Y, T, X=W, W=W) print(est.effect(W[:2])) ``` ``` -------------------------------- ### DomainAdaptationLearner usage example Source: https://www.pywhy.org/EconML/_modules/econml/metalearners/_metalearners.html Example of using DomainAdaptationLearner with LinearRegression models. It generates synthetic data, fits the estimator, and prints the effect for the first three samples. The output shows the estimated effects. ```python import numpy as np import scipy.special np.set_printoptions(suppress=True) ``` ```python from econml.metalearners import DomainAdaptationLearner from sklearn.linear_model import LinearRegression np.random.seed(123) X = np.random.normal(size=(1000, 5)) T = np.random.binomial(1, scipy.special.expit(X[:, 0])) y = (1 + .5*X[:, 0]) * T + X[:, 0] + np.random.normal(size=(1000,)) est = DomainAdaptationLearner( models=LinearRegression(), final_models=LinearRegression() ) est.fit(y, T, X=X) ``` ```python >>> est.effect(X[:3]) array([0.51237..., 1.99866..., 0.68552...]) ``` -------------------------------- ### Simple example with default models Source: https://www.pywhy.org/EconML/_autosummary/econml.iv.dr.IntentToTreatDRIV.html This example demonstrates how to use IntentToTreatDRIV with default models. It defines a data generating process (dgp) and a true heterogeneity function, then fits the estimator on synthetic data. ```python from econml.iv.dr import IntentToTreatDRIV # Define the data generation functions def dgp(n, p, true_fn): X = np.random.normal(0, 1, size=(n, p)) Z = np.random.binomial(1, 0.5, size=(n,)) nu = np.random.uniform(0, 10, size=(n,)) coef_Z = 0.8 C = np.random.binomial( 1, coef_Z * scipy.special.expit(0.4 * X[:, 0] + nu) ) # Compliers when recomended C0 = np.random.binomial( 1, 0.06 * np.ones(X.shape[0]) ) # Non-compliers when not recommended T = C * Z + C0 * (1 - Z) y = true_fn(X) * T + 2 * nu + 5 * (X[:, 3] > 0) + 0.1 * np.random.uniform(0, 1, size=(n,)) return y, T, Z, X def true_heterogeneity_function(X): return 5 * X[:, 0] np.random.seed(123) y, T, Z, X = dgp(1000, 5, true_heterogeneity_function) est = IntentToTreatDRIV() est.fit(Y=y, T=T, Z=Z, X=X) ``` ```python >>> est.effect(X[:3]) array([-4.52684..., 6.38767..., -2.67082...]) ``` -------------------------------- ### Import numpy and set print options for DynamicDML example Source: https://www.pywhy.org/EconML/_modules/econml/panel/dml/_dml.html Hidden setup code that imports numpy and suppresses scientific notation for the example output. ```python import numpy as np np.set_printoptions(suppress=True) ``` -------------------------------- ### DomainAdaptationLearner.__init__() Source: https://www.pywhy.org/EconML/ Initializes the DomainAdaptationLearner. ```APIDOC ## DomainAdaptationLearner.__init__() ### Description Initializes the DomainAdaptationLearner. ### Method __init__ ### Parameters No parameters documented. ### Returns No return value documented. ``` -------------------------------- ### Import numpy and scipy.special for CausalForestDML example Source: https://www.pywhy.org/EconML/_modules/econml/dml/causal_forest.html Hidden setup code that imports numpy and scipy.special and sets numpy print options to suppress scientific notation. This is required for the subsequent example to run correctly. ```python import numpy as np import scipy.special np.set_printoptions(suppress=True) ``` -------------------------------- ### GRFTree.init Source: https://www.pywhy.org/EconML/_autosummary/econml.grf._base_grftree.GRFTree.html Initializes this instance. ```APIDOC ## GRFTree.init ### Description Initializes this instance. ### Method init ### Signature `init()` ### Returns None. ``` -------------------------------- ### DMLOrthoForest.__init__() Source: https://www.pywhy.org/EconML/index.html Initializes a DMLOrthoForest instance. ```APIDOC ## DMLOrthoForest.__init__() ### Description Initializes a DMLOrthoForest instance. ### Method Constructor ### Endpoint `DMLOrthoForest.__init__()` ### Parameters No parameters documented. ### Returns A new DMLOrthoForest instance. ``` -------------------------------- ### strata_from_discrete_arrays: Combine discrete arrays for stratification Source: https://www.pywhy.org/EconML/_modules/econml/utilities.html Combines multiple discrete arrays into a single array for stratification purposes. Each distinct combination of the input arrays gets its own label. The example shows input arrays and the resulting output array. ```python [docs] def strata_from_discrete_arrays(arrs): """ Combine multiple discrete arrays into a single array for stratification purposes. For example, if arrs is `[[0 1 2 0 1 2 0 1 2 0 1 2], [0 1 0 1 0 1 0 1 0 1 0 1], [0 0 0 0 0 0 1 1 1 1 1 1]]` then output will be `[0 8 4 6 2 10 1 9 5 7 3 11]` Every distinct combination of these discrete arrays will have it's own label. """ if not arrs: return None curr_array = np.zeros(shape=np.ravel(arrs[0]).shape, dtype='int') for arr in arrs: enc = LabelEncoder() temp = enc.fit_transform(np.ravel(arr)) curr_array = temp + curr_array * len(enc.classes_) return curr_array ``` -------------------------------- ### Example: Output of discrete treatment example Source: https://www.pywhy.org/EconML/_autosummary/econml._ortho_learner.html Shows the output of the discrete treatment example, including the score_, const_marginal_effect(), effect(), and the coefficient of the final model. ```python >>> est.score_ np.float64(0.00672978...) >>> est.const_marginal_effect() array([[1.008402...]]) >>> est.effect() array([1.008402...]) >>> est.score(y, T, W=W) np.float64(0.00310431...) >>> est.ortho_learner_model_final_.model.coef_[0] np.float64(1.00840240...) ``` -------------------------------- ### LinearDRLearner Example Usage Source: https://www.pywhy.org/EconML/_modules/econml/dr/_drlearner.html Example demonstrating how to use LinearDRLearner to estimate treatment effects and extract coefficients. ```APIDOC ## LinearDRLearner Example Usage ### Description Example demonstrating how to use LinearDRLearner to estimate treatment effects and extract coefficients. ### Example ```python import numpy as np import scipy.special from econml.dr import LinearDRLearner np.random.seed(123) X = np.random.normal(size=(1000, 3)) T = np.random.binomial(2, scipy.special.expit(X[:, 0])) y = (1 + .5*X[:, 0]) * T + X[:, 0] + np.random.normal(size=(1000,)) est = LinearDRLearner() est.fit(y, T, X=X, W=None) est.effect(X[:3]) # array([ 0.432365..., 0.359694..., -0.085428...]) est.effect_interval(X[:3]) # (array([ 0.084048..., -0.177951... , -0.734747...]), # array([0.780683..., 0.897341..., 0.563889...])) est.coef_(T=1) # array([ 0.450666..., -0.008821..., 0.075271...]) est.coef__interval(T=1) # (array([ 0.156245..., -0.252216..., -0.159709...]), # array([0.745086..., 0.234572..., 0.310252...])) est.intercept_(T=1) # np.float64(0.909121...) est.intercept__interval(T=1) # (np.float64(0.668518...), np.float64(1.149723...)) ``` ### Attributes - **score_** (float) - The MSE in the final doubly robust potential outcome regressions, i.e., \frac{1}{n_t} \sum_{t=1}^{n_t} \frac{1}{n} \sum_{i=1}^n (Y_{i, t}^{DR} - \hat{\theta}_t(X_i))^2, where n_t is the number of treatments (excluding control). If sample_weight is not None at fit time, then a weighted average across samples is returned. ``` -------------------------------- ### XLearner.__init__ Source: https://www.pywhy.org/EconML/_autosummary/econml.metalearners.XLearner.html Initializes the XLearner estimator with outcome models, CATE models, propensity model, and treatment categories. ```APIDOC ## XLearner.__init__ ### Description Initializes the XLearner estimator with the specified outcome models, CATE models, propensity model, and treatment categories. ### Method Constructor ### Signature `__init__(*, models, cate_models=None, propensity_model=LogisticRegression(), categories='auto', allow_missing=False)` ### Parameters - **models** (outcome estimators for both control units and treatment units) - Required - It can be a single estimator applied to all the control and treatment units or a tuple/list of estimators with one estimator per treatment (including control). Must implement fit and predict methods. - **cate_models** (estimator for pseudo-treatment effects on control and treatments) - Optional - It can be a single estimator applied to all the control and treatments or a tuple/list of estimators with one estimator per treatment (including control). If None, it will be same models as the outcome estimators. Must implement fit and predict methods. - **propensity_model** (estimator for the propensity function) - Optional - Must implement fit and predict_proba methods. The fit method must be able to accept X and T, where T is a shape (n, ) array. - **categories** ('auto' or list, default 'auto') - Optional - The categories to use when encoding discrete treatments (or 'auto' to use the unique sorted values). The first category will be treated as the control treatment. - **allow_missing** (bool) - Optional - Whether to allow missing values in X. If True, will need to supply models, cate_models, and propensity_model that can handle missing values. ### Example ```python from econml.metalearners import XLearner from sklearn.linear_model import LinearRegression np.random.seed(123) X = np.random.normal(size=(1000, 5)) T = np.random.binomial(1, scipy.special.expit(X[:, 0])) y = (1 + .5*X[:, 0]) * T + X[:, 0] + np.random.normal(size=(1000,)) est = XLearner(models=LinearRegression()) est.fit(y, T, X=X) ``` ``` -------------------------------- ### Simple DMLOrthoForest example Source: https://www.pywhy.org/EconML/spec/estimation/forest.html Creates synthetic data and fits a DMLOrthoForest with linear models for treatment and outcome. Prints the estimated effects for the first two samples. ```python >>> T = np.array([0, 1]*60) >>> W = np.array([0, 1, 1, 0]*30).reshape(-1, 1) >>> Y = (.2 * W[:, 0] + 1) * T + .5 >>> est = DMLOrthoForest(n_trees=1, max_depth=1, subsample_ratio=1, ... model_T=sklearn.linear_model.LinearRegression(), ... model_Y=sklearn.linear_model.LinearRegression()) >>> est.fit(Y, T, X=W, W=W) >>> print(est.effect(W[:2])) [1.00... 1.19...] ``` -------------------------------- ### CausalForestDML Usage Example Source: https://www.pywhy.org/EconML/_autosummary/econml.dml.CausalForestDML.html Example of fitting a CausalForestDML model and using the effect and effect_interval methods to estimate treatment effects. ```APIDOC ## CausalForestDML Usage Example ### Description Demonstrates how to fit a CausalForestDML model and use its effect and effect_interval methods. ### Example ```python from econml.dml import CausalForestDML np.random.seed(123) X = np.random.normal(size=(1000, 5)) T = np.random.binomial(1, scipy.special.expit(X[:, 0])) y = (1 + .5*X[:, 0]) * T + X[:, 0] + np.random.normal(size=(1000,)) est = CausalForestDML(discrete_treatment=True) est.fit(y, T, X=X, W=None) >>> est.effect(X[:3]) array([0.62769..., 1.64575..., 0.68497...]) >>> est.effect_interval(X[:3]) (array([0.18798..., 1.17144..., 0.10788...]), array([1.06739..., 2.12006..., 1.26205...])) ``` ``` -------------------------------- ### Bootstrap Inference Example Source: https://www.pywhy.org/EconML/spec/inference.html Example showing how to enable bootstrap inference in an EconML estimator and compute point estimates and confidence intervals. ```APIDOC ## Bootstrap Inference ### Description Enable bootstrap-based confidence intervals for any EconML estimator by passing `inference='bootstrap'` or `inference=BootstrapInference(n_bootstrap_samples=100, n_jobs=-1)`. The intervals are calculated by training multiple versions of the estimator on bootstrap subsamples with replacement and then taking quantiles of the estimate distribution. ### Usage ```python from econml.dml import NonParamDML from sklearn.ensemble import RandomForestRegressor est = NonParamDML(model_y=RandomForestRegressor(n_estimators=10, min_samples_leaf=10), model_t=RandomForestRegressor(n_estimators=10, min_samples_leaf=10), model_final=RandomForestRegressor(n_estimators=10, min_samples_leaf=10)) est.fit(y, t, X=X, W=W, inference='bootstrap') point = est.const_marginal_effect(X) lb, ub = est.const_marginal_effect_interval(X, alpha=0.05) ``` ### Parameters - `inference` (str or BootstrapInference) - Required - Either the string `'bootstrap'` or an instance of `BootstrapInference` with `n_bootstrap_samples` and `n_jobs` parameters. ### Methods - `fit(y, t, X=X, W=W, inference='bootstrap')` - Fits the estimator with bootstrap inference enabled. - `const_marginal_effect(X)` - Returns the point estimate of the constant marginal effect. - `const_marginal_effect_interval(X, alpha=0.05)` - Returns the lower and upper bounds of the confidence interval at the specified alpha level. ### Notes - The `BootstrapInference` class is referenced as `econml.inference.BootstrapInference`. - The `BootstrapEstimator` class (referenced as `econml.inference._bootstrap.BootstrapEstimator`) provides more details on the bootstrap procedure. ``` -------------------------------- ### init() Source: https://www.pywhy.org/EconML/_autosummary/econml.grf._base_grftree.GRFTree.html Initializes this instance. This method should be called before fit. It is a pre-fit step to avoid parallelism issues and to keep the starting seed for replicating randomness. ```APIDOC ## init() ### Description Initializes this instance. This method should be called before fit. It is a pre-fit step to avoid parallelism issues and to keep the starting seed for replicating randomness. ### Method `init()` ``` -------------------------------- ### LinearDRLearner Usage Example Source: https://www.pywhy.org/EconML/spec/estimation/dr.html Example of fitting a LinearDRLearner and computing the conditional average treatment effect (CATE) for given treatment values. ```APIDOC ## LinearDRLearner ### Description Fits a linear double machine learning model to estimate conditional average treatment effects (CATE) as a linear function of features X. ### Method fit ### Signature `est.fit(y, T, X=X, W=W)` ### Parameters - **y** (array-like) - Required - Outcome variable. - **T** (array-like) - Required - Treatment variable. - **X** (array-like) - Optional - Features for heterogeneity. - **W** (array-like) - Optional - Controls/confounders. ### Returns Fitted estimator object. ### Method effect ### Signature `est.effect(X, T0=t0, T1=t1)` ### Parameters - **X** (array-like) - Required - Features for which to compute the effect. - **T0** (optional) - Baseline treatment. - **T1** (optional) - Treatment to compare against. ### Returns Estimated conditional average treatment effect (CATE) for each sample in X. ### Example ```python from econml.dr import LinearDRLearner est = LinearDRLearner() est.fit(y, T, X=X, W=W) est.effect(X, T0=t0, T1=t1) ``` ``` -------------------------------- ### PolicyForest.__init__() Source: https://www.pywhy.org/EconML/index.html Initializes a PolicyForest instance. ```APIDOC ## PolicyForest.__init__() ### Description Initializes a PolicyForest instance. ### Method __init__ ### Parameters No parameters documented. ### Returns No return value documented. ``` -------------------------------- ### TLearner basic usage example Source: https://www.pywhy.org/EconML/_autosummary/econml.metalearners.TLearner.html Shows how to instantiate TLearner with a linear regression model, fit it on synthetic data, and compute the treatment effect for the first three samples. ```python from econml.metalearners import TLearner from sklearn.linear_model import LinearRegression np.random.seed(123) X = np.random.normal(size=(1000, 5)) T = np.random.binomial(1, scipy.special.expit(X[:, 0])) y = (1 + .5*X[:, 0]) * T + X[:, 0] + np.random.normal(size=(1000,)) est = TLearner(models=LinearRegression()) est.fit(y, T, X=X) ``` ```python >>> est.effect(X[:3]) array([0.58547..., 1.82860..., 0.78379...]) ``` -------------------------------- ### DROrthoForest Usage Example Source: https://www.pywhy.org/EconML/spec/estimation/forest.html This example demonstrates how to instantiate and fit a DROrthoForest estimator and then use its effect method to estimate treatment effects on new data points. ```APIDOC ## DROrthoForest ### Description DROrthoForest is an orthogonal random forest estimator for heterogeneous treatment effects with binary or continuous treatments, using a propensity model for the treatment. ### Usage #### Import ```python from econml.orf import DROrthoForest ``` #### Constructor Parameters - `n_trees` (int) - Number of trees in the forest. - `max_depth` (int) - Maximum depth of each tree. - `subsample_ratio` (float) - Ratio of samples to use for each tree. - `propensity_model` (estimator) - Model for the propensity score. - `model_Y` (estimator) - Model for the outcome regression. #### Methods ##### fit(Y, T, X=None, W=None) Fits the estimator to the data. **Parameters:** - `Y` (array-like) - Outcome vector. - `T` (array-like) - Treatment vector. - `X` (array-like, optional) - Features for heterogeneity. - `W` (array-like, optional) - Confounders. **Returns:** - The fitted estimator instance. ##### effect(X=None) Estimates the conditional average treatment effect (CATE) at the given feature points. **Parameters:** - `X` (array-like, optional) - Feature points at which to evaluate the effect. If None, uses the training features. **Returns:** - `ndarray` - Estimated treatment effects. ### Example ```python import numpy as np import sklearn from econml.orf import DROrthoForest np.random.seed(123) T = np.array([0, 1]*60) W = np.array([0, 1, 1, 0]*30).reshape(-1, 1) Y = (.2 * W[:, 0] + 1) * T + .5 est = DROrthoForest(n_trees=1, max_depth=1, subsample_ratio=1, propensity_model=sklearn.linear_model.LogisticRegression(), model_Y=sklearn.linear_model.LinearRegression()) est.fit(Y, T, X=W, W=W) print(est.effect(W[:2])) ``` ``` -------------------------------- ### DRIV example with default models Source: https://www.pywhy.org/EconML/_autosummary/econml.iv.dr.DRIV.html Shows how to instantiate and fit a DRIV estimator with discrete treatment and instrument, using a synthetic data generating process. The example defines helper functions for data generation and true heterogeneity, then fits the model and prints effects for the first three samples. ```python from econml.iv.dr import DRIV # Define the data generation functions def dgp(n, p, true_fn): X = np.random.normal(0, 1, size=(n, p)) Z = np.random.binomial(1, 0.5, size=(n,)) nu = np.random.uniform(0, 10, size=(n,)) coef_Z = 0.8 C = np.random.binomial( 1, coef_Z * scipy.special.expit(0.4 * X[:, 0] + nu) ) # Compliers when recomended C0 = np.random.binomial( 1, 0.06 * np.ones(X.shape[0]) ) # Non-compliers when not recommended T = C * Z + C0 * (1 - Z) y = true_fn(X) * T + 2 * nu + 5 * (X[:, 3] > 0) + 0.1 * np.random.uniform(0, 1, size=(n,)) return y, T, Z, X def true_heterogeneity_function(X): return 5 * X[:, 0] np.random.seed(123) y, T, Z, X = dgp(1000, 5, true_heterogeneity_function) est = DRIV(discrete_treatment=True, discrete_instrument=True) est.fit(Y=y, T=T, Z=Z, X=X) ``` ```python >>> est.effect(X[:3]) array([-4.15076..., 5.99286..., -2.86512...]) ``` -------------------------------- ### LinearDML Usage Example Source: https://www.pywhy.org/EconML/spec/estimation/dml.html Shows how to fit a LinearDML model and estimate the treatment effect. The example uses generic model_y and model_t parameters, which can be any scikit-learn regressor. ```python est.fit(y, t, X=X, W=W) point = est.effect(X, T0=t0, T1=t1) ``` -------------------------------- ### Example: Using _OrthoLearner for continuous treatments Source: https://www.pywhy.org/EconML/_autosummary/econml._ortho_learner.html Shows how to use the _OrthoLearner class for continuous treatments. The example demonstrates fitting the model and accessing various attributes and methods such as score_, const_marginal_effect(), effect(), and the underlying linear regression model. ```python >>> est.score_ np.float64(0.00756830...) >>> est.const_marginal_effect() np.float64(1.02364992...) >>> est.effect() array([1.023649...]) >>> est.effect(T0=0, T1=10) array([10.236499...]) >>> est.score(y, X[:, 0], W=X[:, 1:]) np.float64(0.00727995...) >>> est.ortho_learner_model_final_.model LinearRegression(fit_intercept=False) >>> est.ortho_learner_model_final_.model.coef_ array([1.023649...]) ``` -------------------------------- ### Simple DRLearner example with default models Source: https://www.pywhy.org/EconML/_autosummary/econml.dr.DRLearner.html Shows how to instantiate and fit a DRLearner with default models on synthetic data. Sets a random seed, generates features X, treatment T, and outcome y, then fits the estimator. ```python from econml.dr import DRLearner np.random.seed(123) X = np.random.normal(size=(1000, 3)) T = np.random.binomial(2, scipy.special.expit(X[:, 0])) sigma = 0.001 y = (1 + .5*X[:, 0]) * T + X[:, 0] + np.random.normal(0, sigma, size=(1000,)) est = DRLearner() est.fit(y, T, X=X, W=None) ``` -------------------------------- ### _OrthoLearner Class Example Source: https://www.pywhy.org/EconML/_autosummary/econml._ortho_learner.html Example code demonstrating how to subclass _OrthoLearner to implement a simple double machine learning method, including custom nuisance and final models. ```APIDOC ## _OrthoLearner ### Description Base class for orthogonal learners. This example shows how to subclass it to implement a simple double machine learning method. ### Parameters - **categories** ('auto' or list) - The categories to use when encoding discrete treatments (or 'auto' to use the unique sorted values). The first category will be treated as the control treatment. - **cv** (int, cross-validation generator or an iterable) - Determines the cross-validation splitting strategy. Possible inputs: None (default 3-fold), integer (number of folds), CV splitter, or an iterable yielding (train, test) splits. For integer/None inputs, StratifiedKFold is used for discrete treatments, else KFold with random shuffle. - **random_state** (int, RandomState instance or None) - Seed for random number generator, or RandomState instance, or None to use np.random. - **mc_iters** (int, optional) - Number of times to rerun the first stage models to reduce variance of nuisances. - **mc_agg** ({'mean', 'median'}, default 'mean') - How to aggregate nuisance values across mc_iters iterations. - **allow_missing** (bool) - Whether to allow missing values in X, W. If True, nuisance models must handle missing values. - **use_ray** (bool, default False) - Whether to use ray to parallelize the cross-fitting step. - **ray_remote_func_options** (dict, default None) - Options to pass to the ray.remote decorator. ### Example Code ```python import numpy as np from sklearn.linear_model import LinearRegression from econml._ortho_learner import _OrthoLearner class ModelNuisance: def __init__(self, model_t, model_y): self._model_t = model_t self._model_y = model_y def train(self, is_selecting, folds, Y, T, W=None): self._model_t.fit(W, T) self._model_y.fit(W, Y) return self def predict(self, Y, T, W=None): return Y - self._model_y.predict(W), T - self._model_t.predict(W) class ModelFinal: def __init__(self): return def fit(self, Y, T, W=None, nuisances=None): Y_res, T_res = nuisances self.model = LinearRegression(fit_intercept=False).fit(T_res.reshape(-1, 1), Y_res) return self def predict(self, X=None): return self.model.coef_[0] def score(self, Y, T, W=None, nuisances=None): Y_res, T_res = nuisances return np.mean((Y_res - self.model.predict(T_res.reshape(-1, 1)))**2) class OrthoLearner(_OrthoLearner): def _gen_ortho_learner_model_nuisance(self): return ModelNuisance(LinearRegression(), LinearRegression()) def _gen_ortho_learner_model_final(self): return ModelFinal() np.random.seed(123) X = np.random.normal(size=(100, 3)) y = X[:, 0] + X[:, 1] + np.random.normal(0, 0.1, size=(100,)) est = OrthoLearner(cv=2, discrete_outcome=False, discrete_treatment=False, treatment_featurizer=None, discrete_instrument=False, categories='auto', random_state=None) est.fit(y, X[:, 0], W=X[:, 1:]) ``` ``` -------------------------------- ### BootstrapInference.__init__() Source: https://www.pywhy.org/EconML/ Initializes the BootstrapInference object. ```APIDOC ## BootstrapInference.__init__() ### Description Initializes the BootstrapInference object. ### Method __init__() ### Parameters None documented in source. ``` -------------------------------- ### XLearner simple example Source: https://www.pywhy.org/EconML/_autosummary/econml.metalearners.XLearner.html A basic example of using XLearner with a LinearRegression model. It generates synthetic data, fits the model, and then predicts the effect for the first three samples. ```python from econml.metalearners import XLearner from sklearn.linear_model import LinearRegression np.random.seed(123) X = np.random.normal(size=(1000, 5)) T = np.random.binomial(1, scipy.special.expit(X[:, 0])) y = (1 + .5*X[:, 0]) * T + X[:, 0] + np.random.normal(size=(1000,)) est = XLearner(models=LinearRegression()) est.fit(y, T, X=X) ``` ```python >>> est.effect(X[:3]) array([0.58547..., 1.82860..., 0.78379...]) ``` -------------------------------- ### SparseLinearDRIV simple example with default models Source: https://www.pywhy.org/EconML/_autosummary/econml.iv.dr.SparseLinearDRIV.html This example shows how to use SparseLinearDRIV with default models. It defines a data generating process with a binary instrument and treatment, fits the model, and then evaluates the effect and coefficients. The output shows the estimated effects and confidence intervals. ```python from econml.iv.dr import SparseLinearDRIV # Define the data generation functions def dgp(n, p, true_fn): X = np.random.normal(0, 1, size=(n, p)) Z = np.random.binomial(1, 0.5, size=(n,)) nu = np.random.uniform(0, 10, size=(n,)) coef_Z = 0.8 C = np.random.binomial( 1, coef_Z * scipy.special.expit(0.4 * X[:, 0] + nu) ) # Compliers when recomended C0 = np.random.binomial( 1, 0.06 * np.ones(X.shape[0]) ) # Non-compliers when not recommended T = C * Z + C0 * (1 - Z) y = true_fn(X) * T + 2 * nu + 5 * (X[:, 3] > 0) + 0.1 * np.random.uniform(0, 1, size=(n,)) return y, T, Z, X def true_heterogeneity_function(X): return 5 * X[:, 0] np.random.seed(123) y, T, Z, X = dgp(1000, 5, true_heterogeneity_function) est = SparseLinearDRIV(discrete_treatment=True, discrete_instrument=True) est.fit(Y=y, T=T, Z=Z, X=X) ``` ```python >>> est.effect(X[:3]) array([-4.23922..., 5.89221..., -3.01203...]) >>> est.effect_interval(X[:3]) (array([-6.99782..., 1.96348..., -5.41958...]), array([-1.48062..., 9.82093..., -0.60449...])) >>> est.coef_ array([ 4.65817..., 0.94689... , 0.18314... , 0.23012..., -0.40375...]) >>> est.coef__interval() (array([ 3.51645..., -0.20838..., -0.99568..., -0.89395..., -1.58517...]), array([5.79989..., 2.10218..., 1.36197..., 1.35419..., 0.77767...])) >>> est.intercept_ -0.06535... >>> est.intercept__interval() (-1.20712..., 1.07641...) ``` -------------------------------- ### Import libraries for OrthoForest examples Source: https://www.pywhy.org/EconML/spec/estimation/forest.html Imports numpy, sklearn, and the OrthoForest classes. Sets a random seed for reproducibility. ```python import numpy as np import sklearn from econml.orf import DMLOrthoForest, DROrthoForest np.random.seed(123) ``` -------------------------------- ### residuals_ Source: https://www.pywhy.org/EconML/_autosummary/econml.dml.SparseLinearDML.html Get the residuals. ```APIDOC ## residuals_ ### Description Get the residuals. ### Returns - **residuals_** - The residuals. ``` -------------------------------- ### Simple example with default models using DynamicDML Source: https://www.pywhy.org/EconML/_autosummary/econml.panel.dml.DynamicDML.html Shows how to create synthetic panel data and fit a DynamicDML model with default settings. The example sets a random seed, creates panel groups, features X, treatments T, and outcome y, then fits the model with inference='auto'. ```python from econml.panel.dml import DynamicDML np.random.seed(123) n_panels = 100 # number of panels n_periods = 3 # number of time periods per panel n = n_panels * n_periods groups = np.repeat(a=np.arange(n_panels), repeats=n_periods, axis=0) X = np.random.normal(size=(n, 1)) T = np.random.normal(size=(n, 2)) y = np.random.normal(size=(n, )) est = DynamicDML() est.fit(y, T, X=X, W=None, groups=groups, inference="auto") ``` -------------------------------- ### IVForest.__init__ Source: https://www.pywhy.org/EconML/_modules/econml/grf/classes.html Initializes an IVForest estimator with parameters controlling the forest construction, honesty, inference, and computational settings. ```APIDOC ## IVForest.__init__ ### Description Initializes an IVForest estimator with parameters controlling the forest construction, honesty, inference, and computational settings. ### Method Constructor ### Signature ```python IVForest(n_estimators=100, *, criterion="mse", max_depth=None, min_samples_split=10, min_samples_leaf=5, min_weight_fraction_leaf=0., min_var_fraction_leaf=None, min_var_leaf_on_val=False, max_features="auto", min_impurity_decrease=0., max_samples=.45, min_balancedness_tol=.45, honest=True, inference=True, fit_intercept=True, subforest_size=4, n_jobs=-1, random_state=None, verbose=0, warm_start=False) ``` ### Parameters - **n_estimators** (int) - Optional - Number of trees in the forest. Default: 100. - **criterion** (str) - Optional - Split criterion. Default: "mse". - **max_depth** (int or None) - Optional - Maximum depth of each tree. Default: None. - **min_samples_split** (int) - Optional - Minimum number of samples required to split an internal node. Default: 10. - **min_samples_leaf** (int) - Optional - Minimum number of samples required to be at a leaf node. Default: 5. - **min_weight_fraction_leaf** (float) - Optional - Minimum weighted fraction of the total sum of weights required to be at a leaf node. Default: 0. - **min_var_fraction_leaf** (float or None) - Optional - Minimum variance fraction required for a leaf. Default: None. - **min_var_leaf_on_val** (bool) - Optional - Whether to enforce min_var_fraction_leaf on validation set. Default: False. - **max_features** (str or int) - Optional - Number of features to consider for the best split. Default: "auto". - **min_impurity_decrease** (float) - Optional - Minimum impurity decrease required for a split. Default: 0. - **max_samples** (float) - Optional - Fraction of samples to use for each tree. Default: .45. - **min_balancedness_tol** (float) - Optional - Tolerance for balancedness of splits. Default: .45. - **honest** (bool) - Optional - Whether to use honest splitting. Default: True. - **inference** (bool) - Optional - Whether to enable inference. Default: True. - **fit_intercept** (bool) - Optional - Whether to fit an intercept. Default: True. - **subforest_size** (int) - Optional - Size of subforests for inference. Default: 4. - **n_jobs** (int) - Optional - Number of jobs for parallel processing. Default: -1. - **random_state** (int or None) - Optional - Seed for random number generation. Default: None. - **verbose** (int) - Optional - Verbosity level. Default: 0. - **warm_start** (bool) - Optional - Whether to reuse previous fit. Default: False. ### Returns None ``` -------------------------------- ### KernelDML simple example with discrete treatment Source: https://www.pywhy.org/EconML/_autosummary/econml.dml.KernelDML.html Shows how to instantiate KernelDML with discrete_treatment=True, dim=10, and bw=5, fit it on synthetic data, and then estimate the effect on the first three samples. The output shows approximate effect values. ```python from econml.dml import KernelDML np.random.seed(123) X = np.random.normal(size=(1000, 5)) T = np.random.binomial(1, scipy.special.expit(X[:, 0])) y = (1 + .5*X[:, 0]) * T + X[:, 0] + np.random.normal(size=(1000,)) est = KernelDML(discrete_treatment=True, dim=10, bw=5) est.fit(y, T, X=X, W=None) ``` ```python >>> est.effect(X[:3]) array([0.63042..., 1.86101..., 0.74220...]) ``` -------------------------------- ### DML example outputs: effect, intervals, coefficients Source: https://www.pywhy.org/EconML/_autosummary/econml.dml.DML.html Shows example outputs from calling est.effect, est.effect_interval, est.coef_, est.coef__interval, est.intercept_, and est.intercept__interval after fitting the DML model. Values are truncated with ellipses. ```python >>> est.effect(X[:3]) array([0.63382..., 1.78225..., 0.71859...]) >>> est.effect_interval(X[:3]) (array([0.27937..., 1.27619..., 0.42091...]), array([0.98827... , 2.28831..., 1.01628...])) >>> est.coef_ array([ 0.42857..., 0.04488..., -0.03317..., 0.02258..., -0.14875...]) >>> est.coef__interval() (array([ 0.25179..., -0.10558..., -0.16723... , -0.11916..., -0.28759...]), array([ 0.60535..., 0.19536..., 0.10088..., 0.16434..., -0.00990...])) >>> est.intercept_ 1.01166... >>> est.intercept__interval() (0.87125..., 1.15207...) ``` -------------------------------- ### SparseLinearDRIV.__init__() Source: https://www.pywhy.org/EconML/ Initializes a SparseLinearDRIV estimator. ```APIDOC ## SparseLinearDRIV.__init__() ### Description Initializes a SparseLinearDRIV estimator. ### Method __init__ ### Endpoint SparseLinearDRIV.__init__() ### Parameters None explicitly documented. ### Returns None. ```