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:
objectA factor-by-P-spline interaction: one smooth curve of
variableper level ofgroup.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 throughSuperGLM.factor_smooth. (Anszmodel 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 onenull_jcomponent per degree:null_0the level’s constant,null_1its 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 throughnull_3is the curve’s degree in x; fromnull_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 itsnull_jsmoothing 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, withm=2) unpenalized, as mgcv’sszdoes.- 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 hasmcoordinates.basis="fs"refuses anmthat float64 does not reliably represent, where a closed-form lower bound on its difference penalty’s condition number reaches2^47: none belowk=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 ordersksupports. With the wiggle’s smoothing parameter fixed at zero the penalty’s condition does not enter, and everymis 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 unlessselect=True, since the sum-to-zero contrast cannot identify the deviations with an empty level; withselect=Truethe 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
LambdaPolicyapplies to every component; a mapping names components,"wiggle"and"null_0"…"null_{m-1}"underbasis="fs","wiggle"(and"null"withselect=True) underbasis="sz", and estimates the ones it does not name. None estimates every component by REML. Underbasis="fs", in a predictor with an intercept, a fit refusesnull_0fixed 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’sselect=TRUE) with its own smoothing parameter, reading the lines as random effects shrunk toward the population curve asfsdoes with itsnull_jcomponents. It makes curves finite and unique for a level whose line separates the response or a term whose levels hold fewer distinctxvalues 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=Noneand 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( ) 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,
Build compact support-space geometry with a fixed natural basis.
- validate_population_prediction_values(
- x: NDArray,
- group: NDArray,
Validate rows for a population prediction that skips this deviation.
- validate_prediction_values(
- x: NDArray,
- group: NDArray,
Validate new rows and return the numeric marginal and fitted-level codes.
- marginal_basis(
- x: NDArray,
Evaluate the fitted natural marginal basis on requested numeric values.
- score(
- x: NDArray,
- group: NDArray,
- beta: NDArray,
Score fitted level-specific deviations without expanding factor geometry.
- transform(
- x: NDArray,
- group: NDArray,
Materialize a small prediction matrix for compatibility and references.
- reconstruct(
- beta: NDArray,
Return fitted natural-basis coefficients by level.
With
szlevels left out of the population (#432,_population_map) each level’s coefficients are its deviation from the population curve as predicted,_identified_blocksminus the offsetc, which the result carries aspopulation_offset(the main effect’s reported curve addsb(x)' c).