Anomaly Detection
Anomaly detection is the task of identifying data points, patterns, or observations that deviate significantly from expected behavior — often called outliers, novelties, or anomalies. It matters enormously in modern AI because anomalies frequently signal the most actionable events: fraud, system failures, medical emergencies, and security breaches. The counterintuitive fact is that anomaly detection is hardest precisely when it matters most — in high-stakes domains, anomalies are so rare that even a 99%-accurate detector produces mostly false alarms.
The central insight of anomaly detection is deceptively simple: learn what "normal" looks like, then flag anything that doesn't fit. Rather than trying to enumerate every possible type of anomaly (an impossible task), you model the normal region of your data and treat the unknown fringes as suspicious. The "aha moment" is realizing that anomaly detection is fundamentally an unsupervised problem in disguise — even when you have some labels, the rarest class (the anomaly) almost never has enough examples to learn from directly, so the model must lean on its understanding of normal.
How It Works
Anomaly detection algorithms assign each data point an anomaly score — a number representing how unusual the point is. High scores mean "probably anomalous." You then choose a threshold above which you raise an alert. The two most important algorithmic families for classical ML practitioners are statistical/density-based methods and distance-based methods.
Anomaly Score via Distance
The simplest possible score is the distance from a point to its nearest neighbors. If a point is far from everything else, it's suspicious:
Explanation: A point's anomaly score is its average distance to its k nearest neighbors — isolated points score high, clustered points score low.
Symbols:
- — the data point being evaluated
- — the number of nearest neighbors to consider
- — the -th nearest neighbor of
- — a distance function (usually Euclidean)
Isolation Forest Contamination Threshold
Isolation Forest estimates a score for each point based on how few splits it takes to isolate it in a random tree. The key formula normalizes the path length:
Explanation: A score near 1 means the point was very easy to isolate (anomalous); near 0.5 means normal; values well below 0.5 indicate a very typical point.
Symbols:
- — anomaly score for point with training samples
- — average path length to isolate across all trees
- — expected path length for a dataset of size (normalization constant)
Gaussian Density Score
For purely statistical methods, you model normal data as a Gaussian and score points by their log-likelihood:
Explanation: This is the squared Mahalanobis distance — points far from the mean in directions where the data varies little receive the highest anomaly scores.
Symbols:
- — mean vector of the training data
- — covariance matrix of the training data
- — inverse covariance (accounts for feature correlations)
---
Code: Your First Anomaly Detector in 15 Lines
```python import numpy as np from sklearn.ensemble import IsolationForest import matplotlib.pyplot as plt
# Generate normal data with a few injected anomalies rng = np.random.default_rng(42) normal = rng.normal(loc=0, scale=1, size=(200, 2)) anomalies = rng.uniform(low=-6, high=6, size=(10, 2)) X = np.vstack([normal, anomalies])
# Fit Isolation Forest model = IsolationForest(n_estimators=100, contamination=0.05, random_state=42) labels = model.fit_predict(X) # -1 = anomaly, +1 = normal scores = model.decision_function(X) # higher = more normal print(f"Anomalies found: {(labels == -1).sum()}") # → ~10 ``` ```python
# Visualize plt.scatter(X[labels == 1, 0], X[labels == 1, 1], c='steelblue', label='Normal', alpha=0.6) plt.scatter(X[labels == -1, 0], X[labels == -1, 1], c='crimson', marker='x', s=100, label='Anomaly') plt.legend(); plt.title("Isolation Forest Detections"); plt.show() ``` The `contamination` parameter is the estimated fraction of anomalies in your data — one of the most important hyperparameters to tune carefully. ---
Three Classical Algorithms Side-by-Side
| Algorithm | How It Defines "Unusual" | Scales to High Dims? | Key Hyperparameter |
|---|---|---|---|
| Isolation Forest | Easy to isolate in random trees | ✅ Yes | `contamination`, `n_estimators` |
| Local Outlier Factor | Low density vs. neighbors | ⚠️ Moderate | `n_neighbors` |
| One-Class SVM | Outside learned boundary | ❌ Slow | `nu`, kernel type |
| Elliptic Envelope | Far from Gaussian center | ❌ Assumes Gaussian | `contamination` |
```python from sklearn.neighbors import LocalOutlierFactor from sklearn.svm import OneClassSVM from sklearn.covariance import EllipticEnvelope
# All follow the same sklearn API pattern: lof = LocalOutlierFactor(n_neighbors=20, contamination=0.05) labels_lof = lof.fit_predict(X) # -1 = anomaly ocsvm = OneClassSVM(kernel='rbf', nu=0.05) labels_svm = ocsvm.fit_predict(X) # -1 = anomaly ee = EllipticEnvelope(contamination=0.05, random_state=42) labels_ee = ee.fit_predict(X) # -1 = anomaly ```
Fei Tony Liu completed his PhD at Monash University in Australia, where he developed the Isolation Forest algorithm as part of his dissertation research around 2008. His key insight — that anomalies are easier to isolate than normal points, so you could detect them without ever explicitly defining what normal looks like — flipped the conventional density-estimation approach on its head. Surprisingly, the algorithm's core idea came from thinking about binary space partitioning in computer graphics, not statistics.
Intuition
Imagine you run a coffee shop and you've seen thousands of customers. You have a clear picture of "normal" — people order a coffee, maybe a pastry, pay, and leave in 5–15 minutes. Now someone walks in, orders nothing, sits in the corner for 3 hours, pays with exact change in pennies, and leaves through the fire exit. You didn't need a list of "suspicious behaviors" to flag this — you flagged it because it was far from everything you'd ever seen as normal. Anomaly detection works exactly this way: build a map of the normal neighborhood, then raise an alarm for anything that lands in the wilderness outside it. The "isolation" intuition is equally vivid — if you tried to describe that unusual customer to someone using yes/no questions, you'd nail it in two questions ("Did they order anything? No. Did they stay over 2 hours? Yes."). A normal customer would take twenty questions to distinguish from the crowd.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
Isolation Forest · Local Outlier Factor · One-Class SVM · Imbalanced Learning · Autoencoders