SplineCategorical#

class superglm.SplineCategorical(spline_name: str, cat_name: str)#

Bases: object

Varying-coefficient interaction: spline curve per categorical level.

For each non-base level of the categorical, produces one group of K B-spline columns masked by the level indicator. The base level’s effect is absorbed into the main spline term.

build_discrete(
x_spline: NDArray,
x_cat: NDArray,
parent_specs: dict,
n_bins: int,
sample_weight: NDArray | None = None,
separation: tuple | None = None,
) → list[GroupInfo]#

Build spline-by-category groups from compressed spline support.

score(
x_spline: NDArray,
x_cat: NDArray,
beta: NDArray,
) → NDArray#

Score the interaction directly from its public runtime blocks.

level_design_rows(
x: NDArray,
) → Iterator[tuple[str, NDArray]]#

Each non-base level with the matrix taking its fitted coefficients to its curve at x.

Shape (len(x), width): rows @ beta_level is that level’s column of level_log_deviations(), held-range clip included, so a covariance of the fitted coefficients gives the curve’s variance as rows @ cov @ rows.T.

level_log_deviations(
x: NDArray,
beta: NDArray,
) → NDArray#

Every non-base level’s log deviation from the base curve at x.

Shape (len(x), len(non_base)), columns in _non_base order, and the same numbers score gives a row of that level at that x: it goes through the same coefficients and the same held-range clip. One basis evaluation serves every level, so the cost is linear in the number of levels, which a loop over score or reconstruct is not.

level_index(
x_cat: NDArray,
) → NDArray#

Each row’s position j in _non_base, or -1 for a row of the base level.

The labels are routed as score routes them – grouping, unseen policy and all – so this is the level a row’s curve is read for.

banded_deviation(
band: NDArray,
x_cat: NDArray,
cells: NDArray,
) → NDArray#

Each row’s cell: cells[band, j] for a row of non-base level j, 0 for any other.

cells is (bands, len(non_base)) in _non_base order and band gives each row’s band; the row’s level is level_index(), so a table of cells rates a row on the level its curve would have been read for.