Skip to content
class

TransformedDistribution

extendsDistribution
TransformedDistribution(base_distribution: Distribution, transforms: Transform | list[Transform], validate_args: bool | None = None)

Pushforward of a base distribution through a (composite) bijector.

Constructs a new Distribution whose samples are obtained by pushing samples from base_distribution through the supplied chain of Transform instances, with log_prob accounting for the Jacobian correction via the change-of-variable formula. This is the canonical way to build normalising flows in Lucid: stack any number of bijections on top of a tractable base (typically a Normal) to obtain expressive densities while retaining exact log-likelihood evaluation and reparameterised sampling.

Parameters

base_distributionDistribution
Underlying distribution whose samples will be pushed through transforms.
transformsTransform or list[Transform]
A single transform or an ordered list applied left-to-right. Internally wrapped in a list.
validate_argsbool= None
Forwarded to Distribution.

Notes

Sampling (with T=Tn∘⋯∘T1T = T_n \circ \cdots \circ T_1 the composite bijector):

Y=T(X),X∼pbaseY = T(X), \quad X \sim p_{\text{base}}

Reparameterised sampling is available iff the base distribution supports it (has_rsample is forwarded).

Density (change of variables):

log⁡pY(y)=log⁡pX(T−1(y))−∑i=1nlog⁡ ⁣∣det⁡∂Ti(xi−1)∂xi−1∣\log p_Y(\mathbf{y}) = \log p_X(T^{-1}(\mathbf{y})) - \sum_{i=1}^{n} \log\!\left|\det \frac{\partial T_i(\mathbf{x}_{i-1})}{\partial \mathbf{x}_{i-1}}\right|

where x0=T−1(y)\mathbf{x}_0 = T^{-1}(\mathbf{y}) and xi=Ti(xi−1)\mathbf{x}_i = T_i(\mathbf{x}_{i-1}). The implementation walks the transforms in reverse, inverting one step at a time and accumulating the Jacobian correction.

Examples

>>> import lucid
>>> from lucid.distributions import Normal
>>> from lucid.distributions.transforms import (
...     ExpTransform, TransformedDistribution)
>>> # LogNormal = ExpTransform(Normal(0, 1))
>>> log_normal = TransformedDistribution(Normal(loc=0.0, scale=1.0), [ExpTransform()])
>>> log_normal.rsample((4,)).shape
(4,)
>>> log_normal.log_prob(lucid.tensor(1.0))  # log N(0 | 0, 1) - log(1)
tensor(-0.9189)

Used by 5

Constructors

1
dunder

__init__

→None
__init__(base_distribution: Distribution, transforms: Transform | list[Transform], validate_args: bool | None = None)

Construct a transformed distribution.

Parameters

base_distributionDistribution
Underlying distribution whose samples will be pushed through transforms.
transformsTransform | list[Transform]
A single transform or an ordered list applied left-to-right.
validate_argsbool | None= None
Forwarded to Distribution.

Properties

1
prop

support

→Constraint | None
support: Constraint | None

Support of the transformed distribution — codomain of the outer transform.

Instance methods

6
fn

cdf

→Tensor
cdf(value: Tensor)

Cumulative distribution function of the transformed variable.

Inverts the transform chain, scores the preimage under the base distribution's CDF, and flips the answer to 1−F1 - F when the chain is decreasing:

FY(y)=FX(T−1(y))(T increasing),1−FX(T−1(y))(T decreasing).F_Y(y) = F_X(T^{-1}(y)) \quad (T \text{ increasing}), \qquad 1 - F_X(T^{-1}(y)) \quad (T \text{ decreasing}).

Parameters

valueTensor
Point(s) of the transformed variable.

Returns

Tensor

Probabilities in [0,1][0, 1].

Raises

NotImplementedError
If the base distribution has no CDF, or a transform in the chain has no defined Transform.sign.

Examples

>>> import lucid
>>> from lucid.distributions import Normal
>>> from lucid.distributions.transforms import ExpTransform, TransformedDistribution
>>> d = TransformedDistribution(Normal(0.0, 1.0), [ExpTransform()])
>>> d.cdf(lucid.tensor(1.0))  # the median of a LogNormal(0, 1) is 1
tensor(0.5)
fn

expand

→Self
expand(batch_shape: Sequence[int], _instance: Self | None = None)

Return a new TransformedDistribution instance with parameters expanded to batch_shape.

fn

icdf

→Tensor
icdf(value: Tensor)

Inverse CDF (quantile function) of the transformed variable.

Flips the probability to 1−p1 - p when the chain is decreasing, takes the base distribution's quantile, and pushes it forward through the transforms.

Parameters

valueTensor
Probabilities in [0,1][0, 1].

Returns

Tensor

Quantiles of the transformed variable.

Raises

NotImplementedError
If the base distribution has no inverse CDF, or a transform in the chain has no defined Transform.sign.

Examples

>>> import lucid
>>> from lucid.distributions import Normal
>>> from lucid.distributions.transforms import AffineTransform, TransformedDistribution
>>> d = TransformedDistribution(Normal(0.0, 1.0), [AffineTransform(0.0, -1.0)])
>>> d.icdf(lucid.tensor(0.975))  # a decreasing map reflects the quantile
tensor(1.96)
fn

log_prob

→Tensor
log_prob(value: Tensor)

Evaluate the log-density of value under the transformed distribution.

Uses the change-of-variable formula:

log⁡p(y)=log⁡pbase(T−1(y))−∑ilog⁡ ⁣∣det⁡∂Ti/∂xi∣\log p(y) = \log p_{\text{base}}(T^{-1}(y)) - \sum_i \log\!\bigl|\det \partial T_i / \partial x_i\bigr|

with the chain unwound by walking the transforms in reverse.

fn

rsample

→Tensor
rsample(sample_shape: tuple[int, ...] = ())

Push a reparameterised base sample through the transform chain.

fn

sample

→Tensor
sample(sample_shape: tuple[int, ...] = ())

Push a (non-reparameterised) base sample through the transform chain.