feat(stats): add Savitzky-Golay smoothing filter - #1986
Conversation
Merging this PR will not alter performance
Performance Changes
Comparing Footnotes
|
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>
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
Signed-off-by: Shizoqua <136805224+Shizoqua@users.noreply.github.com>
|
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 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.