RollingMedianAD#
Description#
RollingMedianAD computes the rolling median absolute deviation (MAD): for the
trailing window it takes the median of the absolute deviations from the window
median,
Unlike RollingMad, which is the mean absolute deviation, this is
a robust scale estimate: a few extreme samples cannot inflate it. It is the scale
primitive behind the Hampel and ImpulseClip
despikers. The raw MAD is returned; multiply by 1.4826 for a Gaussian-consistent
standard-deviation estimate.
Parameters:
window_size: (int) Trailing-window length. Must be positive.start_policy: How the initial phase is handled beforewindow_sizesamples are available:"strict":NaNuntil the window is full."expanding": compute over the samples seen so far, from the first sample."zero": treat missing samples as zeros (a full window of zeros to start).
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#
import numpy as np
from screamer import RollingMedianAD
x = np.array([1.0, 2.0, 1.0, 2.0, 50.0, 2.0, 1.0])
print(RollingMedianAD(5)(x)) # robust scale, barely moved by the spike
Implementation Details#
O(W) per step: the window median shifts every step, so the absolute deviations
cannot be updated incrementally. Each step copies the active window, finds the median
with std::nth_element, then finds the median of the absolute deviations the same
way. Strictly causal.