Reconstruction Error as an Anomaly Score
An autoencoder trained only on normal data learns to compress and rebuild normal patterns well. When it meets something abnormal, its reconstruction is bad — and that badness, measured directly, is a usable anomaly score with no labeled anomalies required.
Prerequisites: The Multilayer Perceptron, Outlier Detection and Winsorization
Labeled anomalies are rare almost by definition — a "flash crash," a "spoofing episode," a "broken feed" happens a handful of times a year, if that, and each one is a little different from the last. Training a supervised classifier to spot the next one from a dozen examples of the last few is a losing proposition. What you do have, in abundance, is normal data — millions of ordinary ticks, ordinary days, ordinary spreads. An autoencoder turns that asymmetry into an advantage: train a network to compress and then rebuild only normal data, and its failure to rebuild something well becomes the anomaly detector, with zero labeled anomalies required to train it.
The analogy: a forger who only ever copied Rembrandts
Imagine a forger who spent twenty years perfecting the ability to reproduce Rembrandt paintings, brushstroke by brushstroke, from memory alone — hand them a Rembrandt, take it away, and they can recreate it almost perfectly. Now hand that same forger a Picasso and ask for a faithful copy from memory. It will come out wrong — not randomly wrong, but wrong in a specific, informative way: the forger will unconsciously drag Picasso's shapes back toward Rembrandt-like textures and proportions, because that's the only vocabulary they've built. The size of the mistake — how far the copy strays from the original — tells you the copy's source wasn't a Rembrandt, without the forger ever being shown a Picasso before or being told what "not a Rembrandt" looks like. An autoencoder trained only on normal market data is that forger, and reconstruction error is the size of the mistake.
The maths
An autoencoder is two networks glued together: an encoder that compresses input down to a lower-dimensional code (with ), and a decoder that expands back to a reconstruction :
In words: squeeze through a narrow bottleneck of size , then try to rebuild it. Because is small, the network cannot just memorize an identity map — it is forced to learn which patterns in the training data are common enough to be worth encoding efficiently, and it discards the rest as noise it can't afford to keep.
Training minimizes reconstruction error over the normal training set, typically mean squared error:
In words: average, over every input dimension, the squared gap between the original value and the rebuilt value. Small loss means the network's compressed vocabulary was rich enough to capture this input well.
At inference time, for a new point , compute the same reconstruction error and use it directly as an anomaly score:
In words: run through the trained encoder-decoder, measure how far the output strayed from the input, and call that number the anomaly score. Points that look like the training distribution get reconstructed well (low ); points that don't — because the network never learned a compact way to represent whatever makes them unusual — get reconstructed poorly (high ). Flag anything above a chosen threshold .
Worked example 1: computing reconstruction error on two inputs
An autoencoder was trained on ordinary 5-minute return vectors. Feed it a normal input and it reconstructs :
Now feed it an anomalous input, a flash-crash-style vector , and because the network never learned to represent violent one-bar reversals compactly, it reconstructs something close to its normal vocabulary, :
The anomalous score, 16.01, is roughly 14,500 times larger than the normal score, 0.0011. That ratio — not the absolute numbers, which depend on scaling — is what makes reconstruction error such a strong discriminator here: the network's bottleneck simply has no efficient way to encode a 3.5-standard-deviation reversal followed by an equally violent snapback, because it never had to during training.
Worked example 2: setting a threshold from a normal-only sample
Suppose reconstruction errors on 10 held-out normal days come out as: (arbitrary units). Mean and standard deviation:
Sample variance (using ):
A common rule of thumb sets . Any new day scoring above 1.60 gets flagged. This threshold was calibrated using only normal data — no anomaly examples were needed, which is exactly the point: you're setting a bar for "unusually bad reconstruction relative to how normal data usually reconstructs," not a bar tuned against known anomaly shapes.
Autoencoder training loss on normal data should settle to a stable low floor as more normal examples are seen — much like a running average converging on the truth. A model whose reconstruction loss hasn't stabilized yet will produce an unreliable, drifting anomaly score, so check convergence before trusting the threshold.
Reconstruction error works as an anomaly score because the autoencoder's bottleneck is a forced compression, trained exclusively on what "normal" looks like. It doesn't know what an anomaly is — it only knows what normal is, and reports how far anything strays from that. This is unsupervised in the truest sense: no anomaly labels ever enter training.
What this means in practice
This pattern shows up as a first-line detector for broken data feeds, spoofing-like order-book shapes, sensor faults on alternative-data pipelines, and pre-screening candidate regime changes before a human or a more expensive model looks at them. Its big advantage is that it needs no examples of the failure mode you're trying to catch — useful precisely because novel anomalies, by definition, don't look like the last one. Its big limitation is the flip side: if an anomaly happens to be reconstructible by the learned bottleneck (say, it's a smooth, low-dimensional shift the network's compact code space can represent), it will slip through with a low score, undetected.
The trap: contaminating the training set with anomalies. If even a small fraction of "normal" training data actually contains the anomaly pattern you're trying to catch, the autoencoder will dutifully learn to reconstruct that well too, and your detector goes blind to it — the entire method depends on the training distribution being clean. A second, subtler trap: reconstruction error is sensitive to input scale, so a feature with naturally larger magnitude (say, volume versus a return) can dominate the squared-error sum and drown out anomalies that only show up in the smaller-scale features. Standardize every input feature before training, and re-standardize new data with the same fitted parameters, not freshly computed ones.
Related concepts
Practice in interviews
Further reading
- Goodfellow, Bengio & Courville, Deep Learning, ch. 14
- Chandola, Banerjee & Kumar, Anomaly Detection: A Survey (2009)