Distributions.jl
Distributions.jl copied to clipboard
ForwardDiff of `logpdf` of `NegativeBinomial` gives wrong results for `p==1`
This was discovered and documented in #1568 via tests, but #1579 reverted those tests. I still think this is worth tracking:
using Distributions, ForwardDiff
using Distributions: digamma
mydiffp(r, p, k) = isone(p) && iszero(k) ? r : r/p - k/(1 - p)
mydiffr(r, p, k) = log(p) - inv(k + r) - digamma(r) + digamma(r + k + 1)
f1(_p) = logpdf(NegativeBinomial(1.0, _p), 0)
f2(_r) = logpdf(NegativeBinomial(_r, 1.0), 0)
ForwardDiff.derivative(f1, 1.0) ≈ mydiffp(1.0, 1.0, 0) # false
ForwardDiff.derivative(f2, 1.0) ≈ mydiffr(1.0, 1.0, 0) # false
Should be solved by https://github.com/JuliaDiff/ForwardDiff.jl/pull/481
That is not required. I found a fix (and a bug on the way) without it.
Basically,
- we can eliminate the special case branch alltogether by using
xlogy
andxlog1py
- we just have to fix the derivatives of these functions, defined in DiffRules and LogExpFunctions
I'll open the three required PRs.
These tests pass with https://github.com/JuliaDiff/DiffRules.jl/pull/85 and https://github.com/JuliaStats/Distributions.jl/pull/1583.