Skip to content

Marginalized responses

The score of a marginalize block holds the density of the block's responses with the latent summed out (response laws). MarginalizedResponse is the law such a response is drawn from when a run is given no value for it, bound to the values of one run. draw_classes draws the latents of nested grouped blocks jointly from the GroupLevel of each.

marginal

Responses whose density a marginalized score carries.

A marginalize block scores its responses with the latent summed out, so a response of the block adds no density of its own to the joint. The response still has a law. MarginalizedResponse is that law at the values of one run: it draws the response with the latent drawn too, and gives the distribution of each row with the latent summed out, whose mean is the class-weighted mean of the row.

GroupLevel and draw_classes state how the latents of nested grouped blocks are drawn jointly, given what has already been bound.

MarginalizedResponse

MarginalizedResponse(declared: MarginalizedFactor, law: ResponseLaw, environment: Environment)

Bases: ContinuousMorphism

A marginalized block's response law, bound to one run's values.

PARAMETER DESCRIPTION
declared

The response's declared morphism, which scores nothing.

TYPE: MarginalizedFactor

law

How the response is distributed given an environment.

TYPE: ResponseLaw

environment

The values of one run the law reads.

TYPE: Environment

Source code in src/quivers/continuous/marginal.py
def __init__(
    self, declared: MarginalizedFactor, law: ResponseLaw, environment: Environment
) -> None:
    super().__init__(declared.domain, declared.codomain)
    self._declared = declared
    self._law = law
    self._environment = environment

support property

support: Constraint

The support of the response family.

RETURNS DESCRIPTION
Constraint

The declared family's support.

rsample

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

Draw the latent and then the response.

PARAMETER DESCRIPTION
x

The step's input, which the law does not read.

TYPE: Tensor

sample_shape

Leading independent sampling dimensions.

TYPE: Size DEFAULT: Size()

RETURNS DESCRIPTION
Tensor

Draws of every row, (*sample_shape, rows, *event).

Source code in src/quivers/continuous/marginal.py
def rsample(
    self, x: torch.Tensor, sample_shape: torch.Size = torch.Size()
) -> torch.Tensor:
    """Draw the latent and then the response.

    Parameters
    ----------
    x : torch.Tensor
        The step's input, which the law does not read.
    sample_shape : torch.Size
        Leading independent sampling dimensions.

    Returns
    -------
    torch.Tensor
        Draws of every row, ``(*sample_shape, rows, *event)``.
    """
    del x
    if not sample_shape:
        return self._law.draw(self._environment)
    draws = [
        self._law.draw(self._environment) for _ in range(math.prod(sample_shape))
    ]
    return torch.stack(draws).reshape(*sample_shape, *draws[0].shape)

rsample_event

rsample_event(x: Tensor, sample_shape: Size, event: BoundEvent | None) -> Tensor

Draw the response on its declared truncation interval.

PARAMETER DESCRIPTION
x

The step's input, which the law does not read.

TYPE: Tensor

sample_shape

Leading independent sampling dimensions.

TYPE: Size

event

The recording roles, which the law holds itself.

TYPE: BoundEvent or None

RETURNS DESCRIPTION
Tensor

Draws of every row.

Source code in src/quivers/continuous/marginal.py
def rsample_event(
    self,
    x: torch.Tensor,
    sample_shape: torch.Size,
    event: BoundEvent | None,
) -> torch.Tensor:
    """Draw the response on its declared truncation interval.

    Parameters
    ----------
    x : torch.Tensor
        The step's input, which the law does not read.
    sample_shape : torch.Size
        Leading independent sampling dimensions.
    event : BoundEvent or None
        The recording roles, which the law holds itself.

    Returns
    -------
    torch.Tensor
        Draws of every row.
    """
    del event
    return self.rsample(x, sample_shape)

sampling_distribution

sampling_distribution(x: Tensor, event: BoundEvent | None = None) -> Distribution

The law of each row with the block's latents summed out.

PARAMETER DESCRIPTION
x

The step's input, which the law does not read.

TYPE: Tensor

event

The recording roles, which the law holds itself.

TYPE: BoundEvent or None DEFAULT: None

RETURNS DESCRIPTION
Distribution

One distribution per row, given the values the block does not sum out and no response.

Source code in src/quivers/continuous/marginal.py
def sampling_distribution(
    self, x: torch.Tensor, event: BoundEvent | None = None
) -> Distribution:
    """The law of each row with the block's latents summed out.

    Parameters
    ----------
    x : torch.Tensor
        The step's input, which the law does not read.
    event : BoundEvent or None
        The recording roles, which the law holds itself.

    Returns
    -------
    Distribution
        One distribution per row, given the values the block does not
        sum out and no response.
    """
    del x, event
    return self._law.marginal(self._environment)

log_prob

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

Score nothing: the block's score step holds the density.

PARAMETER DESCRIPTION
x

The step's input.

TYPE: Tensor

y

The response.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

Zero.

Source code in src/quivers/continuous/marginal.py
def log_prob(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
    """Score nothing: the block's score step holds the density.

    Parameters
    ----------
    x : torch.Tensor
        The step's input.
    y : torch.Tensor
        The response.

    Returns
    -------
    torch.Tensor
        Zero.
    """
    return self._declared.log_prob(x, y)

log_prob_event

log_prob_event(x: Tensor, y: Tensor, event: BoundEvent) -> Tensor

Score nothing: the block's score step holds the recorded events.

PARAMETER DESCRIPTION
x

The step's input.

TYPE: Tensor

y

The recorded response.

TYPE: Tensor

event

The recording roles.

TYPE: BoundEvent

RETURNS DESCRIPTION
Tensor

Zero.

Source code in src/quivers/continuous/marginal.py
def log_prob_event(
    self, x: torch.Tensor, y: torch.Tensor, event: BoundEvent
) -> torch.Tensor:
    """Score nothing: the block's score step holds the recorded events.

    Parameters
    ----------
    x : torch.Tensor
        The step's input.
    y : torch.Tensor
        The recorded response.
    event : BoundEvent
        The recording roles.

    Returns
    -------
    torch.Tensor
        Zero.
    """
    del event
    return self._declared.log_prob(x, y)

GroupLevel didactic-model

GroupLevel(*, log_weights: Tensor = field(opaque=True, extras=OPAQUE_BY_IDENTITY), parent: Tensor | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: ValueModel

One block of a nest of grouped blocks, at one run's values.

PARAMETER DESCRIPTION
log_weights

The log weight of each unit and class, (units, *enclosing, K) with the class axes of the enclosing blocks outermost first: the log prior of the class, with the log likelihood of every response of the block that is already bound.

TYPE: Tensor DEFAULT: field(opaque=True, extras=OPAQUE_BY_IDENTITY)

parent

The enclosing block's unit each unit belongs to, (units,); None for the outermost block.

TYPE: Tensor or None DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY)

draw_classes

draw_classes(levels: Sequence[GroupLevel]) -> tuple[Tensor, ...]

Draw the classes of a nest of grouped blocks jointly.

The evidence of each inner unit is summed over its class and added to the class weights of the unit that encloses it, from the innermost block out. The classes are then drawn from the outermost block in, each given the classes of the units that enclose it. The draw is exact: the classes of the innermost units have the joint distribution that the weights of all the levels define.

PARAMETER DESCRIPTION
levels

The blocks, outermost first.

TYPE: Sequence[GroupLevel]

RETURNS DESCRIPTION
tuple[Tensor, ...]

For each block, the class of the unit of it that encloses each unit of the innermost block, (units of the innermost block,).

Source code in src/quivers/continuous/marginal.py
def draw_classes(levels: Sequence[GroupLevel]) -> tuple[torch.Tensor, ...]:
    """Draw the classes of a nest of grouped blocks jointly.

    The evidence of each inner unit is summed over its class and added to
    the class weights of the unit that encloses it, from the innermost
    block out. The classes are then drawn from the outermost block in, each
    given the classes of the units that enclose it. The draw is exact: the
    classes of the innermost units have the joint distribution that the
    weights of all the levels define.

    Parameters
    ----------
    levels : Sequence[GroupLevel]
        The blocks, outermost first.

    Returns
    -------
    tuple[torch.Tensor, ...]
        For each block, the class of the unit of it that encloses each unit
        of the innermost block, ``(units of the innermost block,)``.
    """
    evidence: list[torch.Tensor] = [level.log_weights for level in levels]
    for index in range(len(levels) - 1, 0, -1):
        parent = levels[index].parent
        assert parent is not None
        summed = torch.logsumexp(evidence[index], dim=-1)
        evidence[index - 1] = evidence[index - 1].index_add(0, parent, summed)
    drawn: list[torch.Tensor] = []
    for index, level in enumerate(levels):
        if index:
            assert level.parent is not None
            drawn = [classes[level.parent] for classes in drawn]
        units = torch.arange(evidence[index].shape[0], device=evidence[index].device)
        drawn.append(
            torch.distributions.Categorical(
                logits=evidence[index][(units, *drawn)]
            ).sample()
        )
    return tuple(drawn)