Anomaly Detection and Imbalanced Learning
Anomaly and Novelty Detection · 14 min
Chandola, Banerjee and Kumar's widely cited survey defines anomaly detection as the problem of finding patterns in data that do not conform to expected normal behavior. The problem is commonly organized by how much supervision is available: in the supervised setting, labeled examples of both normal and anomalous instances exist for training, letting the problem be approached with ordinary classification techniques provided enough anomalous examples exist; in the semi-supervised or novelty-detection setting, only normal examples are available during training, and the system must learn a profile of normal behavior and flag deviations from it at test time without ever having seen an anomaly; and in the fully unsupervised setting, no labels are available at all, and the method must rely on the working assumption that anomalies are comparatively rare and structurally different from the bulk of the data, an assumption the survey notes does not always hold and can itself be a source of missed detections.
The survey's taxonomy distinguishes three canonical types of anomalies. A point anomaly is a single data instance that is anomalous with respect to the rest of the data, independent of any surrounding context, such as an unusually large transaction amount. A contextual anomaly is an instance that is anomalous only within a specific context even though the same value might be entirely normal elsewhere, such as a temperature reading that is unremarkable in summer but anomalous in winter. A collective anomaly is a group of related instances that is anomalous relative to the entire dataset as a collection, even though no individual instance within that group is necessarily anomalous in isolation, such as a sustained sequence of small transactions that together indicate fraud even though each one alone looks unremarkable.
Isolation Forest, introduced by Liu, Ting and Zhou, takes a contrasting algorithmic strategy to many earlier anomaly detectors. Rather than first building a dense profile of what normal behavior looks like and measuring each point's deviation from it, Isolation Forest directly isolates anomalies by building an ensemble of randomly generated partitioning trees, each formed by repeatedly selecting a random attribute and a random split value. The method exploits a simple structural property: because anomalies are, by assumption, few and different from the rest of the data, they tend to be separated into their own partition after only a small number of random splits, producing a short average path length across the tree ensemble, while normal points typically require substantially more splits to be isolated. This isolation-based strategy achieves linear time complexity and low memory requirements because a small random sub-sample of the data suffices, and it was shown to outperform several distance-based and density-based methods, particularly on large datasets.
Class Imbalance: Resampling and SMOTE · 15 min
Class imbalance arises when one class vastly outnumbers another in the training data, a common situation in fraud detection, rare disease diagnosis, and industrial fault detection, where the events that matter most are, by their nature, rare. Because a classifier trained to maximize overall accuracy can achieve a very high score by simply predicting the majority class every time, plain accuracy becomes a badly misleading evaluation metric under severe imbalance, an effect often called the accuracy paradox. This motivates evaluating imbalanced-classification systems with metrics that specifically track performance on the minority class, such as precision, recall, and precision-recall curves, which tend to be far more informative under severe imbalance than accuracy or even plain ROC-AUC, since a large number of true negatives from the abundant majority class can make a receiver operating characteristic curve look deceptively strong even while the classifier performs poorly on the minority class that motivated building the system in the first place.
Chawla and colleagues introduced SMOTE, the Synthetic Minority Over-sampling Technique, to address imbalance directly at the data level. Rather than simply duplicating existing minority-class examples, which risks overfitting to exact repeated points, or discarding majority-class examples, which risks losing potentially useful information, SMOTE generates new synthetic minority-class examples by interpolating between an existing minority example and one of its nearest minority-class neighbors: a new synthetic point is placed some fraction of the distance along the line segment connecting the two. The original paper combined this synthetic over-sampling of the minority class with under-sampling of the majority class and demonstrated improved performance, evaluated in ROC space, across the C4.5 decision tree, Ripper rule learner, and Naive Bayes classifiers, outperforming simple adjustment of loss ratios or class priors alone.
SMOTE's straightforward linear interpolation is not without trade-offs: if the minority class is noisy or not well clustered in feature space, interpolating between two nearby minority points can place synthetic examples in regions that actually overlap with the majority class, potentially making the classification problem harder rather than easier; this motivates later boundary-aware variants that are more selective about which neighbor pairs to interpolate between, favoring pairs that sit safely within minority-class territory. It is also worth emphasizing that resampling operates at the data level and is only one lever available for handling imbalance; algorithm-level strategies that instead adjust the classifier's loss function or decision threshold directly, the subject of the next lesson, are a complementary approach that does not require altering the training data at all, and the two can be combined in practice.
Cost-Sensitive Learning and Threshold Selection · 15 min
Elkan's analysis of the foundations of cost-sensitive learning starts from an observation with wide practical consequence: in many of the domains where anomaly detection and class imbalance matter most, such as missing an actual fraud case, an undiagnosed illness, or an impending equipment failure, the cost of a false negative is far higher than the cost of a false positive, a false alarm. A classifier trained to minimize plain, unweighted error rate implicitly treats these two kinds of mistakes as equally costly, an assumption that is rarely accurate in exactly the settings where rare, high-stakes events are the object of interest, and one that resampling techniques like SMOTE do not directly address, since they change the training data's class balance rather than the relative costs the classifier is ultimately being judged against. This gap between what the training objective optimizes and what actually matters operationally is precisely what cost-sensitive learning is designed to close.
Elkan formalizes this by working directly with a cost matrix specifying the cost of each of the four possible outcomes, correctly or incorrectly predicting each class, and derives how the optimal decision threshold for a probabilistic classifier should be set given these costs. Rather than the conventional default of classifying an example as positive whenever its predicted probability exceeds 0.5, the analysis shows that the optimal threshold is a function of the relative costs, and that as the cost of missing a positive case grows relative to the cost of a false alarm, the optimal threshold moves below 0.5, making the classifier more willing to flag examples as positive at the expense of accepting more false alarms. This threshold adjustment can be applied to any well-calibrated probabilistic classifier after training, without needing to retrain the underlying model.
Elkan's analysis also clarifies the relationship between resampling and cost-sensitive learning: rebalancing the training set is, at best, an approximate proxy for directly reweighting misclassification costs, since it shifts the effective decision threshold for some model families, such as Naive Bayes and decision trees, but has comparatively little effect on others, meaning resampling alone does not reliably substitute for explicitly incorporating costs into training or decision-making. Where the underlying costs can be estimated with reasonable confidence, adjusting the decision threshold on a well-calibrated classifier is often the more principled and directly interpretable choice. Taken together, the module's three lenses, characterizing what makes an event anomalous, correcting for severe class imbalance during training, and making cost-aware decisions at prediction time, are complementary facets of the same underlying challenge: building systems that reliably act on rare, high-stakes events rather than optimizing a metric that rewards ignoring them.
The Accuracy Paradox on an Imbalanced Dataset
Out of 1,000 transactions, 950 are legitimate and 50 are fraudulent. A classifier that always predicts 'legitimate' gets all 950 negatives right and misses all 50 positives, for an accuracy of 950/1000 = 95%, yet its recall on the class that actually matters is 0/50 = 0%, showing why accuracy alone is a poor metric under severe class imbalance.
- Anomaly detection methods split along a key axis: profile-based methods characterize what 'normal' looks like and flag deviations from it, while isolation-based methods like Isolation Forest instead directly exploit that anomalies are easy to isolate from the rest of the data.
- Under severe class imbalance, accuracy is close to useless as an evaluation metric because a classifier can score extremely high by simply predicting the majority class every time; precision, recall, and cost-weighted metrics are needed to see whether the minority class is actually being detected.
- Resampling techniques like SMOTE and algorithm-level cost-sensitive learning attack the same underlying problem from different angles — one reshapes the training data, the other reshapes the decision threshold or loss function — and Elkan's analysis shows they are not always interchangeable in their effect on a given model family.
Recall Practice
Glossary
- Point anomaly
- A single data instance that is anomalous with respect to the rest of the data, independent of any surrounding context.
- Contextual anomaly
- A data instance that is anomalous only within a specific context (such as a particular time or location) even though the same value might be normal in a different context.
- Collective anomaly
- A group of related data instances that is anomalous relative to the entire dataset as a collection, even when the individual instances are not anomalous on their own.
- Isolation Forest
- An ensemble anomaly-detection algorithm that isolates instances via random recursive partitioning and scores them by how few splits (how short a path) were needed to isolate them, exploiting that anomalies are 'few and different.'
- SMOTE (Synthetic Minority Over-sampling Technique)
- A resampling method that combats class imbalance by generating new synthetic minority-class examples through interpolation between existing minority examples and their nearest minority-class neighbors.
- Cost-sensitive learning
- An approach to classification that explicitly incorporates the (possibly asymmetric) real-world costs of different error types, such as false negatives versus false positives, into training or decision-making, rather than treating all misclassifications as equally bad.
Generate a Synthetic Minority Example and Diagnose the Accuracy Paradox, by Hand
A fully paper-and-pencil, simulated two-part exercise. Part 1 (SMOTE by hand): given minority-class point A = (2,3) and its nearest minority-class neighbor B = (6,7), compute a synthetic point using interpolation fraction 0.5: new point = A + 0.5 times (B - A) = (2,3) + 0.5 times (4,4) = (2,3) + (2,2) = (4,5). Part 2 (accuracy paradox by hand): given a dataset of 1,000 transactions, 950 legitimate and 50 fraudulent, hand-fill the confusion matrix for a trivial classifier that always predicts 'legitimate' (true negatives = 950, false positives = 0, false negatives = 50, true positives = 0), then compute its accuracy (950/1000 = 95%) and recall (0/50 = 0%) to see how a seemingly strong accuracy score can hide complete failure on the class that actually matters. No real SMOTE library, dataset, or classifier is executed; every number is derived directly from the figures given.
Ready to test yourself?
5 questions on this module.