How it works...
In the 3σ approach, for each time point, we calculated the moving average (μ) and standard deviation (σ) using the last 21 days (not including that day). We used 21 as this is the average number of trading days in a month, and we work with daily data. However, we can choose different values, and then the moving average will react faster/slower to changes. We can also use (exponentially) weighted moving average if we find it more meaningful in our particular case.
The condition for a given observation x to be qualified as an outlier is x > μ + 3σ or x < μ - 3σ.
In the first step, we calculated the rolling metrics using the rolling method of a pandas DataFrame. We specified the window's size and the metrics we would like to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access