-
Notifications
You must be signed in to change notification settings - Fork 40
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
Julia implementations for poiscdf
and poislogcdf
#112
Conversation
Codecov Report
@@ Coverage Diff @@
## master #112 +/- ##
==========================================
- Coverage 39.74% 39.50% -0.25%
==========================================
Files 12 12
Lines 478 481 +3
==========================================
Hits 190 190
- Misses 288 291 +3
Continue to review full report at Codecov.
|
The generic implementations of (log)pdf are compared with Rmath in https://github.com/JuliaStats/StatsFuns.jl/blob/master/test/generic.jl. Similarly, one should check the implementations of (log)cdf. |
Co-authored-by: David Widmann <[email protected]>
Cool 👍 But there are some that already have these implemented, e.g. EDIT: If that's not the case, I'll imlement one ofc. I was just making sure given that there are already similar methods which I assumed were already tested. |
No, I think it is missing. |
Co-authored-by: David Widmann <[email protected]>
src/distrs/pois.jl
Outdated
poiscdf(λ::Number, x::Number; precision=0) = last(gamma_inc(floor(x + 1), λ, precision)) | ||
poisccdf(λ::Number, x::Number; precision=0) = first(gamma_inc(floor(x + 1), λ, precision)) |
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.
Btw, should precision
be argument rather than kwarg? @devmotion
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.
It doesn't matter but IMO it is a bit more natural as a keyword argument since we don't dispatch on it.
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 expose the precision
argument here. The implementation in SpecialFunctions happens to support it but it's not something that is generally available I don't think it will be in demand here. If it is then I think it will be more natural to get the lower precision version as part of a method for Float32
.
@@ -19,6 +19,13 @@ poislogpdf(λ::T, x::T) where {T <: Real} = xlogy(x, λ) - λ - loggamma(x + 1) | |||
|
|||
poislogpdf(λ::Number, x::Number) = poislogpdf(promote(float(λ), x)...) |
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 think I get why this is done btw: loggamma(::ForwardDiff.Dual, ::Int)
won't have a diff-rule defined for it.
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.
Actually, it doesn't seem like these have rules defined in DiffRules.jl!
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.
There is no DiffRule defined for loggamma
and gamma_inc
anyway so it shouldn't matter at the moment 🤷 I added ChainRules definitions to SpecialFunctions (JuliaMath/SpecialFunctions.jl#305) but currently they can't be used with Zygote due to FluxML/Zygote.jl#873.
So can we avoid these conversions if possible for now and only add them later if needed?
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'll have to look at it later, but it seems like there are particular execution branches (depending on the values) which ForwardDiff.jl fails to automatically obtain derivatives for. So when I just tried a couple of values everything seemed to work, but when I let HMC loose on a distribution using this function under the hood, it eventually runs into a MethodError
.
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.
Haha, man, you're always like one step ahead: I just looked at your PR and that's exactly the "solution" i arrived at literally 10mins ago, i.e. we can only take derivative wrt. second argument but that's also what we care about so w/e.
There is no DiffRule defined for loggamma and gamma_inc anyway so it shouldn't matter at the moment
But wouldn't adding those fix this issue? Same as you've done in ChainRules now?
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.
Sure, if I used the correct derivatives it should be fine 😛 AFAICT the convention in DiffRules would be to set the derivatives of the first argument to NaN, if it is not implemented properly.
Regarding the testing then see #113. I think we should just start to use the native implementations unconditionally. Then they'll be tested automatically against the Rmath versions with the current test setup. |
@@ -19,6 +19,16 @@ poislogpdf(λ::T, x::T) where {T <: Real} = xlogy(x, λ) - λ - loggamma(x + 1) | |||
|
|||
poislogpdf(λ::Number, x::Number) = poislogpdf(promote(float(λ), x)...) | |||
|
|||
function poislogcdf(λ::T, x::T) where {T<:Real} | |||
return loggamma(floor(x + 1), float(λ)) - logfactorial(floor(x)) |
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.
Very minor detail, but it seems a little odd to me use logfactorial
here together with loggamma
. In particular when we also use loggamma(x+1)
in the pdf.
@torfjelde It would be great to have this one completed. Do you expect to have the cycles to complete it soon? |
Sorry, been quite busy lately. I'm travelling this weekend so won't be able to do much, but I'll try to get it done early next week if that's okay? |
No worries. I ended up doing it as part of #113 |
Btw, should we just close this PR then? |
Yeah. Let's do that (and then I'll have to get #113 completed) |
This PR adds Julia implementations for
poiscdf
andpoislogcdf
.I'm a bit uncertain where tests for these should go, so I would appreciate some advice on that:)