feat(stats): add Savitzky-Golay smoothing filter - #1986
Conversation
Merging this PR will not alter performance
Performance Changes
Comparing Footnotes
|
60dbb8a to
b646dd1
Compare
MaxHalford
left a comment
There was a problem hiding this comment.
Overall looks good, but I would like to see a use case so users can understand when to use this
| self._coeffs = savgol_coeffs( | ||
| window_length=window_size, | ||
| polyorder=polyorder, | ||
| pos=window_size - 1, | ||
| use="dot", | ||
| ) |
There was a problem hiding this comment.
Is this a list or a numpy array? I think it should be the former
|
Thanks @MaxHalford! I added a use case to the docstring: a noisy temperature probe drifting upward, where smoothing reveals the underlying trend so it can be fed to a model or a threshold. New commit 7121884 includes the example and the doctest output. |
Adds stats.SavitzkyGolay, a rolling Savitzky-Golay filter that fits a polynomial of a given degree to a sliding window of observations by least squares and returns the smoothed value of the most recent point. Because the fit is evaluated at the last point of the window, the filter is causal and can be used online, e.g. to smooth sensor or market signals before feature extraction. The coefficients are computed once via scipy.signal.savgol_coeffs and applied with a fixed-size deque; get() returns None until the window is full. Signed-off-by: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Signed-off-by: Shizoqua <136805224+Shizoqua@users.noreply.github.com>
Signed-off-by: Shizoqua <136805224+Shizoqua@users.noreply.github.com>
7121884 to
23f2f93
Compare
|
A commenter in the issue you reference mentioned the article Why and how Savitzky–Golay filters should be replaced, which argues that one should replace the SG filter with other, more stable and less noisy, filters like sinc or Whittaker–Henderson. Is it not better to implement these alternative filters rather than Savitzky–Golay? Also, did you read the Contributing guide? Your PR cover letter makes me think you did not, but I could be wrong. |
Adds
stats.SavitzkyGolay, a rolling Savitzky-Golay smoothing filter as requested in #1424.Savitzky-Golay fits a polynomial of a given degree to a sliding window of observations by least squares and returns the smoothed value of the most recent point via the precomputed
scipy.signal.savgol_coeffsdot product. Because the polynomial is evaluated at the last point of the window, the filter is causal and compatible with online learning.__init__, applied over a fixed-sizedequeget()returnsNoneuntil the window has enough observationswindow_size/polyorderfollow scipy's constraint (polyorder < window_size, enforced by scipy)stats/__init__.pyand the docs navVerification:
pytest tests/stats— 175 passed (incl. doctests)ruff check/ruff format --checkcleanDCO sign-off included.