@@ -19,9 +19,9 @@ wants to extract the realization of a model in a constrained space.
19
19
# Fields
20
20
$(TYPEDFIELDS)
21
21
"""
22
- struct ValuesAsInModelContext{T, C<: AbstractContext } <: AbstractContext
22
+ struct ValuesAsInModelContext{C<: AbstractContext } <: AbstractContext
23
23
" values that are extracted from the model"
24
- values:: T
24
+ values:: OrderedDict
25
25
" whether to extract variables on the LHS of :="
26
26
include_colon_eq:: Bool
27
27
" child context"
@@ -114,34 +114,32 @@ function dot_tilde_assume(
114
114
end
115
115
116
116
"""
117
- values_as_in_model(model::Model, include_colon_eq::Bool[, varinfo::AbstractVarInfo, context::AbstractContext])
118
- values_as_in_model(rng::Random.AbstractRNG, model::Model, include_colon_eq::Bool[, varinfo::AbstractVarInfo, context::AbstractContext])
117
+ values_as_in_model(model::Model, include_colon_eq::Bool, varinfo::AbstractVarInfo[, context::AbstractContext])
119
118
120
119
Get the values of `varinfo` as they would be seen in the model.
121
120
122
- If no `varinfo` is provided, then this is effectively the same as
123
- [`Base.rand(rng::Random.AbstractRNG, model::Model)`](@ref).
121
+ More specifically, this method attempts to extract the realization _as seen in
122
+ the model_. For example, `x[1] ~ truncated(Normal(); lower=0)` will result in a
123
+ realization that is compatible with `truncated(Normal(); lower=0)` -- i.e. one
124
+ where the value of `x[1]` is positive -- regardless of whether `varinfo` is
125
+ working in unconstrained space.
124
126
125
- More specifically, this method attempts to extract the realization _as seen in the model_.
126
- For example, `x[1] ~ truncated(Normal(); lower=0)` will result in a realization compatible
127
- with `truncated(Normal(); lower=0)` regardless of whether `varinfo` is working in unconstrained
128
- space.
129
-
130
- Hence this method is a "safe" way of obtaining realizations in constrained space at the cost
131
- of additional model evaluations.
127
+ Hence this method is a "safe" way of obtaining realizations in constrained
128
+ space at the cost of additional model evaluations.
132
129
133
130
# Arguments
134
131
- `model::Model`: model to extract realizations from.
135
132
- `include_colon_eq::Bool`: whether to also include variables on the LHS of `:=`.
136
133
- `varinfo::AbstractVarInfo`: variable information to use for the extraction.
137
- - `context::AbstractContext`: context to use for the extraction. If `rng` is specified, then `context`
138
- will be wrapped in a [`SamplingContext`](@ref) with the provided `rng `.
134
+ - `context::AbstractContext`: base context to use for the extraction. Defaults
135
+ to `DynamicPPL.DefaultContext() `.
139
136
140
137
# Examples
141
138
142
139
## When `VarInfo` fails
143
140
144
- The following demonstrates a common pitfall when working with [`VarInfo`](@ref) and constrained variables.
141
+ The following demonstrates a common pitfall when working with [`VarInfo`](@ref)
142
+ and constrained variables.
145
143
146
144
```jldoctest
147
145
julia> using Distributions, StableRNGs
@@ -191,19 +189,10 @@ true
191
189
function values_as_in_model (
192
190
model:: Model ,
193
191
include_colon_eq:: Bool ,
194
- varinfo:: AbstractVarInfo = VarInfo () ,
192
+ varinfo:: AbstractVarInfo ,
195
193
context:: AbstractContext = DefaultContext (),
196
194
)
197
195
context = ValuesAsInModelContext (include_colon_eq, context)
198
196
evaluate!! (model, varinfo, context)
199
197
return context. values
200
198
end
201
- function values_as_in_model (
202
- rng:: Random.AbstractRNG ,
203
- model:: Model ,
204
- include_colon_eq:: Bool ,
205
- varinfo:: AbstractVarInfo = VarInfo (),
206
- context:: AbstractContext = DefaultContext (),
207
- )
208
- return values_as_in_model (model, true , varinfo, SamplingContext (rng, context))
209
- end
0 commit comments