Skip to content

feat(stats): add Savitzky-Golay smoothing filter - #1986

Open
Shizoqua wants to merge 1 commit into
online-ml:mainfrom
Shizoqua:feat/1424-savitzky-golay
Open

feat(stats): add Savitzky-Golay smoothing filter#1986
Shizoqua wants to merge 1 commit into
online-ml:mainfrom
Shizoqua:feat/1424-savitzky-golay

Conversation

@Shizoqua

Copy link
Copy Markdown

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_coeffs dot product. Because the polynomial is evaluated at the last point of the window, the filter is causal and compatible with online learning.

>>> from river import stats
>>> stat = stats.SavitzkyGolay(window_size=5, polyorder=2)
  • Coefficients computed once in __init__, applied over a fixed-size deque
  • get() returns None until the window has enough observations
  • window_size/polyorder follow scipy's constraint (polyorder < window_size, enforced by scipy)
  • Registered in stats/__init__.py and the docs nav
  • Release-note entry and CodSpeed benchmark included

Verification:

  • pytest tests/stats — 175 passed (incl. doctests)
  • Matches a manual least-squares polynomial fit to the same window; reproduces a degree-≤2 polynomial exactly
  • ruff check / ruff format --check clean

DCO sign-off included.

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>
@codspeed-hq

codspeed-hq Bot commented Aug 12, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 98 untouched benchmarks
🆕 1 new benchmark
⏩ 16 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
🆕 Simulation test_savitzky_golay_update N/A 5.1 ms N/A

Comparing Shizoqua:feat/1424-savitzky-golay (8c5cc9c) with main (0b8596f)

Open in CodSpeed

Footnotes

  1. 16 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant