FactorSmooth#

class superglm.FactorSmooth(
variable: Hashable,
*,
group: Hashable,
basis: Literal['fs', 'sz'] = 'fs',
kind: Literal['ps'] = 'ps',
k: int = 6,
m: int = 2,
levels=None,
unseen: Literal['population', 'error'] = 'population',
missing: Literal['error'] = 'error',
lambda_policy: LambdaPolicy | dict[str, LambdaPolicy] | None = None,
name: str | None = None,
select: bool = False,
)#

Bases: object

A factor-by-P-spline interaction: one smooth curve of variable per level of group.

Both bases store each level’s curve in the natural parameterization of the marginal P-spline: per level, the coordinates in which the wiggle penalty is diagonal, then the unpenalized polynomial coordinates. The coefficients are neither the B-spline coefficients nor mgcv’s s(x, g, bs="sz") coordinates; read a level’s curve through SuperGLM.factor_smooth. (An sz model saved by 0.35.0 or earlier keeps the marginal-basis coordinates.)

Parameters:
variablehashable

The numeric column each level’s curve is a function of.

grouphashable

The grouping column; one curve per level.

basis{“fs”, “sz”}, default “fs”

"fs" penalizes every level’s curve fully, its polynomial part through one null_j component per degree: null_0 the level’s constant, null_1 its line, and so on, each orthogonal over the training rows to the lower degrees. The degree is that of the B-spline coefficients as a polynomial in their index, which through null_3 is the curve’s degree in x; from null_4 (m >= 5) the curve stays piecewise cubic. An fs model saved before 0.44.0 keeps the null axes it was fitted on and predicts as before, but its null_j smoothing parameters belong to those axes and do not compare with a fresh fit’s. "sz" fits sum-to-zero deviations from a population curve and leaves each level’s polynomial part (its line, with m=2) unpenalized, as mgcv’s sz does.

kind{“ps”}, default “ps”

The marginal basis, a cubic P-spline.

kint, default 6

Basis functions per level, at least 5.

mint, default 2

Difference order of the wiggle penalty, between 1 and k - 1; each level’s polynomial part has m coordinates. basis="fs" refuses an m that float64 does not reliably represent, where a closed-form lower bound on its difference penalty’s condition number reaches 2^47: none below k=73, then a band of middle orders (k=100: 17 <= m <= 75), and orders near 480 and above, whose penalty’s scale leaves float64’s range; the error names the orders k supports. With the wiggle’s smoothing parameter fixed at zero the penalty’s condition does not enter, and every m is accepted.

levelslist | tuple | Series | ndarray | CategoricalDtype, optional

The grouping column’s level universe. Under basis="fs" a declared level with no training rows keeps its curve block and shrinks to zero through the penalty. basis="sz" rejects one unless select=True, since the sum-to-zero contrast cannot identify the deviations with an empty level; with select=True the empty level is predicted at the population curve.

unseen{“population”, “error”}, default “population”

Predict-time policy for group levels outside the universe. "population" predicts those rows at the population curve; "error" raises.

missing{“error”}, default “error”

Missing values in either column, or a non-finite variable, raise.

lambda_policyLambdaPolicy, dict of str to LambdaPolicy, or None

Control of the smoothing parameters. One LambdaPolicy applies to every component; a mapping names components, "wiggle" and "null_0" … "null_{m-1}" under basis="fs", "wiggle" (and "null" with select=True) under basis="sz", and estimates the ones it does not name. None estimates every component by REML. Under basis="fs", in a predictor with an intercept, a fit refuses null_0 fixed at zero (the levels’ constants would alias it) and warns when it is fixed below the REML floor.

namestr or None

The term’s name; defaults to "{variable}:{group}:{basis}".

selectbool, default False

basis="sz" only. Adds a second penalty component "null" on every level’s line (Marra & Wood 2011, as mgcv’s select=TRUE) with its own smoothing parameter, reading the lines as random effects shrunk toward the population curve as fs does with its null_j components. It makes curves finite and unique for a level whose line separates the response or a term whose levels hold fewer distinct x values than the line has coefficients. Without it the lines stay unpenalized whatever the data, and such levels are named in a fit warning.

property parent_names: tuple[str, str]#

The numeric marginal and grouping columns read by this interaction.

adopt_dtype_categories(categories: list) → None#

Adopt a dtype-declared universe unless one is already declared.

Not reached by the main-loop hooks this release: FactorSmooth lives in the interaction specs, and dm_builder/binding_ops bind main-loop features only. Declare levels= explicitly; this hook exists so the wiring lands in one place when interaction binding is added.

apply_level_binding(binding) → None#

Adopt a full-frame universe when nothing more specific declared one.

Only the levels are read: a penalized term has no base level, so its bindings carry base=None and there is nothing to pin.

resolve_binding(
values: NDArray,
sample_weight=None,
)#

Compute this spec’s full-frame group binding without mutating the spec.

build(
x: NDArray,
group: NDArray,
specs: dict[str, Any],
sample_weight: NDArray[floating] | None = None,
) → GroupInfo#

Build one exact compact factor-by-spline block.

build_discrete(
x: NDArray,
group: NDArray,
specs: dict[str, Any],
n_bins: int,
sample_weight: NDArray[floating] | None = None,
) → GroupInfo#

Build compact support-space geometry with a fixed natural basis.

validate_population_prediction_values(
x: NDArray,
group: NDArray,
) → None#

Validate rows for a population prediction that skips this deviation.

validate_prediction_values(
x: NDArray,
group: NDArray,
) → tuple[NDArray[float64], NDArray[int64]]#

Validate new rows and return the numeric marginal and fitted-level codes.

marginal_basis(
x: NDArray,
) → NDArray[float64]#

Evaluate the fitted natural marginal basis on requested numeric values.

score(
x: NDArray,
group: NDArray,
beta: NDArray,
) → NDArray[float64]#

Score fitted level-specific deviations without expanding factor geometry.

transform(
x: NDArray,
group: NDArray,
) → NDArray[float64]#

Materialize a small prediction matrix for compatibility and references.

reconstruct(
beta: NDArray,
) → dict[str, Any]#

Return fitted natural-basis coefficients by level.

With sz levels left out of the population (#432, _population_map) each level’s coefficients are its deviation from the population curve as predicted, _identified_blocks minus the offset c, which the result carries as population_offset (the main effect’s reported curve adds b(x)' c).