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
{{ message }}
Repository navigation
ENH/REF: params_aux keys for auxiliary parameters #4664
A design idea to make auxiliary parameters behave similar to params in results instance when calling model methods.
This is partially a follow-up to issues with scale in GLM loglike, score and hessian.
But I ran into a similar problem with pen_weight in penalized models, where pen_weight is currently a model attribute, but we want to estimate it and treat it like a parameter, i.e. use the same model with several results instances that have different pen_weights.
The idea is that the results instance calls the model method, specifically loglike, score and hessian, always with the auxiliary parameters as additional keywords
For a more generic solution we would need to add a params_aux_keys to know which auxiliary parameters are required.
Limitation:
This does not work for auxiliary parameters that are defined in instances of some classes attached to the model, for example parameterized
families like NegativeBinomial in GLM,
norms in RLM or
penalty classes in PenalizedMixin.
It most likely also will not work for special fit methods, e.g. alpha in fit_regularized.
Or it needs to be extended to models where auxiliary params work through other paths than loglike, score and hessian.
related: #4585 hyper parameters, this would cover some of those, those that enter loglike #4616 problem with concentrated out scale in GLM #3359 scale should be results attribute #1776 fixing nuisance parameter during estimation
bump,
I just ran into this with alpha/pen_weight in penalized estimation. #5296 (comment)
It affects the parameter estimate in fit, but it's not an estimated parameter.
I should be a __init__ parameter conceptually, but we want to choose it optimally without recreating the model for each different values.
The current pattern is that we attach it as model attribute, so it doesn't have to be provided by the optimizers when calling loglike, score and hessian.
However, if it is also a fit argument, then the model attribute can become state, out-of-sync and incorrect for a given results instance. This is a bit similar to using scale in GLM which is now a loglike keyword.
I just had the same idea again as described in this issue, i.e. use the model attributes during fit, but the results class has to provide the extra keywords when calling the relevant model methods, mainly loglike and related methods.
i.e. models need: if xxx is None: xxx = self.xxx
#4585 already mentions penalization hyper parameters in the context of TheilGLS, among other types of hyper parameters
an alternative in some cases would be to manipulate the model attribute internally, e.g. during hyper parameter search, but not to let the user change it, i.e. we don't add it as a fit keyword.
I'm doing this a bit currently in experimental or example code for penalization weight.
However, this is not a clean solution and still requires that we are careful and don't change the model attribute when the user has a results instance to hold on to.
(aside: TheilGLS doesn't have a penalized loglike)
detail: auxiliary loglike keywords are not checked for type or errors, i.e. we assume that the caller is the results instance or a user that knows the requirements on the keyword values.
a variation on this:
Similar to predict it would be possible to add results methods that just delegate to the model methods with the appropriate args, params, and keywords.
However, predict is a commonly used user facing method, while calling loglike for llf, aic or score_obs for some score based analysis is mostly iternal. So, extra results methods are not worth the effort.
some more difficult cases:
Similar to scale in GLM, we have to decide whether other auxiliary parameters are used in other results attributes or methods, e.g. resid_xxx.
score_factor and hessian_factor will not have the penalization.
clarifying names:
I think we should privatize, add a leading underscore to results attributes that are also model attributes needed during fitting, e.g. rename GLM.scale -> GLM._scale.
In penalized estimation as in GAM #5296 df_resid also needs to be results specific, and I needed to manipulate the model and results attributes to change df_resid to take account of the effective degrees of freedom. In GLM.estimate_scale we need to be able to use the results.df_resid in the scale computation.
We need a better way to override and set nuisance parameter attributes with appropriate computation in subclasses. The main issue is finding the place in the computational sequence when the need to be changed so that follow-up computation are correct.
another idea:
raise if results instance does not provide auxiliary parameters to loglike, or score in sandwich cov types, or wherever else those are relevant.
possibly add a _in_fit boolean attribute to models. If True, then we use internal model attribute. If False, then we raise unless extra, auxiliary parameter is provided (is not None). This would also prevent users to call an underlying model method without specifying all relevant parameters.
I think this issue is too broad for 0.10.
Changes that affect more core models should be made early in a release cycle because there is a wide range where this might cause problems or needs fine tuning.
(In GAM I partially avoid the problem because the pen_weight alpha is not a fit keyword, but it is a _fit_pirls keyword that is used internally in the bandwidth/pen_weight search.
Activity
josef-pkt commentedon Oct 24, 2018
bump,
I just ran into this with alpha/pen_weight in penalized estimation.
#5296 (comment)
It affects the parameter estimate in fit, but it's not an estimated parameter.
I should be a
__init__parameter conceptually, but we want to choose it optimally without recreating the model for each different values.The current pattern is that we attach it as model attribute, so it doesn't have to be provided by the optimizers when calling loglike, score and hessian.
However, if it is also a fit argument, then the model attribute can become state, out-of-sync and incorrect for a given results instance. This is a bit similar to using scale in GLM which is now a loglike keyword.
I just had the same idea again as described in this issue, i.e. use the model attributes during fit, but the results class has to provide the extra keywords when calling the relevant model methods, mainly loglike and related methods.
i.e. models need:
if xxx is None: xxx = self.xxx#4585 already mentions penalization hyper parameters in the context of TheilGLS, among other types of hyper parameters
josef-pkt commentedon Oct 24, 2018
an alternative in some cases would be to manipulate the model attribute internally, e.g. during hyper parameter search, but not to let the user change it, i.e. we don't add it as a fit keyword.
I'm doing this a bit currently in experimental or example code for penalization weight.
However, this is not a clean solution and still requires that we are careful and don't change the model attribute when the user has a results instance to hold on to.
(aside: TheilGLS doesn't have a penalized loglike)
detail: auxiliary loglike keywords are not checked for type or errors, i.e. we assume that the caller is the results instance or a user that knows the requirements on the keyword values.
josef-pkt commentedon Oct 24, 2018
a variation on this:
Similar to predict it would be possible to add results methods that just delegate to the model methods with the appropriate args,
params, and keywords.However, predict is a commonly used user facing method, while calling loglike for llf, aic or score_obs for some score based analysis is mostly iternal. So, extra results methods are not worth the effort.
some more difficult cases:
Similar to scale in GLM, we have to decide whether other auxiliary parameters are used in other results attributes or methods, e.g. resid_xxx.
score_factor and hessian_factor will not have the penalization.
josef-pkt commentedon Nov 4, 2018
clarifying names:
I think we should privatize, add a leading underscore to results attributes that are also model attributes needed during fitting, e.g. rename
GLM.scale->GLM._scale.In penalized estimation as in GAM #5296 df_resid also needs to be results specific, and I needed to manipulate the model and results attributes to change df_resid to take account of the effective degrees of freedom. In GLM.estimate_scale we need to be able to use the
results.df_residin the scale computation.We need a better way to override and set nuisance parameter attributes with appropriate computation in subclasses. The main issue is finding the place in the computational sequence when the need to be changed so that follow-up computation are correct.
josef-pkt commentedon Nov 28, 2018
another idea:
raise if results instance does not provide auxiliary parameters to loglike, or score in sandwich cov types, or wherever else those are relevant.
possibly add a
_in_fitboolean attribute to models. If True, then we use internal model attribute. If False, then we raise unless extra, auxiliary parameter is provided (is not None). This would also prevent users to call an underlying model method without specifying all relevant parameters.(e.g. another refactor of GLM scale handling?)
josef-pkt commentedon Jan 1, 2019
I think this issue is too broad for 0.10.
Changes that affect more core models should be made early in a release cycle because there is a wide range where this might cause problems or needs fine tuning.
(In GAM I partially avoid the problem because the pen_weight alpha is not a fit keyword, but it is a
_fit_pirlskeyword that is used internally in the bandwidth/pen_weight search.