Skip to content

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:A and 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 is v[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}_K of :math:K \times K correlation matrices, parameterized via the Cholesky factor for numerical stability. The accompanying cholesky_quad_form deterministic morphism reconstructs a full covariance :math:\Sigma = \mathrm{diag}(s)\, R\, \mathrm{diag}(s) from a correlation :math:R and a per-component scale :math:s \in \mathrm{PositiveReals}(K).

  • Truncated. Given a base family :math:F and 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:|A|, the number of independent rows: the product of index_shape when that is given.

TYPE: int

family

Per-row distribution family.

TYPE: ContinuousMorphism

domain

The program's input space (broadcast conditioning).

TYPE: AnySpace DEFAULT: None

index_shape

The extents of the plate's axes, outermost first, when the plate ranges over more than one axis; None for the single axis (index_size,).

TYPE: tuple[int, ...] | None DEFAULT: None

RAISES DESCRIPTION
ValueError

If index_shape does not multiply out to index_size.

Source code in src/quivers/continuous/plate.py
def __init__(
    self,
    index_size: int,
    family: ContinuousMorphism,
    domain: AnySpace | None = None,
    *,
    index_shape: tuple[int, ...] | None = None,
) -> None:
    shape = (index_size,) if index_shape is None else tuple(index_shape)
    if not shape or math.prod(shape) != index_size:
        raise ValueError(
            f"PlateDraw: index_shape {shape} does not multiply out to "
            f"index_size {index_size}"
        )
    # A matrix-valued space (a Cholesky factor, a covariance) keeps
    # `dim` as its matrix size and states its flat element count as
    # `shape`; other continuous spaces state only `dim`.
    per_row_shape: tuple[int, ...]
    if isinstance(family.codomain, _CLASS_SPACES):
        per_row_shape = ()
        per_row_dim = 1
    elif isinstance(family.codomain, _MATRIX_SPACES):
        per_row_shape = family.codomain.shape
        per_row_dim = math.prod(per_row_shape)
    elif isinstance(family.codomain, ContinuousSpace):
        per_row_shape = family.codomain.event_shape
        per_row_dim = math.prod(per_row_shape)
    else:
        per_row_shape = tuple(family.codomain.shape)
        per_row_dim = (
            int(torch.tensor(per_row_shape).prod().item()) if per_row_shape else 1
        )
    explicit_shape = (
        isinstance(family.codomain, Euclidean)
        and family.codomain.intrinsic_shape is not None
    )
    if (
        per_row_shape == (1,)
        and family.support.event_dim == 0
        and not explicit_shape
    ):
        per_row_shape = ()
    # `plate_rows` keeps the row structure of the flattened
    # codomain recoverable: a consumer that reads the variable as
    # a *parameter* needs the per-row width, since the plate axis
    # is a batch axis in that position rather than an event axis.
    # The rows a gather along the outermost axis selects are the
    # slabs over the remaining axes, so that axis is the codomain's
    # row count.
    flat_codomain = Euclidean(
        name=f"plate({'x'.join(map(str, shape))}x{family.codomain!s})",
        dim=index_size * per_row_dim,
        plate_rows=shape[0],
    )
    actual_domain = domain if domain is not None else family.domain
    super().__init__(actual_domain, flat_codomain)
    self._index_size = index_size
    self._index_shape = shape
    self._family = family
    from quivers.continuous.inline import FixedDistribution

    self._fixed_parameters = isinstance(family, FixedDistribution)
    self._per_row_shape = per_row_shape
    self._per_row_dim = per_row_dim
    # Width of the conditioning input a single row of the family
    # consumes. Fixed families ignore it (they read only the row
    # count); conditional families feed it through their parameter
    # network, so it must equal the domain's feature width.
    dom = actual_domain
    if hasattr(dom, "dim"):
        self._domain_width = max(
            1, int(dom.row_width if isinstance(dom, Euclidean) else dom.dim)
        )
    elif hasattr(dom, "shape"):
        shape = tuple(dom.shape)
        self._domain_width = int(torch.tensor(shape).prod().item()) if shape else 1
    else:
        self._domain_width = 1

index_shape property

index_shape: tuple[int, ...]

The extents of the plate's axes, outermost first.

RETURNS DESCRIPTION
tuple[int, ...]

The extents.

rsample

rsample(x: Tensor | None = None, sample_shape: Size = Size()) -> Tensor

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: Tensor DEFAULT: None

sample_shape

Leading independent sampling dimensions.

