Skip to content

fix(splines): continue the M-spline boundary pieces when extrapolating - #162

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/161-mspline-extrapolation
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/161-mspline-extrapolation

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #161.

With policy={"out_of_range": "extrapolate"} (or "warn"), MSplineTransformer set every negative basis value to zero. That broke the documented extension on the "extended boundary polynomial pieces of the B/M-spline basis" and left a kink at the fitted range.

Root cause

MSplineTransformer._mspline_basis (pretab/expansion/spline/m_spline.py) returned np.maximum(values * (degree + 1) / knot_span, 0.0). Inside the knot range the B-spline values are already non-negative, so the clip only ever acted on out-of-range rows. There it removed the negative parts of the cubic continuation that the B-spline transformer keeps.

Fix

The clip at zero now applies only to values inside the knot range [t_0, t_last]. Values beyond it keep the scaled B-spline extension, M_k = (p + 1) / (t_{k+p+1} - t_k) * B_k, exactly. The degenerate-support guard and the NaN handling are unchanged.

Behaviour changes

  • Under "extrapolate" / "warn", out-of-range M-spline rows equal the scaled B-spline extension and can be negative. Before, half of the columns were clipped to zero.
  • Under the default policy (clip) and for every in-range value, the output is unchanged. The golden regression baselines pass without regeneration.
  • ISplineTransformer does not use this basis (it is computed in closed form) and is unaffected.

Documentation

  • MSplineTransformer class docstring: the extrapolated basis is the scaled B-spline extension and can be negative beyond the fitted range.
  • _mspline_basis docstring: non-negative inside the knot range, unclipped outside.
  • docs/representations/spline_expansions.md (M-spline entry): the basis is non-negative inside the range; under "extrapolate" / "warn" it continues the scaled B-spline pieces.
  • BaseSplineTransformer.policy and docs/representations/choosing_a_method.md already describe this behaviour and now hold for M-splines.

Tests

tests/core/test_policy.py::test_extrapolated_mspline_rows_are_the_scaled_bspline_extension ("extrapolate"/"warn" × include_bias): out-of-range rows equal the scaled B-spline extension, include negative values, are continuous across the boundary, and in-range rows match the default fit. All four cases fail on main and pass here. The existing test_extrapolated_mspline_and_tensor_rows_are_not_zeroed still passes.

Reproduction

The script from #161 on this branch:

scaled B-spline extension:
 [[ 82.308 -45.724   7.533  -0.486   0.      0.   ]
 [ 18.25   -3.343   0.147  -0.002   0.      0.   ]
 [  0.      0.     -0.002   0.147  -3.343  18.251]
 [  0.      0.     -0.486   7.533 -45.724  82.308]]
MSplineTransformer:
 [[ 82.308 -45.724   7.533  -0.486   0.      0.   ]
 [ 18.25   -3.343   0.147  -0.002   0.      0.   ]
 [  0.      0.     -0.002   0.147  -3.343  18.251]
 [  0.      0.     -0.486   7.533 -45.724  82.308]]
M_1 just left / right of x_min: -0.054243283499999996 0.0537572835

Checks

  • just test: 4680 passed, 59 skipped, 7 xfailed; coverage 95.71% (gate 90%).
  • Minimum dependencies (CI job: Python 3.11, numpy 1.24.4, pandas 2.0.3, scipy 1.10.1, scikit-learn 1.6.0): 4497 passed, 173 skipped, 7 xfailed; scripts/quickstart.py passes.
  • just docs: strict build succeeds.
  • just check: all pre-commit and pre-push hooks pass (file hygiene, ruff format, ruff lint, prettier, pyright with 0 errors).
  • The change stays inside pretab.expansion.spline.m_spline; no import direction changes.

Merge notes

_mspline_basis clipped every value at zero. Inside the knot range the
scaled B-spline values are non-negative anyway, so the clip acted only on
out-of-range rows under an "extrapolate" / "warn" policy, where it cut the
negative parts off the extended boundary polynomial pieces and left a
kink at the fitted range.

The clip now applies only inside the knot range, so the extrapolated
M-spline equals the scaled B-spline extension. In-range values and the
default clipping policy are unchanged.

Fixes #161

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSplineTransformer with policy out_of_range="extrapolate"/"warn" clips the extended boundary pieces at zero

1 participant