Piecewise#
- class superglm.Piecewise(
- breaks: Sequence[float | str] | int,
- *,
- base: float | Literal['most_exposed', 'first'] | str = 'most_exposed',
- strategy: Literal['quantile'] = 'quantile',
- lower: float | None = None,
- upper: float | None = None,
- extrapolation: Literal['clip', 'extend', 'error'] = 'clip',
- degrees: Sequence[int] | None = None,
Bases:
objectContinuous piecewise-linear feature on stated breakpoints.
Given
Jbreakpoints the knot vector is[lower, *breaks, upper](J + 2knots) and the basis is the degree-1 B-spline (hat) basis on those knots, evaluated directly so the outer segments extend linearly past the boundary knots. The hat at a base knott_ris dropped for identifiability against the intercept, so the group hasJ + 1columns and each retained coefficient isv_j = f(t_j) - f(t_r), the log relativity at knotj.- Parameters:
- breakssequence of float, or int
The breakpoints. A sequence states where the kinks are. An int places that many at exposure-weighted quantiles of x, snapped to observed values (for exploration, not a filed tariff); heaped data can collapse tied quantiles, so fewer may be realised, with a warning. As the
basis=of anOrderedCategoricalthe breaks may be band names (breaks=["Mi060", "Mi066"]), resolved to level positions when the ordered term is constructed; string breaks refuse atbuild()on the numeric axis.- degreessequence of int, optional
One polynomial degree per segment (
len(breaks) + 1entries), default all 1.0is a flat segment (plateau tail);2and above add within-segment curvature. Seams stay value-continuous (Gallant & Fuller 1973; Anderson & Nelson 1975). Legal only as thebasis=of anOrderedCategorical, where the export is one row per band; a numeric-axis build with any degree != 1 refuses. Requires stated breaks.- basefloat or {‘most_exposed’, ‘first’}
Reference knot, mirroring
Categorical.'most_exposed'picks the knot carrying the largest hat-carried mass under the fit weights (the most rows when nosample_weightis given). A float must equal exactly one knot. The resolved base is sticky acrossbuild()calls (_base_knot), so after a refit whose knot set changed,base='first'can remain on a surviving interior knot.- strategy{‘quantile’}
Placement rule, consulted only when breaks is an int.
- lower, upperfloat, optional
Pin the outermost knots; default
min(x)/max(x). Pinning wider than the data states a rated range the tariff must cover. Pinning narrower is allowed; rows outside[lower, upper]followextrapolation: under"clip"they group onto the boundary knots (the tail-grouping idiom), under"extend"they load the linear tails, whose leverage lands on the two boundary slopes, and under"error"the build refuses.- extrapolation{‘clip’, ‘extend’, ‘error’}
Behaviour outside
[lower, upper], mirroringSpline."clip"(default) holds the boundary knot’s value, soPiecewise(breaks, upper=u)fits identically tox.clip(max=u);"extend"continues the boundary segments at their slopes;"error"raises. The policy binds atbuild()as well as at prediction.
Notes
Outside
[t_0, t_{J+1}]the term followsextrapolation; the default mirrors this library’s splines and holds the boundary knot’s value flat. Under"extend"the exported table prints the two boundary slopes, so the rule outside the tabulated range is reproducible by hand, and the exported workbook states whichever rule is in force.In the editor the term gets one control handle per knot (the handle is the coefficient). The editor’s hard cap of 24 handles still applies, so a term with more knots displays a subsample and the rest cannot be dragged.
- build( ) GroupInfo#
Resolve knots and base from x, validate, and return the
J+1columns.Validation order is part of the contract: (1) finite x, (2) non-empty breaks, (3) known strategy, (4) increasing sequence breaks, (6a) lower < upper, then the extrapolation policy binds, (5) int-mode placement, (6b) breaks strictly inside, (7) base resolves to one knot, (8) no zero-mass knot, (9) no in-range empty segment, (10) full column rank against the intercept, (11) thin-segment warning. Band-name breaks and per-segment
degrees=refuse first unless anOrderedCategoricalhosts this spec as itsbasis=.
- transform(
- x: NDArray,
Evaluate the identifiable retained columns on new data.
Out-of-range x follows the term’s
extrapolationpolicy. On the legacy (all-degree-1) path these are theJ+1non-base hats; on the segmented path, the non-base knot-value groups then the curvature columns.
- score(
- x: NDArray,
- beta: NDArray[floating],
Score the fitted piecewise contribution directly on new data.
- reconstruct(
- beta: NDArray[floating],
Coefficients -> per-knot relativities and derived segment slopes.
- ordered_structural_rows() list[StructuralContrastRow]#
Structural contrasts of a fitted piecewise term, for summaries.
One slope-change contrast per stated break and one curvature family per segment of degree >= 2: the fixed-knot truncated-power vocabulary, where each is an ordinary Wald/F test (Smith 1979, Am. Statist. 33(2):57-62; Sprent 1961 for the known-changeover two-phase case). Not per-segment per-power z-rows: under C0 seams within-segment components are not free parameters. Contrast vectors are over the retained columns of
transformand invariant to the base choice.