Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first commt * export context * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * missing comma * Update src/contexts.jl Co-authored-by: David Widmann <devmotion@users.noreply.github.com> * fixed compilation * extract_priors * tests * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * tests for dot * Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * bug * Apply suggestions from code review * added docstring to extract_priors * fixed and added more tests for extract_priors * moved prior extraction to a separate file * Update test/model.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * forgot to move a small piece of code * added extract_priors to docs * Update docs/src/api.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * added qualifiers to docstring of extract_priors * Revert "added qualifiers to docstring of extract_priors" This reverts commit cab9f9c. * "fixed" the doctests as ran in docs making * make calls to doctest consistent * Update test/runtests.jl 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> Co-authored-by: David Widmann <devmotion@users.noreply.github.com> Co-authored-by: Tor Erlend Fjelde <tor.erlend95@gmail.com>
- Loading branch information
1 parent
e6dd4ef
commit e8172f0
Showing
6 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
struct PriorExtractorContext{D<:OrderedDict{VarName,Any},Ctx<:AbstractContext} <: | ||
AbstractContext | ||
priors::D | ||
context::Ctx | ||
end | ||
|
||
PriorExtractorContext(context) = PriorExtractorContext(OrderedDict{VarName,Any}(), context) | ||
|
||
NodeTrait(::PriorExtractorContext) = IsParent() | ||
childcontext(context::PriorExtractorContext) = context.context | ||
function setchildcontext(parent::PriorExtractorContext, child) | ||
return PriorExtractorContext(parent.priors, child) | ||
end | ||
|
||
function setprior!(context::PriorExtractorContext, vn::VarName, dist::Distribution) | ||
return context.priors[vn] = dist | ||
end | ||
|
||
function setprior!( | ||
context::PriorExtractorContext, vns::AbstractArray{<:VarName}, dist::Distribution | ||
) | ||
for vn in vns | ||
context.priors[vn] = dist | ||
end | ||
end | ||
|
||
function setprior!( | ||
context::PriorExtractorContext, | ||
vns::AbstractArray{<:VarName}, | ||
dists::AbstractArray{<:Distribution}, | ||
) | ||
for (vn, dist) in zip(vns, dists) | ||
context.priors[vn] = dist | ||
end | ||
end | ||
|
||
function DynamicPPL.tilde_assume(context::PriorExtractorContext, right, vn, vi) | ||
setprior!(context, vn, right) | ||
return DynamicPPL.tilde_assume(childcontext(context), right, vn, vi) | ||
end | ||
|
||
function DynamicPPL.dot_tilde_assume(context::PriorExtractorContext, right, left, vn, vi) | ||
setprior!(context, vn, right) | ||
return DynamicPPL.dot_tilde_assume(childcontext(context), right, left, vn, vi) | ||
end | ||
|
||
""" | ||
extract_priors([rng::Random.AbstractRNG, ]model::Model) | ||
Extract the priors from a model. | ||
This is done by sampling from the model and | ||
recording the distributions that are used to generate the samples. | ||
!!! warning | ||
Because the extraction is done by execution of the model, there | ||
are several caveats: | ||
1. If one variable, say, `y ~ Normal(0, x)`, where `x ~ Normal()` | ||
is also a random variable, then the extracted prior will have | ||
different parameters in every extraction! | ||
2. If the model does _not_ have static support, say, | ||
`n ~ Categorical(1:10); x ~ MvNormmal(zeros(n), I)`, then the | ||
extracted priors themselves will be different between extractions, | ||
not just their parameters. | ||
Both of these caveats are demonstrated below. | ||
# Examples | ||
## Changing parameters | ||
```jldoctest | ||
julia> using Distributions, StableRNGs | ||
julia> rng = StableRNG(42); | ||
julia> @model function model_dynamic_parameters() | ||
x ~ Normal(0, 1) | ||
y ~ Normal(x, 1) | ||
end; | ||
julia> model = model_dynamic_parameters(); | ||
julia> extract_priors(rng, model)[@varname(y)] | ||
Normal{Float64}(μ=-0.6702516921145671, σ=1.0) | ||
julia> extract_priors(rng, model)[@varname(y)] | ||
Normal{Float64}(μ=1.3736306979834252, σ=1.0) | ||
``` | ||
## Changing support | ||
```jldoctest | ||
julia> using LinearAlgebra, Distributions, StableRNGs | ||
julia> rng = StableRNG(42); | ||
julia> @model function model_dynamic_support() | ||
n ~ Categorical(ones(10) ./ 10) | ||
x ~ MvNormal(zeros(n), I) | ||
end; | ||
julia> model = model_dynamic_support(); | ||
julia> length(extract_priors(rng, model)[@varname(x)]) | ||
6 | ||
julia> length(extract_priors(rng, model)[@varname(x)]) | ||
9 | ||
``` | ||
""" | ||
extract_priors(model::Model) = extract_priors(Random.default_rng(), model) | ||
function extract_priors(rng::Random.AbstractRNG, model::Model) | ||
context = PriorExtractorContext(SamplingContext(rng)) | ||
evaluate!!(model, VarInfo(), context) | ||
return context.priors | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters