Skip to content
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

NegativeBinomial grad with k = 0 failures #680

Closed
staples6 opened this issue Feb 10, 2019 · 3 comments
Closed

NegativeBinomial grad with k = 0 failures #680

staples6 opened this issue Feb 10, 2019 · 3 comments

Comments

@staples6
Copy link

_nbinomlogpdf_grad_1(r, p, k) = sum(1 / (k + r - i) for i in 1:k) + log(p)

If I understand the parameterization of the NegativeBinomial being used, I believe _nbinomlogpdf_grad_1 would need to range from 0:k since there can be 0 failures before the rth success? Is that correct?

I believe that is the cause for the following error:

using Turing

@model nbexample(y, r) = begin
        p ~ Beta(98, 2)
        y ~ NegativeBinomial(r, p)
end

chn = sample(nbexample(0, 10), NUTS(1000, 100, .7))

ERROR: ArgumentError: reducing over an empty collection is not allowed
@mohamed82008
Copy link
Member

I don't think this is the correct fix. When k is 0, this is just the probability of having r successes, which is p^r. The partial of log(p^r) wrt r is log(p). The second partial derivative nicely handles the k = 0 case.

Note that in the above code, I used k + r - i not k + r - 1 - i so I went from i = 1:k as opposed to i = 0:k-1 which is what you would get from the choose operator. So the above code is correct AFAICT. It is also validated against finite difference. The fix is simple, we can do mapreduce(i -> 1 / (k + r - i), +, 1:k, init = zero(p)) + log(p) to handle the empty collection case.

mohamed82008 added a commit that referenced this issue Feb 11, 2019
Fixes #680
@mohamed82008 mohamed82008 mentioned this issue Feb 11, 2019
@mohamed82008
Copy link
Member

PR opened.

@staples6
Copy link
Author

Yes, I see. Thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants