Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error if fitted propensity is passed through nuisance models #10

Merged
merged 12 commits into from
Jun 18, 2024
Merged
6 changes: 3 additions & 3 deletions metalearners/metalearner.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,10 @@ def __init__(
if fitted_nuisance_models is not None:
if not set(fitted_nuisance_models.keys()) <= set(
nuisance_model_specifications.keys()
):
) - {PROPENSITY_MODEL}:
raise ValueError(
"The keys present in fitted_nuisance_models should be a subset of"
f"{set(nuisance_model_specifications.keys())}"
"The keys present in fitted_nuisance_models should be a subset of "
f"{set(nuisance_model_specifications.keys()) - {PROPENSITY_MODEL}}"
)
self._nuisance_models |= deepcopy(fitted_nuisance_models)
not_fitted_nuisance_models -= set(fitted_nuisance_models.keys())
Expand Down
11 changes: 11 additions & 0 deletions tests/test_metalearner.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ def test_feature_set(feature_set, expected_n_features, use_pandas, rng):


def test_model_reusage_init():
# TODO: Split up into several tests.
prefitted_models = [CrossFitEstimator(10, LGBMRegressor)]
ml = _TestMetaLearner(
nuisance_model_factory=LinearRegression,
Expand Down Expand Up @@ -497,6 +498,16 @@ def test_model_reusage_init():
fitted_nuisance_models={"nuisance3": prefitted_models},
)

with pytest.raises(ValueError, match="The keys present"):
RLearner(
propensity_model_factory=LGBMClassifier,
nuisance_model_factory=LGBMRegressor,
is_classification=False,
n_variants=2,
treatment_model_factory=LGBMRegressor,
fitted_nuisance_models={PROPENSITY_MODEL: prefitted_models},
)


@pytest.mark.parametrize(
"fit_params, nuisance_model_names, treatment_model_names, expected",
Expand Down