Gamma_inc derivatives

[Largely edited version]

I was looking at this issue about the derivative of the incomplete gamma function w.r.t the parameter a.

Looks like from this page of wolfram alpha docs that the right formula is :

which depends on the regularized hypergeometric function. After a certain time of translation, namely using this page to match the wolfram alpha defintion to the one from the HypegeometricFunctions.jl package (also, the formula is about non-normalized upper incomplete gamma function, while gamma_inc gives the normalized version), I have found a working implementation :

using SpecialFunctions, HypergeometricFunctions
function Γ(a,x)
    return gamma_inc(a,x)[2]
function ∂Γ_∂a(a,x)
    g = gamma(a)
    dg = digamma(a)
    G = Γ(a,x)
    lx = log(x)
    r = pFq([a,a],[a+1,a+1],-x)
    return a^(-2) * x^a * r/g + (1 - G)*(dg - lx)
function emp(a,x)
    ϵ = 1e-5
    return (Γ(a+ϵ,x) - Γ(a-ϵ,x))/2ϵ

a = 1.9
x = 1.8
(∂Γ_∂a(a,x), emp(a,x))

#a simple test: 
a = 0.1:0.1:5
x = 0.1:0.1:5
diff(a,x) = abs(∂Γ_∂a(a,x) - emp(a,x))
maximum(diff.(a,x')) # 2e-10 on my machine. 

How could I:

  1. Check in a better way that this is yielding the right derivative ?
  2. Tell ForwardDiff to use this instead of erroring out on ForwardDiff.gradient(a -> gamma_inc(a,1)[2],1.2).
inc_gamma_deriv(a,z) = -im*log(z+0im)*exp(-z)*((-z+0im)^(a-1)) 

@JeffreySarnoff I am not sure what this function is the derivative of w.r.t. which variable. Could you ellaborate?

I am looking for the derivative of gamma_inc w.r.t. a.

oh that was with respect to both a and z
the derivative with respect to z is easy
the derivative with respect to a not so much
do you have a domain (constraints on a, constraints on z)?

@JeffreySarnoff I nailled it and found the right function for the derivative w.r.t. a, see the updated top post. The question now is how should i tell ForwardDiff to use it, maybe you could help ?

Edit: My constraints are that a and x are positives, but since the implementation is generic, passing something else (negative or even complex) whould work if gamma,gamma_inc,digamma and log accepts it. However, in both cases, i still need a better way to check the result (not sure it’ll work everywhere in a satisfactory way).

good deriving – I have never used ForwardDiff
the slack channel #autodiff is your friend

1 Like