Exemplo n.º 1
0
                         Lasso(),
                         Lasso(),
                         Lasso(),
                         dml_procedure='dml1',
                         n_rep=n_rep,
                         n_folds=n_folds)
pliv_dml1.fit()
pliv_dml1.bootstrap(n_rep_boot=n_rep_boot)

irm_dml1 = DoubleMLIRM(dml_data_irm,
                       Lasso(),
                       LogisticRegression(),
                       dml_procedure='dml1',
                       n_rep=n_rep,
                       n_folds=n_folds)
irm_dml1.fit()
irm_dml1.bootstrap(n_rep_boot=n_rep_boot)

iivm_dml1 = DoubleMLIIVM(dml_data_iivm,
                         Lasso(),
                         LogisticRegression(),
                         LogisticRegression(),
                         dml_procedure='dml1',
                         n_rep=n_rep,
                         n_folds=n_folds)
iivm_dml1.fit()
iivm_dml1.bootstrap(n_rep_boot=n_rep_boot)


@pytest.mark.ci
@pytest.mark.parametrize('dml_obj', [plr_dml1, pliv_dml1, irm_dml1, iivm_dml1])
def test_doubleml_exception_learner():
    err_msg_prefix = 'Invalid learner provided for ml_g: '
    warn_msg_prefix = 'Learner provided for ml_g is probably invalid: '

    msg = err_msg_prefix + 'provide an instance of a learner instead of a class.'
    with pytest.raises(TypeError, match=msg):
        _ = DoubleMLPLR(dml_data, Lasso, ml_m)
    msg = err_msg_prefix + r'BaseEstimator\(\) has no method .fit\(\).'
    with pytest.raises(TypeError, match=msg):
        _ = DoubleMLPLR(dml_data, BaseEstimator(), ml_m)
    # msg = err_msg_prefix + r'_DummyNoSetParams\(\) has no method .set_params\(\).'
    with pytest.raises(TypeError):
        _ = DoubleMLPLR(dml_data, _DummyNoSetParams(), ml_m)
    # msg = err_msg_prefix + r'_DummyNoSetParams\(\) has no method .get_params\(\).'
    with pytest.raises(TypeError):
        _ = DoubleMLPLR(dml_data, _DummyNoGetParams(), ml_m)

    # msg = 'Learner provided for ml_m is probably invalid: ' + r'_DummyNoClassifier\(\) is \(probably\) no classifier.'
    with pytest.warns(UserWarning):
        _ = DoubleMLIRM(dml_data_irm, Lasso(), _DummyNoClassifier())

    # ToDo: Currently for ml_g (and others) we only check whether the learner can be identified as regressor. However,
    # we do not check whether it can instead be identified as classifier, which could be used to throw an error.
    msg = warn_msg_prefix + r'LogisticRegression\(\) is \(probably\) no regressor.'
    with pytest.warns(UserWarning, match=msg):
        _ = DoubleMLPLR(dml_data, LogisticRegression(), Lasso())

    # we allow classifiers for ml_m in PLR, but only for binary treatment variables
    msg = (r'The ml_m learner LogisticRegression\(\) was identified as classifier '
           'but at least one treatment variable is not binary with values 0 and 1.')
    with pytest.raises(ValueError, match=msg):
        _ = DoubleMLPLR(dml_data, Lasso(), LogisticRegression())

    # we allow classifiers for ml_g for binary treatment variables in IRM
    msg = (r'The ml_g learner LogisticRegression\(\) was identified as classifier '
           'but the outcome variable is not binary with values 0 and 1.')
    with pytest.raises(ValueError, match=msg):
        _ = DoubleMLIRM(dml_data_irm, LogisticRegression(), LogisticRegression())

    # we allow classifiers for ml_g for binary treatment variables in IRM
    msg = (r'The ml_g learner LogisticRegression\(\) was identified as classifier '
           'but the outcome variable is not binary with values 0 and 1.')
    with pytest.raises(ValueError, match=msg):
        _ = DoubleMLIIVM(dml_data_iivm, LogisticRegression(), LogisticRegression(), LogisticRegression())

    # construct a classifier which is not identifiable as classifier via is_classifier by sklearn
    # it then predicts labels and therefore an exception will be thrown
    log_reg = LogisticRegression()
    log_reg._estimator_type = None
    msg = (r'Learner provided for ml_m is probably invalid: LogisticRegression\(\) is \(probably\) neither a regressor '
           'nor a classifier. Method predict is used for prediction.')
    with pytest.warns(UserWarning, match=msg):
        dml_plr_hidden_classifier = DoubleMLPLR(dml_data_irm, Lasso(), log_reg)
    msg = (r'For the binary treatment variable d, predictions obtained with the ml_m learner LogisticRegression\(\) '
           'are also observed to be binary with values 0 and 1. Make sure that for classifiers probabilities and not '
           'labels are predicted.')
    with pytest.raises(ValueError, match=msg):
        dml_plr_hidden_classifier.fit()

    # construct a classifier which is not identifiable as classifier via is_classifier by sklearn
    # it then predicts labels and therefore an exception will be thrown
    # whether predict() or predict_proba() is being called can also be manipulated via the unrelated max_iter variable
    log_reg = LogisticRegressionManipulatedPredict()
    log_reg._estimator_type = None
    msg = (r'Learner provided for ml_g is probably invalid: LogisticRegressionManipulatedPredict\(\) is \(probably\) '
           'neither a regressor nor a classifier. Method predict is used for prediction.')
    with pytest.warns(UserWarning, match=msg):
        dml_irm_hidden_classifier = DoubleMLIRM(dml_data_irm_binary_outcome,
                                                log_reg, LogisticRegression())
    msg = (r'For the binary outcome variable y, predictions obtained with the ml_g learner '
           r'LogisticRegressionManipulatedPredict\(\) are also observed to be binary with values 0 and 1. Make sure '
           'that for classifiers probabilities and not labels are predicted.')
    with pytest.raises(ValueError, match=msg):
        dml_irm_hidden_classifier.fit()
    with pytest.raises(ValueError, match=msg):
        dml_irm_hidden_classifier.set_ml_nuisance_params('ml_g0', 'd', {'max_iter': 314})
        dml_irm_hidden_classifier.fit()

    msg = (r'Learner provided for ml_g is probably invalid: LogisticRegressionManipulatedPredict\(\) is \(probably\) '
           'neither a regressor nor a classifier. Method predict is used for prediction.')
    with pytest.warns(UserWarning, match=msg):
        dml_iivm_hidden_classifier = DoubleMLIIVM(dml_data_iivm_binary_outcome,
                                                  log_reg, LogisticRegression(), LogisticRegression())
    msg = (r'For the binary outcome variable y, predictions obtained with the ml_g learner '
           r'LogisticRegressionManipulatedPredict\(\) are also observed to be binary with values 0 and 1. Make sure '
           'that for classifiers probabilities and not labels are predicted.')
    with pytest.raises(ValueError, match=msg):
        dml_iivm_hidden_classifier.fit()
    with pytest.raises(ValueError, match=msg):
        dml_iivm_hidden_classifier.set_ml_nuisance_params('ml_g0', 'd', {'max_iter': 314})
        dml_iivm_hidden_classifier.fit()
