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 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
6 changes: 5 additions & 1 deletion src/mcse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function _mcse(
end

function _mcse_quantile(x, p, Seff)
Seff === missing && return missing
(Seff === missing || isnan(Seff)) && return Seff
sethaxen marked this conversation as resolved.
Show resolved Hide resolved
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 +133,10 @@ function _mcse_sbm(f, x, batch_size)
any(x -> x === missing, x) && return missing
n = length(x)
i1 = firstindex(x)
if all(==(first(x)), x)
sethaxen marked this conversation as resolved.
Show resolved Hide resolved
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