Selections
A selection represents a set of addresses of random choices. Selections allow users to specify to which subset of the random choices in a trace a given inference operation should apply.
An address that is added to a selection indicates that either the random choice at that address should be included in the selection, or that all random choices made by a generative function traced at that address should be included. For example, consider the following selection:
selection = select(:x, :y)
If we use this selection in the context of a trace of the function baz
below, we are selecting two random choices, at addresses :x
and :y
:
@gen function baz()
@trace(bernoulli(0.5), :x)
@trace(bernoulli(0.5), :y)
end
If we use this selection in the context of a trace of the function bar
below, we are actually selecting three random choices–-the one random choice made by bar
at address :x
and the two random choices made by foo
at addresses :y => :z
and :y => :w
:
@gen function foo()
@trace(normal(0, 1), :z)
@trace(normal(0, 1), :w)
end
@gen function bar()
@trace(bernoulli(0.5), :x)
@trace(foo(), :y)
end
There is an abstract type for selections:
Gen.Selection
— TypeSelection
Abstract type for selections of addresses.
All selections implement the following methods:
Base.in(addr, selection)
Is the address selected?
Base.getindex(selection, addr)
Get the subselection at the given address.
Base.isempty(selection)
Is the selection guaranteed to be empty?
get_address_schema(T)
Return a shallow, compile-time address schema, where T
is the concrete type of the selection.
There are various concrete types for selections, each of which is a subtype of Selection
. Users can construct selections with the following methods:
Gen.select
— Functionselection = select(addrs...)
Return a selection containing a given set of addresses.
Examples:
selection = select(:x, "foo", :y => 1 => :z)
selection = select()
selection = select(:x => 1, :x => 2)
Gen.selectall
— Functionselection = selectall()
Construct a selection that includes all random choices.
Gen.complement
— Functioncomp_selection = complement(selection::Selection)
Return a selection that is the complement of the given selection.
An address is in the selection if it is not in the complement selection.
The select
method returns a selection with concrete type DynamicSelection
. The selectall
method returns a selection with concrete type AllSelection
.
The full list of concrete types of selections is shown below. Most users need not worry about these types. Note that only selections of type DynamicSelection
are mutable (using push!
and set_subselection!
).
Gen.EmptySelection
— Typestruct EmptySelection <: Selection end
A singleton type for a selection that is always empty.
Gen.AllSelection
— Typestruct AllSelection <: Selection end
A singleton type for a selection that contains all choices at or under an address.
Gen.HierarchicalSelection
— Typeabstract type HierarchicalSelection <: Selection end
Abstract type for selections that have a notion of sub-selections.
get_subselections(selection::HierarchicalSelection)
Return an iterator over pairs of addresses and subselections at associated addresses.
Gen.DynamicSelection
— Typestruct DynamicSelection <: HierarchicalSelection .. end
A hierarchical, mutable, selection with arbitrary addresses.
Can be mutated with the following methods:
Base.push!(selection::DynamicSelection, addr)
Add the address and all of its sub-addresses to the selection.
Example:
selection = select()
@assert !(:x in selection)
push!(selection, :x)
@assert :x in selection
set_subselection!(selection::DynamicSelection, addr, other::Selection)
Change the selection status of the given address and its sub-addresses that defined by other
.
Example:
selection = select(:x)
@assert :x in selection
subselection = select(:y)
set_subselection!(selection, :x, subselection)
@assert (:x => :y) in selection
@assert !(:x in selection)
Note that set_subselection!
does not copy data in other
, so other
may be mutated by a later calls to set_subselection!
for addresses under addr
.
Gen.StaticSelection
— Typestruct StaticSelection{T,U} <: HierarchicalSelection .. end
A hierarchical selection whose keys are among its type parameters.
Gen.ComplementSelection
— Typestruct ComplementSelection <: Selection end
A hierarchical selection that is the complement of the given selection. An address is in the selection if it is not in the complement selection.
Gen.get_address_schema
— Functionschema = get_address_schema(::Type{T}) where {T <: ChoiceMap}
Return the (top-level) address schema for the given choice map.