Reputation: 1708
I have code interpolating a spline from a CDF that seems valid between the range x=40 and x=110. Below 40, I want a flat line at 0, and after 110, a flat line at 1 towards infinite.
I have come up with this code:
def _find_spline(x_orig: np.ndarray, cdf: np.ndarray) -> BSpline:
# Find the smoothing factor
smoothing_factor, iters = _find_highest_smoothing_factor(x_orig, cdf, 1e-5)
# Fit a linear spline to the first segment
t_begin, c_begin, k_begin = splrep([0, x_orig[0]], [0, cdf[0]], k=1)
# Fit the middle segment with the specified smoothing factor
t_middle, c_middle, k_middle = splrep(x_orig, cdf, s=smoothing_factor)
# Explicitly construct the final segment
t_end = np.array([x_orig[-1], x_orig[-1], 300, 300]) # Linear knots
c_end = np.array([cdf[-1], 1.0]) # Start from the evaluated value
# Combine the knots and coefficients
t_combined = np.concatenate([t_begin[:-1], t_middle[1:-1], t_end])
c_combined = np.concatenate([c_begin[:-1], c_middle[1:-1], c_end])
return BSpline(t_combined, c_combined, k_middle, extrapolate=True)
I have uploaded a full reproducible example here.
however, this generates something like in:
so, the first segment until 40 seems fine and evaluates to 0... but I don't know why the spline goes down to 0 at the end at 110, I want it just to continue straight...
What is wrong?
Upvotes: 0
Views: 46
Reputation: 26040
You cannot just have a constant extrapolation to fall out of the spline construction by itself, and you'll have to implement it yourselves (unless linear interpolation is OK in which case you just use np.interp
).
There are several ways to implement it:
In fact, I would recommend the latter approach.
This scipy page has a several examples (it starts with an interp1d
example, which is very much not recommended anymore; the other ones are relevant):
https://docs.scipy.org/doc/scipy/tutorial/interpolate/extrapolation_examples.html
Upvotes: 2