Jarwin
Jarwin

Reputation: 1133

scipy.interpolate.CubicSpline is giving ambiguous result

I have to find interploation between two sets of points X,Y such that:

X = [2011, 2012, 2013, 2014, 2015, 2016, 2017]  
Y = [0.947079910779078, 0.958840491330058, 0.948653507807658, .94687561871641, 0.94732364567882, 0.953963141055096, 0.943468711524127]

I tried finding the interpolation through CubicSpline

F_spline = scipy.interpolate.CubicSpline(X, Y, extrapolate='periodic'))

But if i cross-check, I get,

In [98]: F_spline_val = [F_spline(year) for year in X]
Out[98]: [array(0.94707991),
          array(0.95884049),
          array(0.94865351),
          array(0.94687562),
          array(0.94732365),
          array(0.95396314),
          array(0.94707991)]

Notice the last value here, calculated for 2017, is 0.94707991, whereas in the original variable, the value for 2017 was 0.943468711524127. Which is clearly inaccurate. What am I missing here? Why there is an ambiguous value for 2017?

Upvotes: 0

Views: 66

Answers (1)

Mr. T
Mr. T

Reputation: 12410

Your problem is the use of the keyword extrapolate = periodic. This assumes, as one might guess, that there is a periodicity in the dataset and it has to fulfill the condition that y[0] == y[-1].

Since it is not clear, why your year-related data should be periodic and you violate the assumption, set extrapolate = True and you don't get strange results on the fringes of your dataset.

Upvotes: 1

Related Questions