categorical
categorical
¶
Categorical distribution for ngboost-lightning (binary and multiclass).
Provides a k_categorical(K) factory that creates a Categorical distribution
class with n_params = K - 1 for K classes. A convenience alias
Bernoulli = k_categorical(2) covers binary classification.
Internal parameters are logits [logit_1, ..., logit_{K-1}] with class 0
as the reference (logit_0 = 0). Probabilities are computed via softmax over
all K logits.
Bernoulli
module-attribute
¶
Bernoulli = k_categorical(2)
Bernoulli distribution for binary classification.
Convenience alias for k_categorical(2). Has n_params = 1 with
a single logit parameter. probs[:, 1] gives the probability of
class 1.
Categorical
¶
Bases: Distribution
Categorical distribution with softmax-logit parameterization.
Do not instantiate this class directly. Use k_categorical(K) to
create a class for a specific number of classes, or use the
Bernoulli alias for binary classification.
Internal parameters are [logit_1, ..., logit_{K-1}] with class 0
as the reference (logit_0 = 0). Probabilities are computed via
softmax over all K logits.
| ATTRIBUTE | DESCRIPTION |
|---|---|
K |
Number of classes (set by
TYPE:
|
n_params |
TYPE:
|
probs |
Class probabilities, shape
TYPE:
|
logits |
Full logit vector (including reference), shape
TYPE:
|
Construct Categorical from internal logit parameters.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Internal parameters, shape
TYPE:
|
Source code in ngboost_lightning/distributions/categorical.py
fit
staticmethod
¶
Estimate initial logits from class labels.
Computes (weighted) class frequencies and converts to log-odds
relative to class 0: logit_k = log(p_k / p_0) for k=1..K-1.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Integer class labels, shape
TYPE:
|
sample_weight
|
Per-sample weights, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Logit vector, shape |
Source code in ngboost_lightning/distributions/categorical.py
score
¶
Per-sample categorical cross-entropy (negative log-likelihood).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Integer class labels, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
NLL values, shape |
Source code in ngboost_lightning/distributions/categorical.py
d_score
¶
Gradient of NLL w.r.t. logits [logit_1, ..., logit_{K-1}].
For the softmax-logit parameterization
d(NLL)/d(logit_k) = p_k - I(y == k) for k = 1..K-1
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Integer class labels, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Gradient array, shape |
Source code in ngboost_lightning/distributions/categorical.py
metric
¶
Fisher Information for softmax-logit parameterization.
For classes 1..K-1 (excluding reference class 0): FI = diag(p[1:]) - p[1:] @ p[1:]^T
This is non-diagonal for K > 2.
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
FI tensor, shape |
Source code in ngboost_lightning/distributions/categorical.py
natural_gradient
¶
Natural gradient via base class solve (non-diagonal Fisher).
For K=2 (Bernoulli), the Fisher is scalar p*(1-p) and this
reduces to (p - y) / (p * (1-p)).
For K>2, uses np.linalg.solve(FI, d_score).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Integer class labels, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Natural gradient, shape |
Source code in ngboost_lightning/distributions/categorical.py
mean
¶
Class probabilities (the "mean" of a categorical).
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Probability matrix, shape |
Note
This returns a 2D array unlike regression distributions which return 1D. The classifier wrapper handles conversion to class labels.
Source code in ngboost_lightning/distributions/categorical.py
sample
¶
Draw n samples per distribution instance.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Number of samples to draw.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Class labels, shape |
Source code in ngboost_lightning/distributions/categorical.py
cdf
¶
Cumulative distribution function for categorical.
Returns the cumulative probability up to and including class y:
P(Y <= y) = sum(probs[:, :y+1]).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Integer class labels, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
CDF values, shape |
Source code in ngboost_lightning/distributions/categorical.py
ppf
¶
Percent point function (inverse CDF) for categorical.
Returns the smallest class label k such that P(Y <= k) >= q.
| PARAMETER | DESCRIPTION |
|---|---|
q
|
Quantiles, values in [0, 1], shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Integer class labels, shape |
Source code in ngboost_lightning/distributions/categorical.py
logpdf
¶
Log probability mass function.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Integer class labels, shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray[floating]
|
Log-PMF values, shape |
Source code in ngboost_lightning/distributions/categorical.py
k_categorical
¶
k_categorical(n_classes: int) -> type[Categorical]
Create a Categorical distribution class for K classes.
| PARAMETER | DESCRIPTION |
|---|---|
n_classes
|
Number of classes (must be >= 2).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
type[Categorical]
|
A |
Examples:
>>> Bernoulli = k_categorical(2)
>>> Bernoulli.n_params
1
>>> Cat5 = k_categorical(5)
>>> Cat5.n_params
4