RollingRms
Description
The RollingRms class calculates the root mean square (RMS) of values within a specified moving window. This provides a measure of the magnitude of variation in the data, capturing both positive and negative values as positive magnitudes.
Parameters:
window_size: Specifies the size of the rolling window.start_policy: Defines how the function handles the initial phase when fewer thanwindow_sizedata points are available. This parameter accepts one of the following three values:"strict": ReturnsNaNfor all calculations untilwindow_sizeelements have been processed."expanding": Adapts the computation by dynamically reducing the window size to include all available data, starting from a single point and growing untilwindow_sizeis reached."zero": Simulates a full initial window of zeros, effectively pre-filling the data stream withwindow_sizezeros before processing the actual input.
Usage Example and Plot
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from screamer import RollingRms
data = np.cumsum(np.random.normal(size=300))
fig = make_subplots(
rows=2, cols=1,
shared_xaxes=True,
row_heights=[2/3, 1/3],
vertical_spacing=0.1
)
fig.add_trace(go.Scatter(y=data, mode='lines', name='Input Data'), row=1, col=1)
fig.add_trace(go.Scatter(y=RollingRms(30)(data), mode='lines', name='Rolling RMS', line=dict(color='teal')), row=2, col=1)
fig.update_layout(
title="Rolling RMS with Window Size 30",
xaxis_title="Index",
yaxis=dict(title="Input Data"),
yaxis2=dict(title="Rolling RMS"),
margin=dict(l=20, r=20, t=80, b=20),
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
fig.show()
Implementation Details
Algorithm
RollingRms implements cyclic buffers to accumulate windowed statistics.
Complexity
Time Complexity:
O(log(1))per new element due to the insertion and deletion operations in the heaps.Space Complexity:
O(window_size), as only elements within the current window are stored.