Migration: P-splines on uneven knots take the general difference penalty#

Ships in 0.40.

What changed#

A P-spline (Spline(kind="ps")) penalises differences between neighbouring coefficients. That penalty measures wiggliness only when the knots are evenly spaced. Evenly spaced knots are unchanged: those the "uniform" rule places, and stated or quantile-placed knots that fall at the same even spacing, such as fitted_knots passed back with fitted_boundary. Other knots stated with knots=[...], or placed by a quantile rule ("quantile", "quantile_rows" or "quantile_tempered"), are unevenly spaced, and for those the penalty now is the general difference penalty:

Spline(kind="ps", knots=[18, 25, 35, 50, 70])     # general penalty
Spline(kind="ps", n_knots=8, knot_strategy="uniform")  # standard penalty, unchanged

The general penalty of Li and Cao, “General P-splines for non-uniform B-splines” (2022, arXiv:2201.06808), smooths towards a straight line, that is towards polynomials of degree below the penalty order m, wherever the knots sit. It is scaled to the size of the standard penalty for the same number of coefficients, so a fixed spline_penalty smooths about as strongly as it did.

Sometimes the general penalty cannot be computed reliably. Its largest and smallest stiffnesses then differ by more than a factor of 1/sqrt(eps), about 7e7. This happens on heavily skewed columns such as freMTPL2’s Density, and on knots crowded into a small part of the axis. With cubic splines and m = 3, it also happens from about 60 knots, even when the knots are only a little uneven. For those the penalty is the standard one with the polynomials of degree below m taken out of it. A heavily smoothed term is still a straight line. The switch between the two is a step, not a gradual change: a knot more or fewer, or a refit on new data, can move a term across the limit and change its fit by more than the knots alone explain; diagnostics() and knot_summary() name the penalty each term took as difference_penalty.

Why#

With uneven knots, the standard penalty no longer measures wiggliness. As the smoothing parameter grows, the fit is pulled towards a shape set by where the knots fall. The general penalty removes that dependence on knot placement, so a heavily smoothed term is a straight line whatever its knots.

Who is affected#

  • Uniform knots, or stated knots at the same even spacing: nothing changes.

  • Stated knots or a quantile rule: the fit can change. The smoothing parameter, the effective degrees of freedom and the curve can all move.

  • A penalty order m above the degree: the standard penalty is kept, so nothing changes.

  • bs and cr splines: the general penalty does not change them, because their penalties already handle uneven knots. On strongly skewed knots, and for their units, see the bullets below.

  • Explicit cr, bs or cr_cardinal on strongly skewed knots (quantile knots on a long-tailed column, for example), and an explicit cr term used in an interaction on a long-tailed column, whatever its own knots: under fit_reml, REML counts more penalised directions, so the smoothing parameter, the effective degrees of freedom and the curve can all move.

  • Explicit cr, bs or cr_cardinal with select=True and discrete=True on strongly skewed knots: under fit_reml, a fit that 0.39 refused now runs.

  • Explicit cr, bs or cr_cardinal with select=True on 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 names kind="ps". On lognormal data with quantile knots, cr and bs start 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, bs or cr_cardinal with select=True on 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_cardinal or bs fitted with fit() at a fixed spline_penalty, held by LambdaPolicy.fixed under fit_reml(), or with fixed lambdas in SuperLSS: the fit changes. The penalty is now measured in the spline’s knot intervals instead of the column’s units. In 0.39 the same spline_penalty barely 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_cardinal or bs fitted with fit_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 larger by h**-3, where h is the mean knot interval in the column’s units: 729 times for nine intervals across a column from 0 to 1. A penalty order m other than 2 takes h**-(2m - 1).

  • SuperGLM.fit_reml() or SuperLSS.fit_reml() with such a spline on a column in large units: a smoothing parameter that 0.39 stopped at the cap (1e10, or max_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 numeric initial_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.

  • ns splines: nothing changes. They keep the standard penalty.

What to do#

  • Saved models: a model fitted and saved under 0.39 predicts the same under 0.40, and its summary is unchanged. Only a refit uses the new penalty.

  • Uniform knots: nothing.

  • To keep a 0.39 fixed-penalty fit of an explicit cr, cr_cardinal or bs spline, or to reuse a smoothing parameter a 0.39 fit_reml() reported: multiply it by h**-3, where h is the mean knot interval, the fitted boundary’s width over the number of knot intervals (n_knots + 1). A penalty order m other than 2 takes h**-(2m - 1).

  • Stated or quantile-placed knots: refit under 0.40 and compare the validation deviance and the curve with the 0.39 fit on the same data. A change in the curve is expected. Re-check any threshold tuned on the old fit.

  • Editor knot and kind changes: an editor session can record a spline’s knots and its kind in a structure file. Upgrade to 0.40 before you apply such a file: a 0.39 or older superglm refuses a structure file that has a knots or basis entry.

Verification trail#

  • The general penalty is taken from Li and Cao (2022) as cited above.

  • The behaviour is pinned in tests/test_pspline_general_penalty.py: a straight line is unpenalised on stated and quantile-placed knots, the most-smoothed fit is a line, and the uniform rule, evenly spaced knots and an order above the degree keep the standard penalty.

  • The same file pins the knots too uneven for the general penalty. These are a crowded cluster of stated knots, knots 1e-160 apart, m=3 on lognormal(0, 2) quantile knots, and a tensor interaction with a skewed quantile margin. Each fit completes. On the crowded and the 1e-160 knots the line stays unpenalised. On quantile_rows knots of lognormal(0, 1.5) data REML counts the penalty’s full rank. tests/test_realdata_parity.py fits select=True on freMTPL2’s VehAge and Density with quantile_rows knots.

Performance#

Measured on the full freMTPL2 frequency book (678,013 rows): a Poisson fit_reml with a log-exposure offset, eight threads in every pool, runs in A-B-B-A order. Columns are clipped as in tests/test_realdata_parity.py. The backend dispatched was gram in every run.

Like for like. BonusMalus (quantile, 12 knots), DrivAge (quantile_tempered, 12) and VehPower (quantile_tempered, 8) take the general penalty both before and after the rescaling, so only its size differs.

Build

Wall time (s)

Peak RSS (MiB)

Deviance

EDF

REML iterations

Unscaled general penalty

4.07, 4.13

1223, 1331

217456.43271

31.14590

7

Scaled, with the fallback

4.57, 3.92

1275, 1273

217456.43274

31.14588

7

The smoothing parameters differ by the scale factor: BonusMalus 2.117 and 2.924, DrivAge 21.67 and 41.48, VehPower 2.071 and 6.423.

With a strongly skewed column. VehAge and BonusMalus (quantile, 12 knots), Density (quantile, 10) and DrivAge (uniform, 10). Density’s general penalty has a condition of 1.7e9, so it now takes the fallback, and VehAge’s quantile knots fall at the even spacing and take the standard penalty.

Build

Wall time (s)

Peak RSS (MiB)

Deviance

EDF

REML iterations

Density lambda

0.39.0 (standard penalty)

36.55, 32.68

841, 821

211895.68

37.68

8

13550

Scaled, with the fallback

39.30, 38.34

824, 809

211888.12

40.38

10

907.5

Unscaled general penalty (earlier session)

35.86, 32.62

835, 823

211899.95

40.37

8

6.088

The first two rows are one A-B-B-A session. Against 0.39 the fit is about 12% slower on this set: REML takes two more iterations to settle Density’s penalty, and each iteration is about 10% cheaper. The deviance is 7.6 lower, and peak memory is unchanged.