Skip to content

Commit 446f06d

Browse files
Added missing posterior_mean and rand for test models (#499)
* added missing posterior_mean and rand implementation, in simple checks to make sure these things do not go unnoticied * bump patch version * Update src/test_utils.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fixed tests * fixed MatrixvariateDemoModels * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 3f80199 commit 446f06d

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DynamicPPL"
22
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
3-
version = "0.23.3"
3+
version = "0.23.4"
44

55
[deps]
66
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/test_utils.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,36 @@ function Random.rand(
669669
return vals
670670
end
671671

672+
const MatrixvariateAssumeDemoModels = Union{
673+
Model{typeof(demo_assume_matrix_dot_observe_matrix)}
674+
}
675+
function posterior_mean(model::MatrixvariateAssumeDemoModels)
676+
# Get some containers to fill.
677+
vals = Random.rand(model)
678+
679+
vals.s[1, 1] = 19 / 8
680+
vals.m[1] = 3 / 4
681+
682+
vals.s[1, 2] = 8 / 3
683+
vals.m[2] = 1
684+
685+
return vals
686+
end
687+
function Base.rand(
688+
rng::Random.AbstractRNG, ::Type{NamedTuple}, model::MatrixvariateAssumeDemoModels
689+
)
690+
# Get template values from `model`.
691+
retval = model(rng)
692+
vals = (s=retval.s, m=retval.m)
693+
# Fill containers with realizations from prior.
694+
for i in LinearIndices(vals.s)
695+
vals.s[i] = rand(rng, InverseGamma(2, 3))
696+
vals.m[i] = rand(rng, Normal(0, sqrt(vals.s[i])))
697+
end
698+
699+
return vals
700+
end
701+
672702
"""
673703
A collection of models corresponding to the posterior distribution defined by
674704
the generative process

test/model.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,26 @@ end
185185
end
186186
end
187187
end
188+
189+
@testset "TestUtils" begin
190+
@testset "$(model.f)" for model in DynamicPPL.TestUtils.DEMO_MODELS
191+
x = rand(model)
192+
# Ensure log-probability computations are implemented.
193+
@test logprior(model, x) DynamicPPL.TestUtils.logprior_true(model, x...)
194+
@test loglikelihood(model, x)
195+
DynamicPPL.TestUtils.loglikelihood_true(model, x...)
196+
@test logjoint(model, x) DynamicPPL.TestUtils.logjoint_true(model, x...)
197+
@test logjoint(model, x) !=
198+
DynamicPPL.TestUtils.logjoint_true_with_logabsdet_jacobian(model, x...)
199+
# Ensure `varnames` is implemented.
200+
vi = last(
201+
DynamicPPL.evaluate!!(
202+
model, SimpleVarInfo(OrderedDict()), SamplingContext()
203+
),
204+
)
205+
@test all(collect(keys(vi)) .== DynamicPPL.TestUtils.varnames(model))
206+
# Ensure `posterior_mean` is implemented.
207+
@test DynamicPPL.TestUtils.posterior_mean(model) isa typeof(x)
208+
end
209+
end
188210
end

0 commit comments

Comments
 (0)