From global to contextual
In b2 you built the statistical detectors - z-score, IQR, and robust MAD - and felt their limit: they judge every point against the whole dataset, so they miss anything that is only odd for its moment. Tonight you fix that. Rolling and seasonal detectors compare each point to what was normal right there - its recent neighbours, or its weekday - and that is what finally catches the contextual anomaly. At the end you drive a live detector that scores five methods in front of you.
Judge each point against its neighbours 6 min live
A rolling detector keeps a moving window of recent days and asks: is today far from the local mean, in units of the local standard deviation? Because the window travels with the series, it adapts to a drifting trend that would fool a global threshold. EWMA does the same with an exponentially-weighted memory instead of a hard window.
LiveRolling and EWMA, and where they still fail3 min▶
- Rolling z-score - local mean and std over a trailing window; flags points beyond k local sigmas. Adapts to trend and slow drift.
- EWMA - an exponentially-weighted moving average; recent points count more, no hard window edge. Smooth and responsive.
- The catch - if the window spans the weekly cycle, the seasonal swing inflates the local std and the weekend dips look like anomalies. Rolling fixes trend, not seasonality. That is what the seasonal detector is for.
Rolling detectors are the backbone of ops monitoring. Most metric-alerting systems are, under the hood, a rolling mean and a band. They are cheap, streaming-friendly, and explainable - which is why they are everywhere. Their weakness is exactly the seasonal one you are about to fix.
Subtract the pattern, then flag the remainder 5 min live
The strongest simple detector on seasonal data does one clever thing: it removes the expected weekly pattern first. Decompose the series into trend, season, and residual (STL), and run your detector on the residual alone. Now a weekday-at-weekend-level jumps out, because against its own weekday it is far from normal.
Self-studyBuild the rolling and STL-residual detectors2 min read▶
Both are a few lines on the Lumen series from b1:
The STL-residual detector is the one that finally catches the contextual anomaly - because on its own weekday, it is far from normal.
The live detector ★ real math in your browser
This is a genuine detector, not a mock-up. It runs z-score, IQR, robust MAD, rolling-window, and seasonal-residual detectors on a synthetic Lumen series with four injected anomalies, flags points in red, rings the true anomalies, and scores precision, recall, and F1 live. Switch the method and the sensitivity, and watch the trade-off. Note which anomalies each method can and cannot see - the global ones catch the obvious spikes and miss the contextual dip; the seasonal-residual detector catches them all.
Compare all five on Lumen ★ 8 min · build your own
Score every detector so far side by side and see the seasonal one win.
Run the z-score, IQR, MAD, rolling, and STL-residual detectors on the same Lumen series.
Score precision, recall, and F1 for each against the labels.
Confirm the seasonal-residual detector has the best F1, and identify which anomaly each simpler method misses.
Deseasonalizing is the highest-leverage move in time-series detection. Before reaching for isolation forests or autoencoders, remove the known pattern - weekday, holiday, campaign - and detect on what is left. Half of "our model is too noisy" turns out to be a seasonal pattern nobody subtracted.
Try it yourself - this week ◐ 30-45 min total
- Build the rolling z-score and EWMA detectors on your own series and see how they track a drifting trend.
- Decompose with STL and run a robust threshold on the residual. Does it catch a contextual anomaly the global methods miss?
- Sweep the sensitivity and plot precision vs recall - find the point that matches your cost of a miss vs a false alarm.
- On the live detector, find a method + sensitivity that reaches F1 = 100%, and one that floods with false alarms.
- Optional: read the STL decomposition section of FPP3 to see how the seasonal component is estimated.
Sources covered (cited)
Taught from the detection canon. This session covers:
Three questions before you go 🎯 ◐ 90 seconds
1 · What does a rolling-window detector fix that a global z-score does not?
The window travels with the series, so it adapts to a drifting trend. It does NOT fix seasonality - a weekly swing inside the window still trips it.
2 · Why does the STL-residual detector catch the contextual anomaly?
Subtracting the seasonal component leaves a flat residual. Against its own weekday, the contextual point now stands out - which it never did globally.
3 · Turning sensitivity up does what?
Sensitivity is a precision/recall trade-off. More sensitive = catch more real anomalies but flag more false ones. Where you set it is a business decision.