1

i'm trying to understand how scipy.interpolate.CubicSpline(..., extrapolate=True) works internally: How exactly does Scipy implement the extrapolation of a point x beyond [a, b] mathematically?

I'm looking for a formula such as: score(x) = exp(-x + b) if x > b (which seems NOT to be how they implement it!).

Tried looking at the source code, was unable to find the out how it's implemented. Documentation does not give any details either.

1 Answers1

3

A spline is a piecewise polynomial, i.e., there are several subintervals of data interval [a, b] on each of which it's just a polynomial. With extrapolate=True the polynomial from the leftmost interval is used for x < a, and the polynomial from the rightmost interval is used for x > b. This is what the documentation describes as

extrapolate to out-of-bounds points based on first and last intervals,

Very simple and very useless. Here is an illustration.

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import CubicSpline
f = CubicSpline([-2, -1, 0, 1, 2], [1, 1, 0, 2, 2], extrapolate=True)
t = np.linspace(-4, 4)
plt.plot(t, f(t))
plt.show()

All the given y-values are between 0 and 2, but the cubic polynomials make a lousy extrapolation.

plot