Skip to content

ENH/WIP Generic elastic net code - #2385

Merged
josef-pkt merged 27 commits into
statsmodels:masterfrom
kshedden:elastic_net2
May 7, 2016
Merged

josef-pkt merged 27 commits into
statsmodels:masterfrom
kshedden:elastic_net2

Conversation

@kshedden

@kshedden kshedden commented May 5, 2015

Copy link
Copy Markdown
Contributor

This is a generic elastic net implementation that will work with any model that implements log-likelihood, score, and Hessian. It replaces the previous elastic net code that was duplicated between OLS regression and proportional hazards regression. The new code is also used to add regularization to GLM.

Notes:

  • To make the generic code work, some minor changes to GLMResults were needed: there is no generic way to attach mu to results, so instead we use fittedvalues (same thing) instead of mu in the results.
  • Changes to GLM: moved some code from fit into __init__ so that loglike, etc. can be called without calling fit first.
  • Changes to test_glm : the "hacked" version of Poisson with offsets was incompatible with the refactored GLMResults, so instead I created a proper set of R results for the Poisson offset fit to cpunish.
  • Added a fake scale=1 parameter to PHRegResults -- the results need to follow a generic signature to work with elastic net.
  • I changed the signatures of fit_regularized, moving some named arguments into kwargs. I'm not sure if it breaks compatibility. I think we should do this so that we can implement various types of regularized fitting according to the 'method' argument, and each method will have its own set of keywords as appropriate.
  • I fixed an apparent bug in loglike for GLM (arguments in wrong order). Loglike is not used much since we usually do IRLS. Surprisingly it passed the tests with the arguments in the wrong order.
  • The OLS fit_regularized can be run with the loss function being either the residual sum of squares, or the profile log likelihood. Only the former is comparable to R.
  • Test agreement for OLS is very good. For Cox and GLM it is OK. Generally our results give better results on the fit function so I think R glmnet is making some shortcuts.
  • This could be refactored to work with mixins as in ENH: penalized MLE #2374.

@josef-pkt

Copy link
Copy Markdown
Member

Good to see this, I will start to look at it in a few days.

aside about the mixins: The main reason for it right now was that I'm using the standard scipy optimizer via the main fit method and we don't have an option yet to specify which objective function is used (It's always the loglike method.) This is not necessary with a specific, non-scipy fitting method.

A second reason to combine this with the Mixin version is if we want to share and unify the Results for penalized likelihood. I haven't gotten that far yet, but my target was an M-estimator sandwich cov_params for the non-zero params as default, which will also need the penalized hessian. Maybe that could also be just a common penalized Results class.

Another aside: I just saw that SAS switches optimizer in an unrelated model/procedure between bfgs, quasi-newton and steepest descent (IIRC) depending on the number of parameters that need to be estimated. We might also recommend different versions of penalized estimation depending on the scale of the data once we get the different versions implemented.

@josef-pkt

Copy link
Copy Markdown
Member

As FYI:
My other target for generic penalization with scipy optimizers, is to use non-orthogonal L2 penalization with the target for penalization splines and GAM, which in the simplest case will penalize the difference between consecutive parameters.
(I haven't quite figured out yet how to use feature selection regularization for knot selection. as alternative to penalization splines.)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 83.92% when pulling f7fc512 on kshedden:elastic_net2 into a1f53b0 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 83.92% when pulling e7ee707 on kshedden:elastic_net2 into a1f53b0 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 83.92% when pulling f2e3e7c on kshedden:elastic_net2 into a1f53b0 on statsmodels:master.

Comment thread statsmodels/base/elastic_net.py Outdated

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.

spelling method is loglike no dash

@josef-pkt

Copy link
Copy Markdown
Member

I did a quick skimming and spot reading. I didn't read enough yet to understand all the bullet items in the PR description.

I think it looks very good overall. Changes to existing code will need another backwards compatibility review, but I didn't see any problems.

_offset in linear model is only for internal use. If we want to make it an option for the user, then we would need more invasive changes.

We still need to do more work in unifying internal APIs across models. My guess is that we still run into problems recreating results instances.
(One option I was thinking about was to use model.fit(start_params=my_params. maxiter=0 , ...) to just create a new results instance. but that's mostly in the idea stage.)

As a quick check whether the most important results are available in a results instance, I usually use a smoke test for summary, and maybe t_test(np.eye(len(params)))

About the returned results (i didn't go through all the details yet):
Since you are refitting the model with the reduced number of exog, the cov (hessian) will be evaluated at the unpenalized parameters, not at the reported, penalized parameters.
Maybe this could be fixed with model.fit(start_params=my_params. maxiter=0 , ...). ?
As I mentioned before, in my current view, I think we should return the sandwich (Fan and Li) that corresponds to the equivalent M-estimator. this might require the penalized hessian, and score_obs to call the sandwich_covariance code, or calculate directly if we only want the simple sandwich.

I will go over some details again and run some examples in a few days, after getting some other things out of the way.

@josef-pkt

Copy link
Copy Markdown
Member

For performance: In the long term, I would like to refactor and add code so we can avoid creating new model and results instances repeatedly in loops, if that turns out to be costly. see #2287

It won't be immediately relevant for this. It's better to rely on generic code in the first round, but eventually we could add some private methods to the models for computational shortcuts when we don't need all the extra features that we have added to the models and results.

@josef-pkt

Copy link
Copy Markdown
Member

Another still open issue for the results instance that I just remembered

PR for current discrete fit_regularized added nan handling for the cov_params (used for t_test IIRC).
My recent trend was to consider dropped variables as equivalent to imposing a zero constraint for the implementation of inferential results and cov_params usages.

For my PenalizedMixin version I was experimenting with returning fit_constrained with zero constraints and penalized likelihood, as "finishing" fit.

josef-pkt added a commit to josef-pkt/statsmodels that referenced this pull request May 7, 2015
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 83.92% when pulling 40e3b04 on kshedden:elastic_net2 into a1f53b0 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

@kshedden I started to work on a first version for creating results instances from a auxiliary regression in #2374

I think we can largely skip creating the correct results instance in this PR, and try to develop it in a setting that is easier to unit test across models. I ran into several problems in #2374 that require more refactoring or a different approach.
(current discrete fit_regularized doesn't refit a reduced model and adds model specific results creation and avoided thereby to have to find a generic solution.)

We should merge the consistency improvements in the models, GLM, linear models, ..., soon(ish), so we have a common basis to write the various generic extensions

(I will soon go back to maintenance to get 0.7 out of master, so we can merge several of the waiting PRs. It's starting to get annoying to have code spread over related but distinct branches and PRs.)

from statsmodels.tools.numdiff import approx_fprime_cs
# TODO: diag workaround proplem with numdiff for 1d
return np.diag(approx_fprime_cs(mu, self))
return np.zeros_like(mu)

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.

There is a problem now with inheriting from VarianceFunction
We need to decide whether it is a base class that can be used for inheritance or a default constant variance class.

I think the only example for user defined variance function is in my risk ratio experiments with Poisson that has almost Binomial variance.

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 we will be better off introducing a separate Constant variance class.

But, AFAIK there is currently no usecase anymore for inheritance from VarianceFunction inside statsmodels.

@josef-pkt

Copy link
Copy Markdown
Member

The scale handling in the regression linear model looks fine, I don't expect any problems with this way, and it makes it more usable for other cases.

I'll try to read roughly through the elastic net code itself in the next few days.
The only issue that I saw so far is that I want to get away from alpha for penalization weights pen_weights or penal_weights.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.02%) to 85.402% when pulling f40c7f2 on kshedden:elastic_net2 into 0fac349 on statsmodels:master.

from numpy.testing import assert_equal
llf_r = plf(params)
llf_sm = plf(sm_result.params)
assert_equal(np.sign(llf_sm - llf_r), 1)

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.

numpy.testing also has a assert less

@josef-pkt

Copy link
Copy Markdown
Member

I went briefly through the latest changes and spot checked a few of the overall changes (for GLM)

looks fine to me. I guess we will need some followups until this is settled but I think we should merge it soon.

ready to merge?

@kshedden

kshedden commented May 7, 2016

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Let me look it through it one more time then we can
go ahead. Leland will need this as the basis for his GSOC project.

On Fri, May 6, 2016 at 8:49 PM, Josef Perktold [email protected]
wrote:

I went briefly through the latest changes and spot checked a few of the
overall changes (for GLM)

looks fine to me. I guess we will need some followups until this is
settled but I think we should merge it soon.

ready to merge?

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#2385 (comment)

@kshedden

kshedden commented May 7, 2016

Copy link
Copy Markdown
Contributor Author

@josef-pkt OK, went through this again, I think we should merge it.

@josef-pkt

josef-pkt commented May 7, 2016 •

Copy link
Copy Markdown
Member

Good, merging

Thanks, to a penalized future

@josef-pkt
josef-pkt merged commit de15ec8 into statsmodels:master May 7, 2016
@kshedden
kshedden deleted the elastic_net2 branch January 25, 2018 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants