Skip to content

[Merged by Bors] - Define rand defaults for AbstractProbabilisticProgram #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf"
keywords = ["probablistic programming"]
license = "MIT"
desc = "Common interfaces for probabilistic programming"
version = "0.6.2"
version = "0.6.3"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
21 changes: 21 additions & 0 deletions src/abstractprobprog.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AbstractMCMC
using DensityInterface
using Random


"""
Expand Down Expand Up @@ -60,3 +61,23 @@ m = decondition(condition(m, obs))
should hold for generative models `m` and arbitrary `obs`.
"""
function condition end


"""
rand([rng=Random.default_rng()], [T=NamedTuple], model::AbstractProbabilisticProgram) -> T

Draw a sample from the prior and prior-predictive distribution of the model specified by the
probabilistic program.

The sample will be returned as format specified by `T`.
"""
Base.rand(rng::Random.AbstractRNG, ::Type, model::AbstractProbabilisticProgram)
function Base.rand(rng::Random.AbstractRNG, model::AbstractProbabilisticProgram)
return rand(rng, NamedTuple, model)
end
function Base.rand(::Type{T}, model::AbstractProbabilisticProgram) where {T}
return rand(Random.default_rng(), T, model)
end
function Base.rand(model::AbstractProbabilisticProgram)
return rand(Random.default_rng(), NamedTuple, model)
end
37 changes: 37 additions & 0 deletions test/abstractprobprog.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AbstractPPL
using Random
using Test

mutable struct RandModel <: AbstractProbabilisticProgram
rng
T
end

function Base.rand(rng::Random.AbstractRNG, ::Type{T}, model::RandModel) where {T}
model.rng = rng
model.T = T
return nothing
end

@testset "AbstractProbabilisticProgram" begin
@testset "rand defaults" begin
model = RandModel(nothing, nothing)
rand(model)
@test model.rng == Random.default_rng()
@test model.T === NamedTuple
rngs = [Random.default_rng(), Random.MersenneTwister(42)]
Ts = [NamedTuple, Dict]
@testset for T in Ts
model = RandModel(nothing, nothing)
rand(T, model)
@test model.rng == Random.default_rng()
@test model.T === T
end
@testset for rng in rngs
model = RandModel(nothing, nothing)
rand(rng, model)
@test model.rng === rng
@test model.T === NamedTuple
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using Test
@testset "AbstractPPL.jl" begin
include("deprecations.jl")
include("varname.jl")
include("abstractprobprog.jl")
include("graphinfo/graphinfo.jl")
@testset "doctests" begin
DocMeta.setdocmeta!(
Expand Down