|
15 | 15 | end
|
16 | 16 |
|
17 | 17 | # https://github.com/TuringLang/DynamicPPL.jl/issues/28#issuecomment-829223577
|
18 |
| - @testset "arrays of distributions" begin |
19 |
| - @model function test(x, y) |
20 |
| - y .~ Normal.(x) |
| 18 | + @testset "dot tilde: arrays of distributions" begin |
| 19 | + @testset "assume" begin |
| 20 | + @model function test(x, size) |
| 21 | + y = Array{Float64,length(size)}(undef, size...) |
| 22 | + y .~ Normal.(x) |
| 23 | + return y, getlogp(__varinfo__) |
| 24 | + end |
| 25 | + |
| 26 | + for ysize in ((2,), (2, 3), (2, 3, 4)) |
| 27 | + for x in ( |
| 28 | + # scalar |
| 29 | + randn(), |
| 30 | + # drop trailing dimensions |
| 31 | + ntuple(i -> randn(ysize[1:i]), length(ysize))..., |
| 32 | + # singleton dimensions |
| 33 | + ntuple( |
| 34 | + i -> randn(ysize[1:(i-1)]..., 1, ysize[(i+1):end]...), |
| 35 | + length(ysize), |
| 36 | + )..., |
| 37 | + ) |
| 38 | + model = test(x, ysize) |
| 39 | + y, lp = model() |
| 40 | + @test lp ≈ sum(logpdf.(Normal.(x), y)) |
| 41 | + |
| 42 | + ys = [first(model()) for _ in 1:10_000] |
| 43 | + @test norm(mean(ys) .- x, Inf) < 0.1 |
| 44 | + @test norm(std(ys) .- 1, Inf) < 0.1 |
| 45 | + end |
| 46 | + end |
21 | 47 | end
|
22 | 48 |
|
23 |
| - for ysize in ((2,), (2, 3), (2, 3, 4)) |
24 |
| - # drop trailing dimensions |
25 |
| - for xsize in ntuple(i -> ysize[1:i], length(ysize)) |
26 |
| - x = randn(xsize) |
27 |
| - y = randn(ysize) |
28 |
| - z = logjoint(test(x, y), VarInfo()) |
29 |
| - @test z ≈ sum(logpdf.(Normal.(x), y)) |
| 49 | + @testset "observe" begin |
| 50 | + @model function test(x, y) |
| 51 | + y .~ Normal.(x) |
30 | 52 | end
|
31 | 53 |
|
32 |
| - # singleton dimensions |
33 |
| - for xsize in ntuple(i -> (ysize[1:(i-1)]..., 1, ysize[(i+1):end]...), length(ysize)) |
34 |
| - x = randn(xsize) |
35 |
| - y = randn(ysize) |
36 |
| - z = logjoint(test(x, y), VarInfo()) |
37 |
| - @test z ≈ sum(logpdf.(Normal.(x), y)) |
| 54 | + for ysize in ((2,), (2, 3), (2, 3, 4)) |
| 55 | + for x in ( |
| 56 | + # scalar |
| 57 | + randn(), |
| 58 | + # drop trailing dimensions |
| 59 | + ntuple(i -> randn(ysize[1:i]), length(ysize))..., |
| 60 | + # singleton dimensions |
| 61 | + ntuple( |
| 62 | + i -> randn(ysize[1:(i-1)]..., 1, ysize[(i+1):end]...), |
| 63 | + length(ysize), |
| 64 | + )..., |
| 65 | + ) |
| 66 | + y = randn(ysize) |
| 67 | + z = logjoint(test(x, y), VarInfo()) |
| 68 | + @test z ≈ sum(logpdf.(Normal.(x), y)) |
| 69 | + end |
38 | 70 | end
|
39 | 71 | end
|
40 | 72 | end
|
|
0 commit comments