Skip to content

Commit b386d5c

Browse files
authored
Handle tiny θ in gammalogpdf (#163)
1 parent 1d392b8 commit b386d5c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/distrs/gamma.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ gammalogpdf(k::Real, θ::Real, x::Real) = gammalogpdf(promote(k, θ, x)...)
2222
function gammalogpdf(k::T, θ::T, x::T) where {T<:Real}
2323
# we ensure that `log(x)` does not error if `x < 0`
2424
= max(x, 0) / θ
25-
val = -loggamma(k) + xlogy(k - 1, xθ) - log(θ) -
25+
val = -loggamma(k) - log(θ) -
26+
# xlogy(k - 1, xθ) - xθ -> -∞ for xθ -> ∞ so we only add the first term
27+
# when it's safe
28+
if isfinite(xθ)
29+
val += xlogy(k - 1, xθ)
30+
end
2631
return x < 0 ? oftype(val, -Inf) : val
2732
end
2833

test/rmath.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ end
264264
((Float16(1), Float16(1)), (Float16(0):Float16(0.05):Float16(12))),
265265
((1f0, 1f0), (Float16(0):Float16(0.05):Float16(12))),
266266
((2, 3), (0//1:12//1)),
267+
((3.0, 1e-310), (0.0:0.05:12.0))
267268
])
268269

269270
rmathcomp_tests("hyper", [

0 commit comments

Comments
 (0)