TYPE: Size DEFAULT: Size()

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
def rsample(
    self,
    x: torch.Tensor | None = None,
    sample_shape: torch.Size = torch.Size(),
) -> torch.Tensor:
    """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.

    Parameters
    ----------
    x : Tensor
        Resolved response-family parameters, shared or one row per observation.
    sample_shape : torch.Size
        Leading independent sampling dimensions.

    Returns
    -------
    Tensor
        Response draws with retained sample, observation, array, and event axes.

    Raises
    ------
    ValueError
        If selected indices or observation and parameter layouts violate the plate contract.
    """
    del sample_shape  # plate latents are batch-invariant
    device = x.device if x is not None else None
    # Draw ``|A|`` independent rows by sampling the per-row family
    # with a leading sample axis of length ``|A|`` from a single
    # conditioning row. Threading the row count through
    # ``sample_shape`` rather than the input's batch axis keeps the
    # draw genuinely independent even for families whose fixed
    # parameterization advertises a scalar batch shape (a
    # positive-support family realised as a restricted base
    # measure, say); those ignore the input's leading axis but
    # still honour the explicit sample shape.
    per_row = isinstance(
        getattr(self._family, "param_source", None), LookupSource
    ) or (
        not self._fixed_parameters
        and x is not None
        and self._index_size > 1
        and x.numel() == self._index_size * self._domain_width
    )
    if per_row:
        row_input = self._row_conditioning(x, self._index_size, device)
        base = self._family.rsample(row_input)
    else:
        row_input = self._row_conditioning(x, 1, device)
        base = self._family.rsample(
            row_input, sample_shape=torch.Size((self._index_size,))
        )
    expected = self._index_size * self._per_row_dim
    if base.numel() != expected:
        raise ValueError(
            f"plate rsample: per-row family produced {base.numel()} "
            f"elements for {self._index_size} rows of per-row width "
            f"{self._per_row_dim}; expected {expected}"
        )
    return self._to_structured(base)

canonical_latent

canonical_latent(value: Tensor) -> Tensor

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
def canonical_latent(self, value: torch.Tensor) -> torch.Tensor:
    """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.
    """
    if value.numel() != self._index_size * self._per_row_dim:
        return value
    return self._to_structured(value)

log_prob

