|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import math |
| 4 | + |
| 5 | +from sklearn.base import clone |
| 6 | + |
| 7 | +from sklearn.linear_model import LogisticRegression |
| 8 | +from sklearn.ensemble import RandomForestRegressor |
| 9 | + |
| 10 | +import doubleml as dml |
| 11 | + |
| 12 | +from ...tests._utils import draw_smpls |
| 13 | +from ._utils_ssm_manual import fit_selection, tune_nuisance_ssm |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope='module', |
| 17 | + params=[RandomForestRegressor(random_state=42)]) |
| 18 | +def learner_g(request): |
| 19 | + return request.param |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope='module', |
| 23 | + params=[LogisticRegression(random_state=42)]) |
| 24 | +def learner_m(request): |
| 25 | + return request.param |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope='module', |
| 29 | + params=['missing-at-random', 'nonignorable']) |
| 30 | +def score(request): |
| 31 | + return request.param |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope='module', |
| 35 | + params=[True, False]) |
| 36 | +def normalize_ipw(request): |
| 37 | + return request.param |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(scope='module', |
| 41 | + params=[True, False]) |
| 42 | +def tune_on_folds(request): |
| 43 | + return request.param |
| 44 | + |
| 45 | + |
| 46 | +def get_par_grid(learner): |
| 47 | + if learner.__class__ in [RandomForestRegressor]: |
| 48 | + par_grid = {'n_estimators': [5, 10, 20]} |
| 49 | + else: |
| 50 | + assert learner.__class__ in [LogisticRegression] |
| 51 | + par_grid = {'C': np.logspace(-2, 2, 10)} |
| 52 | + return par_grid |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(scope='module') |
| 56 | +def dml_ssm_fixture(generate_data_selection_mar, generate_data_selection_nonignorable, |
| 57 | + learner_g, learner_m, score, |
| 58 | + normalize_ipw, tune_on_folds): |
| 59 | + par_grid = {'ml_g': get_par_grid(learner_g), |
| 60 | + 'ml_pi': get_par_grid(learner_m), |
| 61 | + 'ml_m': get_par_grid(learner_m)} |
| 62 | + n_folds_tune = 4 |
| 63 | + n_folds = 2 |
| 64 | + |
| 65 | + # collect data |
| 66 | + np.random.seed(42) |
| 67 | + if score == 'missing-at-random': |
| 68 | + (x, y, d, z, s) = generate_data_selection_mar |
| 69 | + else: |
| 70 | + (x, y, d, z, s) = generate_data_selection_nonignorable |
| 71 | + |
| 72 | + n_obs = len(y) |
| 73 | + all_smpls = draw_smpls(n_obs, n_folds) |
| 74 | + |
| 75 | + ml_g = clone(learner_g) |
| 76 | + ml_pi = clone(learner_m) |
| 77 | + ml_m = clone(learner_m) |
| 78 | + |
| 79 | + np.random.seed(42) |
| 80 | + if score == 'missing-at-random': |
| 81 | + obj_dml_data = dml.DoubleMLData.from_arrays(x, y, d, z=None, s=s) |
| 82 | + dml_sel_obj = dml.DoubleMLSSM(obj_dml_data, |
| 83 | + ml_g, ml_pi, ml_m, |
| 84 | + n_folds=n_folds, |
| 85 | + score=score, |
| 86 | + normalize_ipw=normalize_ipw, |
| 87 | + draw_sample_splitting=False) |
| 88 | + else: |
| 89 | + assert score == 'nonignorable' |
| 90 | + obj_dml_data = dml.DoubleMLData.from_arrays(x, y, d, z=z, s=s) |
| 91 | + dml_sel_obj = dml.DoubleMLSSM(obj_dml_data, |
| 92 | + ml_g, ml_pi, ml_m, |
| 93 | + n_folds=n_folds, |
| 94 | + score=score, |
| 95 | + normalize_ipw=normalize_ipw, |
| 96 | + draw_sample_splitting=False) |
| 97 | + |
| 98 | + # synchronize the sample splitting |
| 99 | + np.random.seed(42) |
| 100 | + dml_sel_obj.set_sample_splitting(all_smpls=all_smpls) |
| 101 | + |
| 102 | + np.random.seed(42) |
| 103 | + # tune hyperparameters |
| 104 | + tune_res = dml_sel_obj.tune(par_grid, tune_on_folds=tune_on_folds, n_folds_tune=n_folds_tune, |
| 105 | + return_tune_res=False) |
| 106 | + assert isinstance(tune_res, dml.DoubleMLSSM) |
| 107 | + |
| 108 | + dml_sel_obj.fit() |
| 109 | + |
| 110 | + np.random.seed(42) |
| 111 | + smpls = all_smpls[0] |
| 112 | + if tune_on_folds: |
| 113 | + g0_best_params, g1_best_params, pi_best_params, m_best_params = tune_nuisance_ssm( |
| 114 | + y, x, d, z, s, |
| 115 | + clone(learner_g), clone(learner_m), clone(learner_m), |
| 116 | + smpls, score, n_folds_tune, |
| 117 | + par_grid['ml_g'], par_grid['ml_pi'], par_grid['ml_m']) |
| 118 | + |
| 119 | + else: |
| 120 | + xx = [(np.arange(len(y)), np.array([]))] |
| 121 | + g0_best_params, g1_best_params, pi_best_params, m_best_params = tune_nuisance_ssm( |
| 122 | + y, x, d, z, s, |
| 123 | + clone(learner_g), clone(learner_m), clone(learner_m), |
| 124 | + xx, score, n_folds_tune, |
| 125 | + par_grid['ml_g'], par_grid['ml_pi'], par_grid['ml_m']) |
| 126 | + |
| 127 | + g0_best_params = g0_best_params * n_folds |
| 128 | + g1_best_params = g1_best_params * n_folds |
| 129 | + pi_best_params = pi_best_params * n_folds |
| 130 | + m_best_params = m_best_params * n_folds |
| 131 | + |
| 132 | + np.random.seed(42) |
| 133 | + res_manual = fit_selection(y, x, d, z, s, |
| 134 | + clone(learner_g), clone(learner_m), clone(learner_m), |
| 135 | + all_smpls, score, |
| 136 | + normalize_ipw=normalize_ipw, |
| 137 | + g_d0_params=g0_best_params, g_d1_params=g1_best_params, |
| 138 | + pi_params=pi_best_params, m_params=m_best_params) |
| 139 | + |
| 140 | + res_dict = {'coef': dml_sel_obj.coef[0], |
| 141 | + 'coef_manual': res_manual['theta'], |
| 142 | + 'se': dml_sel_obj.se[0], |
| 143 | + 'se_manual': res_manual['se']} |
| 144 | + |
| 145 | + return res_dict |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.ci |
| 149 | +def test_dml_ssm_coef(dml_ssm_fixture): |
| 150 | + assert math.isclose(dml_ssm_fixture['coef'], |
| 151 | + dml_ssm_fixture['coef_manual'], |
| 152 | + rel_tol=1e-9, abs_tol=1e-4) |
| 153 | + |
| 154 | + |
| 155 | +@pytest.mark.ci |
| 156 | +def test_dml_ssm_se(dml_ssm_fixture): |
| 157 | + assert math.isclose(dml_ssm_fixture['se'], |
| 158 | + dml_ssm_fixture['se_manual'], |
| 159 | + rel_tol=1e-9, abs_tol=1e-4) |
0 commit comments