class

Uniform

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

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 aRa \in \mathbb{R}.
highTensor or float
Upper bound bRb \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)={1bax[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 ⁣(xaba,  0,  1)F(x; a, b) = \mathrm{clip}\!\left(\frac{x - a}{b - a},\; 0,\; 1\right)

Moments:

E[X]=a+b2,Var[X]=(ba)212,H[X]=log(ba)\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[(Xm)2k]=(ba)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 UUniform(0,1)U \sim \mathrm{Uniform}(0, 1) and F1F^{-1} is the quantile function of any 1-D distribution, then F1(U)F^{-1}(U) has that distribution.

Reparameterised sampling uses the location-scale transform X=a+(ba)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,))
Tensor([...])
>>> d.log_prob(lucid.tensor(0.5))
Tensor(0.0)

Methods (9)

dunder

__init__

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

Construct a Uniform distribution on [low, high).

Parameters

lowTensor | float
Lower bound aRa \in \mathbb{R}.
highTensor | float
Upper bound bRb \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)=1ba,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)
prop

support

Constraint
support: Constraint
source

The support constraint of the Uniform distribution.

Returns the real constraint as a conservative fallback, because the bounds may be arbitrary tensors and a precise interval constraint would require tensor-aware bound tracking.

Returns

Constraint

The real constraint object.

prop

mean

Tensor
mean: Tensor
source

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.0)
prop

variance

Tensor
variance: Tensor
source

Variance of the Uniform distribution.

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

Returns

Tensor

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

Examples

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

rsample

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

Reparameterised sample using the location-scale transform.

X=a+(ba)U,UUniform(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,))
>>> (x >= -1.0).all() and (x < 1.0).all()
True
fn

log_prob

Tensor
log_prob(value: Tensor)
source

Log-density of value under the Uniform distribution.

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

logp(x;a,b)={log(ba)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

cdf

Tensor
cdf(value: Tensor)
source

Cumulative distribution function of the Uniform distribution.

F(x;a,b)=clip ⁣(xaba,  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

icdf

Tensor
icdf(value: Tensor)
source

Inverse CDF (quantile function) of the Uniform distribution.

F1(u;a,b)=a+u(ba)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.0)
fn

entropy

Tensor
entropy()
source

Shannon entropy of the Uniform distribution (in nats).

H(X)=log(ba)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.0)