class

HalfNormal

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

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

If XN(0,σ2)X \sim \mathcal{N}(0, \sigma^2) then XHalfNormal(σ)|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),x0p(x; \sigma) = \frac{\sqrt{2}}{\sigma\sqrt{\pi}} \exp\!\left(-\frac{x^2}{2\sigma^2}\right), \quad x \geq 0

Log-PDF:

logp(x)=log2+logN(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(12/π)\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,))
>>> (samples >= 0.0).all()

Methods (6)

dunder

__init__

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

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

mean

Tensor
mean: Tensor
source

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

Returns

Tensor

Mean values of shape batch_shape.

prop

variance

Tensor
variance: Tensor
source

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

Returns

Tensor

Variance values of shape batch_shape.

fn

rsample

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

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

fn

log_prob

Tensor
log_prob(value: Tensor)
source

Log-probability density of the HalfNormal distribution.

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

Parameters

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

Returns

Tensor

Log-density values of the same shape as value.

fn

entropy

Tensor
entropy()
source

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