Seasonal hybrid ESD

Also known as: S-H-ESD · Twitter anomaly detection

On this page 7

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.

  1. 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.
  2. Median substitution: the STL trend is replaced by the median of the series, because the trend component can absorb anomalies and hide them.
  3. Residual computation: each residual equals the observed value minus the seasonal component minus the median.
  4. Robust ESD testing: the generalized ESD test runs on the residuals, using the median and MAD in place of the mean and standard deviation.
  5. 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.

ParameterTypical valueWhat it controls
max_anoms0.01 to 0.10Upper bound on reported anomalies as a share of data
alpha0.05Significance level of each ESD test step
directionpos, neg or bothWhich side of the distribution is tested
periodLength of one seasonal cycleSeasonal component extracted by STL
longtermEnabled for multi-week dataPiecewise median over windows, usually two weeks
thresholdNone, med_max, p95 or p99Filters out anomalies below a daily-maximum cutoff
Main S-H-ESD parameters in the reference implementation

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.

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.

MethodCore ideaBest suited forMain weakness
S-H-ESDSTL residuals plus robust ESD testUnivariate seasonal metricsFixed single seasonality
Plain generalized ESDESD test on raw valuesRoughly normal, nonseasonal dataSeasonal peaks flagged as outliers
Spectral residualSaliency in the frequency domainFast streaming detectionLess interpretable thresholds
Forecasting-based detectionPrediction error of a modelComplex patterns and covariatesRequires training and tuning
Change point detectionShifts in mean or varianceLevel shifts and regime changesIgnores short spikes
S-H-ESD compared with neighboring anomaly detection methods

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.

SymptomLikely causeRemedy
Weekly peaks flagged dailyOnly the daily period modeledModel the weekly period or deseasonalize twice
Drift reported as anomaliesGlobal median on long dataEnable piecewise longterm medians
Incident only partly flaggedmax_anoms bound reachedRaise max_anoms for the window
Level shift missed after a dayNew level absorbed by seasonalityAdd a change point detector
Too many minor alertsNo magnitude filterApply a p95 or p99 threshold
Typical problems and their remedies

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.