Selections

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
end

@gen function bar()
    @trace(bernoulli(0.5), :x)
    @trace(foo(), :y)
end

There is an abstract type for selections:

Gen.SelectionType.
abstract type Selection end

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.

source

There are various concrete types for selections, each of which is a subtype of Selection. Users can construct selections with the following methods:

Gen.selectFunction.
selection = 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)
source
Gen.selectallFunction.
selection = selectall()

Construct a selection that includes all random choices.

source
Gen.complementFunction.
comp_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.

source

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!).

EmptySelection
AllSelection
HierarchicalSelection
DynamicSelection
StaticSelection
ComplementSelection