Skip to content

Commit

Permalink
Prevent integer overflow in tail ESS (#133)
Browse files Browse the repository at this point in the history
* Add failing test

* Fix failing test

* Increment patch number

* Fix comment
  • Loading branch information
sethaxen authored Jan 8, 2025
1 parent 438da79 commit 5a32333
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MCMCDiagnosticTools"
uuid = "be115224-59cd-429b-ad48-344e309966f0"
authors = ["David Widmann", "Seth Axen"]
version = "0.3.13"
version = "0.3.14"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
2 changes: 1 addition & 1 deletion src/ess_rhat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function _ess(
::Val{:tail}, x::AbstractArray{<:Union{Missing,Real}}; tail_prob::Real=1//10, kwargs...
)
# workaround for https://github.com/JuliaStats/Statistics.jl/issues/136
T = Base.promote_eltype(x, tail_prob)
T = float(Base.promote_eltype(x, tail_prob))
pl = convert(T, tail_prob / 2)
pu = convert(T, 1 - tail_prob / 2)
S_lower = _ess(Base.Fix2(Statistics.quantile, pl), x; kwargs...)
Expand Down
11 changes: 11 additions & 0 deletions test/ess_rhat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,17 @@ mymean(x) = mean(x)
@test all(>(rhat_cutoff), R_tail)
end

# check that issue #132 is fixed
@testset "tail ESS does not overflow for integer types" begin
x = rand(1:10_000, 1_000, 4, 100)
S, _ = ess_rhat(x; kind=:tail)
@test all(isfinite, S)
@test eltype(S) == Float64
S = ess(x; kind=:tail)
@test all(isfinite, S)
@test eltype(S) == Float64
end

@testset "bulk and tail ESS and R-hat for heavy tailed" begin
# sampling Cauchy distribution with large max depth to allow for better tail
# exploration. From https://avehtari.github.io/rhat_ess/rhat_ess.html chains have
Expand Down

2 comments on commit 5a32333

@sethaxen
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/122573

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v0.3.14 -m "<description of version>" 5a32333b77f471112abd2fb53bd613f94de78864
git push origin v0.3.14

Please sign in to comment.