Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csaps.CubicSmoothingSpline does not perform natural boundary condition extrapolation correctly #52

Open
shusheer opened this issue Jul 24, 2021 · 2 comments

Comments

@shusheer
Copy link
Contributor

shusheer commented Jul 24, 2021

csaps.CubicSmoothingSpline purports to perform extrapolation using the "natural" boundary condition.

From https://github.com/espdev/csaps/blob/master/docs/formulation.rst:
"csaps spline is cubic only and it has natural boundary condition type."

This means the first derivative should be constant outside the range of observations provided to the smoother. This is not currently the behaviour.

Note that the scipy.interpolate.CubicSpline also exhibits the same behaviour, even when we explicitly specify bc_type='natural'

Example code to test:

import matplotlib.pyplot as plt

import numpy as np

from csaps import CubicSmoothingSpline

from scipy.interpolate import CubicSpline

xs = [1,2,3,4,5,6,7,8]
ys = [4.5, 3.6, 1.6, 0.0, -3.3, -3.1, -1.8, -1.7]

csaps_smoother = CubicSmoothingSpline(xs, ys)

scipy_smoother = CubicSpline(xs,ys, bc_type='natural')

extrapolated = np.linspace(min(xs)-10,max(xs)+10,int(21+(max(xs)-min(xs))))


fig, axs = plt.subplots(2, 2,figsize=(12,12))
axs[0, 0].plot(xs, ys,'.')
axs[0, 0].plot(extrapolated,csaps_smoother(extrapolated),'-')
axs[0, 0].set_title('csaps, deriv=0')
axs[0, 1].plot(xs, ys,'.')
axs[0, 1].plot(extrapolated,scipy_smoother(extrapolated),'-')
axs[0, 1].set_title('scipy, deriv=0')
axs[1, 0].plot(xs, csaps_smoother(xs,nu=1),'.')
axs[1, 0].plot(extrapolated,csaps_smoother(extrapolated,nu=1),'-')
axs[1, 0].set_title('csaps, deriv=1')
axs[1, 1].plot(xs, scipy_smoother(xs,nu=1),'.')
axs[1, 1].plot(extrapolated,scipy_smoother(extrapolated,nu=1),'-')
axs[1, 1].set_title('scipy, deriv=1')
plt.show()

I believe this issue is a superset of Issue #46 (periodic functions) wherein csaps.CubicSmoothingSpline._make_spline does not take account of boundary condition.

@shusheer
Copy link
Contributor Author

See also scipy/scipy#14472

@ev-br
Copy link

ev-br commented Aug 6, 2021

Commented in the scipy issue. Basically, CubicSpline only guarantees the value of derivatives at the boundary, and its extrapolation mode may be not very useful. However it provides all the info for a user to do the extrapolation, and it needs to be taken care of by the user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants