Skip to content

BUG: GLM score and hessian - #4620

Merged
josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:bug_glm_score_hessian
May 13, 2018
Merged

josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:bug_glm_score_hessian

Conversation

@josef-pkt

@josef-pkt josef-pkt commented May 11, 2018 •

Copy link
Copy Markdown
Member

fix bugs in GLM score and hessian

first commit are only unit tests to see the results with current code
bugfixes will follow

closes #4616 closes #4614

@josef-pkt josef-pkt added comp-genmod type-bug-wrong serious bugs that silently return incorrect numbers backport-0.9.0 labels May 11, 2018
@josef-pkt josef-pkt added this to the 0.9 milestone May 11, 2018
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.003%) to 82.884% when pulling b37ed1d on josef-pkt:bug_glm_score_hessian into f790b88 on statsmodels:master.

@coveralls

coveralls commented May 11, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 82.895% when pulling 3c892ec on josef-pkt:bug_glm_score_hessian into f790b88 on statsmodels:master.

@codecov-io

codecov-io commented May 11, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4620 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4620      +/-   ##
==========================================
+ Coverage   80.28%   80.29%   +0.01%     
==========================================
  Files         564      564              
  Lines       85664    85693      +29     
  Branches     9681     9684       +3     
==========================================
+ Hits        68772    68811      +39     
+ Misses      14664    14656       -8     
+ Partials     2228     2226       -2
Impacted Files Coverage Δ
statsmodels/genmod/tests/test_constrained.py 100% <ø> (ø) ⬆️
statsmodels/discrete/tests/test_sandwich_cov.py 98.14% <100%> (+1.69%) ⬆️
statsmodels/genmod/tests/test_glm.py 98.75% <100%> (+0.01%) ⬆️
statsmodels/genmod/generalized_linear_model.py 90.39% <100%> (+0.84%) ⬆️
statsmodels/stats/descriptivestats.py 24.13% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f790b88...3c892ec. Read the comment docs.

@josef-pkt josef-pkt closed this May 11, 2018
@thequackdaddy

Copy link
Copy Markdown
Member

This looked good to me. Math makes sense, I think.

Why did it get closed?

@josef-pkt josef-pkt reopened this May 11, 2018
@josef-pkt

Copy link
Copy Markdown
Member Author

I was reading on my ipad, so maybe I accidentally hit the close button.

@josef-pkt

Copy link
Copy Markdown
Member Author

everything was green, but I canceled the ci builds after reopening.

@josef-pkt

Copy link
Copy Markdown
Member Author

this PR sets now scale=1 during gradient optimization.
It covers now all issues raised in #4616.

@thequackdaddy

thequackdaddy commented May 12, 2018 •

Copy link
Copy Markdown
Member

@josef-pkt This looks really good. Before this, I was never able to use the scipy optimizers without optim_hessian='eim'

But now it seems to work well.

The models I'm running are large-ish. 11.9M records, 53 df model. The fit operation takes around 7 minutes for newton and oim, 6 minutes for newton and eim, and around 8 for irls.

Curiously, I think the oim optimization actually requires slightly fewer iterations... I think 5 for oim vs 7 for eim for a big Tweedie model, but I guess each iteration is significantly slower.

As for irls, one on-going issue that I have with it is that it is very memory-intense. On my computer with 64 GB of RAM, newton takes a meager 20-25 GB of RAM. irls takes over 50... I'm fairly certain that creates a big bottleneck that slows down the irls optimization.

@josef-pkt

Copy link
Copy Markdown
Member Author

another cross check

enabling a disabled discrete test comparing Probit with GLM, I get difference in hessian at MLE of Probit:

hess1 = res1.model.hessian(res1.params)
hess2 = res2.model.hessian(res1.params)

(Pdb) np.abs(np.max(hess1 - hess2))
3.2821745321598428e-11

(Pdb) np.abs(np.max(hess1 / hess2 - 1))
6.8633987382327177e-12

So, now they agree, and I should have gotten more suspicious when it didn't agree when I added GLM.hessian.
(Not confirmed numerically: I think in spite of the hessian bug, the hessian with canonical link was still correct, that's why tests didn't fail in typical cases like GLM-Logit, GLM-Poisson or GLM-Gaussian.
The second term for OIM should be zero, and will be zero (+ numerical noise) also with the bug.)

@josef-pkt

Copy link
Copy Markdown
Member Author

This should be finished and ready to merge when green

@josef-pkt

Copy link
Copy Markdown
Member Author

all green, merging

@thequackdaddy Thanks for checking.

@josef-pkt
josef-pkt merged commit c72e966 into statsmodels:master May 13, 2018
@josef-pkt
josef-pkt deleted the bug_glm_score_hessian branch May 13, 2018 20:44
josef-pkt added a commit to josef-pkt/statsmodels that referenced this pull request May 14, 2018
# assert_allclose(res1.bse * res1.scale, res2.bse, rtol=1e-13)
# else:
# assert_allclose(res1.bse, res2.bse, rtol=1e-13)
rtol = getattr(res1, 'rtol', 1e-13)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be getting cls.rtol? That is the attribute that is set down on L514.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a mistake.
However, why do the tests not fail if rtol is 1e-13?

Essentially this means that GLM and the corresponding discrete model agree on very high precision with the same optimizer newton. 1e-13 should be much stricter than convergence tolerance.

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

Labels

comp-genmod type-bug-wrong serious bugs that silently return incorrect numbers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: GLM families Gamma, inverseGaussian have wrong derivatives BUG: GLM Binomial count, incorrect score, score_obs

5 participants