mixle.inference.glm module

Generalized linear models and penalized / robust / quantile regression on plain arrays.

A array-level regression toolkit (operating on a design matrix X and response y, independent of the PPL DSL in mixle.ppl.regression):

  • glm() – exponential-family GLMs by iteratively reweighted least squares, with explicit family/link objects (Gaussian, Binomial, Poisson, Gamma, inverse-Gaussian, negative-binomial), offsets, prior weights, and optional sandwich (robust) standard errors.

  • ridge_regression(), elastic_net() (and lasso()) – L2 / L1 / mixed penalised linear regression; the elastic net is solved by coordinate descent.

  • robust_regression() – Huber / Tukey M-estimation, down-weighting outliers via IRLS on a robust scale.

  • quantile_regression() – the conditional tau-quantile by IRLS on the check loss.

Each fit returns a small result object exposing the coefficients, standard errors, fitted values, a predict method, and the relevant goodness-of-fit summary.

class Link(name, g, inv, mu_eta)[source]

Bases: object

A link function eta = g(mu) with its inverse and derivative dmu/deta.

Parameters:
name: str
g: Callable[[ndarray], ndarray]
inv: Callable[[ndarray], ndarray]
mu_eta: Callable[[ndarray], ndarray]
class Family(name, variance, canonical, unit_deviance, estimate_dispersion, extra=1.0)[source]

Bases: object

An exponential-family error model: variance function, canonical link, deviance, dispersion.

Parameters:
name: str
variance: Callable[[ndarray], ndarray]
canonical: str
unit_deviance: Callable[[ndarray, ndarray], ndarray]
estimate_dispersion: bool
extra: float = 1.0
class GLMResult(coef, se, fitted, deviance, dispersion, log_likelihood, n_iter, family, link, cov, _link=None)[source]

Bases: object

Fitted GLM.

Parameters:
coef

(p,) coefficient estimates.

Type:

numpy.ndarray

se

(p,) standard errors (model-based, or robust if requested).

Type:

numpy.ndarray

fitted

(n,) fitted means mu.

Type:

numpy.ndarray

deviance

residual deviance.

Type:

float

dispersion

estimated/assumed dispersion phi.

Type:

float

log_likelihood

maximised log-likelihood.

Type:

float

n_iter

IRLS iterations to convergence.

Type:

int

family / link

names.

cov

(p, p) coefficient covariance.

Type:

numpy.ndarray

coef: ndarray
se: ndarray
fitted: ndarray
deviance: float
dispersion: float
log_likelihood: float
n_iter: int
family: str
link: str
cov: ndarray
predict(x, *, offset=None)[source]

Predict the mean response mu at new design rows x.

Parameters:
Return type:

ndarray

property aic: float
property bic: float
z_values()[source]
Return type:

ndarray

p_values()[source]
Return type:

ndarray

glm(x, y, *, family='gaussian', link=None, offset=None, weights=None, max_iter=100, tol=1e-8, robust=False)[source]

Fit a generalized linear model by iteratively reweighted least squares.

Parameters:
  • x (ndarray) – (n, p) design matrix (include an intercept column explicitly if wanted).

  • y (ndarray) – (n,) response (counts, 0/1 or proportions, positive reals, … per the family).

  • family (str | Family) – "gaussian", "binomial", "poisson", "gamma", "inverse_gaussian", "negativebinomial", or a Family.

  • link (str | None) – link name; defaults to the family’s canonical link.

  • offset (ndarray | None) – (n,) known additive term on the linear-predictor scale (e.g. log exposure).

  • weights (ndarray | None) – (n,) prior weights.

  • max_iter (int) – IRLS controls (convergence on the relative deviance change).

  • tol (float) – IRLS controls (convergence on the relative deviance change).

  • robust (bool) – if True report Huber–White sandwich standard errors instead of model-based ones.

Returns:

A GLMResult.

Return type:

GLMResult

class PenalizedResult(coef, intercept, alpha, l1_ratio, n_iter)[source]

Bases: object

Fitted penalized linear regression.

Parameters:
coef

(p,) coefficients (excluding the intercept).

Type:

numpy.ndarray

intercept

fitted intercept.

Type:

float

alpha

overall penalty strength.

Type:

float

l1_ratio

elastic-net mixing (1 = lasso, 0 = ridge).

Type:

float

n_iter

coordinate-descent iterations (0 for the closed-form ridge).

Type:

int

coef: ndarray
intercept: float
alpha: float
l1_ratio: float
n_iter: int
predict(x)[source]
Parameters:

x (ndarray)

Return type:

ndarray

ridge_regression(x, y, alpha=1.0, *, fit_intercept=True)[source]

Ridge (L2-penalised) linear regression in closed form.

Minimises ||y - X b||^2 + alpha ||b||^2; the intercept (if fitted) is not penalised.

Parameters:
Return type:

PenalizedResult

elastic_net(x, y, alpha=1.0, l1_ratio=0.5, *, fit_intercept=True, max_iter=1000, tol=1e-7)[source]

Elastic-net linear regression by cyclic coordinate descent.

Minimises (1/2n) ||y - X b||^2 + alpha ( l1_ratio ||b||_1 + (1 - l1_ratio)/2 ||b||^2 ). l1_ratio = 1 is the lasso (sparse), l1_ratio = 0 is ridge.

Parameters:
Return type:

PenalizedResult

lasso(x, y, alpha=1.0, **kw)[source]

Lasso (L1) linear regression – elastic_net() with l1_ratio = 1.

Parameters:
Return type:

PenalizedResult

class RegressionFit(coef, fitted, scale, n_iter)[source]

Bases: object

Coefficients + fitted values from robust_regression() / quantile_regression().

Parameters:
coef: ndarray
fitted: ndarray
scale: float
n_iter: int
predict(x)[source]
Parameters:

x (ndarray)

Return type:

ndarray

robust_regression(x, y, *, method='huber', c=None, max_iter=100, tol=1e-8)[source]

Robust (M-estimator) linear regression by IRLS with a robust scale.

Down-weights observations with large residuals so a few outliers cannot dominate the fit. huber uses the Huber weight (tuning c = 1.345 for 95% Gaussian efficiency); tukey uses the redescending Tukey biweight (c = 4.685), which rejects gross outliers entirely.

Parameters:
Return type:

RegressionFit

quantile_regression(x, y, tau=0.5, *, max_iter=200, tol=1e-7, eps=1e-6)[source]

Linear quantile regression: the conditional tau-quantile by IRLS on the check loss.

Minimises the pinball loss sum rho_tau(y - X b) via iteratively reweighted least squares with weights tau / |r| for positive residuals and (1 - tau) / |r| for negative ones (a smoothed Newton scheme; eps floors |r| for stability).

Parameters:
Return type:

RegressionFit