Distributions¶
Runtime distributions for the reference evaluator.
distributions
¶
Runtime distributions for the reference evaluator.
A :class:DistributionValue evaluates to a :class:RuntimeDistribution: the
family's name and its named arguments as host values, with no host library
behind it. Sampling and log densities go through a backend. The reference
backend here implements the scalar and simplex families in plain Python, so
the reference machine can run a model end to end and be compared against
an independent oracle; a host provider may install a backend that covers
every family through its own library.
DistributionBackend
¶
Bases: Protocol
What a host must supply to sample and score a family.
sample
¶
Draw one value.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family's source name.
TYPE:
|
arguments
|
The named parameters as host values.
TYPE:
|
rng
|
The generator to draw from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
The drawn host value. |
Source code in src/quivers/qiec/distributions.py
log_prob
¶
Evaluate the log density at a value.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family's source name.
TYPE:
|
arguments
|
The named parameters as host values.
TYPE:
|
value
|
The point evaluated.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The log density. |
Source code in src/quivers/qiec/distributions.py
log_mass
¶
The log of the total mass of a construction.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family's source name.
TYPE:
|
arguments
|
The named parameters as host values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Zero for a probability measure; the log mass of a restriction
or of a mixture of restrictions, which |
Source code in src/quivers/qiec/distributions.py
RuntimeDistribution
dataclass
¶
RuntimeDistribution(family: str, arguments: Mapping[str, object], batch: tuple[int, ...] = (), event: tuple[int, ...] = (), ranks: Mapping[str, int] = MappingProxyType({}), natural: tuple[int, ...] = ())
A constructed distribution, as host data.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family's source name.
TYPE:
|
arguments
|
The named parameters as host values; frozen on construction.
TYPE:
|
batch
|
The plate's batch extents, outermost first; each position is an independent draw scored on its own.
TYPE:
|
event
|
The plate's extended event extents, outermost first; draws along them join one event and are scored together.
TYPE:
|
ranks
|
Each parameter's own tensor rank, from the family registry, so an argument carrying plate dimensions can be told from one that does not.
TYPE:
|
natural
|
The family's own event extents, so a scalar given for a tensor parameter can be broadcast to the shape the family expects.
TYPE:
|
plated
property
¶
Whether the distribution is constructed over a plate.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
__post_init__
¶
Freeze the arguments so a distribution value cannot drift.
Source code in src/quivers/qiec/distributions.py
at
¶
at(position: tuple[int, ...]) -> RuntimeDistribution
The distribution at one position of the plate, unplated.
| PARAMETER | DESCRIPTION |
|---|---|
position
|
A position of the leading plate axes, outermost first; fewer positions than axes leave the remaining axes in place.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RuntimeDistribution
|
The family with its arguments selected at the position and the selected axes dropped from the plate. |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the position has more entries than the plate has axes. |
Source code in src/quivers/qiec/distributions.py
sample
¶
Draw one value through the installed backend.
| PARAMETER | DESCRIPTION |
|---|---|
rng
|
The generator to draw from; the module generator by default,
which :func:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
The drawn host value: one draw of the family, or nested tuples of draws over the plate's batch and event axes. |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the backend cannot sample the family. |
Source code in src/quivers/qiec/distributions.py
log_mass
¶
The log of the distribution's total mass through the installed backend.
| RETURNS | DESCRIPTION |
|---|---|
float
|
Zero for a probability measure; over a plate, the sum of the masses at every position, since the plate's draw is their product. |
Source code in src/quivers/qiec/distributions.py
log_prob
¶
Evaluate the log density at a value through the installed backend.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The point evaluated; over a plate, nested tuples of points shaped like the plate.
TYPE:
|
keep_batch
|
Whether to keep one weight per batch position rather than total them.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
The log density as a float, or, with |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the backend cannot score the family, or the point is not shaped like the plate. |
Source code in src/quivers/qiec/distributions.py
ReferenceBackend
¶
Plain-Python sampling and scoring for every family of the registry.
The scalar core lives beside the backend and the structured and
compositional families in :mod:quivers.qiec.reference_families; a
family outside the tables is reported by name rather than approximated,
so a host provider can be installed for it.
families
property
¶
The families this backend implements.
| RETURNS | DESCRIPTION |
|---|---|
frozenset[str]
|
The family names. |
sample
¶
Draw one value from an implemented family.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family's source name.
TYPE:
|
arguments
|
The named parameters as host values.
TYPE:
|
rng
|
The generator to draw from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
The drawn value: a float, an int, a bool, or a tuple of floats. |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the family is not implemented here or a parameter is missing or malformed. |
Source code in src/quivers/qiec/distributions.py
log_prob
¶
Evaluate the log density of an implemented family.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family's source name.
TYPE:
|
arguments
|
The named parameters as host values.
TYPE:
|
value
|
The point evaluated.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The log density, |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the family is not implemented here or a parameter or the point is malformed. |
Source code in src/quivers/qiec/distributions.py
log_mass
¶
The log of the total mass of a construction.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family's source name.
TYPE:
|
arguments
|
The named parameters as host values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The family's log mass, zero for every probability family. |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the family is not implemented here. |
Source code in src/quivers/qiec/distributions.py
tensor_rank
¶
The nesting depth of a host tensor.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A host value: a scalar, or a tuple of values of one rank.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Zero for a scalar, one more than the first entry's rank for a tuple; an empty tuple has rank one. |
Source code in src/quivers/qiec/distributions.py
seed_reference_rng
¶
Reseed the generator reference draws use by default.
| PARAMETER | DESCRIPTION |
|---|---|
seed
|
The seed; two runs seeded alike draw alike.
TYPE:
|
distribution_backend
¶
distribution_backend() -> DistributionBackend
The backend distribution values currently sample and score through.
| RETURNS | DESCRIPTION |
|---|---|
DistributionBackend
|
The installed backend; the reference backend until replaced. |
Source code in src/quivers/qiec/distributions.py
install_distribution_backend
¶
install_distribution_backend(backend: DistributionBackend) -> DistributionBackend
Replace the backend distribution values sample and score through.
| PARAMETER | DESCRIPTION |
|---|---|
backend
|
The backend to install, such as one over a host library that covers every family.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DistributionBackend
|
The backend that was installed before, so a caller can restore it. |