Warrick
Warrick

Reputation: 727

What derivatives are used for splines in SciPy.interpolate.interp1d?

I'm trying to find out how the spline interpolation in scipy.interpolate.interp1d decides what the derivatives of the fitting/smoothing function should be. From the documentation, I understand that interp1d fits a spline if an int (or quadratic or cubic) is passed to the kind keyword.

But if I'm not providing any derivative information, how does it decide what the derivatives are? I tried to follow the function calls in the source code, but this only got me to the cryptic spleval function, which just seems to call FITPACK. And I wasn't really sure how to find information about FITPACK from their website...

Upvotes: 2

Views: 1225

Answers (1)

user6655984
user6655984

Reputation:

Spline derivatives at the knot points are not explicitly prescribed, they are determined by continuity/smoothness conditions. I'll take the cubic case as an example. You give n x-values and n y-values. A cubic spline has 4*(n-1) coefficients, 4 on each of (n-1) intervals between the given x-values. These coefficients are determined from the following conditions:

  • The spline must be continuous at each interior knot: this is (n-2) equations, as there are (n-2) interior knots. We want both pieces to the left and right of a knot to have the same value at the knot.
  • The first derivative of the spline must be continuous at each interior knot: this is (n-2) equations.
  • The second derivative of the spline must be continuous at each interior knot: this is (n-2) equations.
  • The spline must match each of the given y-values: this is n equations.

The total so far is 4*n-6 equations for 4*n-4 unknowns. Two additional equations are needed; the most popular choice is to require the 3rd derivative to be continuous at the leftmost and rightmost interior knots (this is called the "not a knot" condition). Now we have a linear system of size 4*n-4, which can be solved for the coefficients.


The above should not be confused with Hermite interpolation, which is where one prescribes the values of derivatives as well as of the function itself. This is a less common task, and to my knowledge, SciPy does not have a built-in tool for it.

Upvotes: 2

Related Questions