Skip to content

Subsampled units

A score that is a sum over units is estimated without bias by a subset of them (subsampled units). unit_coefficients reads the coefficient of each unit from the subsampled plates of a run, and refuses a subset that splits a unit.

unit_subsets

Unit weights of a score whose plates are subsampled.

A score S = sum_u w(u) is a sum over its units, so a uniform subset of the units, each weight scaled by the number of units over the number kept, estimates it without bias. A subsampled plate keeps a subset of the units when no unit has elements of the plate both inside and outside the subset. A unit that has both is split: its weight is not a sum over its elements, so no subset of them estimates it.

units_subsampled

units_subsampled(factorization: UnitFactorization, subsets: Mapping[str, Tensor]) -> bool

Whether a subsampled plate concerns a score.

PARAMETER DESCRIPTION
factorization

The score's units.

TYPE: UnitFactorization

subsets

The positions kept of each subsampled plate.

TYPE: Mapping[str, Tensor]

RETURNS DESCRIPTION
bool

True when a subsampled plate is a unit axis, or has elements in the units.

Source code in src/quivers/continuous/unit_subsets.py
def units_subsampled(
    factorization: UnitFactorization, subsets: Mapping[str, torch.Tensor]
) -> bool:
    """Whether a subsampled plate concerns a score.

    Parameters
    ----------
    factorization : UnitFactorization
        The score's units.
    subsets : Mapping[str, torch.Tensor]
        The positions kept of each subsampled plate.

    Returns
    -------
    bool
        ``True`` when a subsampled plate is a unit axis, or has elements
        in the units.
    """
    return any(
        name in subsets
        for name in (
            *factorization.axes,
            *(item.plate for item in factorization.members),
        )
    )

unit_coefficients

unit_coefficients(factorization: UnitFactorization, env: Environment, subsets: Mapping[str, Tensor]) -> Tensor

The coefficient of each unit's weight under subsampled plates.

PARAMETER DESCRIPTION
factorization

The score's units.

TYPE: UnitFactorization

env

The run's values.

TYPE: Environment

subsets

The positions kept of each subsampled plate.

TYPE: Mapping[str, Tensor]

RETURNS DESCRIPTION
Tensor

(units,). With a subsampled unit axis, the number of positions of the axis over the number kept for a unit kept, and zero for any other, the product of these over the subsampled axes. Otherwise the number of units with elements of the subsampled plate over the number of those kept, zero for a unit not kept, and one for a unit with no element of the plate.

RAISES DESCRIPTION
ValueError

If a unit has elements of a subsampled plate inside and outside the subset, or the subsets of two plates would each select the units.

Source code in src/quivers/continuous/unit_subsets.py
def unit_coefficients(
    factorization: UnitFactorization,
    env: Environment,
    subsets: Mapping[str, torch.Tensor],
) -> torch.Tensor:
    """The coefficient of each unit's weight under subsampled plates.

    Parameters
    ----------
    factorization : UnitFactorization
        The score's units.
    env : Environment
        The run's values.
    subsets : Mapping[str, torch.Tensor]
        The positions kept of each subsampled plate.

    Returns
    -------
    torch.Tensor
        ``(units,)``. With a subsampled unit axis, the number of positions
        of the axis over the number kept for a unit kept, and zero for any
        other, the product of these over the subsampled axes. Otherwise
        the number of units with elements of the subsampled plate over the
        number of those kept, zero for a unit not kept, and one for a unit
        with no element of the plate.

    Raises
    ------
    ValueError
        If a unit has elements of a subsampled plate inside and outside
        the subset, or the subsets of two plates would each select the
        units.
    """
    units = 1
    for size in factorization.sizes:
        units *= size
    inner = tuple(
        dict.fromkeys(
            item.plate for item in factorization.members if item.plate in subsets
        )
    )
    counts = {
        plate: _membership(factorization, plate, env, subsets, units) for plate in inner
    }
    axes = [name for name in factorization.axes if name in subsets]
    if axes:
        coefficients = torch.ones(units)
        positions = torch.unravel_index(torch.arange(units), factorization.sizes)
        for axis, size, along in zip(
            factorization.axes, factorization.sizes, positions, strict=True
        ):
            if axis not in subsets:
                continue
            index = subsets[axis].cpu()
            kept = torch.isin(along, index)
            coefficients = coefficients * kept * (size / int(index.numel()))
        for plate, (_, outside) in counts.items():
            _refuse_split(
                factorization, plate, (coefficients > 0) & (outside > 0), kept_by=axes
            )
        return coefficients
    if len(inner) > 1:
        raise ValueError(
            f"the score of the block over {factorization.latent!r} has rows or "
            f"inner units in the subsampled plates {list(inner)}, and each "
            "subset would select the block's units. How often a unit is in both "
            "subsets depends on how the subsets are drawn together, which their "
            "indices do not say; subsample one of the plates"
            + (
                f", or a unit axis {list(factorization.axes)}"
                if factorization.axes
                else ""
            )
        )
    ((plate, (inside, outside)),) = counts.items()
    _refuse_split(factorization, plate, (inside > 0) & (outside > 0), kept_by=None)
    governed = (inside + outside) > 0
    kept = inside > 0
    factor = int(governed.sum()) / int(kept.sum())
    return torch.where(governed, kept * factor, torch.ones(units))