laplace
laplace
¶
Laplace distribution for ngboost-lightning.
Laplace
¶
Bases: Distribution
Laplace distribution with log-scale parameterization.
Internal parameters are [loc, log_scale] where
scale = exp(log_scale). The log-link for scale avoids constrained
optimization during boosting.
The Laplace PDF is f(y) = 1/(2*b) * exp(-|y - loc| / b) where
b = scale.
| ATTRIBUTE | DESCRIPTION |
|---|---|
n_params |
Always 2 (loc and log_scale).
|
loc |
Location values, shape
TYPE:
|
scale |
Scale (diversity) values, shape
TYPE:
|
Construct Laplace from internal parameters.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Internal parameters, shape
TYPE:
|
Source code in ngboost_lightning/distributions/laplace.py
fit
staticmethod
¶
Estimate initial (loc, log_scale) from target data.
Uses the weighted median for loc and weighted MAD for scale, which are the MLEs for Laplace.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Target values, shape
TYPE:
|
sample_weight
|
Per-sample weights, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Parameter vector |
Source code in ngboost_lightning/distributions/laplace.py
score
¶
Per-sample negative log-likelihood.
NLL = log(2*scale) + |y - loc| / scale
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
NLL values, shape |
Source code in ngboost_lightning/distributions/laplace.py
d_score
¶
Analytical gradient of NLL w.r.t. [loc, log_scale].
Derivation
NLL = log(2) + log(scale) + |y - loc| / scale
d(NLL)/d(loc) = sign(loc - y) / scale d(NLL)/d(log_scale) = 1 - |y - loc| / scale (chain rule: d(NLL)/d(log_scale) = d(NLL)/d(scale) * scale = (-|y - loc| / scale^2) * scale = 1 - |y - loc| / scale)
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Gradient array, shape |
Source code in ngboost_lightning/distributions/laplace.py
metric
¶
Fisher Information: diag(1/scale^2, 1) for each sample.
For Laplace(loc, log_scale): FI[0,0] = 1/scale^2 (from the |y-loc|/scale term) FI[1,1] = 1 (from the log(scale) + |y-loc|/scale terms) FI is diagonal because the cross-derivatives vanish in expectation (sign(y-loc) is symmetric).
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
FI tensor, shape |
Source code in ngboost_lightning/distributions/laplace.py
natural_gradient
¶
Natural gradient via diagonal Fisher (fast path).
Since FI is diagonal
nat_grad[:, 0] = d_score[:, 0] * scale^2 nat_grad[:, 1] = d_score[:, 1] / 1.0
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Natural gradient, shape |
Source code in ngboost_lightning/distributions/laplace.py
mean
¶
Conditional mean (= loc for Laplace).
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Mean values, shape |
sample
¶
Draw n samples per distribution instance.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Number of samples to draw.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Samples, shape |
Source code in ngboost_lightning/distributions/laplace.py
cdf
¶
Cumulative distribution function.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Values at which to evaluate the CDF.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
CDF values, same shape as |
Source code in ngboost_lightning/distributions/laplace.py
ppf
¶
Percent point function (inverse CDF / quantile function).
| PARAMETER | DESCRIPTION |
|---|---|
q
|
Quantiles, values in [0, 1].
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Values at the given quantiles, same shape as |
Source code in ngboost_lightning/distributions/laplace.py
logpdf
¶
Log probability density function.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Values at which to evaluate.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Log-density values, same shape as |
Source code in ngboost_lightning/distributions/laplace.py
crps_score
¶
Per-sample CRPS for Laplace.
Closed form
CRPS = |y - loc| + scale * exp(-|y - loc|/scale) - 0.75*scale
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
CRPS values, shape |
Source code in ngboost_lightning/distributions/laplace.py
crps_d_score
¶
Gradient of CRPS w.r.t. [loc, log_scale].
Derivation (let a = |y - loc|, b = scale): CRPS = a + bexp(-a/b) - 0.75b
d(CRPS)/d(loc) = sign(loc - y) * (1 - exp(-a/b))
d(CRPS)/d(log_scale) = scale * (exp(-a/b)*(1 + a/b) - 0.75)
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Gradient array, shape |
Source code in ngboost_lightning/distributions/laplace.py
crps_metric
¶
Riemannian metric for CRPS natural gradient.
For Laplace(loc, log_scale), the CRPS metric is diagonal: met[0,0] = 0.5 (E[1 - exp(-|y-loc|/b)]^2 under Laplace) met[1,1] = 0.25 * scale^2 (from E[(exp(-a/b)*(1+a/b) - 0.75)^2] * scale^2)
Derived from the expected outer product of crps_d_score.
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Metric tensor, shape |
Source code in ngboost_lightning/distributions/laplace.py
crps_natural_gradient
¶
Natural gradient under CRPS metric (fast diagonal path).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Natural gradient, shape |