espdev / csaps

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

questions about AutoSmoothingResult #59

Closed Bhare8972 closed 6 months ago

Bhare8972 commented 2 years ago

To whom it may concern,

Sorry I'm using a ticket to ask a question, but I couldn't see any other way to contact y'all.

This package looks VERY interesting to me. I've been looking for a python implementation of DeBoor smoothing splines, and was afraid I'd have to implement it myself. However, I have a few questions.

First, how does AutoSmoothingResult work? What criterion does it use to find a smoothing parameter? (may I suggest the answer be put in the documentation?)

Second, scipy univariate smoothing splines have an option where essentially you set the desired chi-squared fit and the algorithm automatically adjusts the number of knots until that condition is met. Does csaps have a similar option? (except instead of adjusting knots, the smoothing paramter is adjusted ofc), that is, something like the second equation under "de Boors approach" on the page https://en.wikipedia.org/wiki/Smoothing_spline.

Thanks!

espdev commented 2 years ago

Hi,

The smoothing parameter is computed just like in MATLAB implementation. See: https://csaps.readthedocs.io/en/latest/formulation.html#definition

The calculation of the smoothing spline requires the solution of a linear system whose coefficient matrix has the form , with the matrices and depending on the data sites . The automatically computed smoothing parameter makes ptrace(A) equal (1 - p)trace(B).

https://github.com/espdev/csaps/blob/b7f7cdc59e128124aac5fbdee392eea6c99936a8/csaps/_sspumv.py#L233-L244

"de Boors approach" and iterative adjusting knots algorithm are not used.

Also we can use the smoothing parameter normalization for adjusting to X scale: https://github.com/espdev/csaps/blob/b7f7cdc59e128124aac5fbdee392eea6c99936a8/csaps/_sspumv.py#L247-L261

If you want, you can suggest PR with an another implementation of auto-smoothing algorithm, and we will add it to the package as an option.

Bhare8972 commented 2 years ago

Thank you for the reply.

Is there any material on how to get some intuitive understanding of what autosmooth is doing? Perhaps a research paper citation?

Someday I'll try to implement the smoothing option, and make a PR once I do. But it will be awhile.

espdev commented 2 years ago

Only the following description from Matlab documentation without any links and citations, unfortunately.

2022-07-30_10-23-21

Matlab implementation also has roughness weights through smoothing parameter vector. Currently, this is not implemented in csaps Python package.

Bhare8972 commented 2 years ago

Thank you for your help.

Am thinking about how to implement this feature I want. It requires explicitly calculating the error measure.

Is there an easy way, inside the method _make_spline of the CubicSmoothingSpline class (at https://github.com/espdev/csaps/blob/b7f7cdc59e128124aac5fbdee392eea6c99936a8/csaps/_sspumv.py#L264)

to calculate the error measure? (I can't tell as I don't know what all the variables in _make_spline measure) Or does the spline need to be evaluated at x-locations, after creation, to calculate the error measure?