Skip to content
class

Normal

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

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),x∈Rp(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

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

Notes

Location-scale family

The Normal distribution is closed under linear transformations: if X∼N(μ,σ2)X \sim \mathcal{N}(\mu, \sigma^2) then aX+b∼N(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 Z∼N(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

Used by 5

Constructors

1
dunder

__init__

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

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.

Properties

4
prop

mean

→Tensor
mean: Tensor

Mean of the Normal distribution.

For X∼N(μ,σ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

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

stddev

→Tensor
stddev: Tensor

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

prop

variance

→Tensor
variance: Tensor

Variance of the Normal distribution.

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

Returns

Tensor

Element-wise square of scale (shape batch_shape).

Instance methods

6
fn

cdf

→Tensor
cdf(value: Tensor)

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

entropy

→Tensor
entropy()

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.

fn

expand

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

Return a new Normal with batch dimensions expanded to batch_shape.

fn

icdf

→Tensor
icdf(value: Tensor)

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

Q(p)=μ+σ 2 erfinv⁡(2p−1)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

log_prob

→Tensor
log_prob(value: Tensor)

Log-probability density of the Normal distribution.

log⁡p(x∣μ,σ)=−(x−μ)22σ2−log⁡σ−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) x∈Rx \in \mathbb{R}.

Returns

Tensor

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

fn

rsample

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

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.