StatsFuns.jl icon indicating copy to clipboard operation
StatsFuns.jl copied to clipboard

Relax _gammalogccdf argument types not to be restricted to Float64

Open mashu opened this issue 2 years ago • 3 comments

I am getting the following error when I am trying to use ForwardDiff with Turing to sample Truncated Poisson distribution.

ERROR: MethodError: no method matching _gammalogccdf(::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8})
Stacktrace:
  [1] gammalogccdf(k::Int64, θ::Int64, x::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 8})

Which suggests that there is a problem with dispatch and perhaps types shouldn't be restricted to Float64.

mashu avatar Sep 20 '23 19:09 mashu

The function branches on some values and is therefore written specifically for Float64. It would probably be better to introduce a derivative rule for gammalogccdf. In particular when you computing the derivative with respect to x since it's then just -gammapdf(..., x)/gammaccdf(..., x)

andreasnoack avatar Sep 20 '23 20:09 andreasnoack

That would be great.

mashu avatar Sep 20 '23 21:09 mashu

@andreasnoack so I tried to add the appropriate rule, but tests for frule and rrule fail. For the code see https://github.com/JuliaStats/StatsFuns.jl/compare/master...mashu:StatsFuns.jl:master

mashu avatar Sep 21 '23 12:09 mashu