Generalized Linear Model Extension for Scikit-Learn.
Project description
GLM Extensions for Scikit-Learn
This package provides Generalized Linear Model (GLM) implementations that are missing from scikit-learn, with a familiar sklearn-style API.
Currently supported models include:
- Negative Binomial GLM (NB2 parameterization)
- Binomial GLM
The goal is to offer a lightweight, numerically stable alternative for users who want GLMs integrated naturally into scikit-learn pipelines, without depending on statsmodels.
Motivation
While scikit-learn provides a general GLM framework and logistic regression, it does not include support for:
- Negative Binomial regression (for overdispersed count data)
- Binomial regression
This package fills that gap while following scikit-learn conventions:
fit() / predict()- Compatibility with pipelines and preprocessing
- Cython-compiled fast inference of loss and gradient
Installation
pip install glmext
Example
from glmext.glm import NegativeBinomialRegressor
nb = NegativeBinomialRegressor(
alpha=0.0,
k=2,
max_iter=300,
tol=1e-7,
)
nb.fit(X_train, y_train)
y_pred_nb = nb.predict(X_test)
Mathematical Reference (Negative Binomial GLM)
We use the NB2 parameterization, where the conditional variance is
V(μ) = μ + k μ²
Here:
μis the conditional meank > 0is the dispersion (overdispersion) parameter
1. Canonical Parameter
For the Negative Binomial exponential family, the canonical parameter is
θ_c(μ) = log( k μ / (1 + k μ) )
2. Cumulant Function
The corresponding cumulant function is
b(θ_c) = -1 / k · log(1 - exp(θ_c))
3. GLM Loss (Negative Log-Likelihood)
The per-observation negative log-likelihood is
ℓ(y, θ_c) = -( y · θ_c - b(θ_c) )
Summing over observations gives the total loss.
Derivation of the Closed-Form Loss
Substituting θ_c(μ) and b(θ_c) into the likelihood:
Loss(y, μ) = ∑ [ -( y · log(k μ / (1 + k μ)) - 1/k · log(1 + k μ) ) ]
Expanding logarithms:
Loss(y, μ) = ∑ [ (y + 1/k) · log(1 + k μ) - y · log(k μ) ]
Dropping constant terms independent of μ (e.g. y · log(k)):
Loss(y, μ) = ∑ [ (y + 1/k) · log(1 + k μ) - y · log(μ) ]
This is the exact loss optimized by the implementation.
Link Function and Implementation Details
We parameterize the mean using a log link:
μ = exp(raw_prediction)
log(μ) = raw_prediction
Important:
- This is not the canonical link for the Negative Binomial distribution.
- We intentionally work in log-mean (
log μ) space for numerical stability and ease of optimization.
Poisson Limit
As k → 0, the Negative Binomial converges to a Poisson distribution, and the loss reduces to the standard Poisson negative log-likelihood:
Loss(y, μ) = μ - y · log(μ)
Final Loss Used in Code
Expressed in terms of raw_prediction:
Loss = ∑ [ (y + 1/k) · log(1 + k · exp(raw)) - y · raw ]
Relation to statsmodels
This implementation is numerically equivalent to
statsmodels.genmod.families.NegativeBinomial.loglike_obs()
with the following clarifications.
1. Parameter Naming
statsmodelsusesalphafor the dispersion parameter- This package uses
k
The choice of k avoids a naming conflict with alpha, which is widely used for regularization strength in scikit-learn.
2. Canonical Parameter Notation Discrepancy
The statsmodels documentation sometimes writes the cumulant function as:
b(θ) = -1 / k · log(1 - k · exp(θ))
At first glance, this appears inconsistent with the canonical exponential-family form. The discrepancy arises because the symbol θ is reused for a reparameterized quantity, not the strict canonical parameter.
Distinguishing the Parameters
Canonical parameter (exponential family):
θ_c = log( k μ / (1 + k μ) )
Reparameterized parameter used in practice:
θ_r = log(μ)
These are related by:
exp(θ_c) = k · exp(θ_r) / (1 + k · exp(θ_r))
θ_c = log(k) + θ_r - log(1 + k · exp(θ_r))
Substituting θ_r into the canonical cumulant function yields:
b(θ_r) = -1 / k · log(1 - k · exp(θ_r))
Thus, the appearance of k · exp(θ) inside b(θ) reflects a reparameterization, not a different distribution.
Practical Implication
In practice, both this package and statsmodels:
- Operate directly in
μorlog μspace - Use algebraically equivalent likelihood expressions
- Produce identical numerical results (up to floating-point precision)
The difference is largely one of notation and documentation clarity rather than statistical substance.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file glmext-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: glmext-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 675.6 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab9408649a75ea1a0cdcb363cfe53248c9b62d0112e410d59dc00f15a4d0ed5a
|
|
| MD5 |
7c4105d1629a21041feb8667b5a6d7e6
|
|
| BLAKE2b-256 |
98cb54e8483f407bfd148853d3655c07c750fc612f5dca012e624bc38383ba18
|
File details
Details for the file glmext-1.0.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: glmext-1.0.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 656.3 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ i686, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20c20fc178002784b47205cf80a4d557df13825b9b8be50d1534725605a05c90
|
|
| MD5 |
eaeabe8b5a33f81e48baa8b6ef130354
|
|
| BLAKE2b-256 |
44e88ca7c0a8cdb7c4103d96bc9886e89c41f8bd8772b25dfb12ef1f70340d65
|
File details
Details for the file glmext-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: glmext-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 641.7 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c83be3d9e58d2a6132a3ed5da11631b40e484e2c79a5e25dd60ead77afec6f3
|
|
| MD5 |
23e4074368aa25d012a1722d882cd151
|
|
| BLAKE2b-256 |
8a17e626ea2f08d412ec54baf88c7ef8ade7c8104137ac1334b0e29627b29068
|
File details
Details for the file glmext-1.0.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: glmext-1.0.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 620.8 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ i686, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe0cb46fcf37351e5e4d1571f91f3ef3d41a0fdf6ef85ff437094bc557cb3f0b
|
|
| MD5 |
2c1b9c8d96f7c9a25764c2e016d1cd5b
|
|
| BLAKE2b-256 |
d03793f315c74873dfb21dd25b594b8925f31e1e22a8dbda2113c76f5e9b7a82
|
File details
Details for the file glmext-1.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: glmext-1.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 639.6 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
645beb77306d99e4607aab5dbc6016439ed4ce8d56c36ca4b2a585198487f1dd
|
|
| MD5 |
174287bd0b1ffc9259f0ce13c4041a77
|
|
| BLAKE2b-256 |
8ad39c378dd4073e89bed45972bdcb8ad815019c822adbcdce5f8e02d6bfeaf2
|
File details
Details for the file glmext-1.0.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: glmext-1.0.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 619.3 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ i686, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aeb34af6de22e879ce7235b2e13acabe9a4d24c87c3390c8b16f5f35e27245d2
|
|
| MD5 |
e91278ed349d38bae4b46e5a9b56ce38
|
|
| BLAKE2b-256 |
a2d4967366dd3274472f85f61b57b1667cac95ebe86e55d39bd37c923262053a
|