Skip to content

Commit 583d297

Browse files
committed
Add a Gibbs test for dynamic model with ESS
1 parent 41b25f8 commit 583d297

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

test/mcmc/gibbs.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,28 @@ end
320320
@test isapprox(std(num_ms), 1.8865; atol=0.02)
321321
end
322322

323+
# The below test used to sample incorrectly before
324+
# https://github.com/TuringLang/Turing.jl/pull/2328
325+
@testset "dynamic model with ESS" begin
326+
@model function dynamic_model_for_ess()
327+
b ~ Bernoulli()
328+
x_length = b ? 1 : 2
329+
x = Vector{Float64}(undef, x_length)
330+
for i in 1:x_length
331+
x[i] ~ Normal(i, 1.0)
332+
end
333+
end
334+
335+
m = dynamic_model_for_ess()
336+
chain = sample(m, Gibbs(:b => PG(10), :x => ESS()), 2000; discard_initial=100)
337+
means = Dict(:b => 0.5, "x[1]" => 1.0, "x[2]" => 2.0)
338+
stds = Dict(:b => 0.5, "x[1]" => 1.0, "x[2]" => 1.0)
339+
for vn in keys(means)
340+
@test isapprox(mean(skipmissing(chain[:, vn, 1])), means[vn]; atol=0.1)
341+
@test isapprox(std(skipmissing(chain[:, vn, 1])), stds[vn]; atol=0.1)
342+
end
343+
end
344+
323345
@testset "dynamic model with dot tilde" begin
324346
@model function dynamic_model_with_dot_tilde(
325347
num_zs=10, ::Type{M}=Vector{Float64}

0 commit comments

Comments
 (0)