File Exchange

image thumbnail


version (7.48 KB) by Matthew Taliaferro
Returns optimal cubic smoothing spline using the generalized cross validation method


Updated 01 Jun 2017

View License

Function similar to csaps. Determines the optimal smoothing parameter using the generalized cross validation method from Craven and Wahba. The function returns the output from a smoothing spline fit, the smoothing parameter, the estimated variance, the magnitude of the estimated 95% confidence interval, and the value of the minimizing function.
The generalized cross validation method for choosing the smoothing parameter should be more robust and give "better" answers than the default choice for csaps. It works for data that is not equally spaced.

The code has not been optimized.

Cite As

Matthew Taliaferro (2020). csapsGCV(x,y,p,xx,W) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Very nice, it's great to have a csaps substitute that doesn't require the Curve Fitting Toolbox! One thing I'd recommend changing though: use sparse matrices for Q, B, and T. Otherwise you're wasting a ton of memory and slowing it down a lot when smoothing out large data sets. If you peek into the csaps.m code that's what they're doing, with spdiags instead of diag


When I use the function, it shows the warning below.

Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
> In csapsGCV>GCV at 173
In csapsGCV at 97

How to solve this question?


Comprehensive rewrite of code. The method for finding Q has been fixed. Doesn't use a csaps (and therefore ppval). Uses "ordinary" cross-validation for data with less than 30 data points. An improved method for finding an initial guess.

Added some extra outputs and imputs to more closely mimic csaps.
Corrected a mistake used when calculating the residual sum of squares. This gave incorrect variance estimates.

Added more to the description

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux