Skip to content

Commit

Permalink
Prior should use PriorContext (#2170)
Browse files Browse the repository at this point in the history
* `Prior` now uses `PriorContext`

* `Prior` sampler now implements `step` instead of relying on `SampleFromPrior`

* remove unnecessary `make_prior_model`

* bump patch version
  • Loading branch information
torfjelde authored Feb 9, 2024
1 parent 39f5d5b commit 990eeb2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Turing"
uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
version = "0.30.3"
version = "0.30.4"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
47 changes: 17 additions & 30 deletions src/mcmc/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ DynamicPPL.unflatten(vi::SimpleVarInfo, θ::NamedTuple) = SimpleVarInfo(θ, vi.l
# Algorithm for sampling from the prior
struct Prior <: InferenceAlgorithm end

function AbstractMCMC.step(
rng::Random.AbstractRNG,
model::DynamicPPL.Model,
sampler::DynamicPPL.Sampler{<:Prior},
state=nothing;
kwargs...,
)
vi = last(DynamicPPL.evaluate!!(
model,
VarInfo(),
SamplingContext(
rng, DynamicPPL.SampleFromPrior(), DynamicPPL.PriorContext()
)
))
return vi, nothing
end

"""
mh_accept(logp_current::Real, logp_proposal::Real, log_proposal_ratio::Real)
Expand Down Expand Up @@ -230,36 +247,6 @@ function AbstractMCMC.sample(
chain_type=chain_type, progress=progress, kwargs...)
end

function AbstractMCMC.sample(
rng::AbstractRNG,
model::AbstractModel,
alg::Prior,
ensemble::AbstractMCMC.AbstractMCMCEnsemble,
N::Integer,
n_chains::Integer;
chain_type=DynamicPPL.default_chain_type(alg),
progress=PROGRESS[],
kwargs...
)
return AbstractMCMC.sample(rng, model, SampleFromPrior(), ensemble, N, n_chains;
chain_type, progress, kwargs...)
end

function AbstractMCMC.sample(
rng::AbstractRNG,
model::AbstractModel,
alg::Prior,
N::Integer;
chain_type=DynamicPPL.default_chain_type(alg),
resume_from=nothing,
initial_state=DynamicPPL.loadstate(resume_from),
progress=PROGRESS[],
kwargs...
)
return AbstractMCMC.mcmcsample(rng, model, SampleFromPrior(), N;
chain_type, initial_state, progress, kwargs...)
end

##########################
# Chain making utilities #
##########################
Expand Down
15 changes: 15 additions & 0 deletions test/mcmc/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@
@test all(haskey(x, :lp) for x in chains)
@test mean(x[:s][1] for x in chains) 3 atol=0.1
@test mean(x[:m][1] for x in chains) 0 atol=0.1

@testset "#2169" begin
# Not exactly the same as the issue, but similar.
@model function issue2169_model()
if DynamicPPL.leafcontext(__context__) isa DynamicPPL.PriorContext
x ~ Normal(0, 1)
else
x ~ Normal(1000, 1)
end
end

model = issue2169_model()
chain = sample(model, Prior(), 10)
@test all(mean(chain[:x]) .< 5)
end
end

@testset "chain ordering" begin
Expand Down

2 comments on commit 990eeb2

@torfjelde
Copy link
Member Author

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

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.30.4 -m "<description of version>" 990eeb2c31334bc467d75063d36a891e859d2874
git push origin v0.30.4

Please sign in to comment.