Plates¶
A plate repeats a draw or observation over an index
set, such as one response per participant, as conditionally independent copies given the
parameters passed to it (Koller and Friedman, 2009; Pyro Developers, n.d.-e)
(indexed draws). The copies may still be
dependent once shared uncertain parameters are integrated out. A
PlateDraw draws one value per element of a finite set, as
let v : A <- sample F(...) does in source, and a
VectorisedObserve scores a column of responses at
once, as an indexed observe does. Plate axes index repeated draws, while
event axes index the components of
one draw. The marginalization helpers sum a finite latent out of a plate of scores.
plate
¶
Hierarchical Bayesian modelling primitives.
Adds the operations needed to express models of the kind found in
hierarchical Bayesian regression / latent-class analysis (the
canonical Stan workhorse): finite-domain-indexed draws (plates),
vectorized observations with gather indexing, LKJ priors on
correlation matrices, ordinal monotone splines via cumulative
sum, and generic distribution truncation. Each primitive is
declared with its categorical denotation in :math:\mathbf{Kern};
the runtime is a straight realisation of those denotations.
Categorical foundations
- Plate (indexed draw). Given a finite index set :math:
Aand a parameterized family :math:F : \Theta \to \mathcal{G}(B), a plate draw declares the kernel
.. math::
v : A \to B,\quad v(a) \sim F(\theta(a))
equivalently a single morphism into the function space
:math:\mathbf{1} \to \mathcal{G}(B^A) factoring as the
independent product :math:\prod_{a \in A} F(\theta(a)). Under
the natural isomorphism
:math:\mathbf{Kern}(\mathbf{1}, B^A) \cong \mathbf{Kern}(A, B)
the plate variable IS a :math:\mathbf{Kern}-morphism
:math:A \to B; in the variational backend it is held as a
tensor of shape (|A|, *B.shape) whose prior contribution to
the ELBO is the per-row log-density.
-
Gather. Given a finite index map :math:
\iota : N \to A(a per-observation grouping assignment) and a plate variable :math:v : A \to B, the gathered morphism is the pullback :math:\iota^* v = v \circ \iota : N \to B. Categorically a textbook reindexing morphism in :math:\mathbf{Kern}; runtime isv[indices]along the leading axis. -
Vectorised observe. A batched observation step
.. math::
\mathcal{S}\llbracket\,\mathsf{observe}\ r[n] \sim
F(\theta[n])\ \mathsf{for}\ n\in N\,\rrbracket :
\Phi \to \mathcal{G}_{\le 1}(\Phi)
has score :math:\prod_{n \in N} p_F(r_{\text{obs}}(n);\,
\theta(n,\phi)). The single-observation form is the
|N| = 1 special case; mass-correctness follows from the
Cho-Jacobs Markov-with-conditioning calculus.
- Marginalise (program-level). Given a previously-drawn
discrete latent :math:
c : \Phi \to \mathcal{G}(C), the marginalisation step is the pushforward through the projection :math:\pi_{\Phi \setminus C} : \Phi \times C \to \Phi:
.. math::
\mathsf{marg}(c) = \mathcal{G}(\pi_{\Phi \setminus C})
\circ \mathcal{S}\llbracket\mathsf{draw}\ c\rrbracket
numerically realised as :math:\log \sum_c \exp(\cdot) over
the :math:C axis of the per-class log-likelihoods.
-
cumsum. The deterministic morphism :math:
\mathrm{cumsum} : \mathrm{Euclidean}(K) \to \mathrm{Euclidean}(K), :math:(x_1, \dots, x_K) \mapsto (x_1, x_1 + x_2, \dots, \sum_i x_i). Used for the standard monotonic-spline parameterization of ordinal coefficients. -
LKJ correlation prior. The Lewandowski-Kurowicka-Joe distribution :math:
\mathrm{LKJ}(K, \eta)on the manifold :math:\mathrm{Corr}_Kof :math:K \times Kcorrelation matrices, parameterized via the Cholesky factor for numerical stability. The accompanyingcholesky_quad_formdeterministic morphism reconstructs a full covariance :math:\Sigma = \mathrm{diag}(s)\, R\, \mathrm{diag}(s)from a correlation :math:Rand a per-component scale :math:s \in \mathrm{PositiveReals}(K). -
Truncated. Given a base family :math:
Fand an interval :math:[a, b], the truncated family :math:F_{|[a, b]}has density :math:p_F(x) / (F_{\text{cdf}}(b) - F_{\text{cdf}}(a))on :math:[a, b]and zero elsewhere. Realised via inverse-CDF sampling when available; rejection otherwise.
References
- Cho, K. and Jacobs, B. (2019). Disintegration and Bayesian inversion via string diagrams. Mathematical Structures in Computer Science 29(7), 938–971. doi:10.1017/S0960129518000488.
- Lewandowski, D., Kurowicka, D. and Joe, H. (2009). Generating random correlation matrices based on vines and extended onion method. Journal of Multivariate Analysis 100(9), 1989–2001. doi:10.1016/j.jmva.2009.04.008.
PlateDraw
¶
PlateDraw(index_size: int, family: ContinuousMorphism, domain: AnySpace | None = None, *, index_shape: tuple[int, ...] | None = None)
Bases: ContinuousMorphism
A finite-domain-indexed draw, as a Kern-morphism A → B.
Concretely: v : A → B ~ F(theta) becomes a tensor of shape
(|A|, *B.shape) whose a-th row is an independent
:math:F(\theta_a)-distributed random variable. The variational
posterior factorizes across rows by default; the prior's ELBO
contribution is :math:\sum_a \log p_F(v_a; \theta_a).
Categorically: by the natural isomorphism
:math:\mathbf{Kern}(\mathbf{1}, B^A) \cong \mathbf{Kern}(A, B),
the plate variable IS a Kern-morphism :math:A \to B. The
PlateDraw is realised as a ContinuousMorphism whose
codomain is the flat product-space of index_size copies of
the per-row family's codomain.
The plate may range over several finite axes at once,
:math:A_1 \times \dots \times A_k: the draw is then an
independent array of shape (|A_1|, ..., |A_k|, *B.shape), one
independent F variate per element of the product.
| PARAMETER | DESCRIPTION |
|---|---|
index_size
|
Cardinality :math:
TYPE:
|
family
|
Per-row distribution family.
TYPE:
|
domain
|
The program's input space (broadcast conditioning).
TYPE:
|
index_shape
|
The extents of the plate's axes, outermost first, when the
plate ranges over more than one axis;
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/continuous/plate.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
index_shape
property
¶
The extents of the plate's axes, outermost first.
| RETURNS | DESCRIPTION |
|---|---|
tuple[int, ...]
|
The extents. |
rsample
¶
Reparameterized sample from the plate's prior.
A plate draw is batch-invariant: the latent vector is a
global model quantity shared across every row of an observed
plate. The returned tensor has shape (|A|, *B.shape)
regardless of the program input's leading batch dimension,
with each row drawn independently from the per-row family
F at its declared parameters. x supplies only the
device; it is optional so batch-invariant callers such as
gather need not fabricate one.
This is the standard Pyro / NumPyro semantic: a sample inside
a plate("subj", n_subj) context is one (n_subj,)
vector, not a per-particle (batch, n_subj) replication. The
gather arr[idx] along the plate axis then composes
cleanly with per-row observed-plate axes downstream.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Resolved response-family parameters, shared or one row per observation.
TYPE:
|
sample_shape
|
Leading independent sampling dimensions. |
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Response draws with retained sample, observation, array, and event axes. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If selected indices or observation and parameter layouts violate the plate contract. |
Source code in src/quivers/continuous/plate.py
canonical_latent
¶
Give a clamped plate value its structured (|A|, *B.shape).
A plate latent supplied as a conditioning / observed value may
arrive flattened to (|A| * prod(B.shape),) (the shape a
test point or host-data channel produces when it flattens a
matrix-valued latent). Restoring the structured shape here lets
a gather v[idx] over the plate axis yield (N, *B.shape)
with the per-row event axis intact.
A value whose element count is not exactly |A| * prod(B) is
a genuinely batched latent (a leading parameter-sample axis);
it is returned unchanged so the batched scoring path handles
it.
Source code in src/quivers/continuous/plate.py
log_prob
¶
Prior log-density of the plate sample.
The plate variable v : A -> B scores as the independent
product prod_a p_F(v_a) of the per-row family F; the
contribution is sum_a log p_F(v_a).
Two layouts are accepted. The natural plate latent carries the
plate's axes leading (or holds exactly |A| * prod(event)
elements in any layout, a flat vector included): its rows are scored
under F and reduced to a scalar, wrapped in a length-1
tensor so the downstream log-joint accumulator sees a uniform
1-d shape it can broadcast against the response plate. A
batched value carries a genuine leading batch axis distinct
from |A|; each batch element is scored independently and a
(batch,) vector of per-element densities is returned.
The per-row event size is read from the value's element count
rather than solely from the family's advertised codomain, so a
scalar family carrying an explicit event axis (over=...)
is scored over the full event without loss.
Source code in src/quivers/continuous/plate.py
log_prob_rows
¶
Prior log-density of the plate's elements at a subset of its rows.
The elements kept are the product of the rows kept along each
axis; each is scored under its row's family, as log_prob scores
it, and the scores are summed.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The conditioning input, as
TYPE:
|
y
|
The plate's value at every element, with the plate's axes leading or flat in their order.
TYPE:
|
*rows
|
One index per plate axis, outermost first: the rows kept along that axis.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The summed log-density of the kept elements, shape |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/continuous/plate.py
gather
¶
Pullback v[indices] along a finite index map.
indices is a long-tensor of shape (N,) with entries
in [0, |A|); returns a tensor of shape
(N, *B.shape).
Source code in src/quivers/continuous/plate.py
VectorisedObserve
¶
VectorisedObserve(family: ContinuousMorphism, response: Tensor, *, array_shape: tuple[int, ...] = (), array_is_batch: bool = False)
Bases: ContinuousMorphism
A batched observation step accumulating per-row log-likelihoods.
Categorically, the batched-likelihood kernel
:math:\Phi \to \mathcal{G}_{\le 1}(\Phi) whose score is
:math:\prod_{n \in N} p_F(r_{\text{obs}}(n);\, \theta(n,\phi)).
Realised as a ContinuousMorphism whose domain is the
parameter-input space (the morphism conditions on θ) and whose
codomain is the per-observation response space — so the
existing MonadicProgram observe-step machinery treats
it as an observed site and threads the score through
log_joint via the usual morph.log_prob(theta, response)
call, with log_prob here summing over the leading index axis.
The observed response tensor is registered as a buffer so the
parent program's optimizer tracks it and the runtime never
has to thread it through observations=....
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The per-observation distribution family.
TYPE:
|
response
|
Observed values
TYPE:
|
array_shape
|
Independent coordinates after the outer observation axis.
TYPE:
|
array_is_batch
|
Retain these coordinates in pointwise scores when the checked distribution declares them batch axes rather than event coordinates.
TYPE:
|
Source code in src/quivers/continuous/plate.py
array_shape
property
¶
The extents of the independent array each observation row holds.
Empty for one variate per row; (|B|, |C|) for an observation
drawn over leading over axes beyond its family's event rank.
| RETURNS | DESCRIPTION |
|---|---|
tuple[int, ...]
|
The extents. |
support
property
¶
Inherit the underlying per-row family's support so guides and validators see the correct constraint for the observation.
rsample
¶
Sample the per-observation family at the supplied θ.
x is the θ-tensor (one row per observation index); the
result is the per-observation response sample. Used in
prior-predictive simulation; never called during inference
when the response is observed.
A θ shared by every observation (a single row: literal or program-wide parameters) is drawn once per row of the plate, so the draw has the plate's rows whatever mix of shared and per-row parameters the observation reads.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Resolved response-family parameters, shared or one row per observation.
TYPE:
|
sample_shape
|
Leading independent sampling dimensions. |
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Response draws with retained sample, observation, array, and event axes. |
Source code in src/quivers/continuous/plate.py
rsample_event
¶
Draw the underlying response law through the same observation layout.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Resolved response-family parameters, shared or one row per observation.
TYPE:
|
sample_shape
|
Leading independent sampling dimensions.
TYPE:
|
event
|
Bound recording roles and sampling bounds; None requests ordinary response scoring.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Response draws with retained sample, observation, array, and event axes. |
Source code in src/quivers/continuous/plate.py
sampling_distribution
¶
Build the underlying per-entry law using the observation layout.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Resolved response-family parameters, shared or one row per observation.
TYPE:
|
event
|
Bound recording roles and sampling bounds; None requests ordinary response scoring.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Distribution
|
Underlying response sampling law, normalized on any truncation interval. |
Source code in src/quivers/continuous/plate.py
log_prob
¶
Sum of per-observation log-densities.
y defaults to the registered response buffer; passing a
different value (e.g. a clamped observation) is supported
for fast prior-predictive checks.
Source code in src/quivers/continuous/plate.py
log_prob_pointwise
¶
Score each observation row without summing the observation plate.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Family parameters at each row, or shared across rows.
TYPE:
|
y
|
Observations; the registered response when omitted.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
One log density per observation row. Event dimensions and each
row's array axes are reduced together, as in |
Source code in src/quivers/continuous/plate.py
log_prob_event
¶
Sum unweighted recorded-event likelihoods over this observation plate.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Resolved response-family parameters, shared or one row per observation.
TYPE:
|
y
|
Observed values or censoring endpoints; None uses the registered response.
TYPE:
|
event
|
Bound recording roles and sampling bounds; None requests ordinary response scoring.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Recorded-event log likelihoods under the method's row and event reductions. |
Source code in src/quivers/continuous/plate.py
log_prob_pointwise_event
¶
Score recorded events with the existing row and event reductions.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Resolved response-family parameters, shared or one row per observation.
TYPE:
|
y
|
Observed values or censoring endpoints; None uses the registered response.
TYPE:
|
event
|
Bound recording roles and sampling bounds; None requests ordinary response scoring.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Recorded-event log likelihoods under the method's row and event reductions. |
Source code in src/quivers/continuous/plate.py
log_prob_pointwise_rows
¶
Per-observation log densities at a selected subset of the rows.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The parameters, one row per observation row or one row every
observation shares, as
TYPE:
|
y
|
The observed rows: every row of the plate, or only the kept rows in the order of the index.
TYPE:
|
*rows
|
One index per plate axis of the observation, outermost first: the rows kept along the observation's rows, then every entry of each axis of its array.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
One log density per kept observation row, after event reduction. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/continuous/plate.py
log_prob_pointwise_rows_event
¶
Score selected recorded events with the same plate indexing rules.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Resolved response-family parameters, shared or one row per observation.
TYPE:
|
y
|
Observed values or censoring endpoints; None uses the registered response.
TYPE:
|
event
|
Bound recording roles and sampling bounds; None requests ordinary response scoring.
TYPE:
|
*rows
|
Selected plate indices, outermost observation axis first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Recorded-event log likelihoods under the method's row and event reductions. |
Source code in src/quivers/continuous/plate.py
log_prob_rows_event
¶
Sum recorded-event scores over selected plate rows.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Resolved response-family parameters, shared or one row per observation.
TYPE:
|
y
|
Observed values or censoring endpoints; None uses the registered response.
TYPE:
|
event
|
Bound recording roles and sampling bounds; None requests ordinary response scoring.
TYPE:
|
*rows
|
Selected plate indices, outermost observation axis first.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Recorded-event log likelihoods under the method's row and event reductions. |
Source code in src/quivers/continuous/plate.py
log_prob_rows
¶
Sum the log densities of the selected observation rows.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Shared, full-row, or selected-row family parameters.
TYPE:
|
y
|
Full or selected observation rows.
TYPE:
|
*rows
|
Kept indices for the observation axis and each array axis.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Total log density of the selected rows. |
Source code in src/quivers/continuous/plate.py
log_likelihood
¶
marginalize_categorical
¶
Program-level marginalisation over a discrete latent class.
Given per-class log-likelihoods of shape (N, K) (one row per
observation, one column per latent-class assignment), returns
the marginalised log-likelihood of shape (N,):
.. math::
\log p(r_n) = \log \sum_{k=1}^{K} p(c_n = k)\, p(r_n | c_n = k).
Realises the program-level marginalize c step as the
pushforward through :math:\pi_{\Phi \setminus C}.
Source code in src/quivers/continuous/plate.py
marginalize_grouped
¶
marginalize_grouped(log_likelihood_per_row_per_class: Tensor | list[Tensor] | tuple[Tensor, ...], group_index: Tensor | tuple[Tensor, ...] | list[Tensor | tuple[Tensor, ...]], log_prior_per_group_per_class: Tensor, num_groups: int | tuple[int, ...], *, reduction: str = 'logsumexp', per_group: bool = False) -> Tensor
Per-group marginalisation over a discrete latent class.
Two surface shapes are accepted, the second a strict generalisation of the first.
Single response axis. Given a per-(response, class)
log-likelihood tensor of shape (N, K) and an index map
:math:r : \text{Resp} \to G (or a tuple of co-indexed
index maps into a product grouping plate
:math:G_1 \times \dots \times G_r), return the scalar
.. math::
\sum_{g \in G}\, \rho\!\left[
\log \pi(g, \cdot) + \sum_{n:\, r(n) = g} \ell(n, \cdot)
\right].
Multiple heterogeneous response axes sharing a per-group
class indicator. Given M per-axis log-likelihood tensors
:math:\ell_m \in \mathbb{R}^{N_m \times K} and a parallel
list of index maps :math:r_m : \text{Resp}_m \to G, return
.. math::
\sum_{g \in G}\, \rho\!\left[
\log \pi(g, \cdot)
+ \sum_{m=1}^{M} \sum_{n:\, r_m(n) = g} \ell_m(n, \cdot)
\right].
Each per-axis log-likelihood is scatter-added into the same
(|G|, K) accumulator before the reduction. Categorically
this is the right Kan extension along the coproduct index map
:math:\coprod_m r_m : \coprod_m \text{Resp}_m \to G. The
single-axis form is the unary case (M = 1).
The reduction :math:\rho over the class axis is selected by
reduction: logsumexp for the canonical mixture
marginalisation, sum for joint scoring (no
marginalisation), mean for the symmetric average.
| PARAMETER | DESCRIPTION |
|---|---|
log_likelihood_per_row_per_class
|
Single tensor: a per-(response, class) log-likelihood of
shape
TYPE:
|
group_index
|
Single-axis form: a long tensor of shape
TYPE:
|
log_prior_per_group_per_class
|
Per-(group, class) log-prior of shape
TYPE:
|
num_groups
|
Cardinality of the (product) group plate.
TYPE:
|
reduction
|
Per-group reduction over the class axis.
TYPE:
|
per_group
|
Whether to return the reduced per-group aggregates
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar program-level log-density contribution, or the
per-group aggregates when |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Notes
Edge cases:
K == 1: the log-sum-exp collapses to the body's log-likelihood plus the constant log-prior.- Identity index map (
group_index = arange(N)andnum_groups == N): each row is its own group, recovering the per-row mixture. - Empty fibre: contributes a multiplicative identity to the
product (
log 1 = 0), so an unused group adds zero per class before the reduction. - Multi-axis with a single entry (
M = 1): exactly equivalent to the single-axis form, modulo unboxing the singleton list.
Source code in src/quivers/continuous/plate.py
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 | |
Cited sources¶
Daphne Koller and Nir Friedman. Probabilistic Graphical Models: Principles and Techniques. MIT Press, 2009. ISBN 9780262013192. URL: https://mitpress.mit.edu/9780262013192/probabilistic-graphical-models/.
Pyro Developers. Tensor shapes in Pyro. Pyro Tutorials, version 1.9.1, n.d.-e. URL: https://pyro.ai/examples/tensor_shapes.html (visited on 2026-11-06).