-
-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[On hold] New formulas for derivatives of neg_binomial_lpmf after alpha cutoff #1579
[On hold] New formulas for derivatives of neg_binomial_lpmf after alpha cutoff #1579
Conversation
(stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan, 1.02) |
…binomial_derivatives_at_cutoff
@bob-carpenter May I ask for review on this? The tests here are similar to those written for #1497 (although the problem addressed is different). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. My only requests were around doc, which shouldn't be first person and should be written so that someone who knows C++ but not Mathematica can understand it if you want to leave it in the code.
Also, have you checked that the IP is OK for us to include Mathematica code in comments and use the output of their approximations? I can't see why it wouldn't be, but it'd be good to check before we go too far down the road of using a proprietary software package.
@@ -15,6 +15,12 @@ | |||
namespace stan { | |||
namespace math { | |||
|
|||
namespace internal { | |||
// Exposing to let me use in tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments shouldn't be first person. I'd suggest just eliminating line 19.
Line 20 should say what the value is for---in this case, the threshold beyond which a Poisson approximation is used. It's also OK to say why it's 1e10, but I couldn't follow the logic of why it's set to 1e10.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bob-carpenter, would it be ok if L20 said something like:
// Set to 1e10 based on empirical evaluation. At 1e9, the approximation isn't close enough
// reduces numerically to Poisson | ||
// The derivatives are obtained via Taylor series at alpha -> Inf | ||
// via Mathematica as: | ||
// nb[n_,alpha_,beta_]:= LogGamma[n + alpha] - LogGamma[n + 1] - |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should assume the readers of comments understand mathematica. So this should be put in Stan notation or just eliminated. I couldn't understand what LogGamma
is or Log
or D
. If these comments stay, the functions used need to be explained.
(stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan, 1.0) |
@bob-carpenter, would it be ok if L20 said something like:
// Set to 1e10 based on empirical evaluation. At 1e9, the approximation isn't close enough
Yes, that's clear.
|
Thx @syclik and @bob-carpenter for the notes. I should've marked this PR as "on hold" because if #1497 turns out to be on the right track, I would probably rewrite |
@martinmodrak, do you want to close this PR? #1497 was merged and from reading through the PR, it seems like you are favoring a different implementation. |
Thanks for reminidng me, yes this should be closed. |
Summary
Currently on-hold. If PR #1497 gets merged,
neg_binomial_lpmf
should probably be rewritten using the same approachThe formulas for derivatives of
neg_binomial_lpmf
after delegating to poisson (foralpha > 1e10
) are replaced with Taylor expansions of the exact derivatives foralpha -> Inf
obtained in Mathematica via:(the lowest order of the series that let's me pass the tests was chosen)
Tests
Tests are adapted from #1497 where new tests for
neg_binomial_2_lpmf
were written. Namelyalpha
, as Mathematica refuses to compute exact derivatives in this case.beta
andn
values test whether the change in the derivatives ofneg_binomial_lpmf
atalpha_cutoff +/- 1e-8
is negligible.neg_binomial_lpmf<propto=true, int, var, double>
betweenalpha
just below and just above the cutoff.Side Effects
Not any.
Checklist
Math issue The derivatives of neg_binomial_lpmf are incorrect for large alpha #1578
Copyright holder: Institute of Microbiology of the Czech Academy of Sciences
By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit
)make test-headers
)make doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested