|
| 1 | +""" |
| 2 | + ParamsWithStats |
| 3 | +
|
| 4 | +A struct which contains parameter values extracted from a `VarInfo`, along with any |
| 5 | +statistics associated with the VarInfo. The statistics are provided as a NamedTuple and are |
| 6 | +optional. |
| 7 | +
|
| 8 | + ParamsWithStats( |
| 9 | + varinfo::AbstractVarInfo, |
| 10 | + model::Model, |
| 11 | + stats::NamedTuple=NamedTuple(); |
| 12 | + include_colon_eq::Bool=true, |
| 13 | + include_log_probs::Bool=true, |
| 14 | + ) |
| 15 | +
|
| 16 | +Generate a `ParamsWithStats` by re-evaluating the given `model` with the provided `varinfo`. |
| 17 | +Re-evaluation of the model is often necessary to obtain correct parameter values as well as |
| 18 | +log probabilities. This is especially true when using linked VarInfos, i.e., when variables |
| 19 | +have been transformed to unconstrained space, and if this is not done, subtle correctness |
| 20 | +bugs may arise: see, e.g., https://github.com/TuringLang/Turing.jl/issues/2195. |
| 21 | +
|
| 22 | +`include_colon_eq` controls whether variables on the left-hand side of `:=` are included in |
| 23 | +the resulting parameters. |
| 24 | +
|
| 25 | +`include_log_probs` controls whether log probabilities (log prior, log likelihood, and log |
| 26 | +joint) are added to the resulting statistics NamedTuple. |
| 27 | +
|
| 28 | + ParamsWithStats( |
| 29 | + varinfo::AbstractVarInfo, |
| 30 | + ::Nothing, |
| 31 | + stats::NamedTuple=NamedTuple(); |
| 32 | + include_log_probs::Bool=true, |
| 33 | + ) |
| 34 | +
|
| 35 | +There is one case where re-evaluation is not necessary, which is when the VarInfos all |
| 36 | +already contain `DynamicPPL.ValuesAsInModelAccumulator`. This accumulator stores values |
| 37 | +as seen during the model evaluation, so the values can be simply read off. In this case, |
| 38 | +`model` can be set to `nothing`, and no re-evaluation will be performed. However, it is the |
| 39 | +caller's responsibility to ensure that `ValuesAsInModelAccumulator` is indeed |
| 40 | +present. |
| 41 | +
|
| 42 | +`include_log_probs` controls whether log probabilities (log prior, log likelihood, and log |
| 43 | +joint) are added to the resulting statistics NamedTuple. |
| 44 | +""" |
| 45 | +struct ParamsWithStats{P<:OrderedDict{VarName,Any},S<:NamedTuple} |
| 46 | + params::P |
| 47 | + stats::S |
| 48 | + |
| 49 | + function ParamsWithStats( |
| 50 | + varinfo::AbstractVarInfo, |
| 51 | + model::DynamicPPL.Model, |
| 52 | + stats::NamedTuple=NamedTuple(); |
| 53 | + include_colon_eq::Bool=true, |
| 54 | + include_log_probs::Bool=true, |
| 55 | + ) |
| 56 | + accs = if include_log_probs |
| 57 | + ( |
| 58 | + DynamicPPL.LogPriorAccumulator(), |
| 59 | + DynamicPPL.LogLikelihoodAccumulator(), |
| 60 | + DynamicPPL.ValuesAsInModelAccumulator(include_colon_eq), |
| 61 | + ) |
| 62 | + else |
| 63 | + (DynamicPPL.ValuesAsInModelAccumulator(include_colon_eq),) |
| 64 | + end |
| 65 | + varinfo = DynamicPPL.setaccs!!(varinfo, accs) |
| 66 | + varinfo = last(DynamicPPL.evaluate!!(model, varinfo)) |
| 67 | + params = DynamicPPL.getacc(varinfo, Val(:ValuesAsInModel)).values |
| 68 | + if include_log_probs |
| 69 | + stats = merge( |
| 70 | + stats, |
| 71 | + ( |
| 72 | + logprior=DynamicPPL.getlogprior(varinfo), |
| 73 | + loglikelihood=DynamicPPL.getloglikelihood(varinfo), |
| 74 | + logjoint=DynamicPPL.getlogjoint(varinfo), |
| 75 | + ), |
| 76 | + ) |
| 77 | + end |
| 78 | + return new{typeof(params),typeof(stats)}(params, stats) |
| 79 | + end |
| 80 | + |
| 81 | + function ParamsWithStats( |
| 82 | + varinfo::AbstractVarInfo, |
| 83 | + ::Nothing, |
| 84 | + stats::NamedTuple=NamedTuple(); |
| 85 | + include_log_probs::Bool=true, |
| 86 | + ) |
| 87 | + params = DynamicPPL.getacc(varinfo, Val(:ValuesAsInModel)).values |
| 88 | + if include_log_probs |
| 89 | + has_prior_acc = DynamicPPL.hasacc(varinfo, Val(:LogPrior)) |
| 90 | + has_likelihood_acc = DynamicPPL.hasacc(varinfo, Val(:LogLikelihood)) |
| 91 | + if has_prior_acc |
| 92 | + stats = merge(stats, (logprior=DynamicPPL.getlogprior(varinfo),)) |
| 93 | + end |
| 94 | + if has_likelihood_acc |
| 95 | + stats = merge(stats, (loglikelihood=DynamicPPL.getloglikelihood(varinfo),)) |
| 96 | + end |
| 97 | + if has_prior_acc && has_likelihood_acc |
| 98 | + stats = merge(stats, (logjoint=DynamicPPL.getlogjoint(varinfo),)) |
| 99 | + end |
| 100 | + end |
| 101 | + return new{typeof(params),typeof(stats)}(params, stats) |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +""" |
| 106 | + to_chains( |
| 107 | + Tout::Type{<:AbstractChains}, |
| 108 | + params_and_stats::AbstractArray{<:ParamsWithStats} |
| 109 | + ) |
| 110 | +
|
| 111 | +Convert an array of `ParamsWithStats` to a chains object of type `Tout`. |
| 112 | +
|
| 113 | +This function is not implemented here but rather in package extensions for individual chains |
| 114 | +packages. |
| 115 | +""" |
| 116 | +function to_chains end |
0 commit comments