Foundations to Frontier
Tests
K. Time Series and Sensor Data › K3 Anomaly and Change Detection

Forecasting-based anomaly detection

A forecasting-based detector predicts each new value from the past and flags values that the forecast did not expect. The better the forecast, the smaller the normal errors and the smaller the anomalies it can see. But a forecaster that adapts quickly also follows a persistent change, so it is surprised only once. This page builds a seasonal-profile forecaster and a profile-plus-AR forecaster, sets thresholds from a target false-alarm rate, and measures how each detects spikes and level shifts.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Surprise as a signal

If you know a train usually arrives at 8:02, a train at 8:30 is surprising. A forecasting-based detector works the same way: it predicts the next value and raises an alarm when the real value is too far from the prediction. A sharper prediction makes smaller surprises visible. But if the timetable is updated every time a train is late, a lasting delay soon stops looking surprising.

Anomaly lab
AR(1) noise (φ = 0.8) with a spike of 2 at step 150. Light: profile residual ÷ 0.83; dark: AR residual ÷ 0.5; dashed: ±3.29 (1 in 1000). First crossing after onset — profile: none, AR: 0 steps. The AR residual removes the slowly wandering part, so the same spike stands out more.
Why engineers care

NASA’s Telemanom system (2018) used exactly this idea: an LSTM forecasts spacecraft telemetry, and large forecast errors become alarms.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A forecasting-based detector flags values that…
  2. 2. A better forecaster makes it possible to detect…
  3. 3. A forecaster that adapts quickly may…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Residual score: e_t = y_t − ŷ_t|t−1, standardised by the sd of residuals on normal data. Large |e_t| means surprise.
  • Forecasters: seasonal profiles, ARIMA, exponential smoothing, state-space models, LSTMs and TCNs; any model of normal behaviour will do.
  • Thresholds: set from a target false-alarm rate on normal validation data (for example the 99.9% quantile of |score|), or from a dynamic, local estimate of the error distribution.
  • Whitening: an AR model turns correlated residuals into nearly independent innovations, so a single unusual value is no longer hidden among slowly wandering errors.
  • Adaptation: a model that forecasts from recent values follows a level shift, so the residual is large only at the first step; accumulated statistics (EWMA, CUSUM) or a non-adaptive baseline catch persistent changes.
  • Prediction intervals: probabilistic forecasts give the threshold directly as a quantile.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Thresholds are best set from…
  2. 2. Whitening residuals with an AR model makes them…
  3. 3. Persistent shifts are caught better by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Profile and AR forecasters

Exercise 1.

  • Hourly data with a daily cycle and AR(1) noise (φ = 0.8, innovation sd 0.5), 60 days for training.
  • Forecaster 1: per-hour median profile. Forecaster 2: profile plus an AR(3) model of the profile residual.
  • Thresholds at the 99.9% quantile of |score| on validation data; a spike of 2 added to a test week.

Practice.

  • Fit the forecaster on clean normal data, or use robust fitting, so anomalies do not become part of normal behaviour.
  • Calibrate thresholds on separate validation data at the false-alarm rate operators can tolerate.
  • Combine a fast adaptive detector for spikes with an accumulating or non-adaptive one for shifts.
  • Watch forecast quality over time; a degraded model causes false alarms.
  • Tools: Merlion (forecast-based detectors), statsmodels, Prophet for profiles, Kats, Telemanom.
import numpy as np

def residual_scores(y, forecast, sd):
    return (y - forecast) / sd

def threshold(val_scores, rate=0.001):
    return np.quantile(np.abs(val_scores), 1 - rate)
🐍 Exercise 1: two forecasters, one spike
Fit a seasonal profile and a profile-plus-AR forecaster, set thresholds on validation data, and show that a spike of 4 innovation standard deviations is detected only by the AR forecaster.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Forecasters for detection should be fitted on…
  2. 2. For both spikes and shifts, use…
  3. 3. NASA's LSTM forecasting detector is called…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the AR forecaster sees spikes. With profile residuals r_t = φ r_t−1 + ε_t, the profile forecaster’s error is r_t, whose sd is σ/√(1 − φ²) = 0.83 for σ = 0.5 and φ = 0.8. The AR forecaster’s error is ε_t, with sd 0.5. A spike of size m therefore gives a score of about m/0.83 against the profile and m/0.5 against the AR model, so the same false-alarm rate detects spikes 1.7 times smaller.

Why it misses shifts. After the level jumps by δ, the AR forecast is built from shifted values, so the residuals are δ at the first step and then δ(1 − φ) = 0.2δ: the model absorbs the change.

EWMA undoes whitening. An EWMA with weight λ of the innovations, s_t = λε_t + (1 − λ)s_t−1, with λ = 1 − φ is exactly λ r_t, a multiple of the profile residual. In Exercise 2 the EWMA (λ = 0.2) of AR residuals behaved like the profile detector, within 0.05 in recall everywhere.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Profile residual sd for σ = 0.5 and φ = 0.8:
  2. 2. After a shift δ the AR(1) residuals settle at…
  3. 3. An EWMA of innovations with λ = 1 − φ equals…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

For spikes and for persistent level shifts of different sizes, which score detects more: residuals from a seasonal profile, from a profile-plus-AR forecaster, or an EWMA of the AR residuals?

History.

  • W. A. Shewhart (1920s) introduced control charts on raw measurements; S. W. Roberts (1959) introduced the EWMA chart.
  • Residual-based monitoring of autocorrelated processes (L. C. Alwan and H. V. Roberts, 1988) applied charts to time-series model residuals.
  • K. Hundman et al. (2018) used LSTM forecasts with dynamic thresholds on NASA telemetry.

Exercise 2.

  • Train on 60 days; set each threshold at the 99.9% quantile of |score| on 120 further days of normal data.
  • 200 test weeks per setting, with an anomaly at hour 72: a spike of 1, 2 or 3 at one point, or a level shift of 0.5, 1 or 2 lasting to the end.
  • A spike counts as found if its own score crosses the threshold; a shift if the score crosses within 48 hours of onset.

Seed 0, recall:

score spike 1 spike 2 spike 3 shift 0.5 shift 1 shift 2
profile residual 0.03 0.20 0.68 0.20 0.58 0.98
profile + AR residual 0.23 0.85 1.00 0.14 0.33 0.92
EWMA of AR residual 0.03 0.20 0.69 0.20 0.58 0.98
  • For spikes the AR residual won clearly: 0.85 against 0.20 for a spike of 2.
  • For shifts the profile residual was better: 0.58 against 0.33 for a shift of 1. The AR detector found shifts only at the first step (median delay 0 h) or not at all; the profile detector took a median 15 h for a shift of 1.
  • The EWMA matched the profile detector, as the theory predicts.
  • Seeds 1 and 2: spike of 2, profile 0.12 and 0.20 against AR 0.67 and 0.83; shift of 1, profile 0.23 and 0.35 against AR 0.15 and 0.28. The thresholds varied between seeds (profile 3.0 to 3.7), which moved the shift results most.
🐍 Exercise 2 (research): for spikes and for persistent level shifts of different sizes, which score detects more: residuals from a seasonal profile, from a profile-plus-AR forecaster, or an EWMA of the AR residuals?
Fit profile and profile-plus-AR forecasters, calibrate thresholds at a 1-in-1000 false-alarm rate, and measure recall and delay for spikes and level shifts of three sizes over 200 test weeks each.

Forecasting-based detection in practice

  • Deep forecasters: LSTMs, TCNs and transformers replace AR models for complex multivariate telemetry; the detection logic stays the same.
  • Dynamic thresholds: thresholds from recent error statistics adapt to changing noise; see spacecraft telemetry later in this section.
  • Combining scores: run a one-step residual test for spikes alongside CUSUM or EWMA on raw deviations for shifts, and calibrate the combined false-alarm rate.
  • Retraining: after a confirmed lasting change, refit the model so the new normal is not flagged forever.

References

  1. K. Hundman, V. Constantinou, C. Laporte, I. Colwell and T. Soderstrom (2018). “Detecting spacecraft anomalies using LSTMs and nonparametric dynamic thresholding.” KDD.
  2. S. W. Roberts (1959). “Control chart tests based on geometric moving averages.” Technometrics.
  3. L. C. Alwan and H. V. Roberts (1988). “Time-series modeling for statistical process control.” Journal of Business and Economic Statistics.
  4. D. C. Montgomery (2019). Introduction to Statistical Quality Control, 8th edition. Wiley.
  5. A. Blázquez-García, A. Conde, U. Mori and J. A. Lozano (2021). “A review on outlier/anomaly detection in time series data.” ACM Computing Surveys.