Skip to content

Commit

Permalink
Added missing posterior_mean and rand for test models (#499)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
torfjelde and github-actions[bot] committed Jul 17, 2023
1 parent 3f80199 commit 446f06d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.23.3"
version = "0.23.4"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
30 changes: 30 additions & 0 deletions src/test_utils.jl
Expand Up @@ -669,6 +669,36 @@ function Random.rand(
return vals
end

const MatrixvariateAssumeDemoModels = Union{
Model{typeof(demo_assume_matrix_dot_observe_matrix)}
}
function posterior_mean(model::MatrixvariateAssumeDemoModels)
# Get some containers to fill.
vals = Random.rand(model)

vals.s[1, 1] = 19 / 8
vals.m[1] = 3 / 4

vals.s[1, 2] = 8 / 3
vals.m[2] = 1

return vals
end
function Base.rand(
rng::Random.AbstractRNG, ::Type{NamedTuple}, model::MatrixvariateAssumeDemoModels
)
# Get template values from `model`.
retval = model(rng)
vals = (s=retval.s, m=retval.m)
# Fill containers with realizations from prior.
for i in LinearIndices(vals.s)
vals.s[i] = rand(rng, InverseGamma(2, 3))
vals.m[i] = rand(rng, Normal(0, sqrt(vals.s[i])))
end

return vals
end

"""
A collection of models corresponding to the posterior distribution defined by
the generative process
Expand Down
22 changes: 22 additions & 0 deletions test/model.jl
Expand Up @@ -185,4 +185,26 @@ 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

2 comments on commit 446f06d

@yebai
Copy link
Member

@yebai yebai commented on 446f06d Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/87660

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.23.4 -m "<description of version>" 446f06d07ce7475bd1c413568219bdc56bfd3e47
git push origin v0.23.4

Please sign in to comment.