Skip to content

Refactor discrete_model to avoid duplicate digamma calls - #3592

Closed
jbrockmendel wants to merge 26 commits into
statsmodels:masterfrom
jbrockmendel:cyrefactor
Closed

jbrockmendel wants to merge 26 commits into
statsmodels:masterfrom
jbrockmendel:cyrefactor

Conversation

@jbrockmendel

Copy link
Copy Markdown
Contributor

Also some flake8 fixes e.g. deleting unused imports.

@coveralls

coveralls commented Apr 4, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.001%) to 90.298% when pulling 7757bd0 on jbrockmendel:cyrefactor into a218660 on statsmodels:master.

@coveralls

coveralls commented Apr 16, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.001%) to 90.298% when pulling 2e0002e on jbrockmendel:cyrefactor into a218660 on statsmodels:master.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

I don't have any experience with appveyor. The error code in the log doesn't help me understand why it failed. Any ideas?

@ChadFulton

Copy link
Copy Markdown
Member

Just a hard-to-debug issue with the state space models, not your code's fault.

@josef-pkt

Copy link
Copy Markdown
Member

looks good as far as I can see from reading through the changes, formulas look unchanged

I made a few comments (which I don't see right now) on smaller things, style, a1

@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.

here are the comments

Comment thread statsmodels/discrete/discrete_model.py Outdated
special.digamma(y + mu/alpha) -
special.digamma(mu/alpha))
digamma_part = (
special.digamma(y+mu/alpha)

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.

cosmetic: : I don't like isolated brackets (on separate lines) unless there is a long list of items

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.

we follow old pep-8 with spaces around all operators, +, -, *, /, ... except ** (power)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

cosmetic: : I don't like isolated brackets (on separate lines) unless there is a long list of items

[Begrudging acquiescence]

we follow old pep-8 with spaces around all operators, +, -, *, /, ... except ** (power)

Will fix.

Comment thread statsmodels/discrete/discrete_model.py Outdated

# for dl/dalpha dalpha
digamma_part = (special.digamma(y + mu/alpha) -
special.digamma(mu/alpha))

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.

duplicate computation of mu/alpha, which is already defined as a1

Comment thread statsmodels/discrete/discrete_model.py Outdated
- special.digamma(mu/alpha)
)
digamma_part = (special.digamma(y + mu / alpha)
- special.digamma(mu / alpha))

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.

align below parenthesis, unless we need more space if parenthesis is already far to the right
(next time)

@josef-pkt

Copy link
Copy Markdown
Member

the content looks good and ready to merge

However, there is a merge of master in this branch that needs to be removed by an interactive rebase.
Merge branch 'master' commit 2e0002e

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

It turns out other _score_nbin and some other methods can be simplified pretty significantly. I just pushed additional commits for _score_nbin. I broke it up into a bunch of commits, each of which performs ~1 step of algebra, so easy to check for correctness.

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 1 alert - view on lgtm.com

fixed alerts:

  • 1 for Module-level cyclic import

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 1 alert - view on lgtm.com

fixed alerts:

  • 1 for Module-level cyclic import

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member

14 test failures with many nans, so there is either a bug or it is numerically less stable than the old version.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Rebasing is behaving... oddly. At this point I think it'll be easier to start fresh on a new branch. Closing.

@josef-pkt

Copy link
Copy Markdown
Member

for checking or debugging, you can also compare analytical with numerical derivatives.
In master there is now also a second NegativeBinomialP implementation.

from one of my recent notebooks

In [23]:
from statsmodels.tools.numdiff import (approx_fprime, approx_hess,
                                       approx_hess_cs, approx_fprime_cs)
In [24]:
approx_fprime_cs(params, res_nb.model.loglike) 
Out[24]:
array([-1.85303595, -2.30113604,  0.04309051])
In [25]:
res_nb.model.hessian(params)
Out[25]:
array([[ -2.52875864e+01,  -3.07984357e+01,   6.59431360e-01],
       [ -3.07984357e+01,  -4.23212732e+01,   7.89283944e-01],
       [  6.59431360e-01,   7.89283944e-01,  -2.95904325e-02]])
In [26]:
approx_hess_cs(params, res_nb.model.loglike)
Out[26]:
array([[ -2.52875864e+01,  -3.07984356e+01,   6.59431360e-01],
       [ -3.07984356e+01,  -4.23212732e+01,   7.89283945e-01],
       [  6.59431360e-01,   7.89283945e-01,  -2.95904325e-02]])
In [27]:
approx_hess_cs(params, res_nb.model.loglike) - res_nb.model.hessian(params)
Out[27]:
...

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.

4 participants