Skip to content

Commit

Permalink
Fix betalogcdf with large parameters (#144)
Browse files Browse the repository at this point in the history
* Fix `betalogcdf` with large parameters

* Handle special case of infinite parameter values in `logbetacdf`

* Revert some unrelated changes
  • Loading branch information
devmotion authored May 14, 2022
1 parent c8f50cf commit e5699db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StatsFuns"
uuid = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
version = "1.0.0"
version = "1.0.1"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand All @@ -14,7 +14,7 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[compat]
ChainRulesCore = "1"
HypergeometricFunctions = "0.3"
HypergeometricFunctions = "0.3.10"
InverseFunctions = "0.1"
IrrationalConstants = "0.1"
LogExpFunctions = "0.3.2"
Expand Down
5 changes: 3 additions & 2 deletions src/distrs/beta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ function betalogcdf(α::T, β::T, x::T) where {T<:Real}
_x = clamp(x, 0, 1)
p, q = beta_inc(α, β, _x)
if p < floatmin(p)
# see https://dlmf.nist.gov/8.17#E7
return -log(α) + xlogy(α, _x) + log(_₂F₁(promote(α, 1 - β, α + 1, _x)...)) - logbeta(α, β)
# see https://dlmf.nist.gov/8.17#E8
# we use E8 instead of E7 due to https://github.com/JuliaMath/HypergeometricFunctions.jl/issues/47
return -log(α) + xlogy(α, _x) + xlog1py(β, -_x) + log(_₂F₁(promote+ β, 1, α + 1, _x)...; method=:positive)) - logbeta(α, β)
elseif p <= 0.7
return log(p)
else
Expand Down
8 changes: 8 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ end
# for 64.0f0 the expansion is used but for 64.0 the BigFloat value is rounded
@test Float32(lstirling_asym(64.0)) @inferred lstirling_asym(64.0f0)
end

# https://github.com/JuliaStats/StatsFuns.jl/issues/143
# https://github.com/JuliaMath/HypergeometricFunctions.jl/issues/47
@testset "logbetacdf: numerical issue" begin
# Mathematica: N[Log[CDF[BetaDistribution[6041, 2496], 1/10]], 10]
@test betalogcdf(6041, 2496, 0.1) -9020.029401
@test betainvlogcdf(6041, 2496, betalogcdf(6041, 2496, 0.1)) 0.1
end

2 comments on commit e5699db

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/60251

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.1 -m "<description of version>" e5699db95edc045fccfe557e57b50063fc5e8887
git push origin v1.0.1

Please sign in to comment.