Skip to content

Predictive

Predictive draws posterior predictive samples: it repeatedly draws latents from a fitted guide or a Markov chain Monte Carlo (MCMC) chain and runs the model forward from them, recording every site (predictive sampling). In a regression, the draws carry both the uncertainty in the coefficients and the variation of the response (Gelman et al., 2013), and can be compared with observations or summarized as predictive intervals.

posterior_expectations instead reads each response's native conditional law at the retained joint posterior draws. Recorded chains keep separate chain and draw axes; categorical and ordinal responses return category probabilities. Other responses use the shared analytic or explicitly budgeted finite-mean policy. Upstream observed and lexical values remain conditioned, and observed densities are disabled during this replay. Exact Scan occurrences and their execution order follow the raw likelihood collector's addressing convention. See source conditional effects for the checked model adapter and row-axis contract.

predictive

Posterior predictive sampling.

Given a trained posterior representation (either a variational guide or an MCMC chain), Predictive repeatedly samples latents from the posterior and traces the model forward to produce posterior predictive draws of every site.

Predictive

Predictive(model: MonadicProgram, posterior: Guide | MCMCResult | PriorDraws, num_samples: int | None = None)

Posterior predictive sampler.

Accepts either a trained Guide (variational posterior), an MCMCResult (Monte Carlo posterior), or independent PriorDraws from the retained prior law. Variational case: draws num_samples fresh guide samples. MCMC case: iterates over the recorded posterior draws (one forward trace per draw, up to num_samples if specified).

An input of B rows is B independent runs of the model. A guide's Guide.rsample gives each run its own draw of a site that is not plated, (batch, *constrained_shape), and one draw of a plated site that every run shares. An MCMC draw, at LatentSite.constrained_shape, is one value every run shares.

PARAMETER DESCRIPTION
model

Generative model.

TYPE: MonadicProgram

posterior

Trained posterior representation.

TYPE: Guide or MCMCResult or PriorDraws

num_samples

Number of predictive draws. Defaults to 100 for guides and num_chains * num_samples for MCMC results (use all recorded posterior draws). If supplied as an explicit integer for an MCMC result it is capped at the available draw count.

TYPE: int DEFAULT: None

RAISES DESCRIPTION
TypeError

If posterior is not a supported guide or recorded draw container.

ValueError

If num_samples is not a positive integer, or the posterior has no retained draws.

Source code in src/quivers/inference/predictive.py
def __init__(
    self,
    model: MonadicProgram,
    posterior: Guide | MCMCResult | PriorDraws,
    num_samples: int | None = None,
) -> None:
    if not isinstance(posterior, (Guide, MCMCResult, PriorDraws)):
        raise TypeError(
            f"Predictive: draws must be Guide or MCMCResult or PriorDraws; "
            f"got {type(posterior).__name__}"
        )
    self.model = model
    self.posterior = posterior
    self.num_samples = sample_count(
        num_samples,
        available=(
            posterior.num_chains * posterior.num_samples
            if isinstance(posterior, (MCMCResult, PriorDraws))
            else None
        ),
    )

__call__

__call__(x: Tensor, observations: dict[str, Tensor] | None = None) -> dict[str, Tensor]

Draw posterior predictive samples.

PARAMETER DESCRIPTION
x

Program input. Shape (batch, ...).

TYPE: Tensor

observations

Additional observed data to condition on.

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

RETURNS DESCRIPTION
dict[str, Tensor]

One key per site, holding the stacked trace values: (num_samples, batch, *shape) for a site that is not plated, and for a plated site (num_samples, *shape) for one run and (num_samples, batch, *shape) for batch > 1 runs. Guide and MCMC posteriors produce the same shapes.

RAISES DESCRIPTION
ValueError

If an MCMCResult holds draws of a name that is not a sample site of the model.

