Handlers¶
Conditioning, intervention, reweighting, blocking, replay, and lifting.
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])
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.
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, which forwards sites it has no value for and ignores values at sites the program never samples. |
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.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Site name to the value the site is fixed to.
TYPE:
|
Source code in src/quivers/effects/do.py
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.
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
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 each site the trace holds with the recorded
value and scores it under the site's current distribution, so replay
fixes the values and not the densities: the joint of the replayed run is
the model's density at the recorded values.
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 the trace lacks and 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. |