[Python] Spline lineal continuation on borders

Discussão em 'Python' iniciado por Stack, Dezembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    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)


    however, this generates something like in:

    [​IMG]

    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?

    Continue reading...

Compartilhe esta Página