Source code in src/quivers/inference/predictive.py
@torch.no_grad()
def __call__(
    self,
    x: torch.Tensor,
    observations: dict[str, torch.Tensor] | None = None,
) -> dict[str, torch.Tensor]:
    """Draw posterior predictive samples.

    Parameters
    ----------
    x : torch.Tensor
        Program input. Shape ``(batch, ...)``.
    observations : dict[str, torch.Tensor] or None
        Additional observed data to condition on.

    Returns
    -------
    dict[str, torch.Tensor]
        One key per site, holding the stacked trace values:
        ``(num_samples, batch, *shape)`` for a site that is not
        plated, and for a plated site ``(num_samples, *shape)`` for
        one run and ``(num_samples, batch, *shape)`` for ``batch >
        1`` runs. Guide and MCMC posteriors produce the same shapes.

    Raises
    ------
    ValueError
        If an `MCMCResult` holds draws of a name that is not a
        sample site of the model.
    """
    observations = observations if observations is not None else {}
    if isinstance(self.posterior, Guide):
        latents_iter = (self.posterior.rsample(x) for _ in range(self.num_samples))
    else:
        latents_iter = self._iter_recorded_latents()

    from quivers.inference._posterior import collect_site_draws

    def values() -> Iterator[dict[str, torch.Tensor]]:
        """Stream site values from the existing predictive traces.

        Yields
        ------
        dict[str, torch.Tensor]
            One execution's site tensors in recorded trace order.

        Raises
        ------
        ValueError
            If a retained posterior name is absent from the execution.
        """
        for latents in latents_iter:
            all_obs = {**latents, **observations}
            recorded = trace(self.model, x, observations=all_obs)
            _require_replayed_sites(recorded, latents)
            yield {
                name: site.value
                for name, site in recorded.sites.items()
                if not site.is_deterministic
            }

    return {
        name: value.squeeze(0)
        for name, value in collect_site_draws(
            values(), (1, self.num_samples)
        ).items()
    }

posterior_expectations

posterior_expectations(model: MonadicProgram, posterior: Guide | MCMCResult | PriorDraws, x: Tensor, observations: Mapping[str, Tensor] | None = None, *, sites: Sequence[str], num_samples: int | None = None, mc_samples: int | None = None, seed: int = 0) -> dict[str, Tensor]

Collect native conditional response expectations on retained draw axes.

PARAMETER DESCRIPTION
model

Existing compiled program; no source preparation is performed.

TYPE: MonadicProgram

posterior

Fitted guide or retained constrained joint draws.

TYPE: Guide or MCMCResult or PriorDraws

x

One-run execution input. Multiple independent runs require separate requests so their posterior interpretation remains explicit.

TYPE: Tensor

observations

Covariates and optional response values conditioning the replay. Observed densities are not scored. Upstream observed or lexical values retain their ordinary conditioning semantics.

TYPE: Mapping[str, Tensor] or None DEFAULT: None

sites

Distinct response names, including optional exact Scan occurrences.

TYPE: Sequence[str]

num_samples

Maximum leading draws per recorded chain, or fresh guide draws. None retains recorded draws or takes 100 variational draws.

TYPE: int or None DEFAULT: None

mc_samples

Explicit finite-moment approximation budget when a native analytic expectation is unavailable, following the shared response-law policy.

TYPE: int or None DEFAULT: None

seed

Isolated posterior and response streams; the caller RNG is restored.

TYPE: int DEFAULT: 0

RETURNS DESCRIPTION
dict[str, Tensor]

Conditional native means with chain/draw/native batch/event axes. Categorical and ordinal laws return category probabilities. Scan occurrences stack in visitation order, as for raw log likelihoods.

RAISES DESCRIPTION
(ValueError, TypeError)

If execution/draw axes, selected sites, posterior layout, or response moments are invalid. No response-replication average is substituted for a native analytic expectation.

