Overview¶
Effect handlers for probabilistic programs, executed as lexical kernel handlers.
effects
¶
Algebraic effect handlers for probabilistic programs.
Effect handlers in the Pyro poutine / NumPyro handlers shape,
grounded in the algebraic-effects calculus of
Plotkin and Pretnar 2009
and its probabilistic application in
Scibior et al. 2018 and
Nguyen et al. 2023, and
executed as lexical handlers of the Quivers Indexed Effect Core: a
MonadicProgram runs on the reference machine as the kernel computation
quivers.effects.program_module encodes it to, and every handler here
is a handler of that computation's random, score, or param
instance.
The EffectHandler ABC and the thread-local handler stack live in
quivers.effects.base. Concrete handlers (TraceHandler,
ClampHandler, DoHandler, MaskHandler, ScaleHandler,
BlockHandler, ReplayHandler, LiftHandler, CollapseHandler)
live in per-file modules and are re-exported here along with their
short-name factories. The reparam subpackage collects the
reparameterisation strategies.
The clamp handler is the effect-stack analogue of Pyro's
condition; the name avoids collision with the top-level
quivers.inference.conditioning.condition
factory, which returns a
Conditioned
model wrapper. Both compose observations onto a model; pick
clamp when writing a handler stack, condition when writing a
top-level Conditioned object.
The handler-aware interpreter run_program lives in
quivers.effects.interpreter; the thin quivers.inference.trace.trace
wrapper stacks a TraceHandler on and returns the recorded trace.
CONJUGATE_SOLVERS
module-attribute
¶
CONJUGATE_SOLVERS: Mapping[tuple[str, str], Solver] = {('Normal', 'Normal'): normal_normal, ('Beta', 'Bernoulli'): beta_bernoulli, ('Gamma', 'Poisson'): gamma_poisson, ('Dirichlet', 'Categorical'): dirichlet_categorical}
The closed-form solvers, keyed by (prior family, likelihood family).
SiteKey
¶
The dynamic address of one occurrence of a site: its static identity, its dynamic address frames, and its resumption path.
Solver
¶
Solver = Callable[[Mapping[str, Tensor], Tensor, TorchSampleable, Tensor], Tensor]
A closed-form marginal: given the parent's parameters, the parent's drawn value, the child's sampleable at that value, and the child's value, the log marginal density of the child's value.
SiteAddress
¶
The kernel's dynamic address of one occurrence of a site.
Contribution
dataclass
¶
One score contribution that reached the run's accumulator.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The address of the site the contribution was derived from.
TYPE:
|
role
|
What the contribution was for, from the emitting handler's provenance: a scored draw, a conditioned observation, a replay.
TYPE:
|
weight
|
The contribution as it arrived, after every transformer.
TYPE:
|
path
|
The structural path of the request's origin; a score step of the program has the path of its step, a derived contribution the path it was generated at.
TYPE:
|
RunContext
dataclass
¶
RunContext(kernel: ProgramKernel, validator: RuntimeValidator, contributions: list[Contribution] = list(), interventions: set[SiteKey] = set(), annotations: dict[str, dict[str, object]] = dict(), parameters: dict[str, Tensor] = dict(), observations: dict[str, Tensor] = dict(), supplied: dict[str, Tensor] = dict())
What one run shares with the handlers installed on it.
| PARAMETER | DESCRIPTION |
|---|---|
kernel
|
The program's kernel encoding.
TYPE:
|
validator
|
The host value validator every handler's clauses check with.
TYPE:
|
contributions
|
The score contributions that reached the accumulator, in order.
TYPE:
|
interventions
|
The sites an intervention fixed, by address.
TYPE:
|
annotations
|
Per-site annotations handlers attach, keyed by site label.
TYPE:
|
parameters
|
The learned parameters of the program's morphisms by qualified name, as the parameter store answers them.
TYPE:
|
observations
|
The run's observations at declared sites, by site label.
TYPE:
|
supplied
|
Values standing in for let bindings, by name, which their steps read rather than compute.
TYPE:
|
result_type
property
¶
result_type: TypeExpr
The type of the program's result.
| RETURNS | DESCRIPTION |
|---|---|
TypeExpr
|
The entry computation's result type, which every transparent handler answers with. |
key_of
¶
key_of(request: RuntimeRequest) -> SiteKey
The address of the site a request belongs to.
| PARAMETER | DESCRIPTION |
|---|---|
request
|
A sample request, or a score request derived from one.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SiteKey
|
The request's own address for a sample request; for a derived request, the address of the site it was derived from. |
Source code in src/quivers/effects/base.py
Installation
dataclass
¶
Installation(instance: NamedEffectInstance, runtime: RuntimeHandler)
One lexical handler a stack entry installs on a run.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
The instance the handler handles: the program's
TYPE:
|
runtime
|
The executable handler, whose definition joins the run's module.
TYPE:
|
named
¶
named(name: str) -> Installation
The installation with its handler declared under a name.
A module's handler names and identities are unique, while the prelude factories name every handler of a kind alike and derive its identity from the stack entry that made it; a run renames each installation it makes and derives its identity from the name, so two runs installing the same kinds of handler in the same order build the same module.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The declaration's name, unique within the run.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Installation
|
The installation with the renamed declaration, the rename carried into any per-installation factory the handler has. |
Source code in src/quivers/effects/base.py
EffectHandler
¶
Bases: ABC
A handler of a program's canonical instances, stacked by with.
Subclasses implement :meth:install, which builds the lexical
handlers the entry contributes to one run, and may implement
:meth:finish, which runs after the program returns with the run's
context. The handler activates by being used as a context manager:
with clamp({"z": z_val}):
samples = predictive.rsample(x)
Nested with blocks stack handlers outer-first; the innermost
handler of an instance is installed closest to the program.
__enter__
¶
Push the handler on the thread's stack.
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The handler, so |
__exit__
¶
Pop the handler off the thread's stack.
| PARAMETER | DESCRIPTION |
|---|---|
exc_type
|
The exception type leaving the block, if any.
TYPE:
|
exc
|
The exception, if any.
TYPE:
|
tb
|
Its traceback, if any.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the handler is not the top of the stack, which means the blocks were exited out of order. |
Source code in src/quivers/effects/base.py
install
abstractmethod
¶
install(run: RunContext) -> tuple[Installation, ...]
Build the lexical handlers this entry installs on a run.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The handlers, each naming the instance it handles. |
Source code in src/quivers/effects/base.py
finish
¶
finish(run: RunContext, output: object) -> None
Observe the completed run.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run, with every contribution the accumulator received.
TYPE:
|
output
|
The program's output.
TYPE:
|
Source code in src/quivers/effects/base.py
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
CheckedProgram
¶
CheckedProgram(domain: AnySpace, codomain: AnySpace, module: QiecModule, name: str)
Bases: ContinuousMorphism
A program run as its elaborated computation on the reference machine.
| PARAMETER | DESCRIPTION |
|---|---|
domain
|
The program's declared domain.
TYPE:
|
codomain
|
The program's declared codomain.
TYPE:
|
module
|
The checked module holding the program's entry point.
TYPE:
|
name
|
The program's name.
TYPE:
|
Source code in src/quivers/effects/checked_program.py
observed_names
¶
The names the program observes.
| RETURNS | DESCRIPTION |
|---|---|
set[str]
|
The labels of its |
Source code in src/quivers/effects/checked_program.py
rsample
¶
rsample(x: Tensor, sample_shape: Size = Size(), observations: Mapping[str, Tensor] | None = None) -> Tensor
Run the program forward, once per input row and sample.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input. Shape
TYPE:
|
sample_shape
|
Additional leading sample dimensions; each element is one independent run. |
observations
|
The program's data by parameter name, and values for the sample sites the runs condition on; every other site is drawn.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The returned values, shape |
Source code in src/quivers/effects/checked_program.py
has_conditional_density
¶
No: a program's density at its output marginalizes its draws.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Always |
Source code in src/quivers/effects/checked_program.py
log_prob
¶
Log-probability is not defined for a program's output alone.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input.
TYPE:
|
y
|
Program output.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
Always: the density at the output marginalizes every draw. |
Source code in src/quivers/effects/checked_program.py
log_joint
¶
The joint log-density at given values for every site.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input. Shape
TYPE:
|
intermediates
|
The program's data by parameter name and a value for every sample site, by label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The log joint of each input row's run. Shape |
| RAISES | DESCRIPTION |
|---|---|
ExecutionFailure
|
If a site the program reaches is given no value, or a given site is never reached. |
Source code in src/quivers/effects/checked_program.py
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
CollapseHandler
¶
Bases: EffectHandler
Integrate conjugate parents out of the joint.
| PARAMETER | DESCRIPTION |
|---|---|
pairs
|
Parent site name to child site name.
TYPE:
|
Source code in src/quivers/effects/collapse.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install the collapsing handler of the random instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared, whose observations supply each child's value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The handler, which draws parents without scoring them and scores children under the pair's marginal. |
Source code in src/quivers/effects/collapse.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
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
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
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
HostStep
dataclass
¶
HostStep(instance: EffectInstanceId, effect: EffectRef, kind: str, spec: Step | None, environment: tuple[str, ...], answer_type: TypeExpr, site: str | None = None)
One host part of the program, as the runtime provider serves it.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
The
TYPE:
|
effect
|
The applied
TYPE:
|
kind
|
TYPE:
|
spec
|
The program's step record, or
TYPE:
|
environment
|
The names of the locals the step reads, in the order the environment tuple carries them.
TYPE:
|
answer_type
|
The type the host part returns.
TYPE:
|
site
|
The site label of a draw step, else
TYPE:
|
ProgramKernel
dataclass
¶
ProgramKernel(module: QiecModule, program: MonadicProgram, steps: tuple[HostStep, ...], sites: tuple[str, ...], site_types: dict[str, TypeExpr], data: tuple[str, ...], random: NamedEffectInstance, score: NamedEffectInstance, param: NamedEffectInstance, result_type: TypeExpr, locals: dict[str, Local] = dict())
A program's computation with the record the runtime needs to serve it.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The kernel module: the interfaces, the instances, and the entry computation, without handlers; a run adds its own.
TYPE:
|
program
|
The program encoded.
TYPE:
|
steps
|
The host steps, in body order.
TYPE:
|
sites
|
The site label of every draw, in order.
TYPE:
|
site_types
|
The value type of each site, by label.
TYPE:
|
data
|
The names of the host data inputs after the program's input, in parameter order.
TYPE:
|
random
|
The canonical
TYPE:
|
score
|
The canonical
TYPE:
|
param
|
The canonical
TYPE:
|
result_type
|
The entry computation's result type.
TYPE:
|
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
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
TorchSampleable
dataclass
¶
TorchSampleable(morphism: ContinuousMorphism, input: Tensor, declared: ContinuousMorphism | None = None)
A morphism applied to an input, offered to the kernel as a sampleable.
| PARAMETER | DESCRIPTION |
|---|---|
morphism
|
The morphism the site draws from, under whatever parameters the
run's
TYPE:
|
input
|
The input tensor the morphism is conditioned on, carrying the batch axis.
TYPE:
|
declared
|
The morphism as the program declares it, which a trace records and a strategy reads the family of; the drawing morphism itself when no parameter store rebinds it.
TYPE:
|
__post_init__
¶
rsample
¶
rsample() -> Tensor
Draw a reparameterised sample.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
One draw per batch row. |
log_prob
¶
parameters
¶
parameters() -> dict[str, Tensor]
The distribution parameters the morphism computes at the input.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
The named parameters of the torch distribution the morphism
builds, or |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the morphism exposes neither a distribution builder nor a location-scale pair. |
Source code in src/quivers/effects/sites.py
TraceHandler
¶
Bases: EffectHandler
Record every site visited during a program's execution.
Produces a Trace whose sites dict is keyed by variable name,
whose output is the program's return value, and whose
log_joint is the sum of every site's log_prob. A
TraceHandler is single-use: run the program under one instance,
read trace, then discard. The handler takes no configuration.
Source code in src/quivers/effects/trace_handler.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install observers of the sites and of the let and score steps.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
One forwarding observer of the |
Source code in src/quivers/effects/trace_handler.py
finish
¶
finish(run: RunContext, output: object) -> None
Build the trace from the recorded events and the run's contributions.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run, with every contribution the accumulator received.
TYPE:
|
output
|
The program's output, which the trace does not read; the
wrapper that stacks this handler sets
TYPE:
|
Source code in src/quivers/effects/trace_handler.py
total_log_joint
¶
Sum every site's log_prob into the joint density.
The accumulator uses the narrowest right-aligned shape shared by the nonzero site contributions. Wider axes are summed before addition so broadcasting cannot duplicate scalar terms. Zero contributions do not determine the target shape but are still added.
| PARAMETER | DESCRIPTION |
|---|---|
batch_size
|
Leading extent of the program input. Not consulted: the joint's shape follows from the sites themselves, and a program whose sites reduce their own plate axes has a joint narrower than its input.
TYPE:
|
device
|
Device the accumulator seeds on.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The joint log-density. |
Source code in src/quivers/effects/trace_handler.py
SampleSite
dataclass
¶
SampleSite(name: str, morphism: ContinuousMorphism | None, value: Tensor, log_prob: Tensor, is_observed: bool = False, is_deterministic: bool = False, sampleable: TorchSampleable | None = None, address: SiteAddress | None = None, metadata: dict[str, object] = dict())
Record of a single site in a program trace.
Holds a torch.Tensor per site; not a value type.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Variable name bound at this site.
TYPE:
|
morphism
|
The distribution morphism (
TYPE:
|
value
|
The sampled, observed, or computed value.
TYPE:
|
log_prob
|
The density the site contributed to the run's score, after every score transformer on the stack. Zero for let bindings and for intervened sites.
TYPE:
|
is_observed
|
Whether this site was conditioned on an observed value.
TYPE:
|
is_deterministic
|
Whether this is a let binding, a score step, or an intervened site.
TYPE:
|
sampleable
|
The sampleable the site's request carried, with the morphism and
the input it was conditioned on;
TYPE:
|
address
|
The kernel's dynamic address of the site's request, which is one identity for the run's trace and for every handler that answered it.
TYPE:
|
metadata
|
Annotations handlers attached to the site.
TYPE:
|
Trace
dataclass
¶
Trace(sites: dict[str, SampleSite] = dict(), output: Tensor | dict[str, Tensor] | None = None, log_joint: Tensor | None = None)
Complete execution trace of a monadic program.
Mutable accumulator: sites grows as the program executes;
not a value type.
| PARAMETER | DESCRIPTION |
|---|---|
sites
|
All sites keyed by variable name.
TYPE:
|
output
|
The program's return value. |
log_joint
|
Sum of log-densities across all sites. Shape
TYPE:
|
stochastic_sites
property
¶
stochastic_sites: dict[str, SampleSite]
The sites that are not deterministic.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, SampleSite]
|
Every sampled or observed site. |
latent_sites
property
¶
latent_sites: dict[str, SampleSite]
The sites that are neither observed nor deterministic.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, SampleSite]
|
Every sampled latent site. |
observed_sites
property
¶
observed_sites: dict[str, SampleSite]
The observed sites.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, SampleSite]
|
Every site conditioned on an observation. |
ConjugateReparam
¶
Bases: Reparam
Score a child site under the conjugate marginal of its parent.
Applied to the child site of a conjugate pair, the strategy answers
the site with the run's observation for it, or a draw from the
child's own sampleable when there is none, and scores it under the
closed-form marginal that integrates the parent out, read from the
parent's parameters as the orchestrator saw them. The parent's own
density is left in the joint; collapse the parent with
quivers.effects.collapse when the marginal joint is wanted.
| PARAMETER | DESCRIPTION |
|---|---|
parent
|
The parent site's name.
TYPE:
|
parent_family
|
The parent's family.
TYPE:
|
child_family
|
The child's family.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the pair has no registered marginal. |
Source code in src/quivers/effects/reparam/conjugate.py
apply
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite the child site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The child site.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The value and its marginal log density. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the orchestrator has not seen the parent site. |
ValueError
|
If the parent or the child is not of the declared family. |
Source code in src/quivers/effects/reparam/conjugate.py
LocScaleReparam
¶
Bases: Reparam
Non-centred rewrite for location-scale sample sites.
The site's morphism must expose a _get_params(x) method returning
(loc, scale) tensors of the same shape, the convention every
ConditionalNormal-shaped family in
quivers.continuous.families follows.
| PARAMETER | DESCRIPTION |
|---|---|
centered
|
Interpolation between fully centred (
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/effects/reparam/loc_scale.py
apply
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite one site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The value and its log density under |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the site's morphism exposes no |
Source code in src/quivers/effects/reparam/loc_scale.py
NeuTraReparam
¶
NeuTraReparam(autoguide: Guide)
Bases: Reparam
Reparameterise a site through a trained normalizing-flow guide.
| PARAMETER | DESCRIPTION |
|---|---|
autoguide
|
A trained
TYPE:
|
Source code in src/quivers/effects/reparam/neutra.py
apply
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite one site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The guide's draw for the site, or the site's own draw when the guide does not cover it, and its log density under the site's sampleable; scoring uses the original sampleable throughout, so a partially covered NeuTra still yields a well-defined joint. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the guide covers the site but its draw does not carry it. |
Source code in src/quivers/effects/reparam/neutra.py
Reparam
¶
Bases: ABC
Base class for site-level reparameterisation strategies.
A strategy is a policy, not a handler. The ReparamOrchestrator
handler dispatches each covered site to the matching strategy and
installs the strategy's value and density on the site. Subclasses
override :meth:apply.
apply
abstractmethod
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite one site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site, with a value fixed when one is given.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The value the site takes, in the site's original space so downstream sites see the intended value, and the density a downstream sampler should score the site with. |
Source code in src/quivers/effects/reparam/base.py
ReparamOrchestrator
¶
ReparamOrchestrator(strategies: dict[str, Reparam])
Bases: EffectHandler
Dispatch sample sites to per-site reparam strategies.
| PARAMETER | DESCRIPTION |
|---|---|
strategies
|
Site name to strategy.
TYPE:
|
Source code in src/quivers/effects/reparam/base.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install the dispatching handler of the random instance.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
The handler, which answers covered sites and forwards the rest. |
Source code in src/quivers/effects/reparam/base.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
SiteRequest
dataclass
¶
SiteRequest(name: str, sampleable: TorchSampleable, given: Tensor | None = None, seen: Mapping[str, TorchSampleable] = None, observation: Tensor | None = None)
What a strategy is given for one sample site.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The site's name.
TYPE:
|
sampleable
|
The site's distribution at its input.
TYPE:
|
given
|
A value the site is to take, when an enclosing strategy or a
caller fixed one;
TYPE:
|
seen
|
The sampleables of the sites the orchestrator saw before this one, by name, so a strategy may read a parent's parameters.
TYPE:
|
observation
|
The run's observation for the site, if any.
TYPE:
|
with_value
¶
with_value(value: Tensor) -> SiteRequest
The same request with a value fixed.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value the site is to take.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SiteRequest
|
The request with |
Source code in src/quivers/effects/reparam/base.py
TransformReparam
¶
TransformReparam(bijector: Bijector)
Bases: Reparam
Reparameterise a sample site through a fixed bijector.
The reparameterised base sample lives in the bijector's domain and the site's value in its codomain. The joint density remains that of the original distribution: the strategy scores the value under the site's own sampleable, and the bijector reshapes the sampling geometry a downstream sampler works in.
| PARAMETER | DESCRIPTION |
|---|---|
bijector
|
The measurable bijection
TYPE:
|
Source code in src/quivers/effects/reparam/transform.py
apply
¶
apply(site: SiteRequest) -> tuple[Tensor, Tensor]
Rewrite one site.
| PARAMETER | DESCRIPTION |
|---|---|
site
|
The site.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
The value, drawn from the site's sampleable unless given, and its log density under the sampleable. |
Source code in src/quivers/effects/reparam/transform.py
family_of
¶
family_of(sampleable: TorchSampleable) -> str
The registry family a sampleable's morphism draws from.
| PARAMETER | DESCRIPTION |
|---|---|
sampleable
|
The sampleable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The family's registry name. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the morphism's class is registered for no family. |
Source code in src/quivers/effects/conjugate.py
normal_normal
¶
normal_normal(parent: Mapping[str, Tensor], parent_value: Tensor, child: TorchSampleable, value: Tensor) -> Tensor
The marginal of y ~ Normal(z, tau) under z ~ Normal(mu, sigma).
| PARAMETER | DESCRIPTION |
|---|---|
parent
|
The parent's
TYPE:
|
parent_value
|
The parent's drawn value.
TYPE:
|
child
|
The child at the parent's drawn value, whose
TYPE:
|
value
|
The child's value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the child's location is not the parent's value. |
Source code in src/quivers/effects/conjugate.py
beta_bernoulli
¶
beta_bernoulli(parent: Mapping[str, Tensor], parent_value: Tensor, child: TorchSampleable, value: Tensor) -> Tensor
The marginal of y ~ Bernoulli(z) under z ~ Beta(a, b).
| PARAMETER | DESCRIPTION |
|---|---|
parent
|
The parent's
TYPE:
|
parent_value
|
The parent's drawn value.
TYPE:
|
child
|
The child at the parent's drawn value.
TYPE:
|
value
|
The child's value, zero or one.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the child's probability is not the parent's value. |
Source code in src/quivers/effects/conjugate.py
gamma_poisson
¶
gamma_poisson(parent: Mapping[str, Tensor], parent_value: Tensor, child: TorchSampleable, value: Tensor) -> Tensor
The marginal of y ~ Poisson(z) under z ~ Gamma(alpha, beta).
| PARAMETER | DESCRIPTION |
|---|---|
parent
|
The parent's
TYPE:
|
parent_value
|
The parent's drawn value.
TYPE:
|
child
|
The child at the parent's drawn value.
TYPE:
|
value
|
The child's count.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The negative binomial log mass with |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the child's rate is not the parent's value. |
Source code in src/quivers/effects/conjugate.py
dirichlet_categorical
¶
dirichlet_categorical(parent: Mapping[str, Tensor], parent_value: Tensor, child: TorchSampleable, value: Tensor) -> Tensor
The marginal of y ~ Categorical(z) under z ~ Dirichlet(alpha).
| PARAMETER | DESCRIPTION |
|---|---|
parent
|
The parent's
TYPE:
|
parent_value
|
The parent's drawn value.
TYPE:
|
child
|
The child at the parent's drawn value.
TYPE:
|
value
|
The child's category.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the child's probabilities are not the parent's value. |
Source code in src/quivers/effects/conjugate.py
conjugate_solver
¶
conjugate_solver(parent_family: str, child_family: str) -> Solver
The solver of a conjugate pair.
| PARAMETER | DESCRIPTION |
|---|---|
parent_family
|
The parent's family.
TYPE:
|
child_family
|
The child's family.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Solver
|
The closed-form marginal. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the pair has no registered marginal. |
Source code in src/quivers/effects/conjugate.py
run_program
¶
run_program(program: MonadicProgram, x: Tensor, observations: Mapping[str, Tensor] | None = None) -> Tensor | dict[str, Tensor]
Execute a program on the reference machine under the handler stack.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
Program to execute.
TYPE:
|
x
|
Program input. Shape
TYPE:
|
observations
|
Values to condition observed sites on, keyed by site name. Undeclared keys are host data the program's steps read by name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor or dict[str, Tensor]
|
The program's return value: the single returned variable, or the returned variables keyed by their labels. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If an observation names one variable of a destructuring draw. |
Source code in src/quivers/effects/interpreter.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
program_kernel
¶
program_kernel(program: MonadicProgram, data: Sequence[str] = ()) -> ProgramKernel
Encode a program as a kernel module.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program.
TYPE:
|
data
|
The names of the host data inputs a run supplies beside the program's input, in parameter order.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ProgramKernel
|
The module and the record the runtime serves it by. |