Skip to content

Commit

Permalink
Define rand defaults for AbstractProbabilisticProgram (#79)
Browse files Browse the repository at this point in the history
This PR adds a 3-arg form of `rand` (suggested by @devmotion in TuringLang/DynamicPPL.jl#466 (comment)) to the interface for `AbstractProbabilisticProgram` and implements the default 1- and 2-arg methods that dispatch to this.

Currently tests fail because this breaks the fallbacks for `GraphPPL.Model`, which expects `rand` to forward to its `rand!` method. I'm not certain how we want to define the interface for this `Model`.

Co-authored-by: Xianda Sun <[email protected]>
  • Loading branch information
sethaxen and sunxd3 committed Feb 25, 2023
1 parent f788a0a commit 0f28920
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
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
20 changes: 20 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,22 @@ 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 joint 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
10 changes: 7 additions & 3 deletions src/graphinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ function Random.rand!(m::AbstractPPL.GraphPPL.Model{T}) where T
end

"""
rand!(rng::AbstractRNG, m::Model)
rand(m::Model)
Draw random samples from the model and mutate the node values.
Draw random samples from the model and return the samples as NamedTuple.
# Examples
Expand All @@ -470,11 +470,15 @@ julia> rand(m)
(μ = 1.0, s2 = 1.0907695400401212, y = 0.05821954440386368)
```
"""
function Random.rand(rng::AbstractRNG, sm::Random.SamplerTrivial{Model{Tnames, Tinput, Tvalue, Teval, Tkind}}) where {Tnames, Tinput, Tvalue, Teval, Tkind}
function Base.rand(rng::AbstractRNG, sm::Random.SamplerTrivial{Model{Tnames, Tinput, Tvalue, Teval, Tkind}}) where {Tnames, Tinput, Tvalue, Teval, Tkind}
m = deepcopy(sm[])
get_model_values(rand!(rng, m))
end

function Base.rand(rng::AbstractRNG, ::Type{NamedTuple}, m::Model)
rand(rng, Random.SamplerTrivial(m))
end

"""
logdensityof(m::Model)
Expand Down
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

0 comments on commit 0f28920

Please sign in to comment.