# Gamma\_inc derivatives

**URL:** https://discourse.julialang.org/t/gamma-inc-derivatives/93148
**Category:** New to Julia
**Created:** [January 18, 2023, 11:53am UTC](https://discourse.julialang.org/t/gamma-inc-derivatives/93148 "2023-01-18T11:53:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [January 18, 2023, 11:53am UTC](https://discourse.julialang.org/t/gamma-inc-derivatives/93148/1 "2023-01-18T11:53:36Z")

</div>

[Largely edited version]

I was looking at [this issue](https://github.com/JuliaMath/SpecialFunctions.jl/issues/317) about the derivative of the incomplete gamma function w.r.t the parameter `a`.

Looks like from [this page of wolfram alpha docs](https://functions.wolfram.com/GammaBetaErf/GammaRegularized/introductions/Gammas/ShowAll.html) that the right formula is :

![](https://global.discourse-cdn.com/julialang/original/3X/b/2/b20fa62af6b97ede3d9afbbfc7b74f9c15aea989.gif)

which depends on the regularized hypergeometric function. After a certain time of translation, namely using [this page](https://mathworld.wolfram.com/RegularizedHypergeometricFunction.html) to match the wolfram alpha defintion to the one from the [HypegeometricFunctions.jl](https://github.com/JuliaMath/HypergeometricFunctions.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 :

```julia
using SpecialFunctions, HypergeometricFunctions
function Γ(a,x)
    return gamma_inc(a,x)[2]
end
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)
end
function emp(a,x)
    ϵ = 1e-5
    return (Γ(a+ϵ,x) - Γ(a-ϵ,x))/2ϵ
end

a = 1.9
x = 1.8
Γ(a,x)
(∂Γ_∂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)`.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [January 18, 2023, 1:11pm UTC](https://discourse.julialang.org/t/gamma-inc-derivatives/93148/3 "2023-01-18T13:11:18Z")

</div>

```julia
inc_gamma_deriv(a,z) = -im*log(z+0im)*exp(-z)*((-z+0im)^(a-1)) 

```

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [January 18, 2023, 1:13pm UTC](https://discourse.julialang.org/t/gamma-inc-derivatives/93148/4 "2023-01-18T13:13:53Z")

</div>

@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`.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [January 18, 2023, 1:53pm UTC](https://discourse.julialang.org/t/gamma-inc-derivatives/93148/6 "2023-01-18T13:53:25Z")

</div>

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)?

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [January 18, 2023, 1:54pm UTC](https://discourse.julialang.org/t/gamma-inc-derivatives/93148/7 "2023-01-18T13:54:44Z")

</div>

@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).

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [January 18, 2023, 2:00pm UTC](https://discourse.julialang.org/t/gamma-inc-derivatives/93148/8 "2023-01-18T14:00:36Z")

</div>

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