Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.If you cannot redraw it, it is strange
Imagine learning to sketch heartbeats from memory using only a few strokes. After seeing thousands of normal beats you can redraw any normal one well. Shown an irregular beat, your few strokes cannot capture it, and the sketch differs from the original. A reconstruction-based detector measures exactly that difference.
Autoencoders for anomaly detection were proposed in the 1990s; today variational autoencoders and transformers play the same role on large multichannel telemetry.
- 1. The anomaly score of a reconstruction detector is…
- 2. The compressor is trained on…
- 3. Anomalous windows are reconstructed…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Windows: cut the series into overlapping windows of length w; each becomes a vector.
- PCA: project onto the top k principal components of normal windows and back; score = squared distance to that subspace.
- Autoencoder: a neural encoder maps the window to a small code and a decoder maps it back; trained on normal windows to minimise reconstruction error. Nonlinear, so it can follow curved families of normal shapes.
- Nearest-neighbour (discord) score: distance to the closest normal window; the matrix profile computes it efficiently for all subsequences of a series.
- Evaluation: ROC AUC (the probability that an anomaly outscores a normal window) summarises ranking without choosing a threshold.
- Localisation: window scores can be spread back to points by averaging over the windows that contain each point.
- 1. PCA reconstruction error measures distance to…
- 2. An autoencoder's advantage over PCA:
- 3. ROC AUC is the probability that…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Compressing normal windows
Exercise 1.
- Normal windows of 32 values: a cycle with random period 8–16, phase and amplitude 0.5–1.5, plus noise sd 0.1.
- Anomalies: a spike of 0.8, a slightly too fast cycle (period 6–7.5), the last 8 values frozen, and peaks clipped at 70% of the amplitude.
- PCA on 2000 normal windows with 2 or 8 components; AUC on 500 normal and 500 anomalous windows of each kind.
Practice.
- Train on clean normal data; anomalies in the training set get reconstructed well and stop looking anomalous.
- Choose the code size on validation data: too small and normal windows reconstruct badly, too large and anomalies reconstruct too.
- Normalise windows consistently and pick the window length to cover the patterns of interest (for example one cycle or more).
- Report AUC per anomaly kind, not just overall.
- Tools: PyOD (AutoEncoder, PCA), stumpy (matrix profile), Merlion, TSB-UAD benchmark.
import numpy as np
def pca_score(Xtrain, X, k):
mu = Xtrain.mean(0)
V = np.linalg.svd(Xtrain - mu, full_matrices=False)[2][:k].T
Z = X - mu
return np.sum((Z - Z @ V @ V.T) ** 2, 1)- 1. Contaminated training data make anomalies…
- 2. A code that is too large lets…
- 3. A Python library for the matrix profile:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why varying periods need many components. A cycle with random phase lives in a 2-dimensional linear space (sine and cosine). A random amplitude keeps it there. A random period does not: as the period changes, the window traces a curve that leaves any fixed plane. In Exercise 1 two components captured 54% of the variance of normal windows and eight captured 99%. The true family has only three parameters, so a nonlinear code with 3 or 4 numbers can in principle describe it.
PCA as the optimal linear compressor. Among all k-dimensional linear subspaces, the span of the top k principal components minimises the average squared reconstruction error (Eckart–Young). The leftover error equals the sum of the remaining eigenvalues. An autoencoder with linear layers and squared loss learns the same subspace.
Why contamination hurts. Training minimises the error on the training set. If 5% of training windows are frozen, the compressor spends capacity reconstructing frozen shapes, so new frozen windows have lower error. In Exercise 2 the AUC of 8-component PCA for frozen windows fell from 0.95 to 0.81.
- 1. Windows of a cycle with random phase and fixed period lie in…
- 2. PCA minimises average reconstruction error among…
- 3. A linear autoencoder with squared loss learns…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.On windows whose normal shapes form a curved family, how do PCA with 4 or 8 components, an autoencoder with a 4-number code and nearest-neighbour distance compare on spikes, wrong periods, frozen values and clipped peaks, and how much does training contamination hurt?
History.
- K. Pearson (1901) and H. Hotelling (1933) introduced principal components; J. E. Jackson and G. S. Mudholkar (1979) used the PCA residual (Q statistic) for process monitoring.
- S. Hawkins et al. (2002) and earlier work used replicator (autoencoder) networks for outlier detection.
- E. Keogh, J. Lin and A. Fu (2005) defined time-series discords; C.-C. M. Yeh et al. (2016) introduced the matrix profile.
- P. Malhotra et al. (2016) used LSTM encoder–decoders on multichannel sensor data.
Exercise 2.
- 2000 normal training windows; 500 normal and 500 anomalous test windows of each kind.
- Detectors: PCA with 4 or 8 components; an autoencoder 32–64–4–64–32 (tanh hidden layers, Adam, 120 epochs); squared distance to the nearest training window.
- Contamination: retrain each detector with 5% of training windows replaced by frozen anomalies.
Seed 0, ROC AUC:
| detector | spike | period | frozen | clipped | frozen, contaminated |
|---|---|---|---|---|---|
| PCA, 4 components | 0.653 | 0.970 | 0.834 | 0.505 | 0.829 |
| PCA, 8 components | 0.996 | 1.000 | 0.947 | 0.890 | 0.810 |
| autoencoder, 4-number code | 0.997 | 1.000 | 1.000 | 0.880 | 0.920 |
| nearest neighbour | 0.892 | 1.000 | 0.997 | 0.636 | 0.874 |
- The autoencoder with a 4-number code matched PCA with 8 components within 0.01 and beat it on frozen windows, and was far better than PCA with 4 (spikes 0.997 against 0.653).
- Clipped peaks were the hardest: at most 0.89 for any method; nearest-neighbour distance managed only 0.64, because a clipped window is close to some normal window of smaller amplitude.
- Contamination with 5% frozen windows cut the frozen AUC for every detector except 4-component PCA, which was already limited; 8-component PCA fell from 0.947 to 0.810.
- Seeds 1 and 2 gave the same ranking: autoencoder spike AUC 0.996 and 0.999, nearest-neighbour clipped AUC 0.645 and 0.657, and 8-component PCA frozen AUC after contamination 0.76 in both.
Reconstruction-based detection in practice
- Multichannel data: windows of many sensors together let the model learn correlations, so a broken relationship between channels shows up as error.
- Deep variants: LSTM and convolutional autoencoders, VAEs (with reconstruction probability), USAD, OmniAnomaly and transformer models such as Anomaly Transformer.
- Caution: benchmark studies have found that simple methods, including PCA and nearest-neighbour discords, are often competitive with deep models.
- Combining with forecasting: reconstruction catches shape anomalies, forecasting catches surprises in time; many systems run both.
References
- J. E. Jackson and G. S. Mudholkar (1979). “Control procedures for residuals associated with principal component analysis.” Technometrics.
- S. Hawkins, H. He, G. Williams and R. Baxter (2002). “Outlier detection using replicator neural networks.” DaWaK.
- C.-C. M. Yeh et al. (2016). “Matrix profile I: all pairs similarity joins for time series.” IEEE ICDM.
- P. Malhotra et al. (2016). “LSTM-based encoder-decoder for multi-sensor anomaly detection.” ICML Anomaly Detection Workshop.
- J. Paparrizos et al. (2022). “TSB-UAD: an end-to-end benchmark suite for univariate time-series anomaly detection.” PVLDB.