Description
Sometimes we know that we're not performing inference in a model, in which case it can be useful to have the model perform some additional computations.
For example, people often end up using Dirac
to save quantities that are deterministic when conditioned on the variables in a Chains
(e.g. TuringLang/Turing.jl#2059), but this can then cause issues since samplers will also consider these as random variables to be sampled. generated_quantities
provides a partial solution to this, but
- It doesn't have the same behavior as
~
, which people often want. - It requires an additional pass over the chain after sampling.
Addressing (2) is non-trivial as it requires changing both the internals of varinfos in addition to how transitions are constructed in Turing.jl.
But addressing (1) could be done by adding a simple NotInferringContext
+ a macro @isinferring
, which simply translates into a call isinferring(__context__, __varinfo__)
or something. We could then either provide an alternative to generated_quantities
which makes use of this, or we could include this in generated_quantities
somehow (I'm leaning towards the former).
An alternative way of addressing (1) is to just provide a convenient way to convert a Matrix{<:NamedTuple}
into a Chains
, which I have existing code to do, but the annoying bit with this is that it's "asking a lot" of the user (they need to explicitly filter out the variables in return
they don't want in the chain, and then also make sure to return a NamedTuple
, etc.). Hence I think I'm leaning towards supporting both approaches 🤷