Skip to content
class

HalfCauchy

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

Half-Cauchy distribution — the absolute value of a zero-location Cauchy.

If X∼Cauchy⁡(0,s)X \sim \operatorname{Cauchy}(0, s) then ∣X∣∼HalfCauchy⁡(s)|X| \sim \operatorname{HalfCauchy}(s). The distribution is supported on [0,∞)[0, \infty) and has no finite moments (mean and variance are undefined, like the full Cauchy).

It is a popular weakly-informative prior for scale parameters in Bayesian hierarchical models — heavy tails allow occasional large scales while the mode at zero permits near-zero scales.

Parameters

scaleTensor | float
Scale parameter s>0s > 0. This is the half-width at half-maximum of the Cauchy density folded onto [0,∞)[0, \infty).
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Attributes

scaleTensor
Scale parameter ss.

Notes

PDF:

p(x;s)=2πs(1+(x/s)2),x≥0p(x; s) = \frac{2}{\pi s \left(1 + (x/s)^2\right)}, \quad x \geq 0

Log-PDF:

log⁡p(x)=log⁡2+log⁡Cauchy⁡(x;0,s)\log p(x) = \log 2 + \log\operatorname{Cauchy}(x; 0, s)

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

Neither the mean nor the variance exists because the Cauchy distribution lacks finite moments of any positive order.

Reparameterised sampling folds a Cauchy sample via abs, so gradients propagate through the scale parameter.

Examples

>>> import lucid
>>> from lucid.distributions import HalfCauchy
>>> dist = HalfCauchy(scale=1.0)
>>> samples = dist.rsample((200,))
>>> 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 HalfCauchy distribution.

Parameters

scaleTensor | float
Scale parameter s>0s > 0. The half-width at half-maximum of the Cauchy density folded onto [0,∞)[0, \infty).
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Properties

4
prop

mean

→Tensor
mean: Tensor

Mean of the half-Cauchy — infinite.

Folding the Cauchy onto [0,∞)[0, \infty) removes the cancellation that made the full distribution's mean undefined and leaves a single divergent tail, so the answer is +∞+\infty rather than NaN.

Returns

Tensor

Tensor of inf with shape batch_shape.

prop

mode

→Tensor
mode: Tensor

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

prop

stddev

→Tensor
stddev: Tensor

Standard deviation of the half-Cauchy — infinite.

prop

variance

→Tensor
variance: Tensor

Variance of the half-Cauchy — infinite.

Instance methods

6
fn

cdf

→Tensor
cdf(value: Tensor)

Cumulative distribution function of the half-Cauchy distribution.

F(x)=2πarctan⁡ ⁣(xs),x≥0F(x) = \frac{2}{\pi} \arctan\!\left(\frac{x}{s}\right), \quad x \geq 0

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

Examples

>>> HalfCauchy(scale=1.0).cdf(lucid.tensor(1.0))  # the median is s
tensor(0.5)
fn

entropy

→Tensor
entropy()

Entropy of the half-Cauchy distribution.

H[X]=ln⁡(2πs)H[X] = \ln(2\pi s)

The full Cauchy's ln⁡(4πs)\ln(4\pi s) less ln⁡2\ln 2: folding halves the support and doubles the density on it.

Returns

Tensor

Entropy values of shape batch_shape (nats).

Examples

>>> HalfCauchy(scale=1.0).entropy()  # ln(2 pi)
tensor(1.838)
fn

expand

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

Return a new HalfCauchy with batch dimensions expanded to batch_shape.

fn

icdf

→Tensor
icdf(value: Tensor)

Quantile function of the half-Cauchy distribution.

Q(p)=stan⁡ ⁣(πp2)Q(p) = s \tan\!\left(\frac{\pi p}{2}\right)

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

Examples

>>> HalfCauchy(scale=1.0).icdf(lucid.tensor(0.5))
tensor(1.)
fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-probability density of the HalfCauchy distribution.

log⁡p(x)=log⁡2+log⁡Cauchy⁡(x;0,s)\log p(x) = \log 2 + \log\operatorname{Cauchy}(x; 0, s)

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 Cauchy sample.

Computes abs of a zero-location Cauchy sample, so gradients propagate through the scale parameter.

Parameters

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

Returns

Tensor

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