Skip to content

Preserve model specifications when refitting metalearners - #1084

Merged
kbattocchi merged 3 commits into
py-why:mainfrom
AryanRG:fix/metalearner-treatment-refit
Nov 20, 2026
Merged

kbattocchi merged 3 commits into
py-why:mainfrom
AryanRG:fix/metalearner-treatment-refit

Conversation

@AryanRG

@AryanRG AryanRG commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

TLearner, XLearner and DomainAdaptationLearner replace their models (and cate_models / final_models) arguments during fit with the per-arm list they train. A second fit validates that list instead of the original argument. Refitting the same estimator on data with a different number of treatment categories therefore fails:

import numpy as np
from sklearn.linear_model import LinearRegression
from econml.metalearners import TLearner

rng = np.random.RandomState(0)
X = rng.normal(size=(600, 3))
T3, T2 = rng.randint(0, 3, 600), rng.randint(0, 2, 600)
est = TLearner(models=LinearRegression())
est.fit(X[:, 0] + T3, T3, X=X)
est.fit(X[:, 0] + T2, T2, X=X)
# ValueError: The number of estimators doesn't equal to the number of treatments. ...

The metalearners now keep the constructor model specifications and clone them in fit. Fitted outcome and treatment effect models live in dedicated attributes. Each refit starts with fresh clones of the current specifications. Prediction uses the fitted attributes directly. Older pickles of fitted T-, S-, and Domain Adaptation learners may need to be refit before predicting with this version. This also applies to SHAP for Domain Adaptation.

The guarded metalearner tests and pandas integration case passed on the PR branch and on its integration with current main (6 tests and 4 subtests each). I also checked fitted-model prediction and SHAP, and verified that editing a default propensity model leaves other learners’ defaults intact. Ruff passed on the changed file.

Prepared with AI assistance.

@AryanRG
AryanRG force-pushed the fix/metalearner-treatment-refit branch from a45a09a to 26c8c28 Compare November 13, 2026 08:58
@AryanRG

AryanRG commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Merged main to pick up #1082’s Python 3.9 runner fix. The original product code and tests are unchanged. The 25 workflow tests and six metalearner tests pass locally, including four subtests.

@kbattocchi kbattocchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution - this is a good catch and definitely worth fixing.

However, as to the strategy of the fix, I think it would be better to use the sklearn approach of never modifying initializer arguments in fit - instead we should write the updated models into a separate _models or _cate_models field and leave models alone (likewise, we should actually be doing the cloning of the models in fit as part of this logic, not in the initializer for each class).

@AryanRG

AryanRG commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. I’ve moved cloning into fit and kept the fitted models separate from the constructor specs. The metalearner tests passed on this branch and with current main.

@kbattocchi kbattocchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for revising, this looks much better, just a few smaller comments this time around.

Comment thread econml/metalearners/_metalearners.py Outdated
else:
force_all_finite = False
X, = check_input_arrays(X, force_all_finite=force_all_finite, ensure_2d=True)
models = getattr(self, 'models_', self.models) # fitted estimators from older versions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to fall back to self.models as a default? It seems like we should never do this, since that implies that the estimator hasn't been fit, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That fallback was for older pickles, where models could contain the already fitted models. I’ve removed it. T-learner now reads models_ directly, and I’ve updated the PR description to call out the compatibility change.

Comment thread econml/metalearners/_metalearners.py Outdated
Xs, Ts = broadcast_unit_treatments(X, self._d_t[0] + 1)
feat_arr = np.concatenate((Xs, Ts), axis=1)
prediction = self.overall_model.predict(feat_arr).reshape((-1, self._d_t[0] + 1,) + self._d_y)
model = getattr(self, 'overall_model_', self.overall_model) # fitted estimators from older versions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern about fallback here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same change here. S-learner prediction now reads overall_model_ directly.

models,
cate_models=None,
propensity_model=LogisticRegression(),
propensity_model=_DEFAULT_PROPENSITY_MODEL,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is now unnecessary since we fit only on clones, so this can be reverted to LogisticRegression and the sentinel logic can be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restored the LogisticRegression() default. I kept a small identity check so each learner gets its own instance. Otherwise set_params on one learner’s default would affect the others. Fitting still clones the model per arm.

models,
final_models,
propensity_model=LogisticRegression(),
propensity_model=_DEFAULT_PROPENSITY_MODEL,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about reverting the sentinel applies here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied the same change here. Each learner gets its own default, and an explicitly supplied model is kept as the constructor parameter and cloned during fit.

Comment thread econml/metalearners/_metalearners.py Outdated
X, = check_input_arrays(X, force_all_finite=force_all_finite, ensure_2d=True)
taus = []
for model in self.final_models:
for model in getattr(self, 'final_models_', self.final_models):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern about fallback here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Domain Adaptation now uses final_models_ directly for both prediction and SHAP.

@kbattocchi kbattocchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

@kbattocchi
kbattocchi force-pushed the fix/metalearner-treatment-refit branch from 1a428da to eae1885 Compare November 20, 2026 18:15
@kbattocchi
kbattocchi enabled auto-merge (rebase) November 20, 2026 18:16
@kbattocchi
kbattocchi merged commit 0a42032 into py-why:main Nov 20, 2026
95 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants