Skip to content

ENH: Add parameter name validation #7030

Closed

Description

@mknz

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.

Activity

  1. bashtage commented on Sep 13, 2020

    @bashtage
    Member

    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.

  2. josef-pkt commented on Sep 13, 2020

    @josef-pkt
    Member

    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.

  3. ChadFulton commented on Sep 14, 2020

    @ChadFulton
    Member

    statespace is definitely guilty of this, with incorrect kwargs silently swallowed even outside of fit.

  4. bashtage commented on Sep 14, 2020

    @bashtage
    Member

    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.

  5. josef-pkt commented on Sep 14, 2020

    @josef-pkt
    Member

    see #2163

    I prefer xxx_kwds which sound nicer to me than kwargs

    in GMM I used optim_args, robust sandwiches use cov_kwds

  6. bashtage commented on Sep 14, 2020

    @bashtage
    Member

    I would counter that kwargs is standard Python and instantly recognizable as Key Word ARGumentS.

  7. mknz commented on Sep 17, 2020

    @mknz
    ContributorAuthor

    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.

    I can tackle it if this sounds OK.

  8. self-assigned this
    on Jul 19, 2021
  9. josef-pkt commented on Sep 23, 2021

    @josef-pkt
    Member

    similar for model.__init__

    back to kwargs versus kwds

    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.

  10. josef-pkt commented on Feb 13, 2023

    @josef-pkt
    Member

    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 (?)

  11. added this to the 0.14 milestone on Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions