Skip to content
class

Uniform

extendsDistribution
Uniform(low: Tensor | float, high: Tensor | float, validate_args: bool | None = None)

Continuous Uniform distribution on the half-open interval [a,b)[a, b).

Maximum-entropy distribution over a bounded interval of fixed length: every point in [a,b)[a, b) is equally likely. Widely used as a non-informative prior, as the source of randomness for inverse-CDF sampling of arbitrary distributions, and as a quasi-Monte-Carlo integration measure.

Parameters

lowTensor or float
Lower bound a∈Ra \in \mathbb{R}.
highTensor or float
Upper bound b∈Rb \in \mathbb{R} with b>ab > a.
validate_argsbool= None
If True, validate parameter constraints at construction time.

Notes

Probability density (constant on the support):

p(x;a,b)={1b−ax∈[a,b)0otherwisep(x; a, b) = \begin{cases} \dfrac{1}{b - a} & x \in [a, b) \\[2pt] 0 & \text{otherwise} \end{cases}

Cumulative distribution:

F(x;a,b)=clip ⁣(x−ab−a,  0,  1)F(x; a, b) = \mathrm{clip}\!\left(\frac{x - a}{b - a},\; 0,\; 1\right)

Moments:

E[X]=a+b2,Var[X]=(b−a)212,H[X]=log⁡(b−a)\mathbb{E}[X] = \frac{a + b}{2}, \qquad \mathrm{Var}[X] = \frac{(b - a)^2}{12}, \qquad H[X] = \log(b - a)

Higher moments (centred at the midpoint m=(a+b)/2m = (a+b)/2): E[(X−m)2k]=(b−a)2k/(2k+1) 22k\mathbb{E}[(X-m)^{2k}] = (b-a)^{2k}/(2k+1)\,2^{2k}; odd central moments are zero by symmetry.

The Uniform is the maximum-entropy distribution over a bounded interval of fixed length, and it underpins the inverse-CDF (Smirnov) sampling identity: if U∼Uniform(0,1)U \sim \mathrm{Uniform}(0, 1) and F−1F^{-1} is the quantile function of any 1-D distribution, then F−1(U)F^{-1}(U) has that distribution.

Reparameterised sampling uses the location-scale transform X=a+(b−a)UX = a + (b - a) U so gradients flow through both endpoints.

Examples

>>> import lucid
>>> from lucid.distributions import Uniform
>>> d = Uniform(low=0.0, high=1.0)
>>> d.mean
tensor(0.5)
>>> d.rsample((4,)).shape
(4,)
>>> d.log_prob(lucid.tensor(0.5))
tensor(0.)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(low: Tensor | float, high: Tensor | float, validate_args: bool | None = None)

Construct a Uniform distribution on [low, high).

Parameters

lowTensor | float
Lower bound a∈Ra \in \mathbb{R}.
highTensor | float
Upper bound b∈Rb \in \mathbb{R} with b>ab > a.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Notes

The Uniform distribution has constant PDF on its support:

p(x;a,b)=1b−a,x∈[a,b)p(x; a, b) = \frac{1}{b - a}, \quad x \in [a, b)

All values in the interval are equally likely. The distribution is the maximum-entropy distribution over a bounded interval, and is widely used for non-informative priors and Monte-Carlo sampling via the inverse-CDF method.

Examples

>>> from lucid.distributions import Uniform
>>> d = Uniform(low=0.0, high=1.0)
>>> d.mean
tensor(0.5)

Properties

4
prop

mean

→Tensor
mean: Tensor

Expected value of the Uniform distribution.

E[X]=a+b2E[X] = \frac{a + b}{2}

Returns

Tensor

Midpoint (a+b)/2(a+b)/2, shape batch_shape.

Examples

>>> Uniform(low=2.0, high=8.0).mean
tensor(5.)
prop

mode

→Tensor
mode: Tensor

Mode of the Uniform distribution — nan.

Every point of [low, high] is equally likely, so there is no single mode; nan of the batch shape, as the reference framework answers.

prop

support

→Constraint
support: Constraint

The support constraint of the Uniform distribution.

Returns

Constraint

interval(low, high) — the bounds are the parameter tensors, so each batch member is held to its own interval. Closed at both ends, as in the reference framework, although log_prob scores high itself as -inf.

prop

variance

→Tensor
variance: Tensor

Variance of the Uniform distribution.

Var⁡[X]=(b−a)212\operatorname{Var}[X] = \frac{(b - a)^2}{12}

Returns

Tensor

Variance (b−a)2/12(b-a)^2/12, shape batch_shape.

Examples

>>> Uniform(low=0.0, high=1.0).variance  # 1/12 ≈ 0.0833
tensor(0.08333)

Instance methods

6
fn

cdf

→Tensor
cdf(value: Tensor)

Cumulative distribution function of the Uniform distribution.

F(x;a,b)=clip ⁣(x−ab−a,  0,  1)F(x; a, b) = \text{clip}\!\left(\frac{x - a}{b - a},\; 0,\; 1\right)

Parameters

valueTensor
Values at which to evaluate the CDF.

Returns

Tensor

CDF values in [0,1][0, 1], shape batch_shape.

Examples

>>> Uniform(low=0.0, high=4.0).cdf(lucid.tensor(1.0))
tensor(0.25)
fn

entropy

→Tensor
entropy()

Shannon entropy of the Uniform distribution (in nats).

H(X)=log⁡(b−a)H(X) = \log(b - a)

The Uniform distribution maximises entropy among all distributions supported on a bounded interval of fixed length.

Returns

Tensor

Entropy in nats, shape batch_shape.

Examples

>>> Uniform(low=0.0, high=1.0).entropy()  # log(1) = 0.0
tensor(0.)
fn

expand

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

Return a new Uniform with batch dimensions expanded to batch_shape.

fn

icdf

→Tensor
icdf(value: Tensor)

Inverse CDF (quantile function) of the Uniform distribution.

F−1(u;a,b)=a+u⋅(b−a)F^{-1}(u; a, b) = a + u \cdot (b - a)

Parameters

valueTensor
Probability values u∈[0,1]u \in [0, 1].

Returns

Tensor

Quantiles in [a,b][a, b], shape batch_shape.

Examples

>>> Uniform(low=0.0, high=10.0).icdf(lucid.tensor(0.5))
tensor(5.)
fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-density of value under the Uniform distribution.

Returns −log⁡(b−a)-\log(b - a) inside the support [a,b)[a, b) and −∞-\infty outside:

log⁡p(x;a,b)={−log⁡(b−a)x∈[a,b)−∞otherwise\log p(x; a, b) = \begin{cases} -\log(b - a) & x \in [a, b) \\ -\infty & \text{otherwise} \end{cases}

Parameters

valueTensor
Values at which to evaluate the log-density.

Returns

Tensor

Element-wise log-densities, shape batch_shape.

Examples

>>> Uniform(low=0.0, high=2.0).log_prob(lucid.tensor(1.0))
tensor(-0.6931)
fn

rsample

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

Reparameterised sample using the location-scale transform.

X=a+(b−a)⋅U,U∼Uniform(0,1)X = a + (b - a) \cdot U, \quad U \sim \text{Uniform}(0, 1)

Gradients flow through both aa and bb.

Parameters

sample_shapetuple[int, ...]= ()
Leading shape dimensions for the sample batch. Default is ().

Returns

Tensor

Samples in [a,b)[a, b) of shape sample_shape + batch_shape.

Examples

>>> d = Uniform(low=-1.0, high=1.0)
>>> x = d.rsample((100,))
>>> bool(((x >= -1.0) & (x < 1.0)).all())
True