Skip to content

Commit

Permalink
Add rand for sampling from prior of Models (#381)
Browse files Browse the repository at this point in the history
A simple+intuitively-named way to generate a prior sample as a way to initialize a sampler.

Co-authored-by: Hong Ge <[email protected]>
Co-authored-by: David Widmann <[email protected]>
  • Loading branch information
3 people committed Mar 2, 2022
1 parent 083dfa1 commit c655d8c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.17.7"
version = "0.17.8"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
21 changes: 21 additions & 0 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,27 @@ Get the name of the `model` as `Symbol`.
"""
Base.nameof(model::Model) = model.name

"""
rand([rng=Random.GLOBAL_RNG], [T=NamedTuple], model::Model)
Generate a sample of type `T` from the prior distribution of the `model`.
"""
function Base.rand(rng::Random.AbstractRNG, ::Type{T}, model::Model) where {T}
x = last(
evaluate!!(
model,
SimpleVarInfo{Float64}(),
SamplingContext(rng, SampleFromPrior(), DefaultContext()),
),
)
return DynamicPPL.values_as(x, T)
end

# Default RNG and type
Base.rand(rng::Random.AbstractRNG, model::Model) = rand(rng, NamedTuple, model)
Base.rand(::Type{T}, model::Model) where {T} = rand(Random.GLOBAL_RNG, T, model)
Base.rand(model::Model) = rand(Random.GLOBAL_RNG, NamedTuple, model)

"""
logjoint(model::Model, varinfo::AbstractVarInfo)
Expand Down
22 changes: 22 additions & 0 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,26 @@
call_retval = model()
@test !any(map(x -> x isa DynamicPPL.AbstractVarInfo, call_retval))
end

@testset "rand" begin
model = gdemo_default

Random.seed!(1776)
s, m = model()
sample_namedtuple = (; s=s, m=m)
sample_dict = Dict(:s => s, :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
end

2 comments on commit c655d8c

@devmotion
Copy link
Member

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/55797

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.17.8 -m "<description of version>" c655d8cd2c78aa21f72cdb97069bf20a06c49aa0
git push origin v0.17.8

Please sign in to comment.