Seasonal hybrid ESD (S-H-ESD) is a statistical method for time series anomaly detection that first removes seasonality and trend from a series and then applies a robust version of the generalized extreme studentized deviate (ESD) test to the remaining residuals. In anomaly detection, it is a classic unsupervised baseline for seasonal operational metrics such as request rates, CPU load or user activity.
The method was developed at Twitter and released in 2015 in the open-source AnomalyDetection package for R, which is why it is often called Twitter anomaly detection. Its word “hybrid” refers to replacing the mean and standard deviation of the classic test with the median and the median absolute deviation (MAD), which resist distortion by the anomalies themselves.
How S-H-ESD isolates and tests residuals
S-H-ESD works in two stages: decomposition, which explains away the expected behavior, and hypothesis testing, which decides whether what remains is extreme. A point is flagged only when it deviates strongly from both its seasonal pattern and the typical level of the series.
- Seasonal extraction: the series is decomposed with STL (seasonal-trend decomposition using Loess) for a known period, such as 1,440 minutes for daily cycles in minute-level data.
- Median substitution: the STL trend is replaced by the median of the series, because the trend component can absorb anomalies and hide them.
- Residual computation: each residual equals the observed value minus the seasonal component minus the median.
- Robust ESD testing: the generalized ESD test runs on the residuals, using the median and MAD in place of the mean and standard deviation.
- Anomaly reporting: the points removed in the last significant test iteration and before it are returned as anomalies.
The generalized ESD test, introduced by Rosner in 1983, requires an upper bound k on the number of outliers. It repeatedly removes the most extreme residual, computes a test statistic, and compares it with a critical value derived from the t distribution at significance level α. Unlike a single Grubbs test, it handles multiple outliers without masking effects.
Key parameters and their effect
The behavior of S-H-ESD is controlled by a small set of parameters. The most consequential is max_anoms, the maximum share of points that may be reported, because it sets the upper bound k for the ESD test.
| Parameter | Typical value | What it controls |
|---|---|---|
| max_anoms | 0.01 to 0.10 | Upper bound on reported anomalies as a share of data |
| alpha | 0.05 | Significance level of each ESD test step |
| direction | pos, neg or both | Which side of the distribution is tested |
| period | Length of one seasonal cycle | Seasonal component extracted by STL |
| longterm | Enabled for multi-week data | Piecewise median over windows, usually two weeks |
| threshold | None, med_max, p95 or p99 | Filters out anomalies below a daily-maximum cutoff |
Setting max_anoms too low can hide real incidents once the bound is reached, while setting it too high mainly increases computation, since the test itself still decides significance. The longterm option matters for long series with gradual drift, because a single global median would otherwise turn the drift into a stream of false alarms.
How S-H-ESD compares with related methods
S-H-ESD belongs to the family of decomposition-based statistical detectors. It contrasts with machine learning methods that learn normal behavior from features and with methods designed for structural change rather than isolated spikes.
| Method | Core idea | Best suited for | Main weakness |
|---|---|---|---|
| S-H-ESD | STL residuals plus robust ESD test | Univariate seasonal metrics | Fixed single seasonality |
| Plain generalized ESD | ESD test on raw values | Roughly normal, nonseasonal data | Seasonal peaks flagged as outliers |
| Spectral residual | Saliency in the frequency domain | Fast streaming detection | Less interpretable thresholds |
| Forecasting-based detection | Prediction error of a model | Complex patterns and covariates | Requires training and tuning |
| Change point detection | Shifts in mean or variance | Level shifts and regime changes | Ignores short spikes |
S-H-ESD detects both global and local anomalies. A global anomaly exceeds the normal range of the entire series, while a local anomaly is unusual only for its position in the seasonal cycle, such as moderate traffic at 3 a.m. that would be normal at noon. This makes the method a practical detector of contextual anomalies in seasonal data.
Practical use in monitoring systems
The typical deployment is batch analysis of operational metrics, run every few minutes or hours over a sliding window of recent data. Engineers use it to flag unusual traffic, error rates or resource usage after deployments, outages or abuse events.
Good practice includes choosing a window that covers at least two full seasonal periods, aggregating raw data to a resolution where seasonality is stable, and routing results through an alerting layer that groups consecutive anomalies into a single incident. Ports of the method exist for Python and other languages, and their defaults may differ from the original R package, so parameters should be checked explicitly.
Limitations and common mistakes
S-H-ESD is a robust baseline, but its statistical assumptions limit where it fits. The residuals are assumed to be approximately normal after robust scaling, and only one seasonal period is modeled.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Weekly peaks flagged daily | Only the daily period modeled | Model the weekly period or deseasonalize twice |
| Drift reported as anomalies | Global median on long data | Enable piecewise longterm medians |
| Incident only partly flagged | max_anoms bound reached | Raise max_anoms for the window |
| Level shift missed after a day | New level absorbed by seasonality | Add a change point detector |
| Too many minor alerts | No magnitude filter | Apply a p95 or p99 threshold |
The method is also univariate: it cannot relate anomalies across correlated metrics, so multivariate anomaly detection requires running it per series and correlating the results, or choosing a different technique.
In everyday language
Many online services follow a daily rhythm. Traffic rises in the morning and falls at night. A busy evening is normal, so a simple rule would raise too many alarms.
Seasonal hybrid ESD first subtracts the usual daily pattern. What remains should look like small, random noise. The method then asks a careful statistical question about each large leftover value: is it too big to be chance? It uses the middle value rather than the average, so a few wild values cannot fool it.
Seen in the real world
An operations team at a video streaming service monitors requests per minute across its playback servers. Traffic follows a strong daily cycle, with evening peaks roughly four times higher than the early-morning low. Fixed thresholds either missed night-time problems or produced constant alerts every evening.
The team runs S-H-ESD every 15 minutes over the last 14 days of data, with a daily period of 1,440 points, max_anoms of 0.02, alpha of 0.05 and the p95 threshold filter. One night at 2 a.m., a faulty configuration push halves traffic in one region. The drop stays far below the evening peaks, yet the method flags it as a local anomaly within the next run. Engineers roll back the change within 30 minutes, while the volume of false evening alerts falls sharply.