halfnormal
halfnormal
¶
Half-Normal distribution for ngboost-lightning.
HalfNormal
¶
Bases: Distribution
Half-Normal distribution with log-scale parameterization.
Internal parameter is [log_scale] where scale = exp(log_scale).
The PDF is sqrt(2/pi) / scale * exp(-y^2 / (2*scale^2)) for y >= 0.
Note on Fisher Information
For HalfNormal(log_scale), the Fisher Information is the constant
[[2]] for every sample. This means the natural gradient is simply
d_score / 2.
| ATTRIBUTE | DESCRIPTION |
|---|---|
n_params |
Always 1 (log_scale).
|
scale |
Scale parameter values, shape
TYPE:
|
Construct HalfNormal from internal parameters.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Internal parameters, shape
TYPE:
|
Source code in ngboost_lightning/distributions/halfnormal.py
fit
staticmethod
¶
Estimate initial log_scale from non-negative target data.
Uses scale = sqrt(E[y^2]) which is the MLE for HalfNormal.
| 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/halfnormal.py
score
¶
Per-sample negative log-likelihood.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
NLL values, shape |
Source code in ngboost_lightning/distributions/halfnormal.py
d_score
¶
Analytical gradient of NLL w.r.t. [log_scale].
Derivation
NLL = -log(sqrt(2/pi)) + log(scale) + y^2 / (2*scale^2) d(NLL)/d(scale) = 1/scale - y^2/scale^3 d(NLL)/d(log_scale) = scale * d(NLL)/d(scale) = 1 - y^2/scale^2
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Gradient array, shape |
Source code in ngboost_lightning/distributions/halfnormal.py
metric
¶
Fisher Information: [[2]] for each sample.
Derivation
E[(d(NLL)/d(log_scale))^2] = E[(1 - Y^2/s^2)^2] = 1 - 2*E[Y^2]/s^2 + E[Y^4]/s^4 = 1 - 2 + 3 = 2
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
FI tensor, shape |
Source code in ngboost_lightning/distributions/halfnormal.py
natural_gradient
¶
Natural gradient (fast path since FI is constant 2).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Natural gradient, shape |
Source code in ngboost_lightning/distributions/halfnormal.py
mean
¶
Conditional mean: scale * sqrt(2/pi).
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Mean values, shape |
Source code in ngboost_lightning/distributions/halfnormal.py
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/halfnormal.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/halfnormal.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/halfnormal.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 |