quanttoolbox.spline¶
Python alternatives
Switch to scipy.interpolate.CubicSpline for pure interpolation (already verified to match to machine precision) or scipy.interpolate.UnivariateSpline/make_smoothing_spline for smoothing — more mature and actively maintained. The friction: our p parameter (a [0,1] blend) and scipy's s parameter (a target residual sum-of-squares) are different smoothness parameterizations, not directly interchangeable. Keep this module only where exact p-parameterized behavior needs to match existing MATLAB-based analysis.
spline.spline¶
quanttoolbox.spline.spline
¶
Cubic smoothing splines: fit, evaluate (value/derivative/integral), and invert.
Ported from QuantToolBox/spline/{csspline,dspline,fspline,intspline, invspline}.m (+ band.m/bandrv.m/bandsolpd.m/rotater.m -- see note below).
Consolidation notes:
- The original implements its own banded Cholesky solver
(band.m/bandrv.m/bandsolpd.m, ported from a GAUSS procedure) purely as
an efficiency trick for solving the smoothing-spline system, which has
half-bandwidth 2 by construction. This is replaced here with
scipy.linalg.cho_factor/cho_solveon the (small, dense) system matrix directly -- standard, well-tested LAPACK-backed routines, with no need to hand-roll banded storage/factorization.rotater.m(a GAUSS row-rotation idiom used only to build the banded matrices from column vectors) is likewise not ported; the same tridiagonal/banded matrices are built directly withnumpy.diaginstead. fspline.m(evaluate spline value only) is redundant withdspline.m's k=0 case; onlyevaluate_spline(covering all of value/derivative/integral via aorder=argument, i.e.dspline) is ported.
SplineCoefficients(x, c0, c1, c2, c3)
dataclass
¶
Cubic spline in piecewise form: on [x[i], x[i+1]), s(x) = c0[i] + c1[i]z + c2[i]z^2/2 + c3[i]*z^3/6, z = x - x[i].
evaluate_spline(spline, x, order=0)
¶
Evaluate a spline, its derivatives, or its integral at points x.
order=-1: integral (from the spline's first knot). order=0 (default): value. order=1/2/3: first/second/third derivative.
Original: spline/dspline.m (also covers spline/fspline.m's order=0 case)
Source code in src/quanttoolbox/spline/spline.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
fit_smoothing_spline(x, y, w=None, p=0.5)
¶
Fit a cubic smoothing spline: minimizes p * integral(s''(t)^2 dt) + (1-p) * sum(w_i * (y_i - s(x_i))^2).
p=1: pure interpolating spline (no smoothing). p=0: linear least-squares fit (maximum smoothing). x must be strictly increasing.
Original: spline/csspline.m
Source code in src/quanttoolbox/spline/spline.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
integrate_spline(spline, limits)
¶
Definite integral of the spline between pairs of limits.
limits: (2, n) array, limits[0] = upper bounds, limits[1] = lower bounds.
Original: spline/intspline.m
Source code in src/quanttoolbox/spline/spline.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
invert_spline(spline, y, tol=1e-06)
¶
Invert a (monotonic) spline: find x such that spline(x) = y, via Newton's method starting from the bracketing knot.
Original: spline/invspline.m
Source code in src/quanttoolbox/spline/spline.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |