You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone, thanks for developing/maintaining this valuable tool.
Recently, I noticed that there are no parameter name checks in model initialization, fit, and predict methods. This behavior makes debugging sometimes difficult.
Example:
import statsmodels.api as sm
# Correct parameter name
sm.Poisson(np.random.random((10, 1)), np.random.random((10, 2))).fit(maxiter=10)
# Incorrect parameter name, but still it runs without an error
sm.Poisson(np.random.random((10, 1)), np.random.random((10, 2))).fit(max_iter=10)
Do we have any plan to add parameter name checks?
Ideally, with invalid parameter names, it should raise TypeError.
I fully support this. I find overreliance on **kwargs to be infuriating.
This question is how to do this in a consistent way. Perhaps add model private attributes, _fit_kwargs = {None, Tuple[str]} where None means no check (so this could go on the base model), tuple() would mean no kwargs supported, and ("maxiter",) would mean only maxiter would be accepted.
The problem in fit is that remaining kwargs go into the optimizer and those keywords are not standardized
I just ran the above example. It looks like both spellings work correctly with default optimizers.
Poisson(np.random.random((10, 1)), np.random.random((10, 2))).fit(max_iter=4, method="bfgs")
this doesn't limit the number of iterations, and converges in more steps.
What I didn't understand is that kwargs don't raise in the scipy optimizers if we hand all remaining kwargs to them.
I didn't remember this correctly.
AFAICS, the base.optimizer functions get the kwargs, but use only explicit keywords in the call to the scipy optimizers.
All other kwargs are ignored, if there are any extras not valid for the optimizer.
The list of specific optimizer kwds is only available in the _fit_xxx functions in base.optimize.
Kerby used an allowed list in MixedLM (IIRC) for model.__init__ where we have a similar kwargs problem.
This sin of the past was that optim_kwarg: dict[str,Any] was not used to pass optimizer options. There could be a way forward to get this (basically add optim_kwarg systematically, then warn users to stop passing generic kwargs when present and instead package these into optim_kwarg.
I checked base/optimizer.py and realized that kwargs does not directly go into scipy optimizers. So, even if misspelled kwargs existed, it would be silently ignored.
I think a relatively easy fix is to validate kwarg within each of _fit_xxx functions.
In #7750 for issue #7741 want to start using kwargs for **kwargs and kwds for the ones with specific keys in the signature.
We have _init_keys and _get_init_kwds as general list of "named" keywords often used for a model, that we need to recreate a model.
I want to use _init_kwargs_allowed as attribute for **kwargs that are handled generically and processed by base or super classes.
For GLM I need kwargs_allowed = ["missing", 'missing_idx', 'formula', 'design_info', 'n_trials', "hasconst"]
while named in _init_keys are ['offset', 'exposure', 'freq_weights', 'var_weights', 'family']
Note, many classes send named keywords as **kwargs to a super class.
The distinction is only relevant and can only be verified in final subclasses.
Aside:
'n_trials' is in the kwargs because I had needed it to recreate a Binomial model, which has an internal endog attribute that is not in the original endog parameterization.
All others go to base models.
Activity
bashtage commentedon Sep 13, 2020
I fully support this. I find overreliance on **kwargs to be infuriating.
This question is how to do this in a consistent way. Perhaps add model private attributes,
_fit_kwargs = {None, Tuple[str]}whereNonemeans no check (so this could go on the base model),tuple()would mean no kwargs supported, and("maxiter",)would mean onlymaxiterwould be accepted.josef-pkt commentedon Sep 13, 2020
The problem in fit is that remaining kwargs go into the optimizer and those keywords are not standardized
I just ran the above example. It looks like both spellings work correctly with default optimizers.
Poisson(np.random.random((10, 1)), np.random.random((10, 2))).fit(max_iter=4, method="bfgs")this doesn't limit the number of iterations, and converges in more steps.
What I didn't understand is that kwargs don't raise in the scipy optimizers if we hand all remaining kwargs to them.
I didn't remember this correctly.
AFAICS, the base.optimizer functions get the kwargs, but use only explicit keywords in the call to the scipy optimizers.
All other kwargs are ignored, if there are any extras not valid for the optimizer.
The list of specific optimizer kwds is only available in the
_fit_xxxfunctions in base.optimize.Kerby used an allowed list in MixedLM (IIRC) for
model.__init__where we have a similar kwargs problem.ChadFulton commentedon Sep 14, 2020
statespaceis definitely guilty of this, with incorrect kwargs silently swallowed even outside offit.bashtage commentedon Sep 14, 2020
This sin of the past was that
optim_kwarg: dict[str,Any]was not used to pass optimizer options. There could be a way forward to get this (basically addoptim_kwargsystematically, then warn users to stop passing generic kwargs when present and instead package these intooptim_kwarg.josef-pkt commentedon Sep 14, 2020
see #2163
I prefer
xxx_kwdswhich sound nicer to me thankwargsin GMM I used
optim_args, robust sandwiches usecov_kwdsbashtage commentedon Sep 14, 2020
I would counter that
kwargsis standard Python and instantly recognizable as Key Word ARGumentS.mknz commentedon Sep 17, 2020
I checked
base/optimizer.pyand realized thatkwargsdoes not directly go into scipy optimizers. So, even if misspelledkwargsexisted, it would be silently ignored.I think a relatively easy fix is to validate
kwargwithin each of_fit_xxxfunctions.I can tackle it if this sounds OK.
ENH: Raise when invalid optimization options passed to optimizer
josef-pkt commentedon Sep 23, 2021
similar for
model.__init__back to kwargs versus kwds
In #7750 for issue #7741 want to start using
kwargsfor**kwargsand kwds for the ones with specific keys in the signature.We have
_init_keysand_get_init_kwdsas general list of "named" keywords often used for a model, that we need to recreate a model.I want to use
_init_kwargs_allowedas attribute for**kwargsthat are handled generically and processed by base or super classes.For GLM I need
kwargs_allowed = ["missing", 'missing_idx', 'formula', 'design_info', 'n_trials', "hasconst"]while named in
_init_keysare['offset', 'exposure', 'freq_weights', 'var_weights', 'family']Note, many classes send named keywords as
**kwargsto a super class.The distinction is only relevant and can only be verified in final subclasses.
Aside:
'n_trials' is in the
kwargsbecause I had needed it to recreate a Binomial model, which has an internal endog attribute that is not in the original endog parameterization.All others go to
basemodels.josef-pkt commentedon Feb 13, 2023
closing this as fixed
fit optimizer kwargs are fixed in base.
There might still be methods that let invalid kwargs through, but currently no known cases (?)