|
1 | 1 | module ISTests
|
2 | 2 |
|
3 |
| -using Distributions: Normal, sample |
4 | 3 | using DynamicPPL: logpdf
|
5 | 4 | using Random: Random
|
| 5 | +using StableRNGs: StableRNG |
6 | 6 | using StatsFuns: logsumexp
|
7 | 7 | using Test: @test, @testset
|
8 | 8 | using Turing
|
9 | 9 |
|
10 | 10 | @testset "is.jl" begin
|
11 |
| - function reference(n) |
12 |
| - as = Vector{Float64}(undef, n) |
13 |
| - bs = Vector{Float64}(undef, n) |
14 |
| - logps = Vector{Float64}(undef, n) |
| 11 | + @testset "numerical accuracy" begin |
| 12 | + function reference(n) |
| 13 | + rng = StableRNG(468) |
| 14 | + as = Vector{Float64}(undef, n) |
| 15 | + bs = Vector{Float64}(undef, n) |
15 | 16 |
|
16 |
| - for i in 1:n |
17 |
| - as[i], bs[i], logps[i] = reference() |
| 17 | + for i in 1:n |
| 18 | + as[i] = rand(rng, Normal(4, 5)) |
| 19 | + bs[i] = rand(rng, Normal(as[i], 1)) |
| 20 | + end |
| 21 | + # logevidence = logsumexp(logps) - log(n) |
| 22 | + return (as=as, bs=bs) |
18 | 23 | end
|
19 |
| - logevidence = logsumexp(logps) - log(n) |
20 | 24 |
|
21 |
| - return (as=as, bs=bs, logps=logps, logevidence=logevidence) |
22 |
| - end |
23 |
| - |
24 |
| - function reference() |
25 |
| - x = rand(Normal(4, 5)) |
26 |
| - y = rand(Normal(x, 1)) |
27 |
| - loglik = logpdf(Normal(x, 2), 3) + logpdf(Normal(y, 2), 1.5) |
28 |
| - return x, y, loglik |
29 |
| - end |
30 |
| - |
31 |
| - @model function normal() |
32 |
| - a ~ Normal(4, 5) |
33 |
| - 3 ~ Normal(a, 2) |
34 |
| - b ~ Normal(a, 1) |
35 |
| - 1.5 ~ Normal(b, 2) |
36 |
| - return a, b |
37 |
| - end |
38 |
| - |
39 |
| - alg = IS() |
40 |
| - seed = 0 |
41 |
| - n = 10 |
| 25 | + @model function normal() |
| 26 | + a ~ Normal(4, 5) |
| 27 | + 3 ~ Normal(a, 2) |
| 28 | + b ~ Normal(a, 1) |
| 29 | + 1.5 ~ Normal(b, 2) |
| 30 | + return a, b |
| 31 | + end |
42 | 32 |
|
43 |
| - model = normal() |
44 |
| - for i in 1:100 |
45 |
| - Random.seed!(seed) |
46 |
| - ref = reference(n) |
| 33 | + function expected_loglikelihoods(as, bs) |
| 34 | + return logpdf.(Normal.(as, 2), 3) .+ logpdf.(Normal.(bs, 2), 1.5) |
| 35 | + end |
47 | 36 |
|
48 |
| - Random.seed!(seed) |
49 |
| - chain = sample(model, alg, n; check_model=false) |
50 |
| - sampled = get(chain, [:a, :b, :loglikelihood]) |
| 37 | + alg = IS() |
| 38 | + N = 1000 |
| 39 | + model = normal() |
| 40 | + chain = sample(StableRNG(468), model, alg, N) |
| 41 | + ref = reference(N) |
51 | 42 |
|
52 |
| - @test vec(sampled.a) == ref.as |
53 |
| - @test vec(sampled.b) == ref.bs |
54 |
| - @test vec(sampled.loglikelihood) == ref.logps |
55 |
| - @test chain.logevidence == ref.logevidence |
| 43 | + @test isapprox(mean(chain[:a]), mean(ref.as); atol=0.1) |
| 44 | + @test isapprox(mean(chain[:b]), mean(ref.bs); atol=0.1) |
| 45 | + @test isapprox(chain[:loglikelihood], expected_loglikelihoods(chain[:a], chain[:b])) |
| 46 | + @test isapprox(chain.logevidence, logsumexp(chain[:loglikelihood]) - log(N)) |
56 | 47 | end
|
57 | 48 |
|
58 | 49 | @testset "logevidence" begin
|
59 |
| - Random.seed!(100) |
60 |
| - |
61 | 50 | @model function test()
|
62 | 51 | a ~ Normal(0, 1)
|
63 | 52 | x ~ Bernoulli(1)
|
|
0 commit comments