Skip to content

Commit

Permalink
Handle tiny θ in gammalogpdf (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Feb 15, 2024
1 parent 1d392b8 commit b386d5c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/distrs/gamma.jl
Expand Up @@ -22,7 +22,12 @@ gammalogpdf(k::Real, θ::Real, x::Real) = gammalogpdf(promote(k, θ, x)...)
function gammalogpdf(k::T, θ::T, x::T) where {T<:Real}
# we ensure that `log(x)` does not error if `x < 0`
= max(x, 0) / θ
val = -loggamma(k) + xlogy(k - 1, xθ) - log(θ) -
val = -loggamma(k) - log(θ) -
# xlogy(k - 1, xθ) - xθ -> -∞ for xθ -> ∞ so we only add the first term
# when it's safe
if isfinite(xθ)
val += xlogy(k - 1, xθ)
end
return x < 0 ? oftype(val, -Inf) : val
end

Expand Down
1 change: 1 addition & 0 deletions test/rmath.jl
Expand Up @@ -264,6 +264,7 @@ end
((Float16(1), Float16(1)), (Float16(0):Float16(0.05):Float16(12))),
((1f0, 1f0), (Float16(0):Float16(0.05):Float16(12))),
((2, 3), (0//1:12//1)),
((3.0, 1e-310), (0.0:0.05:12.0))
])

rmathcomp_tests("hyper", [
Expand Down

0 comments on commit b386d5c

Please sign in to comment.