Skip to content

SUMM/Design/ENH: API and structure for hyper parameter selection #4585

Open

Description

@josef-pkt

What should be our long term pattern for hyper parameter selection?

examples: lag selection in tsa models, penalty in penalized models.

tradeoff: clean, more "fool-proof" design versus computational efficiency

Our old pattern was to add those hyper parameters to fit, e.g. some tsa models still have lag selection "ic" in fit. This can potentially take advantage of computationally efficient search without creating new models each time.
Our newer pattern is to put hyper parameters in the model.__init__ to avoid incorrect state, attributes in models. This requires that a new model is created for each set of values for the hyper parameters. This adds additional overhead and cannot take advantage of unchanging intermediate results.

lag selection:
There are some open issues for lag selection in tsa which still needs refactoring, e.g. #4536 for VAR

example penalization:
In penalized or regularized regression, the underlying data, endog and exog, does not change.

In #4576 I avoided the issue of a proper API so far, and make penalization weights as a model attribute, but I change the attribute before running fit. It is essentially a __init__ keyword that can be manipulated as a fit keyword. but will cause incorrect stale state if several results instances refer to the same model.

TheilGLS, and current fit_regularized model methods specify penalization parameters in a fit method. However, in those cases there should not be any penalization attributes attached to the model instance.
However, this requires that there are separate methods that are used by fit_regularized, so un-regularized methods do not need to check model attributes about the penalization.
This does not work if, for example, loglike is used for penalized and unpenalized estimation.
In #4576, I added a keyword for the penalization to loglike and similar, but it is not used because the currently standard results class (not specialized for penalized fit) does not know about those extra keywords.
So if we want to avoid model attributes that change during hyper parameter search, we would have to attach them to the model, and use them as arguments in any model calls.
AFAIR, we do this now (0.9) for scale in GLM to avoid needing scale as model attribute.

fit_constrained:
There might be a similar problem in fit_constrained, when constraints are considered as hyper parameters. I don't remember the details right now, or never looked at it specifically in terms of location for attaching attributes.
The current fit_constrained is somewhat different because it works by creating a new auxiliary model.
An alternative that we don't widely use yet is to add constrained optimization. Example is imposing stationarity in ARMA variants, or in miscmodels TModel that uses the generic reparameterization for imposing constraints.
However, constraints on parameters during optimization are simpler issue, because once we have the params, then we don't need to consider the constraints anymore, i.e. transform_params is internal to fit and an attribute that changes only temporarily.
For imposed binding constraints, the constraints need to be taken into account in the model and results.

Activity

  1. added this to the 0.10 milestone on May 2, 2018
  2. josef-pkt commented on May 4, 2018

    @josef-pkt
    MemberAuthor

    related cross validation cost or loss criteria #2572
    e.g. in Poisson with leave-k-out cross validation we should use loglike or something that is consistent with the estimation objective function.
    e.g. family loglike as function of mu, or more general loglike as function of resid or other parameter that is independent of the hyper parameter like number of variables or size of penalization and can easily be evaluated out of sample.
    (There is a related issue for count models, which for some cases manipulate internal model.endog and model.exog. I don't find it right now.)

  3. jbrockmendel commented on May 4, 2018

    @jbrockmendel
    Contributor

    Our newer pattern is to put hyper parameters in the model.__init__ to avoid incorrect state, attributes in models. This requires that a new model is created for each set of values for the hyper parameters. This adds additional overhead and cannot take advantage of unchanging intermediate results.

    Unreliable/unintentional state is an easy way to screw up, so the newer pattern makes a lot of sense.

    It sounds like you're thinking of a pre-Model class that instantiates/spawns/holds(?) multiple models (and maybe results?). Am I reading you correctly here?

  4. josef-pkt commented on May 4, 2018

    @josef-pkt
    MemberAuthor

    From the outside it would be extra functions that select hyper parameters, e.g. like a function that does a cross validation loop.
    However, computationally it would be easier and faster to change the hyper parameter on an existing model, which however runs into the unreliable state problem if also the user has access to the attribute, or if we keep the same model for different runs of the cross-validation loop.

    Within a loop there is no real problem with stale/incorrect state because the results are only temporarily used.
    The problem is with the final results instance where the user is allowed to compute many things lazily and those will be incorrect if the underlying state has changed.

  5. josef-pkt commented on Jan 25, 2019

    @josef-pkt
    MemberAuthor

    example GAM PR #5370

    I added two methods, select_penweight and select_penweight_kfold to the model class

    select_penweight

    • used gcv type estimation, i.e. uses a one-step approximation to leave-one-out results
    • uses scipy optimizer, default basinhopping, to find optimal penalization weight
    • uses internal function _fit_pirls to avoid recreating a model, but manipulates model attribute

    select_penweight_kfold

    • convenience method to k-fold cross validation implemented in separate modules
    • needs to recreate the model for each new dataset
    • currently no option to provide RandomState
    • using cross-validation class and functions directly requires work by user, i.e. define all components.

    side question
    How do we unit/integration test that a method like select_penweight_kfold works correctly given that it hides everything and returns only limited results information?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions