espdev / csaps

Cubic spline approximation (smoothing)
https://csaps.readthedocs.io
MIT License
164 stars 27 forks source link

Issue with the smooth parameter. #66

Closed lucassherbrook closed 6 months ago

lucassherbrook commented 1 year ago

What the csaps function looks like without smooth parameter:

spline = csaps(x_points, y_points)
plt.plot(x, fit)
plt.scatter(x_points, y_points)
plt.show()

spline_plot_no_smoothing

What the csaps function looks like with any non-default smoothing parameter, even if small:

spline = csaps(x_points, y_points, smooth=.05)
plt.plot(x, fit)
plt.scatter(x_points, y_points)
plt.show()

spline_plot_with_smoothing

espdev commented 1 year ago

Hello @lucassherbrook,

Could you provide a sample of your real data (x_points, y_points) for checking?

The result you showed on the second plot should be with smooth=0.0 (just linear regression). See in the doc: https://csaps.readthedocs.io/en/latest/tutorial.html#bounds-of-smoothing-parameter

lucassherbrook commented 1 year ago

Here is the data I inputted to csaps: spline_data.csv

It outputs linear regression for me when using any smoothing parameter (including for those outside of the [0,1] range).

espdev commented 1 year ago

The X-data nonuniformity on which the spline is built is such that the value of the smoothing parameter is in a very narrow range, close to 1.

There is the value of smooth that is automatically calculated: 0.999999999727055 You can change the value to 0.999998 and you will see that the spline fits your data correctly. 2023-06-13_19-47-56

Nevertheless, this is not very convenient. You can try to use smooth normalization to avoid the problems with X-data nonuniformity and scaling.

yi = csaps(x, y, xi, smooth=0.5, normalizedsmooth=True)

2023-06-13_19-51-52

In this case the effect from the smooth parameter will be almost linear in the range from 0 to 1.