TransformedDistribution
DistributionTransformedDistribution(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_ distributionDistributiontransforms.validate_ argsbool= NoneDistribution.Notes
Sampling (with the composite bijector):
Reparameterised sampling is available iff the base distribution
supports it (has_rsample is forwarded).
Density (change of variables):
where and . 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__init__
→None__init__(base_distribution: Distribution, transforms: Transform | list[Transform], validate_args: bool | None = None)Construct a transformed distribution.
Parameters
base_ distributionDistributiontransforms.validate_ argsbool | None= NoneDistribution.Properties
1Instance methods
6Cumulative distribution function of the transformed variable.
Inverts the transform chain, scores the preimage under the base distribution's CDF, and flips the answer to when the chain is decreasing:
Parameters
valueTensorReturns
TensorProbabilities in .
Raises
NotImplementedErrorTransform.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)Return a new TransformedDistribution instance with parameters expanded to batch_shape.
Inverse CDF (quantile function) of the transformed variable.
Flips the probability to when the chain is decreasing, takes the base distribution's quantile, and pushes it forward through the transforms.
Parameters
valueTensorReturns
TensorQuantiles of the transformed variable.
Raises
NotImplementedErrorTransform.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)Evaluate the log-density of value under the transformed distribution.
Uses the change-of-variable formula:
with the chain unwound by walking the transforms in reverse.
Push a reparameterised base sample through the transform chain.
Push a (non-reparameterised) base sample through the transform chain.