Skip to content

Commit

Permalink
Make MCSE for constant arrays always return a NaN (#128)
Browse files Browse the repository at this point in the history
* Make mcse return NaN for constant arrays

* Add missing tests

* Increment patch number

* Increment patch number

* Update src/mcse.jl

Co-authored-by: David Widmann <[email protected]>

* Require at least Julia v1.8

* Use allequal

* Run CI on lowest supported Julia version

* Fix determination of nan type

* Update version specifiers in CI workflow

Co-authored-by: David Widmann <[email protected]>

---------

Co-authored-by: David Widmann <[email protected]>
sethaxen and devmotion authored Nov 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 877214d commit 2769ffb
Showing 4 changed files with 29 additions and 4 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -14,9 +14,10 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- 'min'
- 'lts'
- '1'
- 'nightly'
- 'pre'
os:
- ubuntu-latest
arch:
4 changes: 2 additions & 2 deletions 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.11"
version = "0.3.12"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
@@ -44,7 +44,7 @@ StatsBase = "0.33.7, 0.34"
StatsFuns = "1"
Tables = "1.11"
Test = "1.6"
julia = "1.6"
julia = "1.8"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
7 changes: 7 additions & 0 deletions src/mcse.jl
Original file line number Diff line number Diff line change
@@ -95,6 +95,9 @@ end

function _mcse_quantile(x, p, Seff)
Seff === missing && return missing
if isnan(Seff)
return oftype(oneunit(eltype(x)) / 1, NaN)
end
S = length(x)
# quantile error distribution is asymptotically normal; estimate σ (mcse) with 2
# quadrature points: xl and xu, chosen as quantiles so that xu - xl = 2σ
@@ -133,6 +136,10 @@ function _mcse_sbm(f, x, batch_size)
any(x -> x === missing, x) && return missing
n = length(x)
i1 = firstindex(x)
if allequal(x)
y1 = f(view(x, i1:(i1 + batch_size - 1)))
return oftype(y1, NaN)
end
v = Statistics.var(
f(view(x, i:(i + batch_size - 1))) for i in i1:(i1 + n - batch_size);
corrected=false,
17 changes: 17 additions & 0 deletions test/mcse.jl
Original file line number Diff line number Diff line change
@@ -83,6 +83,23 @@ using StatsBase
end
end

@testset "mcse for scalar array is always a NaN" begin
x = ones(100)
@testset for kind in [
mean,
median,
std,
mad,
# uses sbm
x -> mean(x),
x -> median(x),
x -> std(x),
x -> mad(x),
]
@test isnan(mcse(x; kind))
end
end

@testset "estimand is within interval defined by MCSE estimate" begin
# we check the MCSE estimates by simulating uncorrelated, correlated, and
# anticorrelated chains, mapping the draws to a target distribution, computing the

2 comments on commit 2769ffb

@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/119689

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.12 -m "<description of version>" 2769ffb292b349f8bb6c35c77ad8ad7f0ab33f89
git push origin v0.3.12

Please sign in to comment.