|
| 1 | +discrete_product = ProductDistribution(bernoulli, binom) |
| 2 | + |
| 3 | +@testset "product of discrete distributions" begin |
| 4 | + @test is_discrete(discrete_product) |
| 5 | + grad_bools = (has_output_grad(discrete_product), has_argument_grads(discrete_product)...) |
| 6 | + @test grad_bools == (false, true, false, true) |
| 7 | + |
| 8 | + p1 = 0.5 |
| 9 | + (n, p2) = (3, 0.9) |
| 10 | + |
| 11 | + # random |
| 12 | + x = discrete_product(p1, n, p2) |
| 13 | + @assert typeof(x) == Gen.get_return_type(discrete_product) == Tuple{Bool, Int} |
| 14 | + |
| 15 | + # logpdf |
| 16 | + x = (true, 2) |
| 17 | + actual = logpdf(discrete_product, x, p1, n, p2) |
| 18 | + expected = logpdf(bernoulli, x[1], p1) + logpdf(binom, x[2], n, p2) |
| 19 | + @test isapprox(actual, expected) |
| 20 | + |
| 21 | + # test logpdf_grad against finite differencing |
| 22 | + f = (x, p1, n, p2) -> logpdf(discrete_product, x, p1, n, p2) |
| 23 | + args = (x, p1, n, p2) |
| 24 | + actual = logpdf_grad(discrete_product, args...) |
| 25 | + for i in [2, 4] |
| 26 | + @test isapprox(actual[i], finite_diff(f, args, i, dx)) |
| 27 | + end |
| 28 | +end |
| 29 | + |
| 30 | +continuous_product = ProductDistribution(uniform, normal) |
| 31 | + |
| 32 | +@testset "product of continuous distributions" begin |
| 33 | + @test !is_discrete(continuous_product) |
| 34 | + grad_bools = (has_output_grad(continuous_product), has_argument_grads(continuous_product)...) |
| 35 | + @test grad_bools == (true, true, true, true, true) |
| 36 | + |
| 37 | + (low, high) = (-0.5, 0.5) |
| 38 | + (mu, std) = (0.0, 1.0) |
| 39 | + |
| 40 | + # random |
| 41 | + x = continuous_product(low, high, mu, std) |
| 42 | + @assert typeof(x) == Gen.get_return_type(continuous_product) == Tuple{Float64, Float64} |
| 43 | + |
| 44 | + # logpdf |
| 45 | + x = (0.1, 0.7) |
| 46 | + actual = logpdf(continuous_product, x, low, high, mu, std) |
| 47 | + expected = logpdf(uniform, x[1], low, high) + logpdf(normal, x[2], mu, std) |
| 48 | + @test isapprox(actual, expected) |
| 49 | + |
| 50 | + # test logpdf_grad against finite differencing |
| 51 | + f = (x, low, high, mu, std) -> logpdf(continuous_product, x, low, high, mu, std) |
| 52 | + # A mutable indexable is required by `finite_diff_vec`, hence the `collect` here: |
| 53 | + args = (collect(x), low, high, mu, std) |
| 54 | + actual = logpdf_grad(continuous_product, args...) |
| 55 | + @test isapprox(actual[1][1], finite_diff_vec(f, args, 1, 1, dx)) |
| 56 | + @test isapprox(actual[1][2], finite_diff_vec(f, args, 1, 2, dx)) |
| 57 | + for i in 2:5 |
| 58 | + @test isapprox(actual[i], finite_diff(f, args, i, dx)) |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +dissimilar_product = ProductDistribution(bernoulli, normal) |
| 63 | + |
| 64 | +@testset "product of dissimilarly-typed distributions" begin |
| 65 | + @test !is_discrete(dissimilar_product) |
| 66 | + grad_bools = (has_output_grad(dissimilar_product), has_argument_grads(dissimilar_product)...) |
| 67 | + @test grad_bools == (false, true, true, true) |
| 68 | + |
| 69 | + p = 0.5 |
| 70 | + (mu, std) = (0.0, 1.0) |
| 71 | + |
| 72 | + # random |
| 73 | + x = dissimilar_product(p, mu, std) |
| 74 | + @assert typeof(x) == Gen.get_return_type(dissimilar_product) == Tuple{Bool, Float64} |
| 75 | + |
| 76 | + # logpdf |
| 77 | + x = (false, 0.3) |
| 78 | + actual = logpdf(dissimilar_product, x, p, mu, std) |
| 79 | + expected = logpdf(bernoulli, x[1], p) + logpdf(normal, x[2], mu, std) |
| 80 | + @test isapprox(actual, expected) |
| 81 | + |
| 82 | + # test logpdf_grad against finite differencing |
| 83 | + f = (x, p, mu, std) -> logpdf(dissimilar_product, x, p, mu, std) |
| 84 | + args = (x, p, mu, std) |
| 85 | + actual = logpdf_grad(dissimilar_product, args...) |
| 86 | + for i in 2:4 |
| 87 | + @test isapprox(actual[i], finite_diff(f, args, i, dx)) |
| 88 | + end |
| 89 | +end |
0 commit comments