Skip to content
class

Laplace

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

Laplace (double-exponential) distribution on R\mathbb{R}.

Symmetric continuous distribution composed of two mirrored Exponential densities placed back-to-back at the location μ\mu. It is the maximum-entropy distribution with a given mean and a given mean absolute deviation, and is the prior whose negative log-density induces the L1 (Lasso) penalty.

Parameters

locTensor or float
Location parameter μ∈R\mu \in \mathbb{R} (mean = median = mode).
scaleTensor or float
Scale / diversity parameter b>0b > 0.
validate_argsbool= None
If True, validate parameter constraints at construction time.

Notes

Probability density:

p(x;μ,b)=12bexp⁡ ⁣(−∣x−μ∣b)p(x; \mu, b) = \frac{1}{2b} \exp\!\left(-\frac{|x - \mu|}{b}\right)

Moments:

E[X]=μ,Var[X]=2b2,H[X]=1+log⁡(2b)\mathbb{E}[X] = \mu, \qquad \mathrm{Var}[X] = 2 b^2, \qquad H[X] = 1 + \log(2b)

The Laplace has heavier tails than the Normal: its kurtosis is 6 vs. 3 for the Normal, and its tails decay exponentially rather than quadratically in log-space.

Sampling uses the closed-form inverse CDF:

X=μ−bsgn⁡(U−12)log⁡(1−2∣U−12∣),U∼Uniform(0,1)X = \mu - b \operatorname{sgn}(U - \tfrac{1}{2}) \log(1 - 2|U - \tfrac{1}{2}|), \quad U \sim \mathrm{Uniform}(0, 1)

so rsample is exact and gradient-friendly through both μ\mu and bb.

Examples

>>> import lucid
>>> from lucid.distributions import Laplace
>>> d = Laplace(loc=0.0, scale=1.0)
>>> d.mean
tensor(0.)
>>> d.rsample((4,)).shape
(4,)
>>> d.log_prob(lucid.tensor(0.0))
tensor(-0.6931)

Used by 3

Constructors

1
dunder

__init__

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

Construct a Laplace distribution.

Parameters

locTensor | float
Location parameter μ∈R\mu \in \mathbb{R} (the mean and median of the distribution).
scaleTensor | float
Scale (diversity) parameter b>0b > 0.
validate_argsbool | None= None
If True, validate parameter constraints at construction time.

Notes

The Laplace distribution has PDF:

p(x;μ,b)=12bexp⁡ ⁣(−∣x−μ∣b)p(x; \mu, b) = \frac{1}{2b} \exp\!\left(-\frac{|x - \mu|}{b}\right)

It is also called the double-exponential distribution because it resembles two mirrored Exponential densities placed back-to-back at the location μ\mu. It is frequently used in robust statistics and as the prior inducing L1 (Lasso) regularisation.

Examples

>>> from lucid.distributions import Laplace
>>> d = Laplace(loc=0.0, scale=1.0)
>>> d.mean
tensor(0.)

Properties

3
prop

mean

→Tensor
mean: Tensor

Expected value of the Laplace distribution.

E[X]=μE[X] = \mu

Returns

Tensor

Location parameter μ\mu, shape batch_shape.

Examples

>>> Laplace(loc=3.0, scale=1.0).mean
tensor(3.)
prop

mode

→Tensor
mode: Tensor

Mode of the Laplace distribution: the location μ\mu.

prop

variance

→Tensor
variance: Tensor

Variance of the Laplace distribution.

Var⁡[X]=2b2\operatorname{Var}[X] = 2b^2

Returns

Tensor

Variance 2b22b^2, shape batch_shape.

Examples

>>> Laplace(loc=0.0, scale=1.0).variance
tensor(2.)

Instance methods

6
fn

cdf

→Tensor
cdf(value: Tensor)

Cumulative distribution function of the Laplace distribution.

F(x;μ,b)=12+12sgn⁡(x−μ)(1−e−∣x−μ∣/b)F(x; \mu, b) = \frac{1}{2} + \frac{1}{2} \operatorname{sgn}(x - \mu) \left(1 - e^{-|x-\mu|/b}\right)

Parameters

valueTensor
Real values at which to evaluate the CDF.

Returns

Tensor

CDF values in [0,1][0, 1], shape batch_shape.

Notes

The sign in front of the second term is +. It was - in both the formula above and the line below it, which makes the function decreasing: cdf(-3) answered 0.975 and cdf(3) answered 0.025, exactly reversed. It stayed in [0,1][0, 1] and was symmetric about 0.5 at the median, so nothing looked odd until the audit checked that a CDF increases.

fn

entropy

→Tensor
entropy()

Shannon entropy of the Laplace distribution (in nats).

H(X)=1+log⁡(2b)H(X) = 1 + \log(2b)

Returns

Tensor

Entropy in nats, shape batch_shape.

Examples

>>> Laplace(loc=0.0, scale=1.0).entropy()  # 1 + log(2) ≈ 1.693
tensor(1.693)
fn

expand

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

Return a new Laplace with batch dimensions expanded to batch_shape.

fn

icdf

→Tensor
icdf(value: Tensor)

Quantile function of the Laplace distribution.

Q(p)=μ−bsgn⁡(p−12)ln⁡ ⁣(1−2∣p−12∣)Q(p) = \mu - b \operatorname{sgn}(p - \tfrac12) \ln\!\left(1 - 2\left|p - \tfrac12\right|\right)

Returns quantiles; ∓∞\mp\infty at p=0p = 0 and 11.

Examples

>>> Laplace(loc=0.0, scale=1.0).icdf(lucid.tensor(0.75))  # ln 2
tensor(0.6931)
fn

log_prob

→Tensor
log_prob(value: Tensor)

Log-density of value under the Laplace distribution.

log⁡p(x;μ,b)=−∣x−μ∣b−log⁡(2b)\log p(x; \mu, b) = -\frac{|x - \mu|}{b} - \log(2b)

Parameters

valueTensor
Real-valued observations.

Returns

Tensor

Element-wise log-densities, shape batch_shape.

Examples

>>> Laplace(loc=0.0, scale=1.0).log_prob(lucid.tensor(0.0))
tensor(-0.6931)
fn

rsample

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

Reparameterised sample via the inverse-CDF method.

Uses the closed-form quantile function:

X=μ−bsgn⁡(u)log⁡(1−2∣u∣)X = \mu - b \operatorname{sgn}(u) \log(1 - 2|u|)

where u=U−0.5u = U - 0.5 and U∼Uniform(0,1)U \sim \text{Uniform}(0, 1). Gradients flow through both μ\mu and bb.

Parameters

sample_shapetuple[int, ...]= ()
Leading shape dimensions for the sample batch. Default is ().

Returns

Tensor

Reparameterised samples of shape sample_shape + batch_shape.

Examples

>>> d = Laplace(loc=0.0, scale=1.0)
>>> x = d.rsample((200,))