from doubleml.datasets import make_plr_CCDDHNR2018, make_irm_data, make_pliv_CHS2015, make_iivm_data

from sklearn.linear_model import Lasso, LogisticRegression

np.random.seed(3141)
dml_data_plr = make_plr_CCDDHNR2018(n_obs=100)
dml_data_pliv = make_pliv_CHS2015(n_obs=100, dim_z=1)
dml_data_irm = make_irm_data(n_obs=100)
dml_data_iivm = make_iivm_data(n_obs=100)

dml_plr = DoubleMLPLR(dml_data_plr, Lasso(), Lasso())
dml_plr.fit()
dml_pliv = DoubleMLPLIV(dml_data_pliv, Lasso(), Lasso(), Lasso())
dml_pliv.fit()
dml_irm = DoubleMLIRM(dml_data_irm, Lasso(), LogisticRegression())
dml_irm.fit()
dml_iivm = DoubleMLIIVM(dml_data_iivm, Lasso(), LogisticRegression(),
                        LogisticRegression())
dml_iivm.fit()

# fit models with callable scores
plr_score = dml_plr._score_elements
dml_plr_callable_score = DoubleMLPLR(dml_data_plr,
                                     Lasso(),
                                     Lasso(),
                                     score=plr_score,
                                     draw_sample_splitting=False)
dml_plr_callable_score.set_sample_splitting(dml_plr.smpls)
dml_plr_callable_score.fit(store_predictions=True)

irm_score = dml_irm._score_elements