-
Notifications
You must be signed in to change notification settings - Fork 25
/
model.jl
210 lines (179 loc) · 6.73 KB
/
model.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# some functors (#367)
struct MyModel
a::Int
end
@model function (f::MyModel)(x)
m ~ Normal(f.a, 1)
return x ~ Normal(m, 1)
end
struct MyZeroModel end
@model function (::MyZeroModel)(x)
m ~ Normal(0, 1)
return x ~ Normal(m, 1)
end
innermost_distribution_type(d::Distribution) = typeof(d)
function innermost_distribution_type(d::Distributions.ReshapedDistribution)
return innermost_distribution_type(d.dist)
end
function innermost_distribution_type(d::Distributions.Product)
dists = map(innermost_distribution_type, d.v)
if any(!=(dists[1]), dists)
error("Cannot extract innermost distribution type from $d")
end
return dists[1]
end
@testset "model.jl" begin
@testset "convenience functions" begin
model = gdemo_default
# sample from model and extract variables
vi = VarInfo(model)
s = vi[@varname(s)]
m = vi[@varname(m)]
# extract log pdf of variable object
lp = getlogp(vi)
# log prior probability
lprior = logprior(model, vi)
@test lprior ≈ logpdf(InverseGamma(2, 3), s) + logpdf(Normal(0, sqrt(s)), m)
# log likelihood
llikelihood = loglikelihood(model, vi)
@test llikelihood ≈ loglikelihood(Normal(m, sqrt(s)), [1.5, 2.0])
# log joint probability
ljoint = logjoint(model, vi)
@test ljoint ≈ lprior + llikelihood
@test ljoint ≈ lp
end
@testset "rng" begin
model = gdemo_default
for sampler in (SampleFromPrior(), SampleFromUniform())
for i in 1:10
Random.seed!(100 + i)
vi = VarInfo()
model(Random.default_rng(), vi, sampler)
vals = DynamicPPL.getall(vi)
Random.seed!(100 + i)
vi = VarInfo()
model(Random.default_rng(), vi, sampler)
@test DynamicPPL.getall(vi) == vals
end
end
end
@testset "defaults without VarInfo, Sampler, and Context" begin
model = gdemo_default
Random.seed!(100)
s, m = model()
Random.seed!(100)
@test model(Random.default_rng()) == (s, m)
end
@testset "nameof" begin
@model function test1(x)
m ~ Normal(0, 1)
return x ~ Normal(m, 1)
end
@model test2(x) = begin
m ~ Normal(0, 1)
x ~ Normal(m, 1)
end
function test3 end
@model function (::typeof(test3))(x)
m ~ Normal(0, 1)
return x ~ Normal(m, 1)
end
function test4 end
@model function (a::typeof(test4))(x)
m ~ Normal(0, 1)
return x ~ Normal(m, 1)
end
@test nameof(test1(rand())) == :test1
@test nameof(test2(rand())) == :test2
@test nameof(test3(rand())) == :test3
@test nameof(test4(rand())) == :test4
# callables
@test nameof(MyModel(3)(rand())) == Symbol("MyModel(3)")
@test nameof(MyZeroModel()(rand())) == Symbol("MyZeroModel()")
end
@testset "Internal methods" begin
model = gdemo_default
# sample from model and extract variables
vi = VarInfo(model)
# Second component of return-value of `evaluate!!` should
# be a `DynamicPPL.AbstractVarInfo`.
evaluate_retval = DynamicPPL.evaluate!!(model, vi, DefaultContext())
@test evaluate_retval[2] isa DynamicPPL.AbstractVarInfo
# Should not return `AbstractVarInfo` when we call the model.
call_retval = model()
@test !any(map(x -> x isa DynamicPPL.AbstractVarInfo, call_retval))
end
@testset "Dynamic constraints" begin
model = DynamicPPL.TestUtils.demo_dynamic_constraint()
vi = VarInfo(model)
spl = SampleFromPrior()
link!!(vi, spl, model)
for i in 1:10
# Sample with large variations.
r_raw = randn(length(vi[spl])) * 10
vi[spl] = r_raw
@test vi[@varname(m)] == r_raw[1]
@test vi[@varname(x)] != r_raw[2]
model(vi)
end
end
@testset "rand" begin
model = gdemo_default
Random.seed!(1776)
s, m = model()
sample_namedtuple = (; s=s, m=m)
sample_dict = Dict(@varname(s) => s, @varname(m) => m)
# With explicit RNG
@test rand(Random.seed!(1776), model) == sample_namedtuple
@test rand(Random.seed!(1776), NamedTuple, model) == sample_namedtuple
@test rand(Random.seed!(1776), Dict, model) == sample_dict
# Without explicit RNG
Random.seed!(1776)
@test rand(model) == sample_namedtuple
Random.seed!(1776)
@test rand(NamedTuple, model) == sample_namedtuple
Random.seed!(1776)
@test rand(Dict, model) == sample_dict
end
@testset "default arguments" begin
@model test_defaults(x, n=length(x)) = x ~ MvNormal(zeros(n), I)
@test length(test_defaults(missing, 2)()) == 2
end
@testset "extract priors" begin
@testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS
priors = extract_priors(model)
# We know that any variable starting with `s` should have `InverseGamma`
# and any variable starting with `m` should have `Normal`.
for (vn, prior) in priors
if DynamicPPL.getsym(vn) == :s
@test innermost_distribution_type(prior) <: InverseGamma
elseif DynamicPPL.getsym(vn) == :m
@test innermost_distribution_type(prior) <: Union{Normal,MvNormal}
else
error("Unexpected variable name: $vn")
end
end
end
end
@testset "TestUtils" begin
@testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS
x = rand(model)
# Ensure log-probability computations are implemented.
@test logprior(model, x) ≈ DynamicPPL.TestUtils.logprior_true(model, x...)
@test loglikelihood(model, x) ≈
DynamicPPL.TestUtils.loglikelihood_true(model, x...)
@test logjoint(model, x) ≈ DynamicPPL.TestUtils.logjoint_true(model, x...)
@test logjoint(model, x) !=
DynamicPPL.TestUtils.logjoint_true_with_logabsdet_jacobian(model, x...)
# Ensure `varnames` is implemented.
vi = last(
DynamicPPL.evaluate!!(
model, SimpleVarInfo(OrderedDict()), SamplingContext()
),
)
@test all(collect(keys(vi)) .== DynamicPPL.TestUtils.varnames(model))
# Ensure `posterior_mean` is implemented.
@test DynamicPPL.TestUtils.posterior_mean(model) isa typeof(x)
end
end
end