Source code in src/quivers/inference/predictive.py
def posterior_expectations(
    model: MonadicProgram,
    posterior: Guide | MCMCResult | PriorDraws,
    x: torch.Tensor,
    observations: Mapping[str, torch.Tensor] | None = None,
    *,
    sites: Sequence[str],
    num_samples: int | None = None,
    mc_samples: int | None = None,
    seed: int = 0,
) -> dict[str, torch.Tensor]:
    """Collect native conditional response expectations on retained draw axes.

    Parameters
    ----------
    model : MonadicProgram
        Existing compiled program; no source preparation is performed.
    posterior : Guide or MCMCResult or PriorDraws
        Fitted guide or retained constrained joint draws.
    x : torch.Tensor
        One-run execution input. Multiple independent runs require separate
        requests so their posterior interpretation remains explicit.
    observations : Mapping[str, torch.Tensor] or None
        Covariates and optional response values conditioning the replay.
        Observed densities are not scored. Upstream observed or lexical
        values retain their ordinary conditioning semantics.
    sites : Sequence[str]
        Distinct response names, including optional exact Scan occurrences.
    num_samples : int or None
        Maximum leading draws per recorded chain, or fresh guide draws.
        None retains recorded draws or takes 100 variational draws.
    mc_samples : int or None
        Explicit finite-moment approximation budget when a native analytic
        expectation is unavailable, following the shared response-law policy.
    seed : int
        Isolated posterior and response streams; the caller RNG is restored.

    Returns
    -------
    dict[str, torch.Tensor]
        Conditional native means with chain/draw/native batch/event axes.
        Categorical and ordinal laws return category probabilities. Scan
        occurrences stack in visitation order, as for raw log likelihoods.

    Raises
    ------
    ValueError, TypeError
        If execution/draw axes, selected sites, posterior layout, or response
        moments are invalid. No response-replication average is substituted
        for a native analytic expectation.
    """
    from quivers.effects.observation_scoring import observation_scoring
    from quivers.inference._likelihood import observation_sites
    from quivers.inference._posterior import (
        constrained_draws,
        draw_count,
        collect_site_draws,
    )
    from quivers.inference._response import (
        category_vector,
        response_mean,
        response_distribution,
        response_rng,
    )
    from quivers.inference.registry import _require_one_run
    from quivers.qiec import IndexLiteral, tensor_shape

    _require_one_run(x, "posterior_expectations")
    names = tuple(sites)
    if (
        not names
        or len(set(names)) != len(names)
        or any(not isinstance(name, str) or not name for name in names)
    ):
        raise ValueError("expectations require distinct nonempty response-site names")
    if mc_samples is not None:
        sample_count(mc_samples)
    shape = draw_count(posterior, num_samples)
    values = dict(observations or {})
    draws = constrained_draws(posterior, x, num_samples=num_samples, seed=seed)
    declared_shapes = {}
    if model.input_contract is not None:
        for parameter in model.input_contract.parameters:
            if parameter.name not in names:
                continue
            split = tensor_shape(parameter.type)
            dimensions = () if split is None else split[1]
            if all(
                isinstance(dimension, IndexLiteral) and isinstance(dimension.value, int)
                for dimension in dimensions
            ):
                declared_shapes[parameter.name] = tuple(
                    dimension.value for dimension in dimensions
                )
    category_axes: dict[str, int] = {}

    def expectations() -> Iterator[dict[str, torch.Tensor]]:
        """Read each selected conditional law once per joint posterior draw.

        Yields
        ------
        dict[str, torch.Tensor]
            Response expectations on their unchanged native batch/event axes.

        Raises
        ------
        TypeError
            If a selected response is unavailable or exposes no native law.
        """
        with (
            torch.no_grad(),
            observation_scoring(False),
            response_rng(seed + 1, (x, *values.values(), *draws.values())),
        ):
            for chain in range(shape[0]):
                for draw in range(shape[1]):
                    latents = {
                        name: value[chain, draw] for name, value in draws.items()
                    }
                    recorded = trace(model, x, values | latents)
                    _require_replayed_sites(recorded, latents)
                    grouped = observation_sites(recorded, set(names))
                    if missing := set(names) - set(grouped):
                        raise TypeError(
                            f"response sites {sorted(missing)} expose no native conditional distribution"
                        )
                    result = {}
                    for name, occurrences in grouped.items():
                        moments = []
                        for site in occurrences:
                            distribution = response_distribution(recorded, site.name)
                            probabilities = category_vector(distribution)
                            if probabilities is None:
                                mean = response_mean(distribution, mc_samples)
                                categories = 0
                            else:
                                mean = probabilities
                                categories = int(mean.shape[-1])
                            if category_axes.setdefault(name, categories) != categories:
                                raise TypeError(
                                    f"response {name!r} changes its native category axis between draws"
                                )
                            moments.append(mean)
                        result[name] = (
                            moments[0] if len(moments) == 1 else torch.stack(moments)
                        )
                    yield result

    output = collect_site_draws(expectations(), shape)
    for name, dimensions in declared_shapes.items():
        if name in output:
            categories = (category_axes[name],) if category_axes[name] else ()
            # Presentation follows the checked input contract; an extra
            # probability axis comes only from the native categorical law.
            output[name] = output[name].reshape(*shape, *dimensions, *categories)
    return output

Cited sources

Andrew Gelman, John B. Carlin, Hal S. Stern, David B. Dunson, Aki Vehtari, and Donald B. Rubin. Bayesian Data Analysis. Chapman and Hall/CRC, 3 edition, 2013. ISBN 9781439840955. URL: https://www.routledge.com/Bayesian-Data-Analysis/Gelman-Carlin-Stern-Dunson-Vehtari-Rubin/p/book/9781439840955.