gamma
gamma
¶
Gamma distribution for ngboost-lightning.
Gamma
¶
Bases: Distribution
Gamma distribution with log-link parameterization.
Internal parameters are [log_alpha, log_beta] where
alpha = exp(log_alpha) is the shape parameter and
beta = exp(log_beta) is the rate parameter. The log-links keep
both parameters positive during unconstrained boosting.
Note on Fisher Information
For Gamma(log_alpha, log_beta), the Fisher Information is
non-diagonal. The off-diagonal entries are -alpha.
This distribution relies on the base class np.linalg.solve
for the natural gradient — no fast-path override.
| ATTRIBUTE | DESCRIPTION |
|---|---|
n_params |
Always 2 (log_alpha and log_beta).
|
alpha |
Shape parameter values, shape
TYPE:
|
beta |
Rate parameter values, shape
TYPE:
|
Construct Gamma from internal parameters.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Internal parameters, shape
TYPE:
|
Source code in ngboost_lightning/distributions/gamma.py
fit
staticmethod
¶
Estimate initial (log_alpha, log_beta) via method of moments.
| 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/gamma.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/gamma.py
d_score
¶
Analytical gradient of NLL w.r.t. [log_alpha, log_beta].
Derivation
NLL = -(alpha-1)log(y) + betay - alpha*log(beta) + gammaln(alpha)
d(NLL)/d(alpha) = -log(y) - log(beta) + digamma(alpha) d(NLL)/d(log_alpha) = d(NLL)/d(alpha) * alpha = alpha * (digamma(alpha) - log(beta) - log(y))
d(NLL)/d(beta) = y - alpha/beta d(NLL)/d(log_beta) = d(NLL)/d(beta) * beta = beta*y - alpha
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Gradient array, shape |
Source code in ngboost_lightning/distributions/gamma.py
metric
¶
Fisher Information for (log_alpha, log_beta) parameterization.
The standard-parameterization Fisher Information for Gamma(alpha, beta) is::
FI = [[trigamma(alpha), -1 / beta], [-1 / beta, alpha / beta ^ 2]]
Applying the Jacobian J = diag(alpha, beta) for the log-link::
FI_log = J ^ T @ FI @ J
gives::
FI_log = [[alpha ^ 2 * trigamma(alpha), -alpha], [-alpha, alpha]]
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
FI tensor, shape |
Source code in ngboost_lightning/distributions/gamma.py
mean
¶
Conditional mean (point prediction): alpha / beta.
| 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/gamma.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/gamma.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/gamma.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/gamma.py
crps_score
¶
Per-sample CRPS for Gamma.
Closed form (Scheuerer & Hamill 2015):
CRPS = y*(2*F(y;a,b) - 1)
- (a/b)*(2*F(y;a+1,b) - 1)
- 1/(b * Beta(a, 0.5))
where a = alpha (shape), b = beta (rate),
F(y;a,b) is the Gamma CDF with shape a and rate b.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
CRPS values, shape |
Source code in ngboost_lightning/distributions/gamma.py
crps_d_score
¶
Gradient of CRPS w.r.t. [log_alpha, log_beta].
Uses central finite differences on the internal parameters for
robustness. The analytical gradient of the Gamma CDF w.r.t. the
shape parameter involves the derivative of the regularized
incomplete gamma function, which is numerically delicate. Finite
differences are accurate to O(eps^2) and fast enough for
the boosting loop since CRPS evaluation is cheap.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Gradient array, shape |
Source code in ngboost_lightning/distributions/gamma.py
crps_metric
¶
Riemannian metric for CRPS natural gradient (MC estimate).
Uses a Monte Carlo estimate: sample from the distribution, compute CRPS gradients, and average the outer product.
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Metric tensor, shape |