Reputation: 77
I have an issue using The scipy.interpolate.CubicSpline function. Here is my code :
CS1 = CubicSpline(T,A,bc_type='not-a-knot',extrapolate=bool, axis=1)
Result : CS1 =
[-8.34442117e+03 -6.94866126e+03 -5.71682333e+03 -4.63872647e+03
-3.70418976e+03 -2.90303229e+03 -2.22507315e+03 -1.66013142e+03
-1.19802617e+03 -8.28576513e+02 -5.41601516e+02 -3.26920268e+02
-1.74351855e+02 -7.37153621e+01 -1.48298738e+01 1.24855245e+01
1.84117475e+01 1.31297102e+01 6.82032749e+00 9.66451413e+00
3.15397607e+01 7.05279383e+01 1.09387991e+02 1.32530056e+02
1.36799756e+02 1.22858734e+02 9.60947464e+01 6.66210660e+01
4.28224903e+01 2.64229282e+01 1.75832317e+01 1.45176021e+01
1.39435432e+01 1.33609464e+01 1.23801442e+01 1.09650786e+01
9.27738095e+00 7.59606003e+00 6.29249366e+00 5.91452686e+00
6.79882387e+00 7.57144653e+00 6.13515774e+00 2.70590543e+00
9.34668162e-01 3.86336659e+00 9.73615276e+00 1.52487556e+01
1.90469811e+01 2.20000000e+01]
There are negative values, which i find odd because the original data is only positive :
[7.0,
12.0,
20.0,
111.0,
132.0,
68.0,
22.0,
14.0,
12.0,
8.0,
6.0,
7.0,
1.0,
13.0,
22.0,
23.0,
5.0,
3.0,
5.0,
65.0,
236.0,
234.0,
105.0,
152.0,
466.0,
401.0,
157.0,
51.0,
21.0,
13.0,
11.0,
19.0,
15.0,
11.0,
9.0,
15.0,
86.0,
276.0,
423.0,
291.0,
108.0,
36.0,
22.0,
21.0,
16.0,
16.0,
13.0,
9.0]
And T is only a list that goes one by one from 1 to 48 (48 is the length of A and T) I feel that the issue is from a boundary issue but the problem is only in the beginning...
Any ideas ?
Upvotes: 0
Views: 1063
Reputation:
Nothing odd here: a cubic spline on positive data can attain negative values, no matter what the boundary conditions are. If it's necessary to maintain positivity, piecewise linear interpolation (degree 1 spline) is an option. Other options are discussed in How can I find a non-negative interpolation function?
Here is an illustration of why this happens: spl = CubicSpline([-2, -1, 1, 2], [10, 1, 1, 10])
This spline fits a parabola to the given points. The parabola dips into negative territory in the middle, between the points.
That in your example this happened near the boundary is not really important; it can happen anywhere.
Upvotes: 1