log_prob(x: Tensor, y: Tensor) -> Tensor

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
def log_prob(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
    """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.
    """
    index_size = self._index_size
    per_row_family = self._per_row_dim
    axes = len(self._index_shape)
    structured = tuple(y.shape[:axes]) == self._index_shape
    batched = (
        y.dim() >= 2 and not structured and y.numel() != index_size * per_row_family
    )
    if not batched:
        total = y.numel()
        if total % index_size != 0:
            raise ValueError(
                f"plate latent has {total} elements, not divisible "
                f"by the plate index size {index_size}"
            )
        row_numel = total // index_size
        event_shape = (
            self._per_row_shape if row_numel == per_row_family else (row_numel,)
        )
        sample = y.reshape(index_size, *event_shape)
        row_input = self._row_conditioning(x, index_size, y.device)
        per_row_lp = self._family.log_prob(row_input, sample)
        return per_row_lp.reshape(-1).sum().unsqueeze(0)
    batch = y.shape[0]
    per_batch = y.numel() // batch
    if per_batch % index_size != 0:
        raise ValueError(
            f"batched plate latent has {per_batch} elements per "
            f"batch row, not divisible by the plate index size "
            f"{index_size}"
        )
    row_numel = per_batch // index_size
    event_shape = (
        self._per_row_shape if row_numel == per_row_family else (row_numel,)
    )
    flat = y.reshape(batch * index_size, *event_shape)
    row_input = self._row_conditioning(x, batch * index_size, y.device)
    per_row_lp = self._family.log_prob(row_input, flat)
    return per_row_lp.reshape(batch, index_size).sum(dim=-1)

log_prob_rows

log_prob_rows(x: Tensor, y: Tensor, *rows: Tensor) -> Tensor

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 log_prob reads it.

TYPE: Tensor

y

The plate's value at every element, with the plate's axes leading or flat in their order.

TYPE: Tensor

*rows

One index per plate axis, outermost first: the rows kept along that axis.

TYPE: Tensor DEFAULT: ()

RETURNS DESCRIPTION
Tensor

The summed log-density of the kept elements, shape (1,).

RAISES DESCRIPTION
ValueError

If rows does not give one index per axis, an index leaves its axis, or y does not hold every element of the plate.

Source code in src/quivers/continuous/plate.py
def log_prob_rows(
    self, x: torch.Tensor, y: torch.Tensor, *rows: torch.Tensor
) -> torch.Tensor:
    """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.

    Parameters
    ----------
    x : torch.Tensor
        The conditioning input, as `log_prob` reads it.
    y : torch.Tensor
        The plate's value at every element, with the plate's axes
        leading or flat in their order.
    *rows : torch.Tensor
        One index per plate axis, outermost first: the rows kept
        along that axis.

    Returns
    -------
    torch.Tensor
        The summed log-density of the kept elements, shape ``(1,)``.

    Raises
    ------
    ValueError
        If ``rows`` does not give one index per axis, an index leaves
        its axis, or ``y`` does not hold every element of the plate.
    """
    shape = self._index_shape
    if len(rows) != len(shape):
        raise ValueError(
            f"PlateDraw.log_prob_rows: {len(rows)} row indices for a plate "
            f"of {len(shape)} axes"
        )
    positions = torch.arange(self._index_size, device=y.device).reshape(shape)
    for axis, (index, extent) in enumerate(zip(rows, shape, strict=True)):
        if index.numel() == 0 or int(index.min()) < 0 or int(index.max()) >= extent:
            raise ValueError(
                f"PlateDraw.log_prob_rows: the rows kept along axis {axis} "
                f"must be a nonempty index into its {extent} rows"
            )
        positions = positions.index_select(axis, index.to(y.device))
    total = y.numel()
    if total % self._index_size != 0:
        raise ValueError(
            f"PlateDraw.log_prob_rows: the plate value has {total} elements, "
            f"not a whole number of rows for the plate's {self._index_size} "
            "elements; a subsampled plate latent is given at every row"
        )
    row_numel = total // self._index_size
    event_shape = (
        self._per_row_shape if row_numel == self._per_row_dim else (row_numel,)
    )
    kept = positions.reshape(-1)
    sample = y.reshape(self._index_size, *event_shape)[kept]
    row_input = self._row_conditioning(x, self._index_size, y.device)[kept]
    return self._family.log_prob(row_input, sample).reshape(-1).sum().unsqueeze(0)

gather

gather(indices: Tensor) -> Tensor

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
def gather(self, indices: torch.Tensor) -> torch.Tensor:
    """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)``.
    """
    return self.rsample()[indices]

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: ContinuousMorphism

response

Observed values r_obs of shape (N, *codom.shape). A one-dimensional continuous codomain also accepts (N,), which gains its event axis in _align_response; a discrete codomain takes (N,) indices and has no event axis.

TYPE: Tensor

array_shape

Independent coordinates after the outer observation axis.

TYPE: tuple[int, ...] DEFAULT: ()

array_is_batch

Retain these coordinates in pointwise scores when the checked distribution declares them batch axes rather than event coordinates.

TYPE: bool DEFAULT: False

Source code in src/quivers/continuous/plate.py
def __init__(
    self,
    family: ContinuousMorphism,
    response: torch.Tensor,
    *,
    array_shape: tuple[int, ...] = (),
    array_is_batch: bool = False,
) -> None:
    super().__init__(family.domain, family.codomain)
    self._family = family
    self._array_shape = tuple(array_shape)
    self._array_is_batch = array_is_batch
    self.register_buffer("_response", response.detach())

array_shape property

array_shape: tuple[int, ...]

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

support

Inherit the underlying per-row family's support so guides and validators see the correct constraint for the observation.

rsample

rsample(x: Tensor, sample_shape: Size = Size()) -> Tensor

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: Tensor

sample_shape

Leading independent sampling dimensions.

TYPE: Size DEFAULT: Size()

RETURNS DESCRIPTION
Tensor

Response draws with retained sample, observation, array, and event axes.

Source code in src/quivers/continuous/plate.py
def rsample(
    self, x: torch.Tensor, sample_shape: torch.Size = torch.Size()
) -> torch.Tensor:
    """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.

    Parameters
    ----------
    x : Tensor
        Resolved response-family parameters, shared or one row per observation.
    sample_shape : torch.Size
        Leading independent sampling dimensions.

    Returns
    -------
    Tensor
        Response draws with retained sample, observation, array, and event axes.
    """
    return self._rsample(x, sample_shape, None)

rsample_event

rsample_event(x, sample_shape, 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: Tensor

sample_shape

Leading independent sampling dimensions.

TYPE: Size

event

Bound recording roles and sampling bounds; None requests ordinary response scoring.

TYPE: dict[str, object] or None

RETURNS DESCRIPTION
Tensor

Response draws with retained sample, observation, array, and event axes.

Source code in src/quivers/continuous/plate.py
def rsample_event(self, x, sample_shape, event):
    """Draw the underlying response law through the same observation layout.

    Parameters
    ----------
    x : Tensor
        Resolved response-family parameters, shared or one row per observation.
    sample_shape : torch.Size
        Leading independent sampling dimensions.
    event : dict[str, object] or None
        Bound recording roles and sampling bounds; None requests ordinary response scoring.

    Returns
    -------
    Tensor
        Response draws with retained sample, observation, array, and event axes.
    """
    return self._rsample(x, sample_shape, event)

sampling_distribution

sampling_distribution(x, event=None)

Build the underlying per-entry law using the observation layout.

PARAMETER DESCRIPTION
x

Resolved response-family parameters, shared or one row per observation.

TYPE: Tensor

event

Bound recording roles and sampling bounds; None requests ordinary response scoring.

TYPE: dict[str, object] or None DEFAULT: None

RETURNS DESCRIPTION
Distribution

Underlying response sampling law, normalized on any truncation interval.

Source code in src/quivers/continuous/plate.py
def sampling_distribution(self, x, event=None):
    """Build the underlying per-entry law using the observation layout.

    Parameters
    ----------
    x : Tensor
        Resolved response-family parameters, shared or one row per observation.
    event : dict[str, object] or None
        Bound recording roles and sampling bounds; None requests ordinary response scoring.

    Returns
    -------
    Distribution
        Underlying response sampling law, normalized on any truncation interval.
    """
    rows = self.response.shape[0]
    if x.ndim and x.shape[0] == 1 and rows > 1:
        x = x.expand(rows, *x.shape[1:])
    if self._array_shape:
        x = self._array_rows(x)
    payload = self._event_layout(
        event, self.response, flatten=bool(self._array_shape)
    )
    return self._family.sampling_distribution(x, payload)

log_prob

log_prob(x: Tensor, y: Tensor | None = None) -> Tensor

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
def log_prob(self, x: torch.Tensor, y: torch.Tensor | None = None) -> torch.Tensor:
    """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.
    """
    return self.log_prob_pointwise(x, y).sum()

log_prob_pointwise

log_prob_pointwise(x: Tensor, y: Tensor | None = None) -> Tensor

Score each observation row without summing the observation plate.

PARAMETER DESCRIPTION
x

Family parameters at each row, or shared across rows.

TYPE: Tensor

y

Observations; the registered response when omitted.

TYPE: Tensor or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

One log density per observation row. Event dimensions and each row's array axes are reduced together, as in log_prob.

Source code in src/quivers/continuous/plate.py
def log_prob_pointwise(
    self, x: torch.Tensor, y: torch.Tensor | None = None
) -> torch.Tensor:
    """Score each observation row without summing the observation plate.

    Parameters
    ----------
    x : torch.Tensor
        Family parameters at each row, or shared across rows.
    y : torch.Tensor or None
        Observations; the registered response when omitted.

    Returns
    -------
    torch.Tensor
        One log density per observation row. Event dimensions and each
        row's array axes are reduced together, as in ``log_prob``.
    """
    return self._log_prob_pointwise(x, y, None)

log_prob_event

log_prob_event(x, y, event)

Sum unweighted recorded-event likelihoods over this observation plate.

PARAMETER DESCRIPTION
x

Resolved response-family parameters, shared or one row per observation.

TYPE: Tensor

y

Observed values or censoring endpoints; None uses the registered response.

TYPE: Tensor or None

event

Bound recording roles and sampling bounds; None requests ordinary response scoring.

TYPE: dict[str, object] or None

RETURNS DESCRIPTION
Tensor

Recorded-event log likelihoods under the method's row and event reductions.

Source code in src/quivers/continuous/plate.py
def log_prob_event(self, x, y, event):
    """Sum unweighted recorded-event likelihoods over this observation plate.

    Parameters
    ----------
    x : Tensor
        Resolved response-family parameters, shared or one row per observation.
    y : Tensor or None
        Observed values or censoring endpoints; None uses the registered response.
    event : dict[str, object] or None
        Bound recording roles and sampling bounds; None requests ordinary response scoring.

    Returns
    -------
    Tensor
        Recorded-event log likelihoods under the method's row and event reductions.
    """
    return self._log_prob_pointwise(x, y, event).sum()

log_prob_pointwise_event

log_prob_pointwise_event(x, y, 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: Tensor

y

Observed values or censoring endpoints; None uses the registered response.

TYPE: Tensor or None

event

Bound recording roles and sampling bounds; None requests ordinary response scoring.

TYPE: dict[str, object] or None

RETURNS DESCRIPTION
Tensor

Recorded-event log likelihoods under the method's row and event reductions.

Source code in src/quivers/continuous/plate.py
def log_prob_pointwise_event(self, x, y, event):
    """Score recorded events with the existing row and event reductions.

    Parameters
    ----------
    x : Tensor
        Resolved response-family parameters, shared or one row per observation.
    y : Tensor or None
        Observed values or censoring endpoints; None uses the registered response.
    event : dict[str, object] or None
        Bound recording roles and sampling bounds; None requests ordinary response scoring.

    Returns
    -------
    Tensor
        Recorded-event log likelihoods under the method's row and event reductions.
    """
    return self._log_prob_pointwise(x, y, event)

log_prob_pointwise_rows

log_prob_pointwise_rows(x: Tensor, y: Tensor, *rows: Tensor) -> Tensor

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 log_prob reads them.

TYPE: Tensor

y

The observed rows: every row of the plate, or only the kept rows in the order of the index.

TYPE: Tensor

*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: Tensor DEFAULT: ()

RETURNS DESCRIPTION
Tensor

One log density per kept observation row, after event reduction.

RAISES DESCRIPTION
ValueError

If rows does not give one index per axis, keeps a subset of an array axis, or leaves its axis, or if x or y has neither the plate's rows nor the kept rows.

Source code in src/quivers/continuous/plate.py
def log_prob_pointwise_rows(
    self, x: torch.Tensor, y: torch.Tensor, *rows: torch.Tensor
) -> torch.Tensor:
    """Per-observation log densities at a selected subset of the rows.

    Parameters
    ----------
    x : torch.Tensor
        The parameters, one row per observation row or one row every
        observation shares, as `log_prob` reads them.
    y : torch.Tensor
        The observed rows: every row of the plate, or only the kept
        rows in the order of the index.
    *rows : torch.Tensor
        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.

    Returns
    -------
    torch.Tensor
        One log density per kept observation row, after event reduction.

    Raises
    ------
    ValueError
        If ``rows`` does not give one index per axis, keeps a subset of
        an array axis, or leaves its axis, or if ``x`` or ``y`` has
        neither the plate's rows nor the kept rows.
    """
    return self._log_prob_pointwise_rows(x, y, rows, None)

log_prob_pointwise_rows_event

log_prob_pointwise_rows_event(x, y, event, *rows)

Score selected recorded events with the same plate indexing rules.

PARAMETER DESCRIPTION
x

Resolved response-family parameters, shared or one row per observation.

TYPE: Tensor

y

Observed values or censoring endpoints; None uses the registered response.

TYPE: Tensor or None

event

Bound recording roles and sampling bounds; None requests ordinary response scoring.

TYPE: dict[str, object] or None

*rows

Selected plate indices, outermost observation axis first.

TYPE: tuple[Tensor, ...] DEFAULT: ()

RETURNS DESCRIPTION
Tensor

Recorded-event log likelihoods under the method's row and event reductions.

Source code in src/quivers/continuous/plate.py
def log_prob_pointwise_rows_event(self, x, y, event, *rows):
    """Score selected recorded events with the same plate indexing rules.

    Parameters
    ----------
    x : Tensor
        Resolved response-family parameters, shared or one row per observation.
    y : Tensor or None
        Observed values or censoring endpoints; None uses the registered response.
    event : dict[str, object] or None
        Bound recording roles and sampling bounds; None requests ordinary response scoring.
    *rows : tuple[Tensor, ...]
        Selected plate indices, outermost observation axis first.

    Returns
    -------
    Tensor
        Recorded-event log likelihoods under the method's row and event reductions.
    """
    return self._log_prob_pointwise_rows(x, y, rows, event)

log_prob_rows_event

log_prob_rows_event(x, y, event, *rows)

Sum recorded-event scores over selected plate rows.

PARAMETER DESCRIPTION
x

Resolved response-family parameters, shared or one row per observation.

TYPE: Tensor

y

Observed values or censoring endpoints; None uses the registered response.

TYPE: Tensor or None

event

Bound recording roles and sampling bounds; None requests ordinary response scoring.

TYPE: dict[str, object] or None

*rows

Selected plate indices, outermost observation axis first.

TYPE: tuple[Tensor, ...] DEFAULT: ()

RETURNS DESCRIPTION
Tensor

Recorded-event log likelihoods under the method's row and event reductions.

Source code in src/quivers/continuous/plate.py
def log_prob_rows_event(self, x, y, event, *rows):
    """Sum recorded-event scores over selected plate rows.

    Parameters
    ----------
    x : Tensor
        Resolved response-family parameters, shared or one row per observation.
    y : Tensor or None
        Observed values or censoring endpoints; None uses the registered response.
    event : dict[str, object] or None
        Bound recording roles and sampling bounds; None requests ordinary response scoring.
    *rows : tuple[Tensor, ...]
        Selected plate indices, outermost observation axis first.

    Returns
    -------
    Tensor
        Recorded-event log likelihoods under the method's row and event reductions.
    """
    return self._log_prob_pointwise_rows(x, y, rows, event).sum()

log_prob_rows

log_prob_rows(x: Tensor, y: Tensor, *rows: Tensor) -> Tensor

Sum the log densities of the selected observation rows.

PARAMETER DESCRIPTION
x

Shared, full-row, or selected-row family parameters.

TYPE: Tensor

y

Full or selected observation rows.

TYPE: Tensor

*rows

Kept indices for the observation axis and each array axis.

TYPE: Tensor DEFAULT: ()

RETURNS DESCRIPTION
Tensor

Total log density of the selected rows.

Source code in src/quivers/continuous/plate.py
def log_prob_rows(
    self, x: torch.Tensor, y: torch.Tensor, *rows: torch.Tensor
) -> torch.Tensor:
    """Sum the log densities of the selected observation rows.

    Parameters
    ----------
    x : torch.Tensor
        Shared, full-row, or selected-row family parameters.
    y : torch.Tensor
        Full or selected observation rows.
    *rows : torch.Tensor
        Kept indices for the observation axis and each array axis.

    Returns
    -------
    torch.Tensor
        Total log density of the selected rows.
    """
    return self.log_prob_pointwise_rows(x, y, *rows).sum()

log_likelihood

log_likelihood(theta: Tensor) -> Tensor

Alias for log_prob(theta); preserved for the Python builder API.

Source code in src/quivers/continuous/plate.py
def log_likelihood(self, theta: torch.Tensor) -> torch.Tensor:
    """Alias for ``log_prob(theta)``; preserved for the Python
    builder API."""
    return self.log_prob(theta)

marginalize_categorical

marginalize_categorical(log_probs_per_class: Tensor) -> Tensor

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
def marginalize_categorical(log_probs_per_class: torch.Tensor) -> torch.Tensor:
    """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}`.
    """
    return torch.logsumexp(log_probs_per_class, dim=-1)

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 (N, K), used in the single-axis form. List or tuple of tensors: per-axis log-likelihoods of shapes (N_m, K), used in the multi-axis form. The class axis K must agree across all entries.

TYPE: torch.Tensor or list/tuple of torch.Tensor

group_index

Single-axis form: a long tensor of shape (N,) (single index map) or a tuple of co-indexed long tensors (product index map). Multi-axis form: a list of length M, parallel to the log-likelihood list; each entry is itself either a single tensor or a product-index-map tuple, exactly as in the single-axis form.

TYPE: torch.Tensor, tuple of torch.Tensor, or list of either

log_prior_per_group_per_class

Per-(group, class) log-prior of shape (num_groups, K), (K,) (broadcast over the group axis), or, for product index maps, (num_groups_1, ..., num_groups_r, K).

TYPE: Tensor

num_groups

Cardinality of the (product) group plate.

TYPE: int or tuple[int, ...]

reduction

Per-group reduction over the class axis. logsumexp is the canonical mixture marginalisation; sum joint-scores without marginalising the class; mean averages symmetrically. Default logsumexp.

TYPE: ('logsumexp', 'sum', 'mean') DEFAULT: "logsumexp"

per_group

Whether to return the reduced per-group aggregates (|G|, *extra) (flat over a product plate) instead of their sum over the group axis. A block nested inside another grouped block contributes one aggregate per position of its group to the enclosing accumulator, so the enclosing block consumes this form.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Tensor

Scalar program-level log-density contribution, or the per-group aggregates when per_group is set.

RAISES DESCRIPTION
ValueError

If reduction is not one of the three reductions, if the index map and log-likelihood shapes disagree, if a prior has a shape that fits neither the group plate nor the class axis, or if per_group is requested for a log-likelihood with no row axis (there is no group axis to keep).

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) and num_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
def marginalize_grouped(
    log_likelihood_per_row_per_class: (
        torch.Tensor | list[torch.Tensor] | tuple[torch.Tensor, ...]
    ),
    group_index: (
        torch.Tensor
        | tuple[torch.Tensor, ...]
        | list[torch.Tensor | tuple[torch.Tensor, ...]]
    ),
    log_prior_per_group_per_class: torch.Tensor,
    num_groups: int | tuple[int, ...],
    *,
    reduction: str = "logsumexp",
    per_group: bool = False,
) -> torch.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.

    Parameters
    ----------
    log_likelihood_per_row_per_class : torch.Tensor or list/tuple of torch.Tensor
        Single tensor: a per-(response, class) log-likelihood of
        shape ``(N, K)``, used in the single-axis form. List or
        tuple of tensors: per-axis log-likelihoods of shapes
        ``(N_m, K)``, used in the multi-axis form. The class axis
        ``K`` must agree across all entries.
    group_index : torch.Tensor, tuple of torch.Tensor, or list of either
        Single-axis form: a long tensor of shape ``(N,)`` (single
        index map) or a tuple of co-indexed long tensors (product
        index map). Multi-axis form: a list of length ``M``,
        parallel to the log-likelihood list; each entry is itself
        either a single tensor or a product-index-map tuple, exactly
        as in the single-axis form.
    log_prior_per_group_per_class : torch.Tensor
        Per-(group, class) log-prior of shape ``(num_groups, K)``,
        ``(K,)`` (broadcast over the group axis), or, for product
        index maps, ``(num_groups_1, ..., num_groups_r, K)``.
    num_groups : int or tuple[int, ...]
        Cardinality of the (product) group plate.
    reduction : {"logsumexp", "sum", "mean"}
        Per-group reduction over the class axis. ``logsumexp`` is
        the canonical mixture marginalisation; ``sum`` joint-scores
        without marginalising the class; ``mean`` averages
        symmetrically. Default ``logsumexp``.
    per_group : bool
        Whether to return the reduced per-group aggregates
        ``(|G|, *extra)`` (flat over a product plate) instead of
        their sum over the group axis. A block nested inside
        another grouped block contributes one aggregate per
        position of its group to the enclosing accumulator, so
        the enclosing block consumes this form.

    Returns
    -------
    torch.Tensor
        Scalar program-level log-density contribution, or the
        per-group aggregates when ``per_group`` is set.

    Raises
    ------
    ValueError
        If ``reduction`` is not one of the three reductions, if the
        index map and log-likelihood shapes disagree, if a prior has
        a shape that fits neither the group plate nor the class
        axis, or if ``per_group`` is requested for a log-likelihood
        with no row axis (there is no group axis to keep).

    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)`` and
      ``num_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.
    """
    if isinstance(log_likelihood_per_row_per_class, (list, tuple)):
        if not isinstance(group_index, list):
            raise ValueError(
                "marginalize_grouped: multi-axis log-likelihood "
                "requires a parallel list of group_index entries; "
                f"got {type(group_index).__name__}"
            )
        if len(log_likelihood_per_row_per_class) != len(group_index):
            raise ValueError(
                "marginalize_grouped: log-likelihood list has "
                f"{len(log_likelihood_per_row_per_class)} entries "
                f"but group_index list has {len(group_index)}"
            )
        return _marginalize_grouped_multi(
            tuple(log_likelihood_per_row_per_class),
            tuple(group_index),
            log_prior_per_group_per_class,
            num_groups,
            reduction=reduction,
            per_group=per_group,
        )
    if reduction not in ("logsumexp", "sum", "mean"):
        raise ValueError(
            f"reduction must be one of 'logsumexp', 'sum', 'mean'; got {reduction!r}"
        )
    if log_likelihood_per_row_per_class.dim() < 1:
        raise ValueError(
            "log_likelihood_per_row_per_class must have at least one "
            "axis (the class axis); got "
            f"{log_likelihood_per_row_per_class.shape}"
        )
    # Two operating modes:
    #
    # 1. ``ll`` has a leading row axis (shape ``(N, *extra, K)``):
    #    scatter-add along the index map to obtain
    #    ``(G, *extra, K)``, apply the per-cell prior, reduce over
    #    the class axis, and sum over groups. The shape of the
    #    return value is ``(*extra,)`` — a scalar if there are no
    #    extra (outer-class) axes, otherwise a tensor whose axes
    #    are the outer-block class axes still in scope.
    #
    # 2. ``ll`` has no row axis (shape ``(*extra, K)``): this
    #    occurs for *intermediate* levels of a nested marginalize
    #    stack, where the innermost level has already integrated
    #    the per-row contributions. There is no scatter step; the
    #    prior is added across the extra axes and the class axis is
    #    reduced. The return shape is ``(*extra_except_class_axis,)``.
    # The mode is detected from the shape of ``group_index``.
    flat_group_index_shape: tuple[int, ...]
    if isinstance(group_index, tuple):
        flat_group_index_shape = group_index[0].shape
    else:
        flat_group_index_shape = group_index.shape
    if flat_group_index_shape:
        n_idx = flat_group_index_shape[0]
        # If the user supplied an index map of length N > 0, the ll
        # MUST carry a leading axis of length N. A length-N index map
        # paired with an ll whose leading axis differs is a shape
        # mismatch — not the "no row axis" intermediate path.
        if (
            n_idx > 0
            and log_likelihood_per_row_per_class.dim() >= 2
            and log_likelihood_per_row_per_class.shape[0] != n_idx
        ):
            raise ValueError(
                "group_index must have shape (N,) matching the "
                "leading axis of the log-likelihood; got "
                f"{flat_group_index_shape} vs N="
                f"{log_likelihood_per_row_per_class.shape[0]}"
            )
        has_row_axis = (
            n_idx > 0
            and log_likelihood_per_row_per_class.dim() >= 2
            and log_likelihood_per_row_per_class.shape[0] == n_idx
        )
    else:
        has_row_axis = False
    if not has_row_axis:
        # No N-axis: apply prior + reduce over the class axis only.
        # ``log_prior_per_group_per_class`` is broadcastable across
        # the extra axes (typically just (K,)).
        if per_group:
            raise ValueError(
                "marginalize_grouped: per_group requires a log-likelihood "
                "with a leading row axis; got shape "
                f"{tuple(log_likelihood_per_row_per_class.shape)}"
            )
        weighted = log_prior_per_group_per_class + log_likelihood_per_row_per_class
        if reduction == "logsumexp":
            return torch.logsumexp(weighted, dim=-1)
        if reduction == "sum":
            return weighted.sum(dim=-1)
        return weighted.mean(dim=-1)
    n_rows = int(log_likelihood_per_row_per_class.shape[0])
    n_classes = int(log_likelihood_per_row_per_class.shape[-1])
    extra_axes = tuple(log_likelihood_per_row_per_class.shape[1:-1])

    # Resolve single-vs-product index map into a flat group index.
    if isinstance(group_index, tuple):
        if not isinstance(num_groups, tuple):
            raise ValueError(
                "marginalize_grouped: product index map (tuple "
                "group_index) requires num_groups to be a tuple of "
                f"matching length; got {type(num_groups).__name__}"
            )
        flat_index, total_groups = _flatten_product_indices(group_index, num_groups)
        # Flatten an (G_1, ..., G_r, K) prior to (G_1 * ... * G_r, K).
        if log_prior_per_group_per_class.dim() == 1:
            log_prior_flat: torch.Tensor = log_prior_per_group_per_class
        elif log_prior_per_group_per_class.dim() == 2 and (
            log_prior_per_group_per_class.shape[0] == total_groups
            or log_prior_per_group_per_class.shape[0] == 1
        ):
            log_prior_flat = log_prior_per_group_per_class
        else:
            expected_axes = len(num_groups) + 1
            if log_prior_per_group_per_class.dim() != expected_axes:
                raise ValueError(
                    "marginalize_grouped: product-index-map prior "
                    f"must have shape (*{num_groups}, K) or (K,); "
                    f"got {tuple(log_prior_per_group_per_class.shape)}"
                )
            log_prior_flat = log_prior_per_group_per_class.reshape(
                total_groups, n_classes
            )
    else:
        if isinstance(num_groups, tuple):
            raise ValueError(
                "marginalize_grouped: scalar group_index requires "
                f"scalar num_groups; got {num_groups!r}"
            )
        if group_index.shape != (n_rows,):
            raise ValueError(
                "group_index must have shape (N,) matching the "
                "leading axis of the log-likelihood; got "
                f"{tuple(group_index.shape)} vs N={n_rows}"
            )
        if num_groups <= 0:
            raise ValueError(f"num_groups must be positive; got {num_groups}")
        demand(
            (group_index >= 0) & (group_index < num_groups),
            lambda: ValueError(
                f"group_index entries must lie in [0, {num_groups}); "
                "out-of-range index detected"
            ),
        )
        flat_index = group_index.to(torch.long)
        total_groups = int(num_groups)
        log_prior_flat = log_prior_per_group_per_class

    # Scatter-add the (N, *extra, K) per-row log-likelihood along
    # the index map to obtain a (|G|, *extra, K) per-group
    # accumulator. ``index_add`` operates on the leading axis;
    # ``extra`` axes ride along as broadcast.
    grouped_shape: tuple[int, ...] = (total_groups,) + extra_axes + (n_classes,)
    grouped = torch.zeros(
        grouped_shape,
        dtype=log_likelihood_per_row_per_class.dtype,
        device=log_likelihood_per_row_per_class.device,
    )
    grouped = grouped.index_add(0, flat_index, log_likelihood_per_row_per_class)
    # Broadcast log_prior_flat shaped ``(K,)`` or ``(G, K)``
    # against the accumulator ``(G, *extra, K)``. We insert
    # singleton axes for ``*extra`` so the broadcast is well-formed.
    prior_view = log_prior_flat
    if prior_view.dim() == 1:
        # (K,) → (1,) * len(extra) + (K,) → broadcasts against
        # (G, *extra, K).
        for _ in extra_axes:
            prior_view = prior_view.unsqueeze(0)
    elif prior_view.dim() == 2 and prior_view.shape[0] == total_groups:
        # (G, K) → (G, *(1,)*len(extra), K).
        for _ in extra_axes:
            prior_view = prior_view.unsqueeze(1)
    weighted = prior_view + grouped
    if reduction == "logsumexp":
        per_group_aggregates = torch.logsumexp(weighted, dim=-1)
    elif reduction == "sum":
        per_group_aggregates = weighted.sum(dim=-1)
    else:  # mean
        per_group_aggregates = weighted.mean(dim=-1)
    if per_group:
        return per_group_aggregates
    # Sum over the group axis; extra axes (outer-block class
    # broadcasts) pass through unchanged.
    return per_group_aggregates.sum(dim=0)

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).