Handlers¶
These handlers cover conditioning, intervention, reweighting, subsampling, blocking, replay, and
lifting. Conditioning fixes a
site's value and adds its likelihood to the score. Intervention (do) replaces the mechanism that
generates a site, which changes which density terms belong to the model. Replay takes values from
an earlier trace, scaling
multiplies score terms, and subsample scores a plate's
sites at a subset of its rows, rescaled, for
minibatch stochastic variational inference.
clamp
¶
Clamp handler: condition named sample sites on given values.
ClampHandler is the effect-stack analogue of Pyro's condition. A
site whose name appears in its data is answered with the given value
and scored under its own distribution, so the value contributes the
site's density to the joint and the trace records the site as observed.
The name avoids collision with the top-level
quivers.inference.conditioning.condition
factory, which returns a Conditioned model wrapper.
ClampHandler
¶
ClampHandler(data: dict[str, Tensor], *, missing: MissingValuePolicy = FORWARD)
Bases: EffectHandler
Condition named sample sites on given values.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Site name to the value the site is clamped to. Other sites pass through untouched under the default missing-value policy.
TYPE:
|
missing
|
Whether an unprovided random request is forwarded or refused. ERROR also consults the run's declared observations, including computed and scanned values, and refuses a genuinely missing random value.
TYPE:
|
Source code in src/quivers/effects/clamp.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install a conditioning handler of the random instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The conditioning handler with the selected missing-value policy. Values at sites the program never samples are ignored. |
Source code in src/quivers/effects/clamp.py
clamp
¶
clamp(data: dict[str, Tensor]) -> ClampHandler
Return a ClampHandler conditioning on the given site values.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Site name to value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ClampHandler
|
The handler. |
Source code in src/quivers/effects/clamp.py
do
¶
Intervention handler: fix named sites without scoring them.
DoHandler implements Pearl's do operator on the effect stack. A
site whose name appears in its data is answered with the given value and
contributes nothing to the joint: under intervention its distribution
is replaced by a point mass. Downstream sites see the intervened value,
and a trace records the site as deterministic.
DoHandler
¶
DoHandler(data: dict[str, Tensor])
Bases: EffectHandler
Intervene on named sites.
An intervened site takes the value given and contributes no factor of its own to the joint. A response whose factor a score step holds loses that factor in the score: the rest of its block, the class prior and the other responses, keeps its own.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Site name to the value the site is fixed to.
TYPE:
|
Source code in src/quivers/effects/do.py
site_values
property
¶
site_values: dict[str, Tensor]
The sites the handler fixes, which is all it does.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Site name to value. |
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install an intervening handler of the random instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared; each intervened site's address is recorded on it, so a trace marks the site deterministic.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The intervening handler. |
Source code in src/quivers/effects/do.py
scale
¶
Scale handler: multiply every density contribution by a factor.
ScaleHandler is a transformer of the program's score instance:
every contribution any site makes passes through it on the way to the
run's accumulator, multiplied by the factor. Stacking scale with
mask composes the two elementwise products.
ScaleHandler
¶
Bases: EffectHandler
Multiply every density contribution by a scalar.
| PARAMETER | DESCRIPTION |
|---|---|
factor
|
The multiplier.
TYPE:
|
Source code in src/quivers/effects/scale.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install a scaling transformer of the score instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The transformer. |
Source code in src/quivers/effects/scale.py
scale
¶
scale(factor: float) -> ScaleHandler
Return a ScaleHandler with the given factor.
| PARAMETER | DESCRIPTION |
|---|---|
factor
|
The multiplier.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ScaleHandler
|
The handler. |
Source code in src/quivers/effects/scale.py
mask
¶
Mask handler: gate every density contribution elementwise.
MaskHandler is a transformer of the program's score instance: the
mask is broadcast against every contribution on its way to the run's
accumulator. A boolean mask switches entries; a float mask scales them.
MaskHandler
¶
MaskHandler(mask_tensor: Tensor)
Bases: EffectHandler
Gate every density contribution elementwise.
| PARAMETER | DESCRIPTION |
|---|---|
mask_tensor
|
The mask, broadcast against each contribution. For a call on an
input of
TYPE:
|
Source code in src/quivers/effects/mask.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install a masking transformer of the score instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The transformer. |
Source code in src/quivers/effects/mask.py
mask
¶
mask(mask_tensor: Tensor) -> MaskHandler
Return a MaskHandler with the given mask.
| PARAMETER | DESCRIPTION |
|---|---|
mask_tensor
|
The mask.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MaskHandler
|
The handler. |
Source code in src/quivers/effects/mask.py
subsample
¶
Subsampled plates: score a plate's sites at a subset of its rows, rescaled.
A program whose observations live in a plate scores every row of the plate on every run. Stochastic variational inference (Hoffman et al. 2013) scores a random subset of the rows instead and multiplies their contribution by the plate's size over the subset's size, which keeps the objective and its gradient unbiased.
SubsampleHandler does this for one plate. Within its block, every site
whose batch axes include the plate, a plate latent or an observation, is
scored at the subset's rows only, and its contribution is multiplied by
N / B for a plate of N rows and a subset of B. A site outside
the plate, a global latent among them, is scored in full and unscaled, and
so is a score step that declares no units, which is a factor of the
whole run. Handlers for
several plates compose: a site in two subsampled plates is scored at the
product of the two subsets and scaled by the product of the two factors,
and a site in one of two sibling plates by its own plate's factor alone.
A score step that declares its units
(UnitFactorization)
is a sum over them, and a subsampled plate keeps a subset of the units when
the plate is a unit axis or no unit has rows both inside and outside the
subset. Each unit kept is scaled by the number of units over the number
kept (see unit_coefficients).
A subset that splits a unit is refused: the joint score of rows that share a
summed-out latent is not a sum over them.
Within a run a subsampled plate keeps its declared extent: its latents are drawn at every row and the program's lets compute every row, so the rows outside the subset keep well-defined values and only their scores are left out. An observation may be given at the plate's extent or at the subset's size, its rows then read in the order of the index; a plate latent is given at the plate's extent.
The guides that factor over a plate's rows score a latent in a subsampled
plate at the same rows with the same factor
(AutoNormalGuide,
AmortizedGuide, whose encoder
still reads every row of its datum, and
AutoDeltaGuide, whose density
is constant); every other guide refuses such a latent, since its density
does not split over the rows. MCMC, the Laplace approximation, annealed
importance sampling, and the Bayesian lift target the exact posterior and
refuse to run inside the block.
SubsampleHandler
¶
SubsampleHandler(plate: str, index: Tensor)
Bases: EffectHandler
Score one plate's sites at a subset of its rows, rescaled.
| PARAMETER | DESCRIPTION |
|---|---|
plate
|
The plate's name: the object its sites range over, as the
program declares it (
TYPE:
|
index
|
The rows scored: a nonempty one-dimensional integer tensor of distinct rows in increasing order.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/effects/subsample.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Register the plate's subset on the run.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
No lexical handler: the run scores the plate's sites at the subset as it builds them. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If another handler on the stack subsamples the same plate, no site of the program ranges over the plate, or the index leaves the plate. |
Source code in src/quivers/effects/subsample.py
plate_extents
¶
plate_extents(run: RunContext) -> dict[str, int]
The extent of every plate a run's sites range over.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, int]
|
Each plate's number of rows, by name, read from the
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If two sites give one plate different extents. |
Source code in src/quivers/effects/subsample.py
site_plates
¶
site_plates(program: MonadicProgram, spec: Draw | Observe) -> tuple[tuple[str, int | None], ...]
The plates a draw or observe step's site ranges over.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program the step belongs to.
TYPE:
|
spec
|
The step. |
| RETURNS | DESCRIPTION |
|---|---|
tuple[tuple[str, int], ...]
|
The site's plate axes, outermost first, as |
Source code in src/quivers/effects/subsample.py
subsampled_rows
¶
subsampled_rows(site: str, axes: tuple[tuple[str, int | None], ...], subsets: dict[str, Tensor]) -> tuple[tuple[Tensor, ...], float] | None
The rows a site is scored at, and its factor, under subsampled plates.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site's label, for the diagnostic.
TYPE:
|
axes
|
The site's plate axes, outermost first, as
TYPE:
|
subsets
|
The rows kept of each subsampled plate, by name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[tuple[Tensor, ...], float] | None
|
One index per plate axis (the subset's rows along a subsampled
axis, every row along another) and the product over the
subsampled axes of the axis's extent over the subset's size;
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a subsampled plate of the site has no fixed extent, or the subset leaves it. |
Source code in src/quivers/effects/subsample.py
active_subsets
¶
active_subsets() -> dict[str, Tensor]
The rows kept of each plate the current thread's stack subsamples.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
The index of each subsampled plate, by name. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If two active handlers subsample one plate. |
Source code in src/quivers/effects/subsample.py
subsample
¶
subsample(plate: str, index: Tensor) -> SubsampleHandler
Return a SubsampleHandler scoring a plate at a subset of its rows.
| PARAMETER | DESCRIPTION |
|---|---|
plate
|
The plate's name.
TYPE:
|
index
|
The rows scored, distinct and in increasing order.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SubsampleHandler
|
The handler, used as a context manager around the calls that
score the program (an |
Source code in src/quivers/effects/subsample.py
active_subsamples
¶
active_subsamples() -> tuple[SubsampleHandler, ...]
The subsample handlers on the current thread's stack, outer first.
| RETURNS | DESCRIPTION |
|---|---|
tuple[SubsampleHandler, ...]
|
The active handlers. |
Source code in src/quivers/effects/subsample.py
refuse_subsampling
¶
Refuse a subsampled plate in a consumer of the exact posterior.
| PARAMETER | DESCRIPTION |
|---|---|
consumer
|
The consumer's name, for the diagnostic.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If any subsample handler is active. |
Source code in src/quivers/effects/subsample.py
block
¶
Block handler: hide sites from the handlers outside it.
BlockHandler answers hidden sites itself, drawing and scoring them
exactly as the run's default handler would, so no handler outside it
sees their requests: an outer trace does not record them and an outer
clamp cannot condition them. Every other site forwards.
BlockHandler
¶
Bases: EffectHandler
Hide sites from outer handlers.
| PARAMETER | DESCRIPTION |
|---|---|
hide
|
Site names to hide; every site when both arguments are absent.
TYPE:
|
expose
|
Site names to leave visible, hiding every other.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If both |
Source code in src/quivers/effects/block.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install a blocking handler of the random instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The blocking handler. |
Source code in src/quivers/effects/block.py
block
¶
block(hide: list[str] | None = None, expose: list[str] | None = None) -> BlockHandler
Return a BlockHandler hiding or exposing the named sites.
| PARAMETER | DESCRIPTION |
|---|---|
hide
|
Site names to hide.
TYPE:
|
expose
|
Site names to leave visible.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BlockHandler
|
The handler. |
Source code in src/quivers/effects/block.py
replay
¶
Replay handler: reinstall the values a trace recorded.
ReplayHandler answers sites with their recorded values unless the
current run gives an explicit value at the site. It scores each replayed
value under the site's current distribution.
ReplayHandler
¶
ReplayHandler(trace: Trace)
Bases: EffectHandler
Replay the values of a recorded trace.
| PARAMETER | DESCRIPTION |
|---|---|
trace
|
The trace whose site values are reinstalled.
TYPE:
|
Source code in src/quivers/effects/replay.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install a scoring replay handler of the random instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The replay handler, which forwards sites already given to the run and sites the trace lacks. It ignores recorded sites the program never samples. |
Source code in src/quivers/effects/replay.py
replay
¶
replay(trace: Trace) -> ReplayHandler
Return a ReplayHandler replaying the given trace.
| PARAMETER | DESCRIPTION |
|---|---|
trace
|
The trace.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReplayHandler
|
The handler. |
Source code in src/quivers/effects/replay.py
lift
¶
Lift handler: draw every learned parameter from a prior.
LiftHandler handles the program's param instance: each parameter a
morphism reads through Param.get is answered with a draw from a
Normal(value, prior_scale) prior centred on its current value, one
draw per parameter per run, and the morphism runs under the drawn
values. The draws are kept in sampled_params keyed by the site and
the parameter's qualified name.
LiftHandler
¶
Bases: EffectHandler
Draw every reachable parameter from a Normal prior.
| PARAMETER | DESCRIPTION |
|---|---|
prior_scale
|
Standard deviation of the Normal prior around each parameter's current value.
TYPE:
|
Source code in src/quivers/effects/lift.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install a prior-drawing handler of the param instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared, whose parameter store holds each parameter's current value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The handler, which answers each parameter's first read with a fresh draw and later reads with the same draw. |
Source code in src/quivers/effects/lift.py
lift
¶
lift(prior_scale: float = 1.0) -> LiftHandler
Return a LiftHandler with the given prior scale.
| PARAMETER | DESCRIPTION |
|---|---|
prior_scale
|
Standard deviation of the prior.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LiftHandler
|
The handler. |