ModelMetrics#

class superglm.ModelMetrics(
model: SuperGLM,
X=None,
y=None,
sample_weight=None,
offset=None,
*,
_fit_data_matches: bool | None = None,
_mu: NDArray | None = None,
_contract_checked: bool = False,
)#

Bases: object

Post-fit diagnostics for a SuperGLM model.

Parameters:
modelSuperGLM

A fitted model.

Xpandas or eager Polars DataFrame

Feature matrix used for fitting (or evaluation).

yarray-like

Response variable.

sample_weightarray-like, optional

Observation weights / sample_weight.

offsetarray-like, optional

Offset term.

Notes

The fitted mean and linear predictor are the model’s predictions on X, the values predict returns, and the deviance, likelihoods, Pearson statistic and residuals() are computed from them. They do not depend on whether X, y, sample_weight and offset are the objects the model was fitted on: a discrete fit’s own mean belongs to its binned design, which predict does not reproduce. SuperGLM.summary reports the fit’s own statistics instead.

On the frame a model was fitted on, leverage and effective degrees of freedom come from the fit itself, and so do the standardized residuals and Cook’s distance built on that leverage. Equal copies of that frame, or a pickled model, are recognised as the training rows only when the model keeps a structured or shape-constrained covariance. Otherwise leverage is re-formed on the rows passed, which for a discrete fit can differ slightly from its binned fit. For a fit whose shape constraints bind, the inverse re-formed for leverage and the degrees of freedom is restricted to the fit’s active face: with Z a basis of the coefficients the binding rows leave free, it is Z (Z' (X' W X + S) Z)^+ Z' on the rows passed, and with no row binding Z = I, the unconstrained fit’s. The covariance behind standard errors and intervals holds no row: which rows bind is itself estimated.

property phi: float#

The scale standard errors, intervals and Wald tests read.

property phi_likelihood: float#

The scale densities read: the log-likelihood, AIC/BIC and quantile residuals.

property deviance: float#

Weighted residual deviance on this object’s evaluation rows.

property null_log_likelihood: float#

Log-likelihood at the intercept-only (null) model.

property explained_deviance: float#

1 - deviance / null_deviance. Analogous to R-squared.

ebic(gamma: float = 0.5) → float#

Extended BIC (Chen & Chen 2008).

property eta: NDArray#

Linear predictor (link-scale fitted values).

A row with no predicted mean, which only a row of weight zero can be here, reads its raw linear predictor, not the stabilized value.

residuals(
kind: str = 'deviance',
*,
seed: int | None = 42,
) → NDArray#

Compute residuals of the specified type.

Deviance and Pearson residuals are multiplied by sqrt(sample_weight) so a weighted row’s squared residual equals the sum over literal replicated rows; response, working and quantile residuals keep per-row semantics. Under weight_semantics="prior" the weight is part of the row’s distribution and stays in every diagnostic that depends on it. Influence diagnostics describe deletion of the whole weighted cell.

Parameters:
kindstr

One of “deviance”, “pearson”, “response”, “working”, “quantile”.

seedint or None

Random seed for quantile residuals (discrete families only). Default 42. Ignored for other types.

property leverage: NDArray#

Compressed-row hat diagonal under the supplied evaluation weights.

This is the influence of one weighted cell as represented in the compressed design, not the leverage of each literal expanded copy. It includes the intercept: sum(h) is the effective degrees of freedom, and every value is invariant to a shift of a column.

property cooks_distance: NDArray#

Compressed-row aggregate Cook’s distance.

Both the Pearson residual contribution and leverage correspond to deletion of the supplied weighted cell, rather than deletion of one literal expanded copy.

property std_deviance_residuals: NDArray#

Standardized compressed-row deviance residuals.

Equal to r_dev / sqrt(phi * (1 - h)) using aggregate weighted residual and compressed-row leverage, so it represents deletion of the whole weighted cell rather than one literal expanded copy.

property std_pearson_residuals: NDArray#

Standardized compressed-row Pearson residuals.

Equal to r_pear / sqrt(phi * (1 - h)) using aggregate weighted residual and compressed-row leverage, so it represents deletion of the whole weighted cell rather than one literal expanded copy.

property coefficient_se: dict[str, NDArray]#

Per-group quasi-likelihood coefficient standard errors.

For known-scale families such as Poisson and Binomial, the covariance is scaled by the Pearson dispersion estimate pearson_chi2 / residual_df, where residual_df follows the family’s sample-weight semantics. For estimated-scale families such as Gaussian, Gamma, and Tweedie, it uses the fitted dispersion phi. Use coefficient_se_raw for the unscaled, exact-family covariance.

This accessor is an explicit quasi-likelihood diagnostic. Rendered coefficient tables retain the fitted-family scale (and hence phi=1 for known-scale families) unless a quasi-summary option is requested by a future API.

Inactive groups get all-zero SEs.

Note: These are conditional-on-the-selected-model SEs from the penalized estimate. They do not account for model selection uncertainty (same convention as glmnet / mgcv).

property coefficient_se_raw: dict[str, NDArray]#

Per-group coefficient standard errors assuming phi=1.

For known-scale families these are the exact-family standard errors, without the Pearson quasi-likelihood correction applied by coefficient_se. For estimated-scale families these omit the fitted dispersion and therefore differ from coefficient_se whenever phi != 1.

Inactive groups get all-zero SEs.

property intercept_se: float#

Quasi-likelihood standard error of the intercept.

Computed from the [0,0] element of the augmented Fisher information inverse, which accounts for covariance between the intercept and all other coefficients. Uses the same dispersion convention as coefficient_se, including Pearson dispersion for known-scale families. Rendered coefficient tables retain fitted-family scale.

property intercept_se_raw: float#

Exact-family intercept SE for known scale, otherwise assuming phi=1.

feature_se(name: str, n_points: int = 200) → dict[str, Any]#

Quasi-likelihood SE of a feature curve, levels, or coefficient.

Uses the same dispersion convention as coefficient_se. Refuses for a term carrying a post-fit shape repair, since the band would be propagated from the unconstrained fit’s covariance around constrained coefficients (summary() withholds the same quantity).

summary(
alpha: float = 0.05,
detail: str = 'compact',
level_display: str = 'expanded',
) → ModelSummary#

Formatted model summary with coefficient table.

The rendered table uses fitted-family covariance (phi=1 for known-scale families, the fitted phi otherwise), not the Pearson quasi-likelihood accessors coefficient_se, intercept_se or feature_se. The dict-like standard_errors payload keeps the historical coefficient_se key for the explicit quasi-likelihood accessor.

Parameters:
alphafloat

Significance level for confidence intervals (default 0.05 → 95% CI).

detailstr

"compact" (default) shows one row per spline group; "full" adds per-coefficient rows (ASCII inline; HTML as a pre-expanded <details>).

level_displaystr

"expanded" (default) shows exact original levels; "grouped" shows one row per fitted group with a membership legend.

Returns:
ModelSummary

Object with __str__ (ASCII), _repr_html_ (HTML), and dict-like access.