Skip to content

Conditioning

condition pairs a MonadicProgram with observed values for named sites and returns a Conditioned model, which threads those observations through tracing and log joint evaluation (conditioning on observations). A forward run draws the remaining sites from their conditional priors and records the scores, which an inference algorithm uses to fit an approximation, reweight draws, or build a chain targeting the posterior (Gelman et al., 2013).

conditioning

Conditioning: pair a program with observed data.

The condition function wraps a MonadicProgram with a dict of observations, producing a Conditioned object that threads those observations through tracing and log-joint evaluation. This is the primary interface for specifying observed data in inference.

Conditioned

Conditioned(model: MonadicProgram, data: dict[str, Tensor])

A monadic program paired with observed data.

PARAMETER DESCRIPTION
model

The generative model.

TYPE: MonadicProgram

data

Observed variable names mapped to their observed values.

TYPE: dict[str, Tensor]

Source code in src/quivers/inference/conditioning.py
def __init__(
    self,
    model: MonadicProgram,
    data: dict[str, torch.Tensor],
) -> None:
    self.model = model
    self.data = data

observed_names property

observed_names: set[str]

Names of all observed variables.

trace

trace(x: Tensor) -> Trace

Execute the model with observations clamped.

PARAMETER DESCRIPTION
x

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

TYPE: Tensor

RETURNS DESCRIPTION
Trace

Execution trace with observed sites clamped.

Source code in src/quivers/inference/conditioning.py
def trace(self, x: torch.Tensor) -> Trace:
    """Execute the model with observations clamped.

    Parameters
    ----------
    x : torch.Tensor
        Program input. Shape (batch, ...).

    Returns
    -------
    Trace
        Execution trace with observed sites clamped.
    """
    return trace(self.model, x, observations=self.data)

condition

condition(model: MonadicProgram, data: dict[str, Tensor]) -> Conditioned

Condition a model on observed data.

PARAMETER DESCRIPTION
model

The generative model.

TYPE: MonadicProgram

data

Observed variable names mapped to their observed values.

TYPE: dict[str, Tensor]

RETURNS DESCRIPTION
Conditioned

Wrapped model with observations.

Source code in src/quivers/inference/conditioning.py
def condition(
    model: MonadicProgram,
    data: dict[str, torch.Tensor],
) -> Conditioned:
    """Condition a model on observed data.

    Parameters
    ----------
    model : MonadicProgram
        The generative model.
    data : dict[str, torch.Tensor]
        Observed variable names mapped to their observed values.

    Returns
    -------
    Conditioned
        Wrapped model with observations.
    """
    return Conditioned(model, data)

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.