Migration: a spline with no kind is a cubic regression spline#
Ships in 0.40.
What changed#
Spline(...) and s(...) with no kind now give a cubic regression spline,
kind="cr". Before 0.40 they gave a P-spline, kind="ps".
Spline(n_knots=10) # cubic regression spline from 0.40
Spline(kind="ps", n_knots=10) # the P-spline that 0.39 fitted
A spline with no kind that cr cannot take builds as kind="ps", the
P-spline that 0.39 built, with a warning. Pass kind= to choose the kind and
silence it. This happens in two cases:
A degree other than 3, such as
Spline(degree=2).A penalty order above 3, such as
Spline(m=4)orSpline(m=(2, 4)).
From 0.40 to 0.41 there was a third: a fit-time shape constraint beside a ps
term with one, because a ps term then fitted its constraint on SCOP and one
model could not use both engines. SCOP is retired, so every constraint fits
on the QP and that case is gone.
Four changes follow from the new default:
A model that names no kind refits as
cr. The curve, the smoothing parameter, the effective degrees of freedom and the deviance can all move. Acrterm withn_knotsinterior knots has two fewer coefficients than apsterm with the samen_knots.A fit-time shape constraint with no kind moves from SCOP to the QP.
Spline(constraint=Constraint.fit.increasing)is now acrterm, which the QP constrains. The QP chooses the smoothing parameter without the constraint and then refits with it. SCOP, which apsterm used up to 0.41, chose it with the constraint in place.crrefuses a degree other than 3. A cubic regression spline is always cubic. In 0.39,Spline(kind="cr", degree=2)fitted a cubic without saying so. It now raises an error that nameskind="ps"andkind="bs".Spline(degree=2)with no kind builds asps, with a warning.An ordered term’s default basis is
cr.OrderedCategorical(...)with nobasisnow usesSpline(kind="cr", n_knots=5). In 0.39 it usedSpline(kind="ps", n_knots=5). The knot count is still limited to the number of levels minus one.
Who is affected#
Code that names a kind: nothing changes, except an explicit
crorcr_cardinalwith a degree other than 3, which now raises, and the explicitcr,bsandcr_cardinalcases below.Explicit
cr,bsorcr_cardinalon strongly skewed knots (quantile knots on a long-tailed column, for example), and an explicitcrterm used in an interaction on a long-tailed column, whatever its own knots: underfit_reml, REML counts more penalised directions, so the smoothing parameter, the effective degrees of freedom and the curve can all move.Explicit
cr,bsorcr_cardinalwithselect=Trueanddiscrete=Trueon strongly skewed knots: underfit_reml, a fit that 0.39 refused now runs.Explicit
cr,bsorcr_cardinalwithselect=Trueon an extremely long tail (quantile knots on a heavy-tailed column): when double precision cannot hold the penalty on the widest knot interval beside the narrowest, a fit that 0.39 fitted now stops with an error that nameskind="ps". On lognormal data with quantile knots,crandbsstart refusing at a log-scale spread of about 2 to 2.75, depending on the sample and the number of knots. In our checks, every fit that 0.39 fitted and 0.40 stops used 20 quantile knots.Explicit
cr,bsorcr_cardinalwithselect=Trueon a column with large or small units (a sum insured or a mileage, or a column in hundredths): 0.39 refused some of these fits with “requires exactly 2 null eigenvalues”. 0.40 fits them, unless the knots are too uneven for double precision (the bullet above).Explicit
cr,cr_cardinalorbsfitted withfit()at a fixedspline_penalty, held byLambdaPolicy.fixedunderfit_reml(), or with fixedlambdasinSuperLSS: the fit changes. The penalty is now measured in the spline’s knot intervals instead of the column’s units. In 0.39 the samespline_penaltybarely smoothed a column measured in thousands, such as a density, and heavily smoothed a share between 0 and 1, and rescaling a column changed the fit. It now smooths the same whatever the units, and about as strongly as a P-spline with the same knots.Explicit
cr,cr_cardinalorbsfitted withfit_reml(): the curve, the effective degrees of freedom and the deviance are unchanged, to the convergence tolerance, unless the 0.39 smoothing parameter stopped at the search’s bound of 1e-6 or 1e10. Those bounds applied in the column’s units and now apply in knot intervals, so such a term can move: smoother on a column in large units, less smooth on one in small units. The reported smoothing parameter is multiplied byh**-3, wherehis the mean knot interval in the column’s units: 729 times larger for nine intervals across a column from 0 to 1, and smaller on a column in large units, such as driver age from 18 to 100 with eight knots (h= 82/9, about 9.1, so about 756 times smaller). A penalty ordermother than 2 takesh**-(2m - 1).SuperGLM.fit_reml()orSuperLSS.fit_reml()with such a spline on a column in large units: a smoothing parameter that 0.39 stopped at the cap (1e10, ormax_lambda) can now stop below it, because the cap no longer depends on the column’s units. The curve is still close to a straight line.SuperLSS.fit_reml()with a numericinitial_lambda: the start now means the same smoothing on every spline, so the search can take another path and stop at another point within its tolerance.Spline(...)ors(...)with no kind: a refit can change. Underfit(), a fixedspline_penaltysmooths about as strongly as it smoothed the 0.39 P-spline, whatever the column’s units.OrderedCategoricalwith nobasis: a refit can change.Interactions of splines with no kind: a refit can change. A tensor interaction or a spline-by-factor interaction takes its margins from its parents’ kind. A
crparent’s margin is a cardinal cubic regression spline placed on the column’s quantiles, whatever the parent’s knots.A spline with no kind on a column with one distinct value: the fit now stops with an error naming the feature and saying to pass
kind="ps". A P-spline fitted it.An ordered term with no
basiswhose levels are all grouped into one band: the fit now stops with an error. A P-spline fitted it.Spline(...)ors(...)with no kind and a penalty order above 3 (m=4, or a tuple such asm=(2, 4)): it is a P-spline, as in 0.39, with a warning.An ordered term with no
basisand one observed level in the data (for example a CV fold or a per-segment fit):fitnow raises aValueErrorwhose message readsFeature '<name>': every row of this ordered term is at one level, so its natural spline basis has no range to place its knots on. Drop the term, or pass basis=Spline(kind='ps'), which fits a term with one observed level.A P-spline fitted it.A spline with no kind and
select=Trueon a column with a very long tail (quantile knots on, for example, a sum insured or a mileage): when the widest knot interval is so much wider than the narrowest that double precision cannot hold the penalty on both, the fit stops with an error that says so and nameskind="ps". A P-spline fitted it.FactorSmoothcurves: nothing changes. They stay P-splines whatever the main effect’s kind.splines=auto-detection andSuperGLMRegressor(spline_features=...): nothing changes. They build P-splines.
What to do#
To keep the 0.39 fit: add
kind="ps"to everySpline(...)ands(...)that names no kind. Addbasis=Spline(kind="ps", n_knots=5)to everyOrderedCategoricalthat omitsbasis.A warning that a spline with no kind builds as
ps: passkind="ps"to keep the fit and silence it, orkind="bs"for another degree.To keep a 0.39 fixed-penalty fit of an explicit
cr,cr_cardinalorbsspline, or to reuse a smoothing parameter a 0.39fit_reml()reported: multiply it byh**-3, wherehis the mean knot interval, the fitted boundary’s width over the number of knot intervals (n_knots + 1). A penalty ordermother than 2 takesh**-(2m - 1).A very long-tailed column with
select=True: passkind="ps", or fit the column’s logarithm.To move to
cr: refit under 0.40 and compare the validation deviance and the curves with the 0.39 fit on the same data.Saved models: a model fitted and saved under 0.39 keeps its P-splines. It predicts the same under 0.40, and refitting it keeps its P-splines.
Structure files: a structure file records a spline’s kind only when the kind was chosen in the editor. Applied to a saved 0.39 model, it gives the same terms as in 0.39. Applied to a model declared again in code with no kind, it gives
crterms. Name the kind in code to keep a P-spline.
Measured#
The freMTPL2 frequency book as published (678,013 rows), Poisson with exposure
weights, fitted with fit_reml(). The splines are DrivAge (n_knots=8), VehAge
(n_knots=12) and BonusMalus (n_knots=12, knot_strategy="quantile_tempered"),
all with no kind, beside LogDensity, VehBrand and Region. Each row is the
same code run on 0.39 and on 0.40, alternated 0.39, 0.40, 0.40, 0.39, with
every thread pool pinned to one thread. Times are for the fit alone; peak
memory is the whole process’s, data loading included. Re-measured on
2026-10-10 at the PR head, after the penalty moved to knot intervals.
Model |
Version |
Kind |
Fit time (s) |
Peak memory (MiB) |
Deviance |
Total EDF |
Solver |
|---|---|---|---|---|---|---|---|
No constraint |
0.39 |
|
11.41, 9.90 |
816 |
213,610.9 |
63.27 |
direct, Gram |
No constraint |
0.40 |
|
9.79, 10.97 |
816 |
215,634.3 |
61.16 |
direct, Gram |
|
0.39 |
|
71.32, 69.14 |
1,015 |
213,678.3 |
59.35 |
SCOP |
|
0.40 |
|
10.03, 9.55 |
1,007 |
215,699.2 |
61.16 |
QP, Gram |
At the head, the solver was checked from the fits themselves: reml_diagnostics() reports the gram backend for both 0.40 fits, and the constrained BonusMalus group’s monotone engine is qp. The two 0.40 fits’ total EDF differ only in the third decimal (61.163 unconstrained, 61.164 constrained).
This table is a measurement of the code on one fixed input, the book as
published. Its claim counts are wrong for policies with IDpol up to 24,500
(Wüthrich & Merz, Statistical Foundations of Actuarial Learning, Section
13.1): those 9,139 policies all show a claim. They put a spike in the claim
rate at VehAge 0 (0.311 against 0.090 at age 1), and almost all of the
2,023 deviance between ps and cr comes from it: with VehAge alone
switched back to ps, the 0.40 fit’s deviance is 213,612.6.
On the corrected counts, taken from the severity file (examples/fremtpl2_data.py;
678,007 rows, 26,383 claims), the observed rate at VehAge 0 is 0.071, in
line with ages 1 to 3 (0.071 to 0.075), and cr is 26.6 deviance worse than
ps rather than 2,023 (20.1 with BonusMalus increasing). The same fits, one
run each, on 0.39.0 and on the 0.40 branch at 7d01428c, where the rows above
reproduce to the printed digit; deviance and EDF only, since these runs were
not timed:
Model |
Version |
Kind |
Deviance |
Total EDF |
|---|---|---|---|---|
No constraint |
0.39 |
|
162,431.8 |
58.76 |
No constraint |
0.40 |
|
162,458.4 |
58.11 |
|
0.39 |
|
162,505.3 |
54.10 |
|
0.40 |
|
162,525.4 |
58.11 |
Here the gap is not VehAge’s: switching VehAge alone back to ps moves
the 0.40 fit by 0.1 deviance.
Verification trail#
tests/test_spline_factory.py::TestDefaultKindIsCrpins the default kind forSpline()ands(), the degree refusal and the penalty-order refusal forcrandcr_cardinal, and thepsa spline with no kind builds wherecrcannot, bit for bit askind="ps".tests/test_monotone_fit.pypins the shape constraint beside apsterm.tests/test_pspline_general_penalty.pypinsselect=Trueand the REML rank ofcrandbsterms on quantile knots of lognormal(0, 2) and lognormal(0, 1.5) data, and the error that names the knot spread on lognormal(0, 3) data.tests/test_ordered_categorical_api.pypins the ordered default basis, its clamp warning, and a bit-identical fit againstSpline(kind="cr", n_knots=5).