Skip to content

Commit 83950f5

Browse files
committed
Fix condition in __gamma_inc_inv to ensure that sqrt argument in
gamma_inc_inv_qsmall is positive
1 parent 8b4aba0 commit 83950f5

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/gamma_inc.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ function gamma_inc_inv_psmall(a::Float64, logr::Float64)
693693
end
694694

695695
"""
696-
gamma_inc_inv_qsmall(a,q)
696+
gamma_inc_inv_qsmall(a, q, qgammaxa)
697697
698698
Compute `x0` - initial approximation when `q` is small from ``e^{-x_{0}} x_{0}^{a} = q \\Gamma(a)``.
699699
Asymptotic expansions Eqn (2.29) in the paper is used here and higher approximations are obtained using
@@ -702,9 +702,9 @@ x \\sim x_{0} - L + b \\sum_{k=1}^{\\infty} d_{k}/x_{0}^{k}
702702
```
703703
where ``b = 1-a``, ``L = \\ln{x_0}``.
704704
"""
705-
function gamma_inc_inv_qsmall(a::Float64, q::Float64)
705+
function gamma_inc_inv_qsmall(a::Float64, q::Float64, qgammaxa::Float64)
706706
b = 1.0 - a
707-
eta = sqrt(-2/a*log(q*gammax(a)*sqrt(twoπ/a)))
707+
eta = sqrt(-2/a*log(qgammaxa))
708708
x0 = a*lambdaeta(eta)
709709
l = log(x0)
710710

@@ -941,8 +941,11 @@ function __gamma_inc_inv(a::Float64, minpq::Float64, pcase::Bool)
941941
logr = (logp + loggamma1pa) / a
942942
if logr < log(0.2*(1 + a)) #small value of p
943943
x0 = gamma_inc_inv_psmall(a, logr)
944-
elseif !pcase && minpq < min(0.02, exp(-1.5*a)/gamma(a)) && a < 10 #small q
945-
x0 = gamma_inc_inv_qsmall(a, minpq)
944+
elseif !pcase && a < 10 && minpq < 0.02 && (qgammaxa = minpq*gammax(a)*sqrt(twoπ/a)) < 1 #small q
945+
# This deviates from the original version. The tmp variable
946+
# here ensures that the argument of sqrt in gamma_inc_inv_qsmall
947+
# is positive
948+
x0 = gamma_inc_inv_qsmall(a, minpq, qgammaxa)
946949
elseif abs(minpq - 0.5) < 1.0e-05
947950
x0 = a - 1.0/3.0 + (8.0/405.0 + 184.0/25515.0/a)/a
948951
elseif abs(a - 1.0) < 1.0e-4

test/gamma_inc.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ end
185185
@test @inferred(gamma_inc_inv(1//2, 0.3f0, 0.7f0)) isa Float32
186186
@test @inferred(gamma_inc_inv(1, 0.2f0, 0.8f0)) isa Float32
187187
end
188+
189+
@testset "Issue 385" begin
190+
a = 0.010316813105574363
191+
q = 0.010101010101010102
192+
@test last(gamma_inc(a, gamma_inc_inv(a, 1 - q, q))) q
193+
end
188194
end
189195

190196
double(x::Real) = Float64(x)

0 commit comments

Comments
 (0)