Anomaly Detection in Time Series Using Statistical Analysis

Ivan Shubin15 Apr 2025

Problem

A static threshold works for disk space but fails for orders or sales, because user activity swings through the day and week. Comparing a metric to its value exactly one week ago is a common shortcut. It breaks because an outage last week becomes this week's baseline, so the same outage at the same hour goes unnoticed. It also cannot see a decline that builds up over several weeks. The alerts must fire within minutes and ignore old incidents.

Example from the article: a metric that normally sits near 600 dropped to 200 one week earlier. A naive week-over-week comparison treats that dip as the norm.

Solution in brief

A small service builds a lower and upper bound for every point from the same time and weekday in the last 4-5 weeks, after removing past incidents with a median-normalized z-score filter. The bounds are written back as metrics, and the alerting tool fires when the metric leaves them. Helper metrics (offset, correction, adjustment factor) cover readability, events and overperformance.

Questions this case answers
  • How to detect incidents in a seasonal business metric using only its own history, when past incidents contaminate the baseline
  • How to turn an anomaly signal into an alert that states the size of the loss and can be adjusted for known events
  • How to alert on a business metric that swings through the day and week, when a static threshold does not work?
  • How to build the expected range of a metric from past weeks when those weeks contain their own incidents?

Tools and methods

  • Percentile prediction range — lower and upper bounds from the same time window on the same weekday in past weeks
  • Median-normalized z-score filter — drops incident weeks from the history before the range is computed
  • Offset metric — distance of the metric outside the range, summed for alerts
  • Grafana — alert rules and dashboards on top of the range metrics
Continue reading — it's free

Google or a one-time email code. No password.