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: object

Continuous piecewise-linear feature on stated breakpoints.

Given J breakpoints the knot vector is [lower, *breaks, upper] (J + 2 knots) 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 knot t_r is dropped for identifiability against the intercept, so the group has J + 1 columns and each retained coefficient is v_j = f(t_j) - f(t_r), the log relativity at knot j.

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 an OrderedCategorical the breaks may be band names (breaks=["Mi060", "Mi066"]), resolved to level positions when the ordered term is constructed; string breaks refuse at build() on the numeric axis.

degreessequence of int, optional

One polynomial degree per segment (len(breaks) + 1 entries), default all 1. 0 is a flat segment (plateau tail); 2 and above add within-segment curvature. Seams stay value-continuous (Gallant & Fuller 1973; Anderson & Nelson 1975). Legal only as the basis= of an OrderedCategorical, 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 no sample_weight is given). A float must equal exactly one knot. The resolved base is sticky across build() 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] follow extrapolation: 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], mirroring Spline. "clip" (default) holds the boundary knot’s value, so Piecewise(breaks, upper=u) fits identically to x.clip(max=u); "extend" continues the boundary segments at their slopes; "error" raises. The policy binds at build() as well as at prediction.

Notes

Outside [t_0, t_{J+1}] the term follows extrapolation; 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(
x: NDArray[floating],
sample_weight: NDArray[floating] | None = None,
) → GroupInfo#

Resolve knots and base from x, validate, and return the J+1 columns.

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 an OrderedCategorical hosts this spec as its basis=.

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

Evaluate the identifiable retained columns on new data.

Out-of-range x follows the term’s extrapolation policy. On the legacy (all-degree-1) path these are the J+1 non-base hats; on the segmented path, the non-base knot-value groups then the curvature columns.

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

Score the fitted piecewise contribution directly on new data.

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

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 transform and invariant to the base choice.