Skip to content

BUG: Handle non-invertible hessian from GLM scipy optimizers - #3299

Merged
josef-pkt merged 1 commit into
statsmodels:masterfrom
thequackdaddy:glm_optimizer_summary_fix
May 15, 2017
Merged

josef-pkt merged 1 commit into
statsmodels:masterfrom
thequackdaddy:glm_optimizer_summary_fix

Conversation

@thequackdaddy

Copy link
Copy Markdown
Member

Hello,

In a GLM if you are using one of the scipy optimizer and the Hessian inversion fails (i.e., it is not positive definite), None is returned. When running res.summary() later, a matrix is divided by None which causes a crash.

This fixes the problem by returning a matrix of nan of the same shape as H.

Example fail:

import statsmodels.api as sm

data = sm.datasets.cpunish.load_pandas()

data.endog[:] = 1
mod = sm.GLM(data.endog, data.exog, family=sm.families.Gamma())
res = mod.fit(maxiter=1, method='bfgs', max_start_irls=0)
res.summary()

@thequackdaddy thequackdaddy changed the title FIX: Return Hinv as same shape as H to allow summary to work BUG: Return Hinv as same shape as H to allow summary to work Nov 30, 2016
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.001%) to 87.973% when pulling 739cdfb on thequackdaddy:glm_optimizer_summary_fix into 5797248 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

temporarily prio-high

unit tests pass, but I need to think of the potential impact of this change.
alternative is to add a None check in summary (we already protect against some other results which might not be available.)
nans can cause problems with some linalg libraries (including hanging or in older times segfaults), e.g. if we want to use svd to check for matrix_rank of cov_params.

@thequackdaddy
thequackdaddy force-pushed the glm_optimizer_summary_fix branch from 739cdfb to 01b081f Compare February 7, 2017 18:02
@coveralls

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 01b081f on thequackdaddy:glm_optimizer_summary_fix into ** on statsmodels:master**.

@josef-pkt

Copy link
Copy Markdown
Member

alternative suggestions (while looking by chance at summary)

AFAICS, we only need to set bse to nans, then we don't need to touch cov_params
i.e. add something like if cov_params_default is None and normalized_cov_params is None: bse = <nans>
in LikelihoodModelResults.bse

AFAIK, bse is not used in computation besides tvalues and similar.

(That avoids having to figure out what further impact nans in cov_params would have. I assume t_test and similar still raise the None exception without doing any computations.)

@thequackdaddy

Copy link
Copy Markdown
Member Author

Should I make this change? Happy either way.

@josef-pkt

Copy link
Copy Markdown
Member

@thequackdaddy Yes, I think the nan bse should work for the use case.

@thequackdaddy
thequackdaddy force-pushed the glm_optimizer_summary_fix branch from 01b081f to a218660 Compare April 10, 2017 04:00
@thequackdaddy thequackdaddy reopened this Apr 10, 2017
@thequackdaddy thequackdaddy changed the title BUG: Return Hinv as same shape as H to allow summary to work BUG: Handle non-invertible hessian from GLM scipy optimizers Apr 10, 2017
@coveralls

coveralls commented Apr 10, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.002%) to 90.299% when pulling f408a81 on thequackdaddy:glm_optimizer_summary_fix into a218660 on statsmodels:master.

@coveralls

coveralls commented Apr 10, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.002%) to 90.299% when pulling f408a81 on thequackdaddy:glm_optimizer_summary_fix into a218660 on statsmodels:master.

@thequackdaddy

Copy link
Copy Markdown
Member Author

Updated the code. Note I had to make a similar change at the end of the _fit_gradient method because we also try to divide None / scale there resulting in the error.

(Sorry for the multiple pushes... I checked out master to look at something and forgot to switch back before pushing.)

@josef-pkt

Copy link
Copy Markdown
Member

looks good to me, with minor changes. needs a rebase before merge (too far behind master)

possible bug not affected by changes in this PR.
(If it's not a bug, then I think it doesn't follow our standard pattern, AFAIR.)

@thequackdaddy
thequackdaddy force-pushed the glm_optimizer_summary_fix branch from f408a81 to 97d5a70 Compare May 14, 2017 20:36
@coveralls

coveralls commented May 14, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.002%) to 90.413% when pulling 97d5a70 on thequackdaddy:glm_optimizer_summary_fix into 86496a3 on statsmodels:master.

@thequackdaddy

Copy link
Copy Markdown
Member Author

@josef-pkt

I've re-based to master.

@josef-pkt josef-pkt left a comment

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 still find this review feature very confusing. I never posted my comments from earlier today.)

Comment thread statsmodels/base/model.py Outdated
if ((not hasattr(self, 'cov_params_default')) and
(self.normalized_cov_params is None)):
hinv = np.empty(len(self.params))
hinv[:] = np.nan

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.

hinv is a confusing name, bse_ would be better
Also, I think it would be more readable without two returns and use

else: 
    bse_ = np.sqrt(np.diag(self.cov_params()))

return bse_

if rslt.normalized_cov_params is None:
n_cov_params = None
else:
n_cov_params = rslt.normalized_cov_params / scale

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.

again confusing name: we use n_xxx like nobs for a count of observations,
Given that it's internal just here I would use cov_p

glm_results = GLMResults(self, rslt.params,
rslt.normalized_cov_params / scale,
n_cov_params,
scale,

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.

not in this PR
scale here looks wrong to me, either we use scaled cov_params instead of normalized_cov_params and scale=1, or we use normalized_cov_params and the actual scale in the extra argument.

@thequackdaddy
thequackdaddy force-pushed the glm_optimizer_summary_fix branch from 97d5a70 to 8e69e83 Compare May 14, 2017 23:34
@coveralls

coveralls commented May 15, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.002%) to 90.413% when pulling 8e69e83 on thequackdaddy:glm_optimizer_summary_fix into 86496a3 on statsmodels:master.

@thequackdaddy

Copy link
Copy Markdown
Member Author

@josef-pkt Updated and re-based.

Comment thread statsmodels/base/model.py
if ((not hasattr(self, 'cov_params_default')) and
(self.normalized_cov_params is None)):
bse_ = np.empty(len(self.params))
bse_[:] = np.nan

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.

FYI for future: np.full to create an array with a selected fill value
But I don't remember when it was added, and the numpy docstring doesn't say.

@josef-pkt

Copy link
Copy Markdown
Member

Looks good, nice and clean, merging

Thanks @thequackdaddy

@josef-pkt
josef-pkt merged commit 7e43328 into statsmodels:master May 15, 2017
@thequackdaddy
thequackdaddy deleted the glm_optimizer_summary_fix branch May 15, 2017 01:23
@josef-pkt josef-pkt added this to the 0.9 milestone Apr 15, 2018
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