RollingZscore#
Description#
The RollingZscore class computes the rolling z-score for each value in a specified moving window, representing how many standard deviations each data point is from the mean of its window. This metric is useful for identifying outliers or deviations in a sequence.
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.
NaN handling#
Policy: ignore. A NaN in any input at index t causes the function to skip that step: output at t is NaN and internal state is unchanged. Subsequent finite samples are processed as if step t had not occurred.
Examples#
Usage example#
Implementation Details#
Algorithm#
RollingZscore 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.