robustrolling¶
High-performance rolling-window statistics for R and Python — eleven algorithms implemented in C++17, exposed through idiomatic bindings in both languages, with O(1) or O(log w) updates per element.
API Reference
Algorithm overview¶
C++ class |
Algorithm |
Complexity |
R function(s) |
Python class / function(s) |
|---|---|---|---|---|
|
Prefix sum + SIMD |
O(n) batch |
|
|
|
Welford online variance (ring buffer) |
O(1) |
|
|
|
Prefix sums of raw moments |
O(n) batch |
|
|
|
Monotonic deque maximum |
O(1) amortised |
|
|
|
Monotonic deque minimum |
O(1) amortised |
|
|
|
Auto-dispatches to one of three below |
— |
|
|
|
Sorted |
O(w) insert |
— |
|
|
|
O(log w) |
— |
|
|
Two heaps + lazy deletion |
O(log w) |
— |
|
|
Terriberry 4th-moment online algorithm |
O(1) |
|
|
|
2-D Welford online covariance |
O(1) |
|
|
Performance¶
Benchmarked on Apple M-series (ARM), window = 100, n = 1 000 000 (median of 10 runs).
Python vs pandas
Function |
robustrolling |
pandas |
speedup |
|---|---|---|---|
|
3.1 ms |
4.4 ms |
1.4x |
|
11.1 ms |
11.7 ms |
1.1x |
|
11.2 ms |
12.2 ms |
1.1x |
|
106 ms |
233 ms |
2.2x |
|
15.2 ms |
9.6 ms |
0.6x |
|
14.0 ms |
9.1 ms |
0.6x |
|
14.3 ms |
9.2 ms |
0.6x |
|
14.8 ms |
18.2 ms |
1.2x |
|
14.6 ms |
36.7 ms |
2.5x |
Python vs Polars
rolling_median uses FlatMedian at window ≤ 600; TwoHeapMedian
wins at large windows where Polars’ fixed O(w) algorithm loses ground.
Function |
robustrolling |
Polars |
speedup |
|---|---|---|---|
|
3.1 ms |
8.0 ms |
2.6x |
|
11.1 ms |
11.4 ms |
1.0x |
|
11.0 ms |
11.6 ms |
1.1x |
|
55 ms |
41 ms |
0.7x (w=100) |
|
15.7 ms |
16.2 ms |
1.0x |
|
13.9 ms |
16.0 ms |
1.2x |
|
14.3 ms |
15.6 ms |
1.1x |
Python — stable vs fast (prefix-sum acceleration)
Function |
stable |
fast |
speedup |
|---|---|---|---|
|
3.2 ms |
0.73 ms |
4.4x |
|
15.2 ms |
3.9 ms |
3.9x |
|
13.9 ms |
10.0 ms |
1.4x |
|
14.4 ms |
7.6 ms |
1.9x |
R vs slider vs RcppRoll
Function |
robustrolling |
slider |
RcppRoll |
vs slider |
vs RcppRoll |
|---|---|---|---|---|---|
|
15.1 ms |
338 ms |
175 ms |
22x |
12x |
|
14.9 ms |
350 ms |
175 ms |
24x |
12x |
|
3.1 ms |
1 523 ms |
37.4 ms |
487x |
12x |
|
16.0 ms |
2 477 ms |
304 ms |
154x |
19x |
|
112 ms |
10 084 ms |
1 938 ms |
90x |
17x |
R — stable vs fast
Function |
stable |
fast |
speedup |
|---|---|---|---|
|
3.2 ms |
0.78 ms |
4.0x |
|
16.2 ms |
4.1 ms |
4.0x |
|
14.5 ms |
10.3 ms |
1.4x |
|
14.4 ms |
7.8 ms |
1.8x |
Install for R¶
# Requires R ≥ 4.0 and a C++17 compiler
install.packages("remotes")
remotes::install_github("IgorPtak/rolling_window")
Install for Python¶
# Requires Python ≥ 3.10 and a C++17 compiler
pip install git+https://github.com/IgorPtak/rolling_window.git#subdirectory=py_package
# Or clone and install locally:
git clone https://github.com/IgorPtak/rolling_window.git
pip install rolling_window/py_package/