|
| 1 | +module TestUtils |
| 2 | + |
| 3 | +using AbstractMCMC |
| 4 | +using DynamicPPL |
| 5 | +using Distributions |
| 6 | +using Test |
| 7 | + |
| 8 | +# A collection of models for which the mean-of-means for the posterior should |
| 9 | +# be same. |
| 10 | +@model function demo_dot_assume_dot_observe( |
| 11 | + x=[10.0, 10.0], ::Type{TV}=Vector{Float64} |
| 12 | +) where {TV} |
| 13 | + # `dot_assume` and `observe` |
| 14 | + m = TV(undef, length(x)) |
| 15 | + m .~ Normal() |
| 16 | + x ~ MvNormal(m, 0.25 * I) |
| 17 | + return (; m=m, x=x, logp=getlogp(__varinfo__)) |
| 18 | +end |
| 19 | + |
| 20 | +@model function demo_assume_index_observe( |
| 21 | + x=[10.0, 10.0], ::Type{TV}=Vector{Float64} |
| 22 | +) where {TV} |
| 23 | + # `assume` with indexing and `observe` |
| 24 | + m = TV(undef, length(x)) |
| 25 | + for i in eachindex(m) |
| 26 | + m[i] ~ Normal() |
| 27 | + end |
| 28 | + x ~ MvNormal(m, 0.25 * I) |
| 29 | + |
| 30 | + return (; m=m, x=x, logp=getlogp(__varinfo__)) |
| 31 | +end |
| 32 | + |
| 33 | +@model function demo_assume_multivariate_observe_index(x=[10.0, 10.0]) |
| 34 | + # Multivariate `assume` and `observe` |
| 35 | + m ~ MvNormal(zero(x), I) |
| 36 | + x ~ MvNormal(m, 0.25 * I) |
| 37 | + |
| 38 | + return (; m=m, x=x, logp=getlogp(__varinfo__)) |
| 39 | +end |
| 40 | + |
| 41 | +@model function demo_dot_assume_observe_index( |
| 42 | + x=[10.0, 10.0], ::Type{TV}=Vector{Float64} |
| 43 | +) where {TV} |
| 44 | + # `dot_assume` and `observe` with indexing |
| 45 | + m = TV(undef, length(x)) |
| 46 | + m .~ Normal() |
| 47 | + for i in eachindex(x) |
| 48 | + x[i] ~ Normal(m[i], 0.5) |
| 49 | + end |
| 50 | + |
| 51 | + return (; m=m, x=x, logp=getlogp(__varinfo__)) |
| 52 | +end |
| 53 | + |
| 54 | +# Using vector of `length` 1 here so the posterior of `m` is the same |
| 55 | +# as the others. |
| 56 | +@model function demo_assume_dot_observe(x=[10.0]) |
| 57 | + # `assume` and `dot_observe` |
| 58 | + m ~ Normal() |
| 59 | + x .~ Normal(m, 0.5) |
| 60 | + |
| 61 | + return (; m=m, x=x, logp=getlogp(__varinfo__)) |
| 62 | +end |
| 63 | + |
| 64 | +@model function demo_assume_observe_literal() |
| 65 | + # `assume` and literal `observe` |
| 66 | + m ~ MvNormal(zeros(2), I) |
| 67 | + [10.0, 10.0] ~ MvNormal(m, 0.25 * I) |
| 68 | + |
| 69 | + return (; m=m, x=[10.0, 10.0], logp=getlogp(__varinfo__)) |
| 70 | +end |
| 71 | + |
| 72 | +@model function demo_dot_assume_observe_index_literal(::Type{TV}=Vector{Float64}) where {TV} |
| 73 | + # `dot_assume` and literal `observe` with indexing |
| 74 | + m = TV(undef, 2) |
| 75 | + m .~ Normal() |
| 76 | + for i in eachindex(m) |
| 77 | + 10.0 ~ Normal(m[i], 0.5) |
| 78 | + end |
| 79 | + |
| 80 | + return (; m=m, x=fill(10.0, length(m)), logp=getlogp(__varinfo__)) |
| 81 | +end |
| 82 | + |
| 83 | +@model function demo_assume_literal_dot_observe() |
| 84 | + # `assume` and literal `dot_observe` |
| 85 | + m ~ Normal() |
| 86 | + [10.0] .~ Normal(m, 0.5) |
| 87 | + |
| 88 | + return (; m=m, x=[10.0], logp=getlogp(__varinfo__)) |
| 89 | +end |
| 90 | + |
| 91 | +@model function _prior_dot_assume(::Type{TV}=Vector{Float64}) where {TV} |
| 92 | + m = TV(undef, 2) |
| 93 | + m .~ Normal() |
| 94 | + |
| 95 | + return m |
| 96 | +end |
| 97 | + |
| 98 | +@model function demo_assume_submodel_observe_index_literal() |
| 99 | + # Submodel prior |
| 100 | + m = @submodel _prior_dot_assume() |
| 101 | + for i in eachindex(m) |
| 102 | + 10.0 ~ Normal(m[i], 0.5) |
| 103 | + end |
| 104 | + |
| 105 | + return (; m=m, x=[10.0], logp=getlogp(__varinfo__)) |
| 106 | +end |
| 107 | + |
| 108 | +@model function _likelihood_dot_observe(m, x) |
| 109 | + return x ~ MvNormal(m, 0.25 * I) |
| 110 | +end |
| 111 | + |
| 112 | +@model function demo_dot_assume_observe_submodel( |
| 113 | + x=[10.0, 10.0], ::Type{TV}=Vector{Float64} |
| 114 | +) where {TV} |
| 115 | + m = TV(undef, length(x)) |
| 116 | + m .~ Normal() |
| 117 | + |
| 118 | + # Submodel likelihood |
| 119 | + @submodel _likelihood_dot_observe(m, x) |
| 120 | + |
| 121 | + return (; m=m, x=x, logp=getlogp(__varinfo__)) |
| 122 | +end |
| 123 | + |
| 124 | +@model function demo_dot_assume_dot_observe_matrix( |
| 125 | + x=fill(10.0, 2, 1), ::Type{TV}=Vector{Float64} |
| 126 | +) where {TV} |
| 127 | + m = TV(undef, length(x)) |
| 128 | + m .~ Normal() |
| 129 | + |
| 130 | + # Dotted observe for `Matrix`. |
| 131 | + x .~ MvNormal(m, 0.25 * I) |
| 132 | + |
| 133 | + return (; m=m, x=x, logp=getlogp(__varinfo__)) |
| 134 | +end |
| 135 | + |
| 136 | +const DEMO_MODELS = ( |
| 137 | + demo_dot_assume_dot_observe(), |
| 138 | + demo_assume_index_observe(), |
| 139 | + demo_assume_multivariate_observe_index(), |
| 140 | + demo_dot_assume_observe_index(), |
| 141 | + demo_assume_dot_observe(), |
| 142 | + demo_assume_observe_literal(), |
| 143 | + demo_dot_assume_observe_index_literal(), |
| 144 | + demo_assume_literal_dot_observe(), |
| 145 | + demo_assume_submodel_observe_index_literal(), |
| 146 | + demo_dot_assume_observe_submodel(), |
| 147 | + demo_dot_assume_dot_observe_matrix(), |
| 148 | +) |
| 149 | + |
| 150 | +# TODO: Is this really the best/most convenient "default" test method? |
| 151 | +""" |
| 152 | + test_sampler_demo_models(meanfunction, sampler, args...; kwargs...) |
| 153 | +
|
| 154 | +Test that `sampler` produces the correct marginal posterior means on all models in `demo_models`. |
| 155 | +
|
| 156 | +In short, this method iterators through `demo_models`, calls `AbstractMCMC.sample` on the |
| 157 | +`model` and `sampler` to produce a `chain`, and then checks `meanfunction(chain)` against `target` |
| 158 | +provided in `kwargs...`. |
| 159 | +
|
| 160 | +# Arguments |
| 161 | +- `meanfunction`: A callable which computes the mean of the marginal means from the |
| 162 | + chain resulting from the `sample` call. |
| 163 | +- `sampler`: The `AbstractMCMC.AbstractSampler` to test. |
| 164 | +- `args...`: Arguments forwarded to `sample`. |
| 165 | +
|
| 166 | +# Keyword arguments |
| 167 | +- `target`: Value to compare result of `meanfunction(chain)` to. |
| 168 | +- `atol=1e-1`: Absolute tolerance used in `@test`. |
| 169 | +- `rtol=1e-3`: Relative tolerance used in `@test`. |
| 170 | +- `kwargs...`: Keyword arguments forwarded to `sample`. |
| 171 | +""" |
| 172 | +function test_sampler_demo_models( |
| 173 | + meanfunction, |
| 174 | + sampler::AbstractMCMC.AbstractSampler, |
| 175 | + args...; |
| 176 | + target=8.0, |
| 177 | + atol=1e-1, |
| 178 | + rtol=1e-3, |
| 179 | + kwargs..., |
| 180 | +) |
| 181 | + @testset "$(nameof(typeof(sampler))) on $(m.name)" for model in DEMO_MODELS |
| 182 | + chain = AbstractMCMC.sample(model, sampler, args...; kwargs...) |
| 183 | + μ = meanfunction(chain) |
| 184 | + @test μ ≈ target atol = atol rtol = rtol |
| 185 | + end |
| 186 | +end |
| 187 | + |
| 188 | +""" |
| 189 | + test_sampler_continuous([meanfunction, ]sampler, args...; kwargs...) |
| 190 | +
|
| 191 | +Test that `sampler` produces the correct marginal posterior means on all models in `demo_models`. |
| 192 | +
|
| 193 | +As of right now, this is just an alias for [`test_sampler_demo_models`](@ref). |
| 194 | +""" |
| 195 | +function test_sampler_continuous( |
| 196 | + meanfunction, sampler::AbstractMCMC.AbstractSampler, args...; kwargs... |
| 197 | +) |
| 198 | + return test_sampler_demo_models(meanfunction, sampler, args...; kwargs...) |
| 199 | +end |
| 200 | + |
| 201 | +function test_sampler_continuous(sampler::AbstractMCMC.AbstractSampler, args...; kwargs...) |
| 202 | + # Default for `MCMCChains.Chains`. |
| 203 | + return test_sampler_continuous(sampler, args...; kwargs...) do chain |
| 204 | + mean(Array(chain)) |
| 205 | + end |
| 206 | +end |
| 207 | + |
| 208 | +end |
0 commit comments