class

Normal

extendsExponentialFamily
Normal(loc: Tensor | float, scale: Tensor | float, validate_args: bool | None = None)
source

Univariate Gaussian (Normal) distribution N(μ,σ2)\mathcal{N}(\mu, \sigma^2).

The Normal distribution is arguably the most important distribution in probability and statistics. Its probability density function is the iconic bell curve:

p(xμ,σ)=12πσ2exp ⁣((xμ)22σ2),xRp(x \mid \mu, \sigma) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\left(-\frac{(x-\mu)^2}{2\sigma^2}\right), \quad x \in \mathbb{R}

where μ\mu is the location (mean) and σ>0\sigma > 0 is the scale (standard deviation).

Parameters

locTensor or float
Mean μ\mu of the distribution. May be any real value. Can be a scalar, a 1-D tensor of means (producing a batch), or any higher-dimensional tensor.
scaleTensor or float
Standard deviation σ>0\sigma > 0. Must be strictly positive. Broadcast-compatible with loc.
validate_argsbool or None= None
Enable parameter and support validation. Default None.

Attributes

locTensor
Broadcast-expanded mean tensor.
scaleTensor
Broadcast-expanded standard-deviation tensor.

Notes

Location-scale family

The Normal distribution is closed under linear transformations: if XN(μ,σ2)X \sim \mathcal{N}(\mu, \sigma^2) then aX+bN(aμ+b,a2σ2)aX + b \sim \mathcal{N}(a\mu + b, a^2\sigma^2). In particular the standard Normal Z=(Xμ)/σZ = (X - \mu)/\sigma satisfies ZN(0,1)Z \sim \mathcal{N}(0, 1).

Central Limit Theorem

By the CLT, the (rescaled) sum of nn i.i.d. random variables with finite mean and variance converges in distribution to a Normal. This explains why the Normal appears naturally across virtually all domains of science and engineering.

Reparameterisation

has_rsample = True — samples are drawn via the standard-Normal reparameterisation trick:

X=μ+σε,εN(0,1)X = \mu + \sigma \varepsilon, \quad \varepsilon \sim \mathcal{N}(0, 1)

so gradients flow through μ\mu and σ\sigma.

Exponential family

The Normal is an exponential-family distribution with natural parameters η1=μ/σ2\eta_1 = \mu/\sigma^2, η2=1/(2σ2)\eta_2 = -1/(2\sigma^2) and sufficient statistics T(x)=(x,x2)T(x) = (x,\, x^2).

Examples

>>> import lucid.distributions as dist
>>> d = dist.Normal(loc=0.0, scale=1.0)
>>> d.mean
Tensor(0.)
>>> d.variance
Tensor(1.)
>>> x = d.rsample((5,))   # 5 standard-Normal draws
>>> lp = d.log_prob(x)    # evaluate log-density at those points

Methods (10)

dunder

__init__

None
__init__(loc: Tensor | float, scale: Tensor | float, validate_args: bool | None = None)
source

Construct a Normal distribution.

Parameters

locTensor or float
Mean μ\mu.
scaleTensor or float
Standard deviation σ>0\sigma > 0.
validate_argsbool or None= None
Validate parameter constraints on construction.
prop

mean

Tensor
mean: Tensor
source

Mean of the Normal distribution.

For XN(μ,σ2)X \sim \mathcal{N}(\mu, \sigma^2),

E[X]=μ\mathbb{E}[X] = \mu

Returns

Tensor

The loc parameter tensor (shape batch_shape).

prop

mode

Tensor
mode: Tensor
source

Mode of the Normal distribution.

The Normal density is symmetric and unimodal; its unique maximum is at x=μx = \mu.

mode(X)=μ\text{mode}(X) = \mu

Returns

Tensor

The loc parameter tensor (shape batch_shape).

prop

variance

Tensor
variance: Tensor
source

Variance of the Normal distribution.

Var[X]=σ2\text{Var}[X] = \sigma^2

Returns

Tensor

Element-wise square of scale (shape batch_shape).

prop

stddev

Tensor
stddev: Tensor
source

Standard deviation of the Normal distribution.

std(X)=σ\text{std}(X) = \sigma

Overrides the base-class default (which would compute variance.sqrt()) for a cheaper, exact result.

Returns

Tensor

The scale parameter tensor (shape batch_shape).

fn

rsample

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

Draw reparameterised samples via the location-scale trick.

Samples are generated as

X=μ+σε,εN(0,1)X = \mu + \sigma \varepsilon, \quad \varepsilon \sim \mathcal{N}(0, 1)

Because the stochasticity is isolated in ε\varepsilon (which does not depend on the parameters), gradients flow back through both loc (μ\mu) and scale (σ\sigma).

Parameters

sample_shapetuple[int, ...]= ()
Shape prefix for the batch of samples.

Returns

Tensor

Samples of shape sample_shape + batch_shape, attached to the autograd graph.

fn

log_prob

Tensor
log_prob(value: Tensor)
source

Log-probability density of the Normal distribution.

logp(xμ,σ)=(xμ)22σ2logσ12log(2π)\log p(x \mid \mu, \sigma) = -\frac{(x - \mu)^2}{2\sigma^2} - \log \sigma - \frac{1}{2}\log(2\pi)

This is computed in a numerically stable form using log_scale rather than squaring and dividing.

Parameters

valueTensor
Observation(s) xRx \in \mathbb{R}.

Returns

Tensor

Log-density at value, shape broadcast(value, batch_shape).

fn

cdf

Tensor
cdf(value: Tensor)
source

Cumulative distribution function of the Normal distribution.

Expressed in terms of the Gauss error function:

F(x)=12[1+erf ⁣(xμσ2)]F(x) = \frac{1}{2}\left[1 + \operatorname{erf} \!\left(\frac{x - \mu}{\sigma\sqrt{2}}\right)\right]

Parameters

valueTensor
Evaluation point(s) xx.

Returns

Tensor

Probabilities in (0,1)(0, 1).

fn

icdf

Tensor
icdf(value: Tensor)
source

Inverse CDF (quantile function / probit function) of the Normal.

Q(p)=μ+σ2erfinv(2p1)Q(p) = \mu + \sigma\,\sqrt{2}\,\operatorname{erfinv}(2p - 1)

Parameters

valueTensor
Probability values p(0,1)p \in (0, 1).

Returns

Tensor

Corresponding quantiles x=Q(p)x = Q(p).

fn

entropy

Tensor
entropy()
source

Shannon differential entropy of the Normal distribution.

The Normal maximises entropy among all distributions with fixed mean and variance. The closed-form entropy is

H[X]=12ln(2πeσ2)=12+12ln(2π)+lnσH[X] = \frac{1}{2}\ln(2\pi e\sigma^2) = \frac{1}{2} + \frac{1}{2}\ln(2\pi) + \ln\sigma

Measured in nats.

Returns

Tensor

Entropy values of shape batch_shape.