Skip to content
class

HalfNormal

extendsDistribution
HalfNormal(scale: Tensor | float, validate_args: bool | None = None)

Half-Normal distribution — the absolute value of a zero-mean Normal.

If X∼N(0,σ2)X \sim \mathcal{N}(0, \sigma^2) then ∣X∣∼HalfNormal⁡(σ)|X| \sim \operatorname{HalfNormal}(\sigma). The distribution is supported on [0,∞)[0, \infty) and arises naturally as a scale prior in hierarchical Bayesian models (it is weakly informative while keeping probability mass away from zero).

Parameters

scaleTensor | float
Scale parameter σ>0\sigma > 0 — the standard deviation of the underlying zero-mean Normal.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Attributes

scaleTensor
Scale parameter σ\sigma.

Notes

PDF:

p(x;σ)=2σπexp⁡ ⁣(−x22σ2),x≥0p(x; \sigma) = \frac{\sqrt{2}}{\sigma\sqrt{\pi}} \exp\!\left(-\frac{x^2}{2\sigma^2}\right), \quad x \geq 0

Log-PDF:

log⁡p(x)=log⁡2+log⁡N(x;0,σ2)\log p(x) = \log 2 + \log\mathcal{N}(x; 0, \sigma^2)

where the right-hand side is the log-density of the full Normal evaluated at xx.

Moments:

  • Mean: E[X]=σ2/πE[X] = \sigma\sqrt{2/\pi}
  • Variance: Var⁡[X]=σ2(1−2/π)\operatorname{Var}[X] = \sigma^2(1 - 2/\pi)

Entropy:

H[X]=12log⁡ ⁣(πeσ22)H[X] = \tfrac{1}{2}\log\!\left(\frac{\pi e \sigma^2}{2}\right)

Reparameterised sampling takes abs of a Normal sample, so gradients flow through σ\sigma unobstructed.

Examples

>>> import lucid
>>> from lucid.distributions import HalfNormal
>>> dist = HalfNormal(scale=1.0)
>>> samples = dist.rsample((300,))
>>> bool((samples >= 0.0).all())
True

Used by 2

Constructors

1
dunder

__init__

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

Initialise a HalfNormal distribution.

Parameters

scaleTensor | float
Scale parameter σ>0\sigma > 0 — the standard deviation of the underlying zero-mean Normal distribution.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Properties

3
prop

mean

→Tensor
mean: Tensor

Mean of the HalfNormal distribution: E[X]=σ2/πE[X] = \sigma\sqrt{2/\pi}.

Returns

Tensor

Mean values of shape batch_shape.

prop

mode

→Tensor
mode: Tensor

Mode of the half-Normal distribution: 0, where the density peaks.

prop

variance

→Tensor
variance: Tensor

Variance of the HalfNormal distribution: Var⁡[X]=σ2(1−2/π)\operatorname{Var}[X] = \sigma^2(1 - 2/\pi).

Returns

Tensor

Variance values of shape batch_shape.

Instance methods

6
fn

cdf

→Tensor
cdf(value: Tensor)

Cumulative distribution function of the half-Normal distribution.

F(x)=2Φ(x/σ)−1=erf⁡ ⁣(xσ2),x≥0F(x) = 2\Phi(x/\sigma) - 1 = \operatorname{erf}\!\left(\frac{x}{\sigma\sqrt 2}\right), \quad x \geq 0

Returns probabilities in [0,1][0, 1]; 0 below 0.

Examples

>>> HalfNormal(scale=1.0).cdf(lucid.tensor(1.0))
tensor(0.6827)
fn

entropy

→Tensor
entropy()

Entropy of the HalfNormal distribution.

H[X]=12log⁡ ⁣(πeσ22)H[X] = \tfrac{1}{2}\log\!\left(\frac{\pi e \sigma^2}{2}\right)

Returns

Tensor

Entropy values of shape batch_shape (nats).

fn

expand

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

Return a new HalfNormal with batch dimensions expanded to batch_shape.

fn

icdf

→Tensor
icdf(value: Tensor)

Quantile function of the half-Normal distribution.

Q(p)=σΦ−1 ⁣(1+p2)Q(p) = \sigma \Phi^{-1}\!\left(\frac{1 + p}{2}\right)

Returns quantiles in [0,∞][0, \infty].

Examples

>>> HalfNormal(scale=1.0).icdf(lucid.tensor(0.5))
tensor(0.6745)
fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-probability density of the HalfNormal distribution.

log⁡p(x)=log⁡2+log⁡N(x;0,σ2)\log p(x) = \log 2 + \log\mathcal{N}(x; 0, \sigma^2)

Parameters

valueTensor
Non-negative points x≥0x \geq 0 at which to evaluate.

Returns

Tensor

Log-density values of the same shape as value; -inf below 0, where the density is 0.

fn

rsample

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

Draw reparameterised samples by folding a Normal sample.

Computes abs of a Normal sample, so gradients propagate through σ\sigma unobstructed.

Parameters

sample_shapetuple[int, ...]= ()
Leading shape of the output sample batch.

Returns

Tensor

Non-negative samples of shape (*sample_shape, *batch_shape).