Skip to content

Commit

Permalink
Improved particle filter error message. (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
yebai authored Apr 15, 2020
1 parent 8342103 commit 6f44f01
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ function Libtask.consume(pc :: ParticleContainer)
if num_done == n
res = Val{:done}
elseif num_done != 0
error("[consume]: mis-aligned execution traces, num_particles= $(n), num_done=$(num_done).")
# The posterior for models with random number of observations is not well-defined.
error("[consume]: mis-aligned execution traces, num_particles= $(n),
num_done=$(num_done). Please make sure the number of observations is NOT random.")
else
# update incremental likelihoods
z2 = logZ(pc)
Expand Down
30 changes: 30 additions & 0 deletions test/inference/smc.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Turing, Random, Test
using StatsFuns

dir = splitdir(splitdir(pathof(Turing))[1])[1]
include(dir*"/test/test_utils/AllUtils.jl")

@turing_testset "smc.jl" begin
@model normal() = begin
a ~ Normal(4,5)
3 ~ Normal(a,2)
b ~ Normal(a,1)
1.5 ~ Normal(b,2)
a, b
end

tested = sample(normal(), SMC(), 100);

# failing test
@model fail_smc() = begin
a ~ Normal(4,5)
3 ~ Normal(a,2)
b ~ Normal(a,1)
if a >= 4.0
1.5 ~ Normal(b,2)
end
a, b
end

@test_throws ErrorException sample(fail_smc(), SMC(), 100)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ include("test_utils/AllUtils.jl")
include("inference/is.jl")
include("inference/mh.jl")
include("inference/ess.jl")
include("inference/smc.jl")
include("inference/AdvancedSMC.jl")
include("inference/Inference.jl")
include("contrib/inference/dynamichmc.jl")
Expand Down

0 comments on commit 6f44f01

Please sign in to comment.