@@ -666,53 +666,54 @@ function fixed(context::FixedContext)
666
666
end
667
667
668
668
"""
669
- RealizationExtractorContext
669
+ ValuesAsInModelContext
670
670
671
- A context that is used to extract realizations from a model.
671
+ A context that is used by [`values_as_in_model`](@ref) to obtain values
672
+ of the model parameters as they are in the model.
672
673
673
674
This is particularly useful when working in unconstrained space, but one
674
675
wants to extract the realization of a model in a constrained space.
675
676
676
677
# Fields
677
678
$(TYPEDFIELDS)
678
679
"""
679
- struct RealizationExtractorContext {T,C<: AbstractContext } <: AbstractContext
680
+ struct ValuesAsInModelContext {T,C<: AbstractContext } <: AbstractContext
680
681
" values that are extracted from the model"
681
682
values:: T
682
683
" child context"
683
684
context:: C
684
685
end
685
686
686
- RealizationExtractorContext (values) = RealizationExtractorContext (values, DefaultContext ())
687
- function RealizationExtractorContext (context:: AbstractContext )
688
- return RealizationExtractorContext (OrderedDict (), context)
687
+ ValuesAsInModelContext (values) = ValuesAsInModelContext (values, DefaultContext ())
688
+ function ValuesAsInModelContext (context:: AbstractContext )
689
+ return ValuesAsInModelContext (OrderedDict (), context)
689
690
end
690
691
691
- NodeTrait (:: RealizationExtractorContext ) = IsParent ()
692
- childcontext (context:: RealizationExtractorContext ) = context. context
693
- function setchildcontext (context:: RealizationExtractorContext , child)
694
- return RealizationExtractorContext (context. values, child)
692
+ NodeTrait (:: ValuesAsInModelContext ) = IsParent ()
693
+ childcontext (context:: ValuesAsInModelContext ) = context. context
694
+ function setchildcontext (context:: ValuesAsInModelContext , child)
695
+ return ValuesAsInModelContext (context. values, child)
695
696
end
696
697
697
- function Base. push! (context:: RealizationExtractorContext , vn:: VarName , value)
698
+ function Base. push! (context:: ValuesAsInModelContext , vn:: VarName , value)
698
699
return setindex! (context. values, copy (value), vn)
699
700
end
700
701
701
- function broadcast_push! (context:: RealizationExtractorContext , vns, values)
702
+ function broadcast_push! (context:: ValuesAsInModelContext , vns, values)
702
703
return push! .((context,), vns, values)
703
704
end
704
705
705
706
# This will be hit if we're broadcasting an `AbstractMatrix` over a `MultivariateDistribution`.
706
707
function broadcast_push! (
707
- context:: RealizationExtractorContext , vns:: AbstractVector , values:: AbstractMatrix
708
+ context:: ValuesAsInModelContext , vns:: AbstractVector , values:: AbstractMatrix
708
709
)
709
710
for (vn, col) in zip (vns, eachcol (values))
710
711
push! (context, vn, col)
711
712
end
712
713
end
713
714
714
715
# `tilde_asssume`
715
- function tilde_assume (context:: RealizationExtractorContext , right, vn, vi)
716
+ function tilde_assume (context:: ValuesAsInModelContext , right, vn, vi)
716
717
value, logp, vi = tilde_assume (childcontext (context), right, vn, vi)
717
718
# Save the value.
718
719
push! (context, vn, value)
@@ -721,7 +722,7 @@ function tilde_assume(context::RealizationExtractorContext, right, vn, vi)
721
722
return value, logp, vi
722
723
end
723
724
function tilde_assume (
724
- rng:: Random.AbstractRNG , context:: RealizationExtractorContext , sampler, right, vn, vi
725
+ rng:: Random.AbstractRNG , context:: ValuesAsInModelContext , sampler, right, vn, vi
725
726
)
726
727
value, logp, vi = tilde_assume (rng, childcontext (context), sampler, right, vn, vi)
727
728
# Save the value.
@@ -731,7 +732,7 @@ function tilde_assume(
731
732
end
732
733
733
734
# `dot_tilde_assume`
734
- function dot_tilde_assume (context:: RealizationExtractorContext , right, left, vn, vi)
735
+ function dot_tilde_assume (context:: ValuesAsInModelContext , right, left, vn, vi)
735
736
value, logp, vi = dot_tilde_assume (childcontext (context), right, left, vn, vi)
736
737
737
738
# Save the value.
@@ -741,13 +742,7 @@ function dot_tilde_assume(context::RealizationExtractorContext, right, left, vn,
741
742
return value, logp, vi
742
743
end
743
744
function dot_tilde_assume (
744
- rng:: Random.AbstractRNG ,
745
- context:: RealizationExtractorContext ,
746
- sampler,
747
- right,
748
- left,
749
- vn,
750
- vi,
745
+ rng:: Random.AbstractRNG , context:: ValuesAsInModelContext , sampler, right, left, vn, vi
751
746
)
752
747
value, logp, vi = dot_tilde_assume (
753
748
rng, childcontext (context), sampler, right, left, vn, vi
@@ -760,10 +755,10 @@ function dot_tilde_assume(
760
755
end
761
756
762
757
"""
763
- extract_realizations (model::Model[, varinfo::AbstractVarInfo, context::AbstractContext])
764
- extract_realizations (rng::Random.AbstractRNG, model::Model[, varinfo::AbstractVarInfo, context::AbstractContext])
758
+ values_as_in_model (model::Model[, varinfo::AbstractVarInfo, context::AbstractContext])
759
+ values_as_in_model (rng::Random.AbstractRNG, model::Model[, varinfo::AbstractVarInfo, context::AbstractContext])
765
760
766
- Extract realizations from the `model` for a given `varinfo` through a evaluation of the model.
761
+ Get the values of `varinfo` as they would be seen in the model.
767
762
768
763
If no `varinfo` is provided, then this is effectively the same as
769
764
[`Base.rand(rng::Random.AbstractRNG, model::Model)`](@ref).
@@ -826,27 +821,27 @@ julia> # (×) Fails! Because `VarInfo` _saves_ the original distributions
826
821
lb ≤ varinfo_invlinked[@varname(y)] ≤ ub
827
822
false
828
823
829
- julia> # Approach 2: Extract realizations using `extract_realizations `.
830
- # (✓) `extract_realizations ` will re-run the model and extract
824
+ julia> # Approach 2: Extract realizations using `values_as_in_model `.
825
+ # (✓) `values_as_in_model ` will re-run the model and extract
831
826
# the correct realization of `y` given the new values of `x`.
832
- lb ≤ extract_realizations (model, varinfo_linked)[@varname(y)] ≤ ub
827
+ lb ≤ values_as_in_model (model, varinfo_linked)[@varname(y)] ≤ ub
833
828
true
834
829
```
835
830
"""
836
- function extract_realizations (
831
+ function values_as_in_model (
837
832
model:: Model ,
838
833
varinfo:: AbstractVarInfo = VarInfo (),
839
834
context:: AbstractContext = DefaultContext (),
840
835
)
841
- context = RealizationExtractorContext (context)
836
+ context = ValuesAsInModelContext (context)
842
837
evaluate!! (model, varinfo, context)
843
838
return context. values
844
839
end
845
- function extract_realizations (
840
+ function values_as_in_model (
846
841
rng:: Random.AbstractRNG ,
847
842
model:: Model ,
848
843
varinfo:: AbstractVarInfo = VarInfo (),
849
844
context:: AbstractContext = DefaultContext (),
850
845
)
851
- return extract_realizations (model, varinfo, SamplingContext (rng, context))
846
+ return values_as_in_model (model, varinfo, SamplingContext (rng, context))
852
847
end
0 commit comments