Enumerative Inference
Enumerative inference can be used to compute the exact posterior distribution for a generative model with a finite number of discrete random choices, to compute a grid approximation of a continuous posterior density, or to perform stratified sampling by enumerating over discrete random choices and sampling the continuous random choices. This functionality is provided by enumerative_inference
.
Gen.enumerative_inference
— Function(traces, log_norm_weights, lml_est) = enumerative_inference(
model::GenerativeFunction, model_args::Tuple,
observations::ChoiceMap, choice_vol_iter
)
Run enumerative inference over a model
, given observations
and an iterator over choice maps and their associated log-volumes (choice_vol_iter
), specifying the choices to be iterated over. An iterator over a grid of choice maps and log-volumes can be constructed with choice_vol_grid
.
Return an array of traces and associated log-weights with the same shape as choice_vol_iter
. The log-weight of each trace is normalized, and corresponds to the log probability of the volume of sample space that the trace represents. Also return an estimate of the log marginal likelihood of the observations (lml_est
).
All addresses in the observations
choice map must be sampled by the model when given the model arguments. The same constraint applies to choice maps enumerated over by choice_vol_iter
, which must also avoid sharing addresses with the observations
. When the choice maps in choice_vol_iter
do not fully specify the values of all unobserved random choices, the unspecified choices are sampled from the internal proposal distribution of the model.
To construct a rectangular grid of choice maps and their associated log-volumes to iterate over, use the choice_vol_grid
function.
Gen.choice_vol_grid
— Functionchoice_vol_grid((addr, vals, [support, dims])::Tuple...; anchor=:midpoint)
Given tuples of the form (addr, vals, [support, dims])
, construct an iterator over tuples of the form (choices::ChoiceMap, log_vol::Real)
via grid enumeration.
Each addr
is an address of a random choice, and vals
are the corresponding values or intervals to enumerate over. The (optional) support
denotes whether each random choice is :discrete
(default) or :continuous
. This controls how the grid is constructed:
support = :discrete
: The grid iterates over each value invals
.support = :continuous
anddims == Val(1)
: The grid iterates over the anchors of 1D intervals whose endpoints are given byvals
.support = :continuous
anddims == Val(N)
whereN
> 1: The grid iterates over the anchors of multi-dimensional regions definedvals
, which is a tuple of interval endpoints for each dimension.
Continuous choices are assumed to have dims = Val(1)
dimensions by default. The anchor
keyword argument controls which point in each interval is used as the anchor (:left
, :right
, or :midpoint
).
The log-volume log_vol
associated with each set of choices
in the grid is given by the log-product of the volumes of each continuous region used to construct those choices. If all addresses enumerated over are :discrete
, then log_vol = 0.0
.
When the space of possible choice maps is not rectangular (e.g. some addresses only exist depending on the values of other addresses), iterators over choice maps and log-volumes can be also be manually constructed.