Skip to main content

Generalized Linear Model Extension for Scikit-Learn.

Project description

GLM Extensions for Scikit-Learn

PyPI version Python versions License Wheel

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

Example notebook

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 mean
  • k > 0 is 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

  • statsmodels uses alpha for 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 μ or log μ 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

glmext-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (675.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

glmext-1.0.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (656.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686manylinux: glibc 2.5+ i686

glmext-1.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (641.8 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

glmext-1.0.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (620.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ i686manylinux: glibc 2.5+ i686

glmext-1.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (639.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

glmext-1.0.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (619.4 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ i686manylinux: glibc 2.5+ i686

File details

Details for the file glmext-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for glmext-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 82d90b9bb88ee8f81b81d71bf1e403fde05db4fe73a835fc147b94fddda1632b
MD5 965366e856b34ceccb758482ee235820
BLAKE2b-256 f72d8f07804444a8a199b55ced4ebe6ba77286415b7fb79a833882a759d91171

See more details on using hashes here.

File details

Details for the file glmext-1.0.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for glmext-1.0.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 64dee017c7a887b2085fde1cc484ddebc5203150145fa9fbd38e6eb8f41d7e41
MD5 87ff03fd03ed71e4b030a6bd794ca54f
BLAKE2b-256 e587629df50d430187b4a31b0d7412147cd7719fbceb56b20f91d5c0bdccf92b

See more details on using hashes here.

File details

Details for the file glmext-1.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for glmext-1.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 031f471493c23b7ac49fe1b402a058c51a81cffc043a624015c39990805bc519
MD5 fd5fa19a7f7bf279fdca74cbe48d5ca2
BLAKE2b-256 ea0fa47edcc900e8a8fa6878468ec6a3f1969e9da1ddb25f1479811ca5bb0070

See more details on using hashes here.

File details

Details for the file glmext-1.0.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for glmext-1.0.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 9abbf96ffe42103986a3512d3bc622ba60915609309e0e737266ca0123a7b66c
MD5 449a686f7930ba11dd95768764b5ecc2
BLAKE2b-256 2b01b9d3092243a68115ea4c5167ba97186ed70d46053f9c3719c5d1d6064c66

See more details on using hashes here.

File details

Details for the file glmext-1.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for glmext-1.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64ddd70da296b0ce2358a5293c1c0057854ba3f1b06a834ce0881b7bf48ca718
MD5 7fb5b41fc4fbe036ca766cfb736bb6d5
BLAKE2b-256 85c404138affb6695ca07b52f32de963e1cbca06accb9c113a143120d67781e1

See more details on using hashes here.

File details

Details for the file glmext-1.0.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for glmext-1.0.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 931b9b5f11485377368b753647ec3a3576035da526dd98168297ce6953b09a33
MD5 0846a6f41f3bdb30e23b654bc5ba1b87
BLAKE2b-256 bc409191b1305ddb64f1b724a1c7d90a298d317ed79e7f5f917e168872f93b6a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page