### Import libraries and setup for TabPFN with DoubleML Source: https://docs.doubleml.org/dev/examples/learners/py_tabpfn.html Imports required libraries for the example, including TabPFN, DoubleML, scikit-learn, LightGBM, and data generation utilities. Suppresses specific warnings related to CPU usage and feature names. Requires separate TabPFN installation; CUDA-enabled PyTorch recommended for GPU acceleration. ```python import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import time from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier from sklearn.linear_model import LinearRegression, LogisticRegression import lightgbm as lgbm from tabpfn import TabPFNRegressor, TabPFNClassifier from tabpfn.constants import ModelVersion import doubleml as dml from doubleml.irm.datasets import make_irm_data_discrete_treatments import warnings warnings.filterwarnings("ignore", message="Running on CPU*", category=UserWarning, module="tabpfn") warnings.filterwarnings("ignore", message=".*does not have valid feature names.*", category=UserWarning) ``` -------------------------------- ### DoubleMLPLR with Optuna Tuning Example Source: https://docs.doubleml.org/dev/api/generated/doubleml.did.DoubleMLDID.html Complete example showing how to use DoubleMLPLR with LightGBM regressors and Optuna for hyperparameter tuning. It includes data generation, model setup, parameter grid functions, tuning with TPE sampler, and fitting the model to get summary results. ```python >>> import numpy as np >>> from doubleml import DoubleMLData, DoubleMLPLR >>> from doubleml.plm.datasets import make_plr_CCDDHNR2018 >>> from lightgbm import LGBMRegressor >>> import optuna >>> # Generate data >>> np.random.seed(42) >>> data = make_plr_CCDDHNR2018(n_obs=500, dim_x=20, return_type='DataFrame') >>> dml_data = DoubleMLData(data, 'y', 'd') >>> # Initialize model >>> dml_plr = DoubleMLPLR( ... dml_data, ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42), ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42) ... ) >>> # Define parameter grid functions >>> def ml_l_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> def ml_m_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> ml_param_space = {'ml_l': ml_l_params, 'ml_m': ml_m_params} >>> # Tune with TPE sampler >>> optuna_settings = { ... 'n_trials': 5, ... 'sampler': optuna.samplers.TPESampler(seed=42), ... } >>> tune_res = dml_plr.tune_ml_models(ml_param_space, optuna_settings=optuna_settings, return_tune_res=True) >>> print(tune_res[0]['ml_l'].best_params) {'learning_rate': 0.03907122389107094} >>> # Fit and get results >>> dml_plr.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.57436 0.045206 12.705519 5.510257e-37 0.485759 0.662961 >>> # Example with scoring methods and directions >>> scoring_methods = { ... 'ml_l': 'neg_mean_squared_error', # Negative metric ... 'ml_m': 'neg_mean_squared_error' ... } >>> optuna_settings = { ... 'n_trials': 50, ... 'direction': 'maximize', # Maximize negative MSE (minimize MSE) ... 'sampler': optuna.samplers.TPESampler(seed=42), ... } >>> tune_res = dml_plr.tune_ml_models(ml_param_space, scoring_methods=scoring_methods, ... optuna_settings=optuna_settings, return_tune_res=True) >>> print(tune_res[0]['ml_l'].best_params) {'learning_rate': 0.04300012336462904} >>> dml_plr.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.574796 0.045062 12.755721 2.896820e-37 0.486476 0.663115 ``` -------------------------------- ### Setup DoubleML data for tuning example Source: https://docs.doubleml.org/dev/guide/learners.html Initializes a DoubleMLData object from simulated data with 200 observations and 200 variables. Required before tuning a DoubleMLPLR model. ```R library(DoubleML) library(mlr3) library(data.table) set.seed(3141) n_obs = 200 n_vars = 200 theta = 3 X = matrix(stats::rnorm(n_obs * n_vars), nrow = n_obs, ncol = n_vars) d = X[, 1:3, drop = FALSE] %*% c(5, 5, 5) + stats::rnorm(n_obs) y = theta * d + X[, 1:3, drop = FALSE] %*% c(5, 5, 5) + stats::rnorm(n_obs) dml_data = double_ml_data_from_matrix(X = X, y = y, d = d) ``` -------------------------------- ### DoubleMLPQ Constructor and Example Source: https://docs.doubleml.org/dev/api/generated/doubleml.irm.DoubleMLPQ.html Example of creating a DoubleMLPQ object, fitting it, and displaying the summary of results. ```APIDOC ## DoubleMLPQ ### Description Class for partially linear quantile regression models based on double machine learning. ### Usage ```python import numpy as np import doubleml as dml from doubleml.irm.datasets import make_irm_data from sklearn.ensemble import RandomForestClassifier np.random.seed(3141) ml_g = RandomForestClassifier(n_estimators=100, max_features=20, max_depth=10, min_samples_leaf=2) ml_m = RandomForestClassifier(n_estimators=100, max_features=20, max_depth=10, min_samples_leaf=2) data = make_irm_data(theta=0.5, n_obs=500, dim_x=20, return_type='DataFrame') obj_dml_data = dml.DoubleMLData(data, 'y', 'd') dml_pq_obj = dml.DoubleMLPQ(obj_dml_data, ml_g, ml_m, treatment=1, quantile=0.5) dml_pq_obj.fit().summary ``` ### Output Example ``` coef std err t P>|t| 2.5 % 97.5 % d 0.553878 0.149858 3.696011 0.000219 0.260161 0.847595 ``` ### Methods - `bootstrap(method, n_rep_boot)` - Multiplier bootstrap for DoubleML models. - `confint(joint, level)` - Confidence intervals for DoubleML models. - `construct_framework()` - Construct a `doubleml.DoubleMLFramework` object. - `draw_sample_splitting()` - Draw sample splitting for DoubleML models. - `evaluate_learners(learners, metric)` - Evaluate fitted learners for DoubleML models on cross-validated predictions. - `fit(n_jobs_cv, store_predictions, ...)` - Estimate DoubleML models. - `get_params(learner)` - Get hyperparameters for the nuisance model of DoubleML models. - `p_adjust(method)` - Multiple testing adjustment for DoubleML models. - `sensitivity_analysis(cf_y, cf_d, rho, ...)` - Performs a sensitivity analysis to account for unobserved confounders. - `sensitivity_benchmark(benchmarking_set, ...)` - Computes a benchmark for a given set of features. - `sensitivity_plot(idx_treatment, value, ...)` - Contour plot of the sensivity with respect to latent/confounding variables. - `set_ml_nuisance_params(learner, treat_var, ...)` - Set hyperparameters for the nuisance models of DoubleML models. - `set_sample_splitting(all_smpls, ...)` - Set the sample splitting for DoubleML models. - `tune(param_grids, tune_on_folds, ...)` - Hyperparameter-tuning for DoubleML models. - `tune_ml_models(ml_param_space, ...)` - Hyperparameter-tuning for DoubleML models using Optuna. ``` -------------------------------- ### Import libraries for DoubleML pension QTE example Source: https://docs.doubleml.org/dev/examples/py_double_ml_pension_qte.html Imports required Python libraries including numpy, pandas, doubleml, sklearn, lightgbm, and plotting libraries. This is the initial setup for the example. ```python import numpy as np import pandas as pd import doubleml as dml import multiprocessing from doubleml.datasets import fetch_401K from sklearn.base import clone from lightgbm import LGBMClassifier, LGBMRegressor import matplotlib.pyplot as plt import seaborn as sns ``` -------------------------------- ### Install Python prerequisites on Linux Source: https://docs.doubleml.org/dev/intro/install.html Install python3 and python3-pip using the system package manager on Linux. ```bash $ sudo apt-get install python3 python3-pip ``` -------------------------------- ### Load DoubleML and mlr3learners libraries Source: https://docs.doubleml.org/dev/examples/R_double_ml_basic_iv.html Loads the DoubleML package and the mlr3learners package, sets a random seed, and suppresses warnings. This is the initial setup for the IV example. ```R library(DoubleML) library(mlr3learners) set.seed(1234) options(warn=-1) ``` -------------------------------- ### DoubleMLAPOS Tuning Example Source: https://docs.doubleml.org/dev/api/generated/doubleml.irm.DoubleMLAPOS.html Example demonstrating how to tune DoubleML models using Optuna with DoubleMLAPOS. ```APIDOC ## DoubleMLAPOS Tuning Example ### Description This example shows how to tune a DoubleML model using Optuna, including defining parameter grid functions, setting up the Optuna sampler, and fitting the model. ### Usage ```python import numpy as np from doubleml import DoubleMLData, DoubleMLPLR from doubleml.plm.datasets import make_plr_CCDDHNR2018 from lightgbm import LGBMRegressor import optuna # Generate data np.random.seed(42) data = make_plr_CCDDHNR2018(n_obs=500, dim_x=20, return_type='DataFrame') dml_data = DoubleMLData(data, 'y', 'd') # Initialize model dml_plr = DoubleMLPLR( dml_data, LGBMRegressor(n_estimators=50, verbose=-1, random_state=42), LGBMRegressor(n_estimators=50, verbose=-1, random_state=42) ) # Define parameter grid functions def ml_l_params(trial): return { 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), } def ml_m_params(trial): return { 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), } ml_param_space = {'ml_l': ml_l_params, 'ml_m': ml_m_params} # Tune with TPE sampler optuna_settings = { 'n_trials': 5, 'sampler': optuna.samplers.TPESampler(seed=42), } tune_res = dml_plr.tune_ml_models(ml_param_space, optuna_settings=optuna_settings, return_tune_res=True) print(tune_res[0]['ml_l'].best_params) # {'learning_rate': 0.03907122389107094} # Fit and get results dml_plr.fit().summary # coef std err t P>|t| 2.5 % 97.5 % # d 0.57436 0.045206 12.705519 5.510257e-37 0.485759 0.662961 ``` ### Example with scoring methods and directions ```python scoring_methods = { 'ml_l': 'neg_mean_squared_error', # Negative metric 'ml_m': 'neg_mean_squared_error' } optuna_settings = { 'n_trials': 50, 'direction': 'maximize', # Maximize negative MSE (minimize MSE) 'sampler': optuna.samplers.TPESampler(seed=42), } tune_res = dml_plr.tune_ml_models(ml_param_space, scoring_methods=scoring_methods, optuna_settings=optuna_settings, return_tune_res=True) print(tune_res[0]['ml_l'].best_params) # {'learning_rate': 0.04300012336462904} dml_plr.fit().summary # coef std err t P>|t| 2.5 % 97.5 % # d 0.574796 0.045062 12.755721 2.896820e-37 0.486476 0.663115 ``` ``` -------------------------------- ### Install DoubleML from a .whl file Source: https://docs.doubleml.org/dev/intro/install.html Installs a released version of DoubleML from a .whl file obtained from GitHub Releases. Requires python and pip to be set up. ```bash $ pip install -U DoubleML-0.5.1-py3-none-any.whl ``` -------------------------------- ### DoubleMLLPQ Constructor and Example Source: https://docs.doubleml.org/dev/api/generated/doubleml.irm.DoubleMLLPQ.html Example of creating a DoubleMLLPQ object and fitting it to estimate the local partial quantile treatment effect. ```APIDOC ## DoubleMLLPQ ### Description Class for double machine learning estimation of local partial quantile treatment effects. ### Constructor `DoubleMLLPQ(obj_dml_data, ml_g, ml_m, treatment=1, quantile=0.5)` ### Parameters - **obj_dml_data** (DoubleMLData) - Required - The DoubleMLData object containing the data. - **ml_g** (estimator) - Required - Machine learning estimator for the outcome regression. - **ml_m** (estimator) - Required - Machine learning estimator for the treatment propensity. - **treatment** (int) - Optional - The treatment value for which the effect is estimated. Default is 1. - **quantile** (float) - Optional - The quantile at which the effect is estimated. Default is 0.5. ### Example ```python >>> import numpy as np >>> import doubleml as dml >>> from doubleml.irm.datasets import make_iivm_data >>> from sklearn.ensemble import RandomForestClassifier >>> np.random.seed(3141) >>> ml_g = RandomForestClassifier(n_estimators=100, max_features=20, max_depth=10, min_samples_leaf=2) >>> ml_m = RandomForestClassifier(n_estimators=100, max_features=20, max_depth=10, min_samples_leaf=2) >>> data = make_iivm_data(theta=0.5, n_obs=1000, dim_x=20, return_type='DataFrame') >>> obj_dml_data = dml.DoubleMLData(data, 'y', 'd', z_cols='z') >>> dml_lpq_obj = dml.DoubleMLLPQ(obj_dml_data, ml_g, ml_m, treatment=1, quantile=0.5) >>> dml_lpq_obj.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.217244 0.636453 0.341336 0.73285 -1.03018 1.464668 ``` ``` -------------------------------- ### DoubleML Setup and Estimation Source: https://docs.doubleml.org/stable/examples/py_double_ml_rdflex.ipynb This snippet shows the typical workflow for DoubleML: importing the library, creating a DoubleMLData object, and fitting a DoubleMLPLR model. ```python import doubleml as dml from doubleml import DoubleMLData from doubleml import DoubleMLPLR dml_data = DoubleMLData(df, y_col='y', d_cols='d', x_cols='x') dml_plr = DoubleMLPLR(dml_data, ml_g, ml_m) dml_plr.fit() ``` -------------------------------- ### Import libraries for RDFlex RDD example Source: https://docs.doubleml.org/dev/examples/py_double_ml_rdflex.html Imports required packages for the RDFlex example, including numpy, pandas, plotly, statsmodels, lightgbm, rdrobust, and doubleml.rdd. The dependencies for doubleml.rdd can be installed separately via pip install rdrobust if necessary. ```python import numpy as np import pandas as pd import plotly.express as px import plotly.graph_objs as go from statsmodels.nonparametric.kernel_regression import KernelReg from lightgbm import LGBMRegressor, LGBMClassifier from rdrobust import rdrobust import doubleml as dml from doubleml.rdd import RDFlex from doubleml.rdd.datasets import make_simple_rdd_data ``` -------------------------------- ### Set up and activate virtualenv on Linux Source: https://docs.doubleml.org/dev/intro/install.html Create a virtual environment named dml-venv and activate it to avoid package conflicts. ```bash $ virtualenv -p python3 dml-venv $ source dml-venv/bin/activate ``` -------------------------------- ### DoubleMLPLIV Usage Example Source: https://docs.doubleml.org/dev/api/generated/doubleml.plm.DoubleMLPLIV.html Example demonstrating how to use DoubleMLPLIV for partially linear IV regression, including data generation, model setup, tuning, and fitting. ```APIDOC ## DoubleMLPLIV Usage Example ### Description This example shows how to use the DoubleMLPLIV class for partially linear IV regression. It covers data generation, model initialization, hyperparameter tuning with Optuna, and fitting the model to obtain estimates. ### Class DoubleMLPLIV ### Module doubleml.plm ### Usage Example ```python >>> import numpy as np >>> from doubleml import DoubleMLData, DoubleMLPLR >>> from doubleml.plm.datasets import make_plr_CCDDHNR2018 >>> from lightgbm import LGBMRegressor >>> import optuna >>> # Generate data >>> np.random.seed(42) >>> data = make_plr_CCDDHNR2018(n_obs=500, dim_x=20, return_type='DataFrame') >>> dml_data = DoubleMLData(data, 'y', 'd') >>> # Initialize model >>> dml_plr = DoubleMLPLR( ... dml_data, ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42), ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42) ... ) >>> # Define parameter grid functions >>> def ml_l_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> def ml_m_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> ml_param_space = {'ml_l': ml_l_params, 'ml_m': ml_m_params} >>> # Tune with TPE sampler >>> optuna_settings = { ... 'n_trials': 5, ... 'sampler': optuna.samplers.TPESampler(seed=42), ... } >>> tune_res = dml_plr.tune_ml_models(ml_param_space, optuna_settings=optuna_settings, return_tune_res=True) >>> print(tune_res[0]['ml_l'].best_params) {'learning_rate': 0.03907122389107094} >>> # Fit and get results >>> dml_plr.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.57436 0.045206 12.705519 5.510257e-37 0.485759 0.662961 >>> # Example with scoring methods and directions >>> scoring_methods = { ... 'ml_l': 'neg_mean_squared_error', # Negative metric ... 'ml_m': 'neg_mean_squared_error' ... } >>> optuna_settings = { ... 'n_trials': 50, ... 'direction': 'maximize', # Maximize negative MSE (minimize MSE) ... 'sampler': optuna.samplers.TPESampler(seed=42), ... } >>> tune_res = dml_plr.tune_ml_models(ml_param_space, scoring_methods=scoring_methods, ... optuna_settings=optuna_settings, return_tune_res=True) >>> print(tune_res[0]['ml_l'].best_params) {'learning_rate': 0.04300012336462904} >>> dml_plr.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.574796 0.045062 12.755721 2.896820e-37 0.486476 0.663115 ``` ### Methods - **tune_ml_models(ml_param_space, optuna_settings=None, scoring_methods=None, return_tune_res=False)** - Tunes the machine learning models using Optuna. Parameters: `ml_param_space` (dict) - parameter grid functions for each nuisance model; `optuna_settings` (dict) - settings for Optuna optimization; `scoring_methods` (dict) - scoring methods for each model; `return_tune_res` (bool) - whether to return tuning results. - **fit()** - Fits the DoubleMLPLIV model. ### Attributes - **summary** - DataFrame with coefficient estimates, standard errors, t-statistics, p-values, and confidence intervals. ``` -------------------------------- ### DoubleMLPLR Tuning and Fitting Example Source: https://docs.doubleml.org/stable/api/generated/doubleml.irm.DoubleMLSSM.html Example code showing how to use DoubleMLPLR with LightGBM regressors, tune hyperparameters using Optuna, and fit the model to get summary results. ```APIDOC ## DoubleMLPLR Tuning and Fitting Example ### Description This example demonstrates the usage of the DoubleMLPLR class for double machine learning with hyperparameter tuning via Optuna. It includes data generation, model initialization, parameter grid definition, tuning with TPE sampler, and fitting the model to obtain summary statistics. ### Method Not applicable (Python code example) ### Endpoint Not applicable (Python code example) ### Parameters Not applicable (Python code example) ### Request Example ```python >>> import numpy as np >>> from doubleml import DoubleMLData, DoubleMLPLR >>> from doubleml.plm.datasets import make_plr_CCDDHNR2018 >>> from lightgbm import LGBMRegressor >>> import optuna >>> # Generate data >>> np.random.seed(42) >>> data = make_plr_CCDDHNR2018(n_obs=500, dim_x=20, return_type='DataFrame') >>> dml_data = DoubleMLData(data, 'y', 'd') >>> # Initialize model >>> dml_plr = DoubleMLPLR( ... dml_data, ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42), ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42) ... ) >>> # Define parameter grid functions >>> def ml_l_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> def ml_m_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> ml_param_space = {'ml_l': ml_l_params, 'ml_m': ml_m_params} >>> # Tune with TPE sampler >>> optuna_settings = { ... 'n_trials': 5, ... 'sampler': optuna.samplers.TPESampler(seed=42), ... } >>> tune_res = dml_plr.tune_ml_models(ml_param_space, optuna_settings=optuna_settings, return_tune_res=True) >>> print(tune_res[0]['ml_l'].best_params) {'learning_rate': 0.03907122389107094} >>> # Fit and get results >>> dml_plr.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.57436 0.045206 12.705519 5.510257e-37 0.485759 0.662961 ``` ### Response #### Success Response Not applicable (Python code example) #### Response Example Not applicable (Python code example) ``` -------------------------------- ### DoubleMLDIDMulti Usage Example Source: https://docs.doubleml.org/dev/api/generated/doubleml.did.DoubleMLDIDMulti.html Example demonstrating how to use DoubleMLDIDMulti (via DoubleMLPLR) to generate data, initialize the model, tune hyperparameters with Optuna, and fit the model to obtain results. ```APIDOC ## DoubleMLDIDMulti Usage Example ### Description This example shows how to use the DoubleMLDIDMulti class (represented here by DoubleMLPLR) to perform double machine learning for partially linear regression models. It covers data generation, model initialization, hyperparameter tuning with Optuna, and model fitting. ### Method Python API ### Endpoint Not applicable (Python library) ### Parameters - `n_obs` (int) - Number of observations (e.g., 500) - `dim_x` (int) - Number of covariates (e.g., 20) - `return_type` (str) - Type of data structure returned (e.g., 'DataFrame') - `data` (DataFrame) - Input data for the model - `y` (str) - Name of the outcome variable - `d` (str) - Name of the treatment variable - `ml_l` (estimator) - Machine learning estimator for the nuisance function l - `ml_m` (estimator) - Machine learning estimator for the nuisance function m - `ml_param_space` (dict) - Dictionary of parameter grid functions for each nuisance function - `optuna_settings` (dict) - Settings for Optuna optimization (e.g., n_trials, sampler, direction) - `scoring_methods` (dict) - Scoring methods for each nuisance function (e.g., 'neg_mean_squared_error') - `return_tune_res` (bool) - Whether to return tuning results ### Request Example ```python import numpy as np from doubleml import DoubleMLData, DoubleMLPLR from doubleml.plm.datasets import make_plr_CCDDHNR2018 from lightgbm import LGBMRegressor import optuna # Generate data np.random.seed(42) data = make_plr_CCDDHNR2018(n_obs=500, dim_x=20, return_type='DataFrame') dml_data = DoubleMLData(data, 'y', 'd') # Initialize model dml_plr = DoubleMLPLR( dml_data, LGBMRegressor(n_estimators=50, verbose=-1, random_state=42), LGBMRegressor(n_estimators=50, verbose=-1, random_state=42) ) # Define parameter grid functions def ml_l_params(trial): return { 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), } def ml_m_params(trial): return { 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), } ml_param_space = {'ml_l': ml_l_params, 'ml_m': ml_m_params} # Tune with TPE sampler optuna_settings = { 'n_trials': 5, 'sampler': optuna.samplers.TPESampler(seed=42), } tune_res = dml_plr.tune_ml_models(ml_param_space, optuna_settings=optuna_settings, return_tune_res=True) print(tune_res[0]['ml_l'].best_params) # Fit and get results dml_plr.fit().summary ``` ### Response #### Success Response - `tune_res` (list) - List of tuning results for each nuisance function, containing best parameters. - `summary` (DataFrame) - Summary of the fitted model including coefficient, standard error, t-statistic, p-value, and confidence intervals. #### Response Example ```python {'learning_rate': 0.03907122389107094} coef std err t P>|t| 2.5 % 97.5 % d 0.57436 0.045206 12.705519 5.510257e-37 0.485759 0.662961 ``` ### Additional Example with Scoring Methods and Directions ```python scoring_methods = { 'ml_l': 'neg_mean_squared_error', # Negative metric 'ml_m': 'neg_mean_squared_error' } optuna_settings = { 'n_trials': 50, 'direction': 'maximize', # Maximize negative MSE (minimize MSE) 'sampler': optuna.samplers.TPESampler(seed=42), } tune_res = dml_plr.tune_ml_models(ml_param_space, scoring_methods=scoring_methods, optuna_settings=optuna_settings, return_tune_res=True) print(tune_res[0]['ml_l'].best_params) dml_plr.fit().summary ``` #### Response Example ```python {'learning_rate': 0.04300012336462904} coef std err t P>|t| 2.5 % 97.5 % d 0.574796 0.045062 12.755721 2.896820e-37 0.486476 0.663115 ``` ``` -------------------------------- ### DoubleMLDIDCS Usage Example Source: https://docs.doubleml.org/dev/api/generated/doubleml.did.DoubleMLDIDCS.html This example shows how to use the DoubleMLDIDCS class (or similar DoubleML classes) to generate data, initialize a model, tune hyperparameters with Optuna, and fit the model to get results. ```APIDOC ## DoubleMLDIDCS Usage Example ### Description This example demonstrates the usage of the DoubleMLDIDCS class (or similar DoubleML classes) for double machine learning. It covers data generation, model initialization, hyperparameter tuning with Optuna, and fitting the model to obtain summary statistics. ### Method Not applicable (Python class usage) ### Endpoint Not applicable (Python class usage) ### Parameters Not applicable (Python class usage) ### Request Example ```python >>> import numpy as np >>> from doubleml import DoubleMLData, DoubleMLPLR >>> from doubleml.plm.datasets import make_plr_CCDDHNR2018 >>> from lightgbm import LGBMRegressor >>> import optuna >>> # Generate data >>> np.random.seed(42) >>> data = make_plr_CCDDHNR2018(n_obs=500, dim_x=20, return_type='DataFrame') >>> dml_data = DoubleMLData(data, 'y', 'd') >>> # Initialize model >>> dml_plr = DoubleMLPLR( ... dml_data, ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42), ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42) ... ) >>> # Define parameter grid functions >>> def ml_l_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> def ml_m_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> ml_param_space = {'ml_l': ml_l_params, 'ml_m': ml_m_params} >>> # Tune with TPE sampler >>> optuna_settings = { ... 'n_trials': 5, ... 'sampler': optuna.samplers.TPESampler(seed=42), ... } >>> tune_res = dml_plr.tune_ml_models(ml_param_space, optuna_settings=optuna_settings, return_tune_res=True) >>> print(tune_res[0]['ml_l'].best_params) {'learning_rate': 0.03907122389107094} >>> # Fit and get results >>> dml_plr.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.57436 0.045206 12.705519 5.510257e-37 0.485759 0.662961 >>> # Example with scoring methods and directions >>> scoring_methods = { ... 'ml_l': 'neg_mean_squared_error', # Negative metric ... 'ml_m': 'neg_mean_squared_error' ... } >>> optuna_settings = { ... 'n_trials': 50, ... 'direction': 'maximize', # Maximize negative MSE (minimize MSE) ... 'sampler': optuna.samplers.TPESampler(seed=42), ... } >>> tune_res = dml_plr.tune_ml_models(ml_param_space, scoring_methods=scoring_methods, ... optuna_settings=optuna_settings, return_tune_res=True) >>> print(tune_res[0]['ml_l'].best_params) {'learning_rate': 0.04300012336462904} >>> dml_plr.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.574796 0.045062 12.755721 2.896820e-37 0.486476 0.663115 ``` ### Response #### Success Response (200) - **summary** (DataFrame) - The fitted model's summary statistics including coefficient, standard error, t-statistic, p-value, and confidence intervals. #### Response Example ``` coef std err t P>|t| 2.5 % 97.5 % d 0.57436 0.045206 12.705519 5.510257e-37 0.485759 0.662961 ``` ``` -------------------------------- ### Setup and Data Generation for Learner Comparison Source: https://docs.doubleml.org/dev/examples/learners/py_learner.html Imports DoubleML and numpy, defines simulation parameters (theta=0, n_obs=500, dim_x=5, n_rep=200), sets a random seed, and generates 200 datasets using make_irm_data with R2_d=0.8 and R2_y=0.8. Each dataset is wrapped in a DoubleMLData object with outcome 'y' and treatment 'd'. Warnings about non-positive nu2 are filtered. ```python import numpy as np import doubleml as dml from doubleml.irm.datasets import make_irm_data import warnings warnings.filterwarnings("ignore", message="The estimated nu2 for d is not positive") theta = 0 n_obs = 500 dim_x = 5 n_rep = 200 np.random.seed(42) datasets = [] for i in range(n_rep): data = make_irm_data(theta=theta, n_obs=n_obs, dim_x=dim_x, R2_d=0.8, R2_y=0.8, return_type='DataFrame') datasets.append(dml.DoubleMLData(data, 'y', 'd')) ``` -------------------------------- ### Create and fit DoubleMLIRM model Source: https://docs.doubleml.org/stable/examples/py_double_ml_plm_irm_hetfx.ipynb Initializes a DoubleMLIRM object with data, treatment, outcome, and confounders, then fits the model. This is the core setup for the IRM example. ```python aipw_obj_2 = DoubleMLIRM(data, 'd2', 'y', ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9', 'x10', 'x11', 'x12', 'x13', 'x14', 'x15', 'x16', 'x17', 'x18', 'x19', 'x20']) aipw_obj_2.fit() ``` -------------------------------- ### Windows: Install DoubleML with pip (with virtualenv) Source: https://docs.doubleml.org/stable/intro/install.html Create and activate a virtual environment named 'dml-venv', then install DoubleML with pip. Verify with pip show, pip freeze, and a Python version check. ```bash $ python -m venv dml-venv $ dml-venv\Scripts\activate ``` ```bash $ pip install -U DoubleML ``` ```bash $ python -m pip show DoubleML # to see which version and where DoubleML is installed $ python -m pip freeze # to see all packages installed in the active virtualenv $ python -c "import doubleml as dml; print(dml.__version__)" ``` -------------------------------- ### Import libraries and generate discrete treatment data Source: https://docs.doubleml.org/dev/examples/py_double_ml_apo.html Imports required Python libraries (numpy, pandas, matplotlib, seaborn, scikit-learn, doubleml) and the data generation function make_irm_data_discrete_treatments. This is the initial setup for the APO estimation example. ```python import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from sklearn.linear_model import LogisticRegression, LinearRegression from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor import doubleml as dml from doubleml.irm.datasets import make_irm_data_discrete_treatments ``` -------------------------------- ### DoubleMLQTE Class Usage Source: https://docs.doubleml.org/stable/api/generated/doubleml.irm.DoubleMLQTE.html Example demonstrating how to instantiate and fit a DoubleMLQTE model using DoubleML. ```APIDOC ## DoubleMLQTE Class ### Description DoubleMLQTE is a class for estimating quantile treatment effects using double machine learning. ### Usage Example ```python >>> import numpy as np >>> import doubleml as dml >>> from doubleml.irm.datasets import make_irm_data >>> from sklearn.ensemble import RandomForestClassifier >>> np.random.seed(3141) >>> ml_g = RandomForestClassifier(n_estimators=100, max_features=20, max_depth=10, min_samples_leaf=2) >>> ml_m = RandomForestClassifier(n_estimators=100, max_features=20, max_depth=10, min_samples_leaf=2) >>> data = make_irm_data(theta=0.5, n_obs=500, dim_x=20, return_type='DataFrame') >>> obj_dml_data = dml.DoubleMLData(data, 'y', 'd') >>> dml_qte_obj = dml.DoubleMLQTE(obj_dml_data, ml_g, ml_m, quantiles=[0.25, 0.5, 0.75]) >>> dml_qte_obj.fit().summary coef std err t P>|t| 2.5 % 97.5 % 0.25 0.274825 0.347310 0.791297 0.428771 -0.405890 0.955541 0.50 0.449150 0.192539 2.332782 0.019660 0.071782 0.826519 0.75 0.709606 0.193308 3.670867 0.000242 0.330731 1.088482 ``` ### Methods - **bootstrap**([method, n_rep_boot]) - Multiplier bootstrap for DoubleML models. - **confint**([joint, level]) - Confidence intervals for DoubleML models. - **draw_sample_splitting**() - Draw sample splitting for DoubleML models. - **fit**([n_jobs_models, n_jobs_cv, ...]) - Estimate DoubleMLQTE models. - **p_adjust**([method]) - Multiple testing adjustment for DoubleML models. - **set_sample_splitting**(all_smpls[, ...]) - Set the sample splitting for DoubleML models. - **tune_ml_models**(ml_param_space[, ...]) - Hyperparameter-tuning for DoubleML models using Optuna. ``` -------------------------------- ### Install DoubleML development version from GitHub Source: https://docs.doubleml.org/dev/intro/install.html Installs the development version of DoubleML from GitHub using remotes::install_github. Requires the remotes package to be previously installed. After installation, load the package with library(DoubleML). ```R remotes::install_github("DoubleML/doubleml-for-r") ``` ```R library(DoubleML) ``` -------------------------------- ### Install DoubleML from CRAN Source: https://docs.doubleml.org/dev/intro/install.html Installs the latest release of the DoubleML package from CRAN using install.packages. After installation, load the package with library(DoubleML). ```R install.packages("DoubleML") ``` ```R library(DoubleML) ``` -------------------------------- ### Tuning DoubleMLPLR with Optuna Source: https://docs.doubleml.org/dev/api/generated/doubleml.did.DoubleMLDIDBinary.html Complete example of tuning a DoubleMLPLR model using Optuna, including data generation, model initialization, parameter grid definition, and tuning with TPE sampler. Demonstrates the use of tune_ml_models and shows the resulting summary. ```python >>> import numpy as np >>> from doubleml import DoubleMLData, DoubleMLPLR >>> from doubleml.plm.datasets import make_plr_CCDDHNR2018 >>> from lightgbm import LGBMRegressor >>> import optuna >>> # Generate data >>> np.random.seed(42) >>> data = make_plr_CCDDHNR2018(n_obs=500, dim_x=20, return_type='DataFrame') >>> dml_data = DoubleMLData(data, 'y', 'd') >>> # Initialize model >>> dml_plr = DoubleMLPLR( ... dml_data, ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42), ... LGBMRegressor(n_estimators=50, verbose=-1, random_state=42) ... ) >>> # Define parameter grid functions >>> def ml_l_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> def ml_m_params(trial): ... return { ... 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True), ... } >>> ml_param_space = {'ml_l': ml_l_params, 'ml_m': ml_m_params} >>> # Tune with TPE sampler >>> optuna_settings = { ... 'n_trials': 5, ... 'sampler': optuna.samplers.TPESampler(seed=42), ... } >>> tune_res = dml_plr.tune_ml_models(ml_param_space, optuna_settings=optuna_settings, return_tune_res=True) >>> print(tune_res[0]['ml_l'].best_params) {'learning_rate': 0.03907122389107094} >>> # Fit and get results >>> dml_plr.fit().summary coef std err t P>|t| 2.5 % 97.5 % d 0.57436 0.045206 12.705519 5.510257e-37 0.485759 0.662961 ``` -------------------------------- ### Verify DoubleML installation without conda environment Source: https://docs.doubleml.org/dev/intro/install.html Check the installed version and location of DoubleML, list all installed packages, and print the DoubleML version. ```bash $ conda list DoubleML # to see which version and where DoubleML is installed $ conda list # to see all installed packages $ python -c "import doubleml as dml; print(dml.__version__)" ``` -------------------------------- ### Verify DoubleML installation without virtual environment Source: https://docs.doubleml.org/dev/intro/install.html Check the installed version and location of DoubleML, list all installed packages, and print the DoubleML version. ```bash $ python3 -m pip show DoubleML # to see which version and where DoubleML is installed $ python3 -m pip freeze # to see all installed packages $ python3 -c "import doubleml as dml; print(dml.__version__)" ``` -------------------------------- ### Example: Adjusting propensity scores with PSProcessor Source: https://docs.doubleml.org/dev/api/generated/doubleml.utils.PSProcessor.html Shows how to instantiate PSProcessor with a clipping threshold and adjust propensity scores using the adjust_ps method. The example prints the adjusted scores, which are clipped to the threshold. ```python >>> import numpy as np >>> from doubleml.utils import PSProcessor >>> ps = np.array([0.001, 0.2, 0.5, 0.8, 0.999]) >>> treatment = np.array([0, 1, 1, 0, 1]) >>> processor = PSProcessor(clipping_threshold=0.01) >>> adjusted = processor.adjust_ps(ps, treatment) >>> print(np.round(adjusted, 3)) [0.01 0.2 0.5 0.8 0.99] ``` -------------------------------- ### Install DoubleML via conda Source: https://docs.doubleml.org/dev/intro/install.html Install the latest DoubleML release from conda-forge. ```bash $ conda install -c conda-forge doubleml ``` -------------------------------- ### Import libraries and set random seeds for DoubleML robust IV example Source: https://docs.doubleml.org/dev/examples/py_double_ml_robust_iv.html Sets up the Python environment for the example by importing necessary libraries (numpy, pandas, sklearn, doubleml) and fixing random seeds for reproducibility. This is the initial step before generating synthetic data and fitting models. ```python import numpy as np import pandas as pd import random from sklearn.base import BaseEstimator, ClassifierMixin from sklearn.linear_model import LinearRegression, LogisticRegression import doubleml as dml np.random.seed(1234) random.seed(1234) ```