Skip to content

Bijectors

A bijector is an invertible differentiable map with its log Jacobian determinant (normalizing flows). Exponentiation, for instance, maps an unconstrained real to a positive value. A change of coordinates changes density per unit volume, so a density computed through a bijector needs the Jacobian correction (Rezende and Mohamed, 2015; Stan Development Team, n.d.-f). Families use bijectors to map their unconstrained parameters onto their supports, and the TransformReparam strategy uses them to reparameterize a site.

bijectors

Bijector library for the compositional measure algebra.

A Bijector is a measurable bijection \(f: X \to Y\) between Borel spaces, together with its inverse \(f^{-1}\) and the log-determinant of its Jacobian. Pushing forward a measure \(\mu\) along \(f\) produces a new measure \(f_*\mu\) with density

\[p_{f_*\mu}(y) = p_\mu(f^{-1}(y))\,\bigl|\det J_{f^{-1}}(y)\bigr|.\]

In log-space (the only sane computational discipline; see TensorFlow Probability's Bijector rationale):

\[\log p_{f_*\mu}(y) = \log p_\mu(f^{-1}(y)) + \log\bigl|\det J_{f^{-1}}(y)\bigr|.\]

Every bijector exposes:

  • forward(x) evaluating \(f\),
  • inverse(y) evaluating \(f^{-1}\),
  • forward_log_det_jacobian(x) evaluating \(\log\bigl|\det J_f(x)\bigr|\),
  • inverse_log_det_jacobian(y) evaluating \(\log\bigl|\det J_{f^{-1}}(y)\bigr|\) (equal to \(-\text{forward\_log\_det\_jacobian}(f^{-1}(y))\) when both are finite).

The library is closed under composition via Compose(b1, b2) and under inversion via Inverse(b), so the family forms a groupoid of measurable isomorphisms.

BIJECTORS_BY_NAME module-attribute

BIJECTORS_BY_NAME: Mapping[str, type[Bijector]] = MappingProxyType({'Identity': IdentityBijector, 'Exp': ExpBijector, 'Log': LogBijector, 'Sigmoid': SigmoidBijector, 'Logit': LogitBijector, 'Softplus': SoftplusBijector, 'StickBreaking': StickBreakingBijector})

The bijectors a QVR source names by their surface spelling.

Pushforward(base, Exp) resolves Exp here. Only bijectors that take no arguments are listed; an affine or composite map is built in Python and passed as a value.

Bijector

Bases: ABC

Abstract base for measurable bijections used in Pushforward.

Implementations override the four primitive methods; the __call__, Compose, and inv helpers compose them.

forward_shape

forward_shape(shape)

Retain intrinsic event axes for an elementwise transformation.

PARAMETER DESCRIPTION
shape

Input event shape.

TYPE: Size

RETURNS DESCRIPTION
Size

Output event shape.

Source code in src/quivers/continuous/bijectors.py
def forward_shape(self, shape):
    """Retain intrinsic event axes for an elementwise transformation.

    Parameters
    ----------
    shape : torch.Size
        Input event shape.

    Returns
    -------
    torch.Size
        Output event shape.
    """
    return torch.Size(shape)

inverse_shape

inverse_shape(shape)

Retain intrinsic event axes for the inverse elementwise map.

PARAMETER DESCRIPTION
shape

Output event shape.

TYPE: Size

RETURNS DESCRIPTION
Size

Input event shape.

Source code in src/quivers/continuous/bijectors.py
def inverse_shape(self, shape):
    """Retain intrinsic event axes for the inverse elementwise map.

    Parameters
    ----------
    shape : torch.Size
        Output event shape.

    Returns
    -------
    torch.Size
        Input event shape.
    """
    return torch.Size(shape)

select_batch

select_batch(batch_shape, active, *, event_shape=(), output_shape=None)

Select declared parameter batches while retaining intrinsic event axes.

PARAMETER DESCRIPTION
batch_shape

Broadcast batch of the measure before transformation.

TYPE: Size

active

Boolean selection over that batch.

TYPE: Tensor

event_shape

Intrinsic coordinates to retain within each selected row.

TYPE: tuple[int, ...] DEFAULT: ()

output_shape

Optional shape for the selected batch axes.

TYPE: Size or None DEFAULT: None

RETURNS DESCRIPTION
Bijector

Transformation with matching selected parameter rows.

RAISES DESCRIPTION
NotImplementedError

If a custom transformation declares no batch-selection capability.

Source code in src/quivers/continuous/bijectors.py
def select_batch(self, batch_shape, active, *, event_shape=(), output_shape=None):
    """Select declared parameter batches while retaining intrinsic event axes.

    Parameters
    ----------
    batch_shape : torch.Size
        Broadcast batch of the measure before transformation.
    active : Tensor
        Boolean selection over that batch.
    event_shape : tuple[int, ...]
        Intrinsic coordinates to retain within each selected row.
    output_shape : torch.Size or None
        Optional shape for the selected batch axes.

    Returns
    -------
    Bijector
        Transformation with matching selected parameter rows.

    Raises
    ------
    NotImplementedError
        If a custom transformation declares no batch-selection capability.
    """
    if self._batch_parameter_names is None:
        raise NotImplementedError(
            f"{type(self).__name__} has no batch-selection capability"
        )
    if not self._batch_parameter_names:
        return self
    from quivers.continuous._batch_selection import select_parameter

    parameters = {
        name: select_parameter(
            torch.as_tensor(getattr(self, name)).expand(
                torch.Size(batch_shape) + torch.Size(event_shape)
            ),
            batch_shape,
            len(event_shape),
            active,
            output_shape=output_shape,
        )
        for name in self._batch_parameter_names
    }
    return type(self)(**parameters)

inverse_log_det_jacobian

inverse_log_det_jacobian(y: Tensor) -> Tensor

Default implementation: invert the forward Jacobian's log-determinant. Subclasses with a more numerically stable closed form should override.

Source code in src/quivers/continuous/bijectors.py
def inverse_log_det_jacobian(self, y: Tensor) -> Tensor:
    """Default implementation: invert the forward Jacobian's
    log-determinant. Subclasses with a more numerically stable
    closed form should override.
    """
    return -self.forward_log_det_jacobian(self.inverse(y))

inv

inv() -> Bijector

Return the bijector's inverse, swapping forward / inverse and negating the Jacobian log-determinant.

Source code in src/quivers/continuous/bijectors.py
def inv(self) -> Bijector:
    """Return the bijector's inverse, swapping `forward` /
    `inverse` and negating the Jacobian log-determinant.
    """
    return InverseBijector(self)

InverseBijector

InverseBijector(base: Bijector)

Bases: Bijector

The inverse of a bijector, deferring all four primitives to the wrapped instance with their roles swapped.

Source code in src/quivers/continuous/bijectors.py
def __init__(self, base: Bijector) -> None:
    self.base = base
    self.jacobian_event_dim = base.jacobian_event_dim

forward_shape

forward_shape(shape)

Use the retained bijector's inverse event-shape contract.

PARAMETER DESCRIPTION
shape

Input event shape.

TYPE: Size

RETURNS DESCRIPTION
Size

Output event shape under inversion.

Source code in src/quivers/continuous/bijectors.py
def forward_shape(self, shape):
    """Use the retained bijector's inverse event-shape contract.

    Parameters
    ----------
    shape : torch.Size
        Input event shape.

    Returns
    -------
    torch.Size
        Output event shape under inversion.
    """
    return self.base.inverse_shape(shape)

inverse_shape

inverse_shape(shape)

Use the retained bijector's forward event-shape contract.

PARAMETER DESCRIPTION
shape

Output event shape.

TYPE: Size

RETURNS DESCRIPTION
Size

Original input event shape.

Source code in src/quivers/continuous/bijectors.py
def inverse_shape(self, shape):
    """Use the retained bijector's forward event-shape contract.

    Parameters
    ----------
    shape : torch.Size
        Output event shape.

    Returns
    -------
    torch.Size
        Original input event shape.
    """
    return self.base.forward_shape(shape)

select_batch

select_batch(batch_shape, active, *, event_shape=(), output_shape=None)

Select the wrapped transformation before taking its inverse.

PARAMETER DESCRIPTION
batch_shape

Retained measure batch.

TYPE: Size

active

Boolean batch selection.

TYPE: Tensor

event_shape

Intrinsic event coordinates.

TYPE: tuple[int, ...] DEFAULT: ()

output_shape

Optional selected batch layout.

TYPE: Size or None DEFAULT: None

RETURNS DESCRIPTION
InverseBijector

Inverse transformation on the selected batch.

Source code in src/quivers/continuous/bijectors.py
def select_batch(self, batch_shape, active, *, event_shape=(), output_shape=None):
    """Select the wrapped transformation before taking its inverse.

    Parameters
    ----------
    batch_shape : torch.Size
        Retained measure batch.
    active : Tensor
        Boolean batch selection.
    event_shape : tuple[int, ...]
        Intrinsic event coordinates.
    output_shape : torch.Size or None
        Optional selected batch layout.

    Returns
    -------
    InverseBijector
        Inverse transformation on the selected batch.
    """
    return InverseBijector(
        self.base.select_batch(
            batch_shape,
            active,
            event_shape=self.base.inverse_shape(event_shape),
            output_shape=output_shape,
        )
    )

ComposeBijector

ComposeBijector(outer: Bijector, inner: Bijector)

Bases: Bijector

Composition outer ∘ inner. Forward applies inner first then outer; inverse applies outer.inverse first then inner.inverse. Log-Jacobians add by the chain rule.

Source code in src/quivers/continuous/bijectors.py
def __init__(self, outer: Bijector, inner: Bijector) -> None:
    self.outer = outer
    self.inner = inner
    self.jacobian_event_dim = max(
        outer.jacobian_event_dim, inner.jacobian_event_dim
    )

forward_shape

forward_shape(shape)

Compose the intrinsic event-shape maps.

PARAMETER DESCRIPTION
shape

Original input event shape.

TYPE: Size

RETURNS DESCRIPTION
Size

Final output event shape.

Source code in src/quivers/continuous/bijectors.py
def forward_shape(self, shape):
    """Compose the intrinsic event-shape maps.

    Parameters
    ----------
    shape : torch.Size
        Original input event shape.

    Returns
    -------
    torch.Size
        Final output event shape.
    """
    return self.outer.forward_shape(self.inner.forward_shape(shape))

inverse_shape

inverse_shape(shape)

Invert event-shape maps in the same order as coordinates.

PARAMETER DESCRIPTION
shape

Final output event shape.

TYPE: Size

RETURNS DESCRIPTION
Size

Original input event shape.

Source code in src/quivers/continuous/bijectors.py
def inverse_shape(self, shape):
    """Invert event-shape maps in the same order as coordinates.

    Parameters
    ----------
    shape : torch.Size
        Final output event shape.

    Returns
    -------
    torch.Size
        Original input event shape.
    """
    return self.inner.inverse_shape(self.outer.inverse_shape(shape))

select_batch

select_batch(batch_shape, active, *, event_shape=(), output_shape=None)

Select both retained transformations under the same batch layout.

PARAMETER DESCRIPTION
batch_shape

Retained measure batch.

TYPE: Size

active

Boolean batch selection.

TYPE: Tensor

event_shape

Intrinsic event coordinates.

TYPE: tuple[int, ...] DEFAULT: ()

output_shape

Optional selected batch layout.

TYPE: Size or None DEFAULT: None

RETURNS DESCRIPTION
ComposeBijector

Composition of the selected transformations.

Source code in src/quivers/continuous/bijectors.py
def select_batch(self, batch_shape, active, *, event_shape=(), output_shape=None):
    """Select both retained transformations under the same batch layout.

    Parameters
    ----------
    batch_shape : torch.Size
        Retained measure batch.
    active : Tensor
        Boolean batch selection.
    event_shape : tuple[int, ...]
        Intrinsic event coordinates.
    output_shape : torch.Size or None
        Optional selected batch layout.

    Returns
    -------
    ComposeBijector
        Composition of the selected transformations.
    """
    return ComposeBijector(
        self.outer.select_batch(
            batch_shape,
            active,
            event_shape=self.inner.forward_shape(event_shape),
            output_shape=output_shape,
        ),
        self.inner.select_batch(
            batch_shape, active, event_shape=event_shape, output_shape=output_shape
        ),
    )

IdentityBijector

Bases: Bijector

The identity map. Useful as a neutral element in compositions and as the trivial pushforward.

ExpBijector

Bases: Bijector

The exponential map \(f(x) = e^x\) from \(\mathbb{R}\) to \((0, \infty)\). Jacobian log-determinant is \(x\).

LogBijector

Bases: Bijector

The logarithm \(f(x) = \log x\) from \((0, \infty)\) to \(\mathbb{R}\). Inverse of ExpBijector; Jacobian log-determinant is \(-\log x\).

SigmoidBijector

Bases: Bijector

The logistic sigmoid \(f(x) = 1/(1 + e^{-x})\) from \(\mathbb{R}\) to \((0, 1)\).

The Jacobian log-determinant is the log-density of the standard logistic distribution, \(-x - 2\log(1 + e^{-x})\), in a form that is stable in both tails.

LogitBijector

Bases: Bijector

The logit \(f(x) = \log(x / (1 - x))\) from \((0, 1)\) to \(\mathbb{R}\). Inverse of SigmoidBijector.

SoftplusBijector

Bases: Bijector

The softplus map \(f(x) = \log(1 + e^x)\) from \(\mathbb{R}\) to \((0, \infty)\). Smooth alternative to ExpBijector with linear tail growth on the positive side.

Forward Jacobian: \(df/dx = \sigma(x)\), so \(\log|df/dx| = -\text{softplus}(-x)\).

Inverse: \(f^{-1}(y) = \log(e^y - 1)\). Inverse Jacobian: \(df^{-1}/dy = 1/(1 - e^{-y})\), so \(\log|df^{-1}/dy| = -\log(1 - e^{-y}) = -\log(-\mathrm{expm1}(-y))\).

AffineBijector

AffineBijector(scale: Tensor | float, shift: Tensor | float)

Bases: Bijector

The affine map \(f(x) = \text{scale} \cdot x + \text{shift}\). scale must be strictly positive; the Jacobian log-determinant is \(\log\,\text{scale}\), broadcast to the input shape.

Source code in src/quivers/continuous/bijectors.py
def __init__(
    self,
    scale: Tensor | float,
    shift: Tensor | float,
) -> None:
    self.scale = torch.as_tensor(scale)
    self.shift = torch.as_tensor(shift)
    if torch.any(self.scale <= 0):
        raise ValueError(
            f"Affine: scale must be strictly positive; got {self.scale!r}"
        )

StickBreakingBijector

Bases: Bijector

The stick-breaking map from \(\mathbb{R}^{K-1}\) to the open \(K\)-simplex \(\{p \in \mathbb{R}^K_{>0} : \sum_k p_k = 1\}\).

Forward: \(y_k = z_k \cdot \prod_{j<k}(1 - z_j)\) for \(k < K-1\), where \(z_k = \sigma(x_k - \log(K - k - 1))\), and \(y_{K-1} = 1 - \sum_{k<K-1} y_k\). The shift by \(\log(K - k - 1)\) centres the prior on the uniform simplex when \(x \sim \mathrm{Normal}(0, 1)\), matching the convention used by Stan and PyMC.

Inverse: \(x_k = \sigma^{-1}(z_k) + \log(K - k - 1)\) where \(z_k = y_k / (1 - \sum_{j<k} y_j)\).

The Jacobian log-determinant in the forward direction sums log(z_k) + log(1 - z_k) + log(remaining) across the K-1 stick-breaks. Used in the desugaring of LogisticNormal and other simplex-valued transforms.

forward_shape

forward_shape(shape)

Add the final simplex coordinate to a vector event.

PARAMETER DESCRIPTION
shape

Real-coordinate vector event shape.

TYPE: Size

RETURNS DESCRIPTION
Size

Simplex event shape with one additional coordinate.

RAISES DESCRIPTION
ValueError

If the input has no intrinsic vector axis.

Source code in src/quivers/continuous/bijectors.py
def forward_shape(self, shape):
    """Add the final simplex coordinate to a vector event.

    Parameters
    ----------
    shape : torch.Size
        Real-coordinate vector event shape.

    Returns
    -------
    torch.Size
        Simplex event shape with one additional coordinate.

    Raises
    ------
    ValueError
        If the input has no intrinsic vector axis.
    """
    if not shape:
        raise ValueError("stick breaking requires an intrinsic vector event axis")
    return torch.Size((*shape[:-1], shape[-1] + 1))

inverse_shape

inverse_shape(shape)

Remove the simplex's redundant coordinate from its event shape.

PARAMETER DESCRIPTION
shape

Simplex event shape.

TYPE: Size

RETURNS DESCRIPTION
Size

Real-coordinate event shape.

RAISES DESCRIPTION
ValueError

If no nonempty simplex coordinate axis is supplied.

Source code in src/quivers/continuous/bijectors.py
def inverse_shape(self, shape):
    """Remove the simplex's redundant coordinate from its event shape.

    Parameters
    ----------
    shape : torch.Size
        Simplex event shape.

    Returns
    -------
    torch.Size
        Real-coordinate event shape.

    Raises
    ------
    ValueError
        If no nonempty simplex coordinate axis is supplied.
    """
    if not shape or shape[-1] < 1:
        raise ValueError("inverse stick breaking requires a simplex event axis")
    return torch.Size((*shape[:-1], shape[-1] - 1))

Cited sources

Danilo Rezende and Shakir Mohamed. Variational Inference with Normalizing Flows. In Proceedings of the 32nd International Conference on Machine Learning, volume 37 of Proceedings of Machine Learning Research, 1530–1538. 2015. URL: https://proceedings.mlr.press/v37/rezende15.html.

Stan Development Team. Reparameterization and change of variables. Stan User's Guide, version 2.40, n.d.-f. URL: https://mc-stan.org/docs/stan-users-guide/reparameterization.html (visited on 2026-11-06).