SpecialFunctions + Turing, AD support for quantile function of Gamma distribution gamma_inv_cdf

mleprovost
Tue, 30 Apr 2024 17:14:39 +0000
General Usage

Hello,

I would like to use AD in Turing.jl to differentiate a custom density that involves the inverse cdf (i.e., quantile function) of a Gamma distribution.
Unfortunately, the quantile function of the Gamma distribution defined in Distributions.jl does not support AD.
The issue boils down to perform AD for the routine gamma_inc_inv from SpecialFunctions (https://github.com/JuliaMath/SpecialFunctions.jl/blob/master/src/gamma_inc.jl:) The function gamma_inc_inv only supports Float64 input arguments.

using ForwardDiff
using SpecialFunctions
ForwardDiff.derivative(x -> gamma_inc_inv(x, 0.1, 0.9), 0.1)

Output:

MethodError: no method matching __gamma_inc_inv(::ForwardDiff.Dual{ForwardDiff.Tag{var"#3#4", Float64}, Float64, 1}, ::ForwardDiff.Dual{ForwardDiff.Tag{var"#3#4", Float64}, Float64, 1}, ::Bool)

Closest candidates are:
  __gamma_inc_inv(::Float64, ::Float64, ::Bool)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/QH8rV/src/gamma_inc.jl:1012
  __gamma_inc_inv(::T, ::T, ::Bool) where T<:Union{Float16, Float32}
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/QH8rV/src/gamma_inc.jl:1089


Stacktrace:
 [1] _gamma_inc_inv
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/QH8rV/src/gamma_inc.jl:1009 [inlined]
 [2] gamma_inc_inv(a::ForwardDiff.Dual{ForwardDiff.Tag{var"#3#4", Float64}, Float64, 1}, p::Float64, q::Float64)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/QH8rV/src/gamma_inc.jl:991
 [3] (::var"#3#4")(x::ForwardDiff.Dual{ForwardDiff.Tag{var"#3#4", Float64}, Float64, 1})
   @ Main ./In[7]:1
 [4] derivative(f::var"#3#4", x::Float64)
   @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:14
 [5] top-level scope
   @ In[7]:1

1 post - 1 participant