SuperGLM¶
SuperGLM
¶
Penalised generalised linear model with splines, group penalties, and REML.
Supports Poisson, Gaussian, Gamma, NB2, Tweedie, and Binomial families with group
lasso, sparse group lasso, or ridge penalties. Smoothing parameters can
be estimated via REML (fit_reml) or cross-validation (cross_validate).
__init__(family='poisson', link=None, penalty=None, selection_penalty=None, spline_penalty=None, penalty_features=None, features=None, splines=None, n_knots=10, degree=3, categorical_base='most_exposed', interactions=None, active_set=False, direct_solve='auto', discrete=False, n_bins=256, tol=1e-06, max_iter=100, convergence='deviance', retain_fit_state=True)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
family
|
str or Distribution
|
Response distribution. Strings |
'poisson'
|
link
|
str or Link
|
Link function. Defaults to the family's configured default link. |
None
|
penalty
|
str or Penalty
|
Penalty type. One of |
None
|
selection_penalty
|
float, {"auto"}, or None
|
Regularisation strength for the group penalty (feature selection).
|
None
|
spline_penalty
|
float
|
Within-group ridge shrinkage for spline smoothing. Defaults to 0.1. |
None
|
penalty_features
|
str or list[str]
|
Restrict the selection penalty to specific feature or group names.
|
None
|
features
|
dict[str, FeatureSpec]
|
Explicit feature specifications mapping column names to feature
objects ( |
None
|
splines
|
list[str]
|
Column names to treat as splines in auto-detect mode. All other columns are auto-detected as categorical or numeric. Mutually exclusive with features. |
None
|
n_knots
|
int or list[int]
|
Number of interior knots for auto-detect splines. |
10
|
degree
|
int
|
B-spline degree for auto-detect splines. |
3
|
categorical_base
|
str
|
Base level strategy for auto-detected categoricals. |
'most_exposed'
|
interactions
|
list[tuple[str, str]]
|
Pairs of feature names to interact. Interaction type is auto-detected from the parent feature specs. |
None
|
active_set
|
bool
|
Use active-set cycling in the BCD solver. |
False
|
direct_solve
|
('auto', 'gram', 'qr')
|
Strategy for the direct IRLS solver (lambda1=0).
|
"auto"
|
discrete
|
bool
|
Use discretized basis matrices for large-n REML (fREML-style). |
False
|
n_bins
|
int or dict[str, int]
|
Number of discretization bins per feature when |
256
|
tol
|
float
|
Convergence tolerance for IRLS / PIRLS. Default |
1e-06
|
max_iter
|
int
|
Maximum IRLS / PIRLS outer iterations. Default |
100
|
convergence
|
('deviance', 'coefficients')
|
Convergence criterion. |
"deviance"
|
retain_fit_state
|
bool
|
If True (default), keep training-scale fit state such as the fitted design matrix for later diagnostics. If False, eagerly computes compact inference state after fitting, then releases row-scale training caches while preserving prediction, summaries, and term confidence intervals. |
True
|
fit(X, y, sample_weight=None, offset=None, *, tol=None, max_iter=None, convergence=None, record_diagnostics=False)
¶
Fit the model to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas or eager Polars DataFrame
|
Feature matrix with columns matching registered features. Lazy frames must be collected before fitting. |
required |
y
|
array - like
|
Response variable. |
required |
sample_weight
|
array - like
|
Observation weights. Their likelihood interpretation is family-specific. Defaults to 1 for all observations. For Tweedie, these are finite, strictly positive EDM prior weights:
Weights affect fitting but do not enter the linear predictor or
automatically scale the conditional mean. The model mean is
|
None
|
offset
|
array - like
|
Offset added to the linear predictor. |
None
|
record_diagnostics
|
bool
|
If True, record per-iteration IRLS diagnostics (W range,
mu/eta range, step halvings, worst-observation indices) on
|
False
|
Returns:
| Type | Description |
|---|---|
SuperGLM
|
The fitted model (self). |
fit_reml(X, y, sample_weight=None, offset=None, *, max_reml_iter=20, reml_tol=1e-06, pirls_tol=None, max_pirls_iter=None, lambda2_init=None, interaction_mode='full', runtime_validation='auto', verbose=False, w_correction_order=1)
¶
Fit with REML estimation of per-term smoothing parameters.
fit_reml() is the smoothness-selection path and does not support a
selection penalty: configure selection_penalty=None or 0.0.
It optimizes a Laplace approximate REML objective over per-term
smoothing parameters. For sparse/group selection, use fit() or
fit_path(). To let REML shrink spline null spaces, use
select=True on the spline terms instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas or eager Polars DataFrame
|
Feature matrix. Lazy frames must be collected before fitting. |
required |
y
|
array - like
|
Response variable. |
required |
sample_weight
|
array - like
|
Observation weights. Their likelihood interpretation is family-specific.
For Tweedie, these are finite, strictly positive EDM prior weights with
|
None
|
offset
|
array - like
|
Offset term. |
None
|
max_reml_iter
|
int
|
Maximum REML outer iterations (default 20). |
20
|
reml_tol
|
float
|
Convergence tolerance on log-lambda (default 1e-6). |
1e-06
|
pirls_tol
|
float
|
Inner PIRLS/IRLS convergence tolerance. Defaults to
constructor |
None
|
max_pirls_iter
|
int
|
Maximum inner PIRLS iterations per REML step. Defaults to
constructor |
None
|
lambda2_init
|
float
|
Initial per-group lambda. Defaults to |
None
|
interaction_mode
|
('full', 'fast_candidate')
|
|
"full"
|
runtime_validation
|
('auto', 'full', 'skip')
|
Controls the post-fit public-runtime parity diagnostic.
|
"auto"
|
verbose
|
bool
|
Print progress. |
False
|
w_correction_order
|
int
|
Order of the W(rho) implicit-differentiation correction. 1 gives the exact objective and gradient with a modified-Newton outer Hessian (default, fast). 2 also includes the available exact d²W/dη² Hessian cross-terms from Wood (2011, Appendix C). Only affects the exact REML path. |
1
|
Returns:
| Type | Description |
|---|---|
SuperGLM
|
The fitted model (self). |
fit_path(X, y, sample_weight=None, offset=None, *, n_lambda=50, lambda_ratio=0.001, lambda_seq=None)
¶
Fit a regularization path from lambda_max down to lambda_min.
Warm-starts each lambda from the previous solution.
predict(X, offset=None)
¶
Predict the response mean for new data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas or eager Polars DataFrame
|
Eager input features with the same columns used during fitting. |
required |
offset
|
NDArray or None
|
Optional offset added to the linear predictor before applying the inverse link. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Predicted mean on the response scale (inverse-link of eta). |
design_summary()
¶
Describe fitted design storage and static route eligibility.
The summary does not build an accelerated matrix or prove that an eligible kernel executed. Fit and REML traces remain authoritative for actual dispatch.
relativities(with_se=False, centering='native')
¶
Extract plot-ready relativity DataFrames for all features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centering
|
('native', 'mean')
|
|
"native"
|
term_inference(name, *, with_se=True, simultaneous=False, n_points=200, alpha=0.05, n_sim=10000, seed=42, centering='native')
¶
Per-term inference: curve, uncertainty, and metadata in one object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centering
|
('native', 'mean')
|
|
"native"
|
plot(terms=None, *, kind='global', ci='pointwise', X=None, sample_weight=None, show_density=True, show_knots=False, show_bases=False, scale='response', ci_style='band', categorical_display='auto', grouped_level_display='auto', engine='matplotlib', n_points=200, figsize=None, title=None, subtitle=None, plotly_style=None, alpha=0.05, n_sim=10000, seed=42, centering='native', **kwargs)
¶
Plot model terms.
Single entry point for all plotting. Dispatches based on terms:
None— all main effects in a grid."age"— one main effect.["age", "region"]— subset of main effects."age:region"— one interaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terms
|
str, list of str, or None
|
Which term(s) to plot. |
None
|
kind
|
('global', 'local')
|
|
"global"
|
ci
|
(None, False, 'pointwise', 'simultaneous', 'both')
|
Confidence interval style. |
None
|
X
|
pandas or eager Polars DataFrame
|
Training data for density overlays. |
None
|
sample_weight
|
array - like
|
Frequency weights / sample_weight for density overlays. |
None
|
show_density
|
bool
|
Show sample_weight/observation density (strip for continuous, bars for categorical). Default True. |
True
|
show_knots
|
bool
|
Show interior knot ticks (spline terms only). |
False
|
show_bases
|
bool
|
Initial visibility for coefficient-weighted spline basis
contributions in the Plotly explorer. Only meaningful when
|
False
|
scale
|
('response', 'link')
|
|
"response"
|
ci_style
|
('band', 'lines')
|
Plotly CI presentation. |
"band"
|
categorical_display
|
('auto', 'bars', 'markers', 'bars+markers')
|
Plotly categorical rendering mode. |
"auto"
|
grouped_level_display
|
('auto', 'expanded', 'collapsed')
|
Display option for grouped categorical levels in main-effect plots.
|
"auto"
|
engine
|
('matplotlib', 'plotly')
|
Plotting backend. |
"matplotlib"
|
centering
|
('native', 'mean')
|
|
"native"
|
n_points
|
int
|
Grid resolution for spline/polynomial curves. |
200
|
figsize
|
tuple
|
Figure size override. |
None
|
title
|
str
|
Figure-level title and subtitle. |
None
|
subtitle
|
str
|
Figure-level title and subtitle. |
None
|
plotly_style
|
dict
|
Plotly main-effect explorer style overrides. Supported keys include
|
None
|
alpha
|
float
|
Significance level for CIs (default 0.05). |
0.05
|
n_sim
|
int
|
Posterior simulations for simultaneous bands. |
10000
|
seed
|
int
|
Random seed for simultaneous bands. |
42
|
**kwargs
|
Forwarded to the underlying renderer (e.g. |
{}
|
Returns:
| Type | Description |
|---|---|
Figure or Figure
|
|
Examples:
metrics(X, y, sample_weight=None, offset=None)
¶
Compute comprehensive diagnostics for the fitted model.
refit_unpenalised(X, y, sample_weight=None, offset=None, *, keep_smoothing=True)
¶
Refit with only active features and no selection penalty.
drop1(X, y, sample_weight=None, offset=None, *, test='Chisq')
¶
Drop-one deviance analysis for each feature.
estimate_theta(X, y, sample_weight=None, offset=None, **kwargs)
¶
Estimate NB theta via profile likelihood, refit, and return result.
estimate_p(X, y, sample_weight=None, offset=None, *, fit_mode='fit', phi_method='mle', method='auto', ci_alpha=None, **kwargs)
¶
Estimate Tweedie p via profile likelihood, refit, and return result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas or eager Polars DataFrame
|
Feature matrix. Lazy frames must be collected before fitting. |
required |
y
|
array - like
|
Response variable. |
required |
sample_weight
|
array - like
|
Finite, strictly positive EDM prior weights, not replication or frequency weights.
The Tweedie variance convention is
|
None
|
offset
|
array - like
|
Offset added to the linear predictor. |
None
|
fit_mode
|
('fit', 'reml', 'inherit')
|
Fitting regime for each candidate |
"fit"
|
phi_method
|
('pearson', 'mle')
|
How to profile out Tweedie dispersion |
"pearson"
|
method
|
('auto', 'joint_ml', 'brent', 'grid', 'grid_refine', 'profile_opt')
|
Search strategy. |
"auto"
|
ci_alpha
|
float
|
Significance level for an explicitly requested likelihood-ratio
profile confidence interval. For example, |
None
|