Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make MCSE for constant arrays always return a NaN #128

Merged
merged 11 commits into from
Nov 18, 2024
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- '1.8'
- '1'
sethaxen marked this conversation as resolved.
Show resolved Hide resolved
- 'nightly'
sethaxen marked this conversation as resolved.
Show resolved Hide resolved
os:
Expand Down
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"
Expand Down Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions src/mcse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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σ
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions test/mcse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading