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()(andlasso()) – 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 conditionaltau-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:
objectA link function
eta = g(mu)with its inverse and derivativedmu/deta.- Parameters:
- name: str
- class Family(name, variance, canonical, unit_deviance, estimate_dispersion, extra=1.0)[source]
Bases:
objectAn exponential-family error model: variance function, canonical link, deviance, dispersion.
- Parameters:
- name: str
- canonical: str
- 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:
objectFitted GLM.
- Parameters:
- coef
(p,)coefficient estimates.- Type:
- se
(p,)standard errors (model-based, or robust if requested).- Type:
- fitted
(n,)fitted meansmu.- Type:
- deviance
residual deviance.
- Type:
- dispersion
estimated/assumed dispersion
phi.- Type:
- log_likelihood
maximised log-likelihood.
- Type:
- n_iter
IRLS iterations to convergence.
- Type:
- family / link
names.
- cov
(p, p)coefficient covariance.- Type:
- 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
muat new design rowsx.
- property aic: float
- property bic: float
- 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 aFamily.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.logexposure).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:
objectFitted penalized linear regression.
- coef
(p,)coefficients (excluding the intercept).- Type:
- intercept
fitted intercept.
- Type:
- alpha
overall penalty strength.
- Type:
- l1_ratio
elastic-net mixing (1 = lasso, 0 = ridge).
- Type:
- n_iter
coordinate-descent iterations (0 for the closed-form ridge).
- Type:
- coef: ndarray
- intercept: float
- alpha: float
- l1_ratio: float
- n_iter: int
- 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.
- 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 = 1is the lasso (sparse),l1_ratio = 0is ridge.
- lasso(x, y, alpha=1.0, **kw)[source]
Lasso (L1) linear regression –
elastic_net()withl1_ratio = 1.
- class RegressionFit(coef, fitted, scale, n_iter)[source]
Bases:
objectCoefficients + fitted values from
robust_regression()/quantile_regression().- coef: ndarray
- fitted: ndarray
- scale: float
- n_iter: int
- 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.
huberuses the Huber weight (tuningc = 1.345for 95% Gaussian efficiency);tukeyuses the redescending Tukey biweight (c = 4.685), which rejects gross outliers entirely.
- 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 weightstau / |r|for positive residuals and(1 - tau) / |r|for negative ones (a smoothed Newton scheme;epsfloors|r|for stability).