Conditioning

Conditioning on observations and evidence in probabilistic programs.

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
29
30
31
32
33
34
35
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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)