Data Mining

Chapter 9
Anomaly and Rare-Event Detection

Bachelors Program in Computer Science Engineering
Data Mining   •   Bachelor

Anomaly and Rare-Event Detection

Introduction

Anomaly detection is the process of identifying observations, events, or patterns that differ substantially from expected behaviour. An anomaly may indicate fraud, an equipment fault, a cyberattack, a safety hazard, a data-quality problem, or simply a legitimate but unusual event. The central difficulty is that unusual does not automatically mean erroneous or harmful.

Anomaly detection is closely related to rare-event detection, but the terms are not identical. Anomaly detection focuses on identifying deviations from a model of normality. Rare-event detection focuses on events with very low frequency, often under severe class imbalance. A rare event may be normal—for example, a highly profitable customer transaction—while an anomaly may not be statistically rare in the whole dataset but may be unusual for a particular customer, machine, or time period.

In supervised classification, the model learns from labelled examples of both classes. In anomaly detection, abnormal examples are often scarce, unknown, changing, or expensive to label. Consequently, methods must often learn primarily from normal data and produce a score that supports investigation or automated action.

Learning Objectives

After studying this chapter, students should be able to:

  1. Distinguish point, contextual, and collective anomalies.
  2. Explain statistical, distance-based, density-based, isolation, and one-class methods.
  3. Compute and interpret common anomaly scores.
  4. Select evaluation measures appropriate for highly imbalanced data.
  5. Design an anomaly-detection workflow for practical applications.
  6. Analyse the operational, ethical, security, and governance implications of anomaly alerts.

1. Types and Terminology

Let an observation be represented by a feature vector x in a feature space ℝd, where d is the number of features. An anomaly score is a function s(x) whose large values generally indicate greater abnormality. A decision threshold τ converts a score into an alert:

alert(x) = 1 if s(x) ≥ τ; otherwise 0

The threshold is not a universal mathematical constant. It should reflect the costs of false positives, false negatives, investigation capacity, and safety requirements.

1.1 Point anomalies

A point anomaly is an individual observation that is unusual relative to the rest of the data. A payment of ₹500,000 may be a point anomaly in a dataset where most payments are below ₹5,000.

1.2 Contextual anomalies

A contextual anomaly is unusual only within a particular context. A network traffic volume of 100 MB may be normal for a server during a software update but suspicious at 3 a.m. Contextual detection therefore uses contextual variables such as time, location, user identity, season, or operating mode.

1.3 Collective anomalies

A collective anomaly is a group or sequence of observations that is abnormal collectively even if each observation appears normal individually. A sequence of small login attempts distributed across many accounts may represent a coordinated attack.

1.4 Important terms

  • Normality model: A representation of expected behaviour.
  • Contamination: The approximate proportion of anomalies in data used for training.
  • False positive: A normal event incorrectly flagged.
  • False negative: An anomaly not detected.
  • Alert rate: The proportion of events sent for investigation.
  • Concept drift: Change in the data-generating process over time.
  • Open-set problem: The possibility that future anomalies belong to previously unknown types.

2. Statistical Detection Methods

Statistical methods define normality using probability distributions or robust summaries. An observation may be considered anomalous if it has a low probability under the estimated normal model.

2.1 Univariate standardisation

For a numerical variable x, the z-score is

z = (x − μ) / σ

where μ is the population or estimated mean and σ is the standard deviation, measured in the same units as x. The z-score is dimensionless. Under a normal-distribution assumption, approximately 99.7% of observations lie within three standard deviations of the mean. A common rule flags |z| > 3, but this is a heuristic rather than a universal law.

The rule is unreliable when the data are highly skewed, heavy-tailed, multimodal, or contaminated by outliers. The median and median absolute deviation (MAD) provide more robust alternatives. For observations x1, …, xn,

MAD = median(|xi − median(x)|)

A robust standardized score is often defined as

zrobust = 0.6745 × (x − median(x)) / MAD

The constant 0.6745 makes the score approximately comparable to a normal z-score when the data are normally distributed. The formula requires MAD > 0.

2.2 Multivariate Gaussian detection

For a vector x with mean vector μ and covariance matrix Σ, the squared Mahalanobis distance is

D²(x) = (x − μ)ᵀ Σ⁻¹ (x − μ)

Here, Σ⁻¹ must exist, or a regularised or pseudoinverse estimate must be used. The distance accounts for scale and correlations between features. If the data truly follow a d-dimensional multivariate normal distribution and parameters are known, D² follows a chi-square distribution with d degrees of freedom. In practice, parameters are estimated, so chi-square cut-offs are approximate and should be validated.

A major advantage over independent z-scores is that Mahalanobis distance detects unusual combinations of otherwise individually ordinary values. Its limitations include sensitivity to covariance estimation, non-Gaussian data, high dimensionality, and anomalous training observations.

2.3 Probability and tail modelling

A density model estimates p(x). Low-density points can be ranked as more suspicious. For a univariate normal model,

p(x) = 1/(σ√(2π)) × exp(−(x − μ)²/(2σ²))

The density has units reciprocal to the measurement units, so densities should not be compared across differently scaled variables without appropriate modelling. In multivariate settings, a log-density is generally more numerically stable than multiplying many small probabilities. Statistical models are useful when their assumptions are credible, interpretable, and monitored.

3. Distance-Based Methods

Distance-based methods identify observations that are far from other observations or from a reference set. Before calculating distances, numerical features should normally be scaled; otherwise, a variable with large units can dominate.

3.1 k-nearest-neighbour distance

For an observation xi, let Nk(xi) denote its k nearest neighbours. A simple score is the distance to the kth nearest neighbour:

sk(xi) = d(xi, x_(k))

where d may be Euclidean distance and x_(k) is the kth nearest neighbour. Larger values suggest isolation from nearby observations. Another option is the mean distance to the k nearest neighbours.

For two vectors x and y with d features, Euclidean distance is

d(x, y) = √(∑_(j=1)d (xj − yj)²)

This method is intuitive and non-parametric, but computationally expensive for very large datasets and sensitive to irrelevant features, scaling, and the curse of dimensionality. In high-dimensional spaces, distances may become increasingly similar, reducing discrimination.

3.2 Clustering-based distance

A clustering algorithm can represent normal regions by centroids. The distance from an observation to its nearest centroid is used as an anomaly score. This is effective when normal observations form compact, well-separated groups. It can fail when clusters have different sizes or densities, or when anomalies form a small but legitimate cluster.

4. Density-Based Detection

Distance asks whether a point is far away; density asks whether it lies in a region with few comparable observations. Density-based approaches are often better when normal data contain clusters of unequal density.

4.1 Local Outlier Factor

Local Outlier Factor (LOF) compares the local density of an observation with the densities of its neighbours. Conceptually, if a point lies in a much sparser region than its neighbours, its LOF score is high. A simplified interpretation is:

LOFk(x) ≈ localdensityofneighbours / localdensityofx

A score near 1 suggests comparable density; a score substantially greater than 1 suggests local isolation. Exact LOF uses reachability distances and averaged local reachability densities, so the displayed expression is an interpretation, not the complete algorithm.

The choice of k is important. A small value detects very local deviations, whereas a larger value captures broader structure. LOF is useful for irregular clusters but can be unstable near boundaries and computationally demanding.

4.2 Kernel and neighbourhood density estimation

Kernel density estimation estimates density by placing a smooth kernel around each observation:

p̂h(x) = 1/(n hd) × ∑_(i=1)n K((x − xi)/h)

where n is the sample size, h is a bandwidth, d is dimensionality, and K is a kernel function. The bandwidth controls smoothness: a small bandwidth may overfit, while a large bandwidth may hide meaningful structure. Density estimation becomes difficult as dimensionality increases.

5. Isolation-Based Detection

Isolation Forest detects anomalies by recursively partitioning the feature space using randomly selected features and split values. Anomalies tend to be isolated in fewer splits because they occupy sparse or unusual regions.

For a binary tree, let h(x) be the path length needed to isolate x. The expected path length is normalised using the average path length c(n) of unsuccessful searches in a binary search tree. A commonly used score is

s(x) = 2^(−E[h(x)] / c(n))

where E[h(x)] is the average path length over trees and n is the subsample size. Larger values indicate easier isolation and therefore greater anomaly likelihood. The score is algorithm-specific and should not be interpreted as a probability unless calibrated.

Isolation Forest is efficient, handles nonlinear structure, and does not require distance calculations. However, random partitions may be misleading when anomalies are not isolated, when categorical variables are poorly encoded, or when rare normal subgroups are incorrectly treated as abnormal.

6. One-Class Methods

One-class methods learn a boundary around normal data and classify points outside that boundary as anomalies. They are particularly appropriate when reliable examples of abnormal behaviour are unavailable.

6.1 One-Class SVM

One-Class Support Vector Machine (One-Class SVM) maps data into a feature space and attempts to separate most observations from the origin using a boundary. With kernel function K, one formulation is:

min_(w,ρ,ξ) 1/2 ||w||² + 1/(νn) ∑i ξi − ρ

subject to

wᵀφ(xi) ≥ ρ − ξi, ξi ≥ 0

Here, w defines the separating surface, ρ is an offset, ξi are slack variables, ν is a parameter controlling the allowed fraction of training errors and support vectors, n is the number of training observations, and φ is the feature mapping. The method is sensitive to kernel and parameter choices, scaling, and contamination.

6.2 Support Vector Data Description

Support Vector Data Description constructs a minimum-radius hypersphere, possibly in a transformed feature space, that encloses most normal observations. Its appeal is geometric interpretability, but performance depends on kernel selection and the shape of normal data.

6.3 Autoencoders

Although not a classical one-class method, an autoencoder can be trained primarily on normal observations. It encodes an input into a lower-dimensional representation and reconstructs it. The reconstruction error is

L(x, x̂) = ∑_(j=1)d (xj − x̂j)²

for numerical features, where x̂ is the reconstruction. A high error may indicate abnormality. The method requires careful threshold selection and can learn to reconstruct anomalies if they are present in training or if the network is excessively expressive. It is also less interpretable than many classical techniques.

7. Comparing Methods

MethodMain ideaStrengthsTypical limitations
z-scoreDeviation from meanSimple and explainableDistribution and contamination sensitivity
Mahalanobis distanceCorrelation-aware distanceMultivariate interpretationCovariance instability
k-nearest neighbourDistance to neighboursNon-parametric and intuitiveScaling and dimensionality
LOFRelative local densityDetects local deviationsParameter and boundary sensitivity
Isolation ForestShort random isolation pathsEfficient and nonlinearScore interpretation and subgroup risk
One-Class SVMBoundary around normal dataFlexible nonlinear boundaryKernel and parameter sensitivity
AutoencoderReconstruction errorHandles complex representationsData, tuning, and explainability demands

No method is universally best. A strong design may use an ensemble of scores, but combining models does not remove the need for validation, threshold selection, monitoring, and human review.

8. Evaluation Under Extreme Class Imbalance

Accuracy is often misleading in rare-event detection. If only 0.1% of events are fraudulent, a model that predicts every event as normal has 99.9% accuracy but detects no fraud.

For a binary detector:

  • True positive (TP): anomaly correctly detected.
  • False positive (FP): normal event incorrectly flagged.
  • True negative (TN): normal event correctly accepted.
  • False negative (FN): anomaly missed.

Precision and recall are

Precision = TP/(TP + FP)
Recall = TP/(TP + FN)

Precision measures the reliability of alerts; recall measures the proportion of actual anomalies detected. The F1 score is the harmonic mean:

F1 = 2 × Precision × Recall/(Precision + Recall)

The F1 score is appropriate when precision and recall have comparable importance, but it does not include true negatives or unequal operational costs.

The false-positive rate is

FPR = FP/(FP + TN)

and the true-positive rate equals recall. Receiver operating characteristic (ROC) curves plot recall against FPR across thresholds. With extreme imbalance, precision–recall curves are often more informative because precision directly reflects alert quality.

8.1 Cost-sensitive evaluation

Let CFP be the cost of a false positive, CFN the cost of a false negative, CTP the benefit of a true positive, and CTN the benefit or cost of a true negative. Expected utility can be represented as

U = CTP×TP + CTN×TN − CFP×FP − CFN×FN

The terms must be expressed in comparable units, such as monetary cost, safety risk, or analyst time. A threshold should be selected using the operational objective, not simply the threshold that maximises a generic metric.

8.2 Evaluation design

Random train–test splitting can produce optimistic results when events are time-dependent or repeated by customer, device, or attacker. Better practice includes:

  1. Temporal validation: train on earlier periods and test on later periods.
  2. Group-based splitting: prevent the same entity appearing in both training and test sets when leakage is possible.
  3. Prevalence reporting: state the anomaly rate in the evaluation population.
  4. Threshold reporting: show precision, recall, alert volume, and investigation capacity at the selected threshold.
  5. Delayed-label evaluation: account for the time required to confirm fraud or equipment failure.

9. A Practical Detection Workflow

A production system should be designed as a decision process rather than as an isolated algorithm.

Step 1: Define the event and consequence

Specify what constitutes an event, what “normal” means, the decision time available, and the cost of each error.

Step 2: Construct meaningful features

Features may include transaction amount, time since previous login, rolling failure count, vibration statistics, destination reputation, or deviation from a user’s historical baseline. Leakage must be avoided: a feature must be available at the moment a decision is made.

Step 3: Clean and scale appropriately

Handle missing values, duplicate events, categorical variables, seasonality, and changing units. Robust scaling may be preferable when extreme values are genuine or expected.

Step 4: Establish a baseline

Begin with transparent rules, robust z-scores, or a simple distance method. A complex model should demonstrate improvement over a meaningful baseline.

Step 5: Train and tune

Use normal-only training when abnormal labels are incomplete. Tune k, contamination, kernel parameters, subsample size, or neural architecture using time-aware validation where appropriate.

Step 6: Calibrate and set thresholds

Anomaly scores from different algorithms are not automatically comparable. Thresholds may be set using labelled validation data, a target alert rate, quantiles of normal scores, or an explicit cost function.

Step 7: Add investigation and feedback

An alert should contain evidence: contributing features, peer comparison, recent history, and model version. Analyst decisions should be recorded for retraining and error analysis.

Step 8: Monitor after deployment

Monitor score distributions, alert rates, precision where labels arrive, missingness, latency, subgroup performance, and concept drift. Retraining without monitoring can cause silent failure.

10. Applications

Fraud detection

Fraud systems combine transaction-level, account-level, network-level, and temporal features. An unusual amount may be weak evidence by itself; an unusual amount from a new device, unusual location, and rapid sequence of failed authentication attempts is stronger contextual evidence. Fraud detection requires low latency, privacy protection, adaptive adversaries, and careful management of customer friction.

Cybersecurity

Intrusion detection may identify unusual login times, data transfer volumes, process behaviour, or communication patterns. Collective and contextual anomalies are particularly important. Attackers may deliberately mimic normal traffic, poison training data, or probe thresholds.

Safety monitoring

In industrial or transport systems, an anomaly may require immediate protective action. High recall may be prioritised, but nuisance alarms can cause alarm fatigue. Safety systems should use fail-safe design, redundancy, explainable rules for critical actions, and human procedures for escalation.

Predictive maintenance

Sensor streams from motors, pumps, or turbines can be transformed into rolling means, variances, frequency features, temperature gradients, and load-adjusted residuals. An anomaly may indicate degradation before failure. However, maintenance decisions should distinguish sensor faults, temporary operating changes, and genuine equipment deterioration.

11. Illustrative Case: Payment-Fraud Triage

Background

A digital bank processes online card payments. Confirmed fraud represents 0.15% of transactions. Investigators can review only 2,000 alerts per day, while the system receives 1,000,000 transactions daily.

Problem

A rule-based system has high recall but generates 12,000 daily alerts. Investigators miss important cases, and legitimate customers experience unnecessary declines.

Analysis

The bank constructs features available at authorisation time: amount relative to the customer’s historical median, device novelty, merchant category, country deviation, number of attempts in the preceding hour, and account linkage to other cards. Robust scaling is used for heavy-tailed amounts. A baseline robust-score model is compared with Isolation Forest and a supervised model trained on delayed fraud labels.

The bank uses a time-based split: earlier months are used for training, a later month for validation, and the following month for testing. Performance is reported at the operational limit of 2,000 alerts rather than at an arbitrary probability threshold. The final ranking combines model evidence with hard safety rules for known compromised devices.

Approach and solution

The system uses a two-stage process. A fast screening model ranks all transactions. A second model analyses the highest-risk transactions using richer relational features. Alerts include the score, threshold, top contributing factors, customer history, and links to related accounts. Confirmed investigator outcomes are fed back after a controlled delay.

Outcome

In this illustrative scenario, the redesigned system reduces the alert volume to the investigation capacity while retaining substantially more confirmed fraud than the previous system. The bank also introduces a customer appeal process, monitors precision by region and customer segment, and reviews threshold changes through model governance.

Learning points

  1. The operational alert budget is part of model design.
  2. Time-aware validation is essential when behaviour changes.
  3. Multiple weak contextual signals can be stronger than one extreme value.
  4. A score is not automatically a probability or a decision.
  5. Human review, feedback, and governance are part of the detection system.

12. Challenges, Ethics, and Governance

False alarms and unequal impact

Anomaly systems can disproportionately flag new customers, minority communities, small businesses, or users in regions with sparse historical data. Performance should be assessed across relevant groups, while recognising that fairness metrics may conflict and that protected attributes may be legally restricted.

Privacy and data minimisation

Detection may use location, device identifiers, behavioural histories, or communication metadata. Organisations should collect only necessary data, define retention periods, restrict access, encrypt sensitive data, and document legitimate purposes.

Explainability and contestability

Affected people may need an understandable reason for a decision and a way to appeal. Explanations should distinguish correlation from causation and should not reveal sensitive detection thresholds in ways that enable evasion.

Adversarial adaptation

Attackers learn from system behaviour. Defences include rate limits, feature diversity, delayed confirmation, adversarial testing, model ensembles, and controlled threshold changes. Training data and labels must also be protected from poisoning.

Human responsibility

Automation should not remove accountability. High-impact actions such as account closure, employment consequences, or safety shutdowns require documented authority, audit trails, override procedures, and periodic independent review.

13. Contemporary Developments and Best Practices

Modern systems increasingly combine streaming analytics, graph features, representation learning, and adaptive thresholds. Graph anomaly detection can identify unusual relationships among accounts, devices, merchants, and IP addresses. Self-supervised learning can exploit large volumes of unlabelled normal data. Federated approaches may allow organisations to learn from distributed data without centralising all raw records, although privacy and security risks remain.

Best practices include:

  • define normality in domain terms rather than assuming rarity equals harm;
  • use robust, leakage-free features and time-aware evaluation;
  • compare complex models with transparent baselines;
  • report precision, recall, alert volume, prevalence, and cost;
  • validate performance across time and important subgroups;
  • provide evidence and explanations with each alert;
  • monitor drift, feedback loops, and score distributions;
  • protect data and document model ownership, versioning, and approval;
  • design human investigation and appeal processes before deployment.

Chapter Summary

Anomaly detection identifies observations that deviate from a model of expected behaviour, whereas rare-event detection addresses events with very low frequency. Point, contextual, and collective anomalies require different representations of normality. Statistical methods use distributional assumptions; distance-based methods use separation; density-based methods compare local concentration; Isolation Forest uses short random partition paths; and one-class methods learn a boundary around normal observations. Each method depends on scaling, parameter choices, data quality, and the structure of normal behaviour.

Accuracy is inadequate under severe class imbalance. Precision, recall, F1, false-positive rate, precision–recall curves, alert volume, and cost-sensitive utility provide more useful evidence. Evaluation must prevent temporal and entity leakage and must reflect the operational threshold. Effective deployment includes feature engineering, baselines, threshold governance, analyst feedback, monitoring, and drift management. Applications in fraud, cybersecurity, safety, and predictive maintenance demonstrate that anomaly detection is a sociotechnical decision system, not merely an algorithm. Privacy, fairness, explainability, adversarial adaptation, and accountability are essential to responsible use.

Key Terms

Anomaly: An observation or event that deviates from expected behaviour.

Rare event: An event with very low frequency in a population.

Point anomaly: An individually unusual observation.

Contextual anomaly: An observation that is unusual given its context.

Collective anomaly: A group or sequence that is abnormal collectively.

Anomaly score: A numerical value ranking observations by abnormality.

Threshold: A value used to convert a score into an alert or decision.

Contamination: The estimated proportion of anomalies in training data.

Mahalanobis distance: A covariance-aware multivariate distance measure.

k-nearest-neighbour distance: Distance from an observation to a selected nearby neighbour.

Local Outlier Factor: A local-density comparison score.

Isolation Forest: An ensemble method based on random isolation paths.

One-Class SVM: A boundary-learning method trained primarily on normal data.

Reconstruction error: Difference between an input and its model reconstruction.

Precision: The proportion of alerts that are confirmed anomalies.

Recall: The proportion of anomalies that are detected.

Concept drift: Change in the data-generating process over time.

False positive: A normal event incorrectly flagged as anomalous.

False negative: An anomaly that the system fails to detect.

Alert fatigue: Reduced attention caused by excessive or low-quality alerts.

Review Questions

Short Answer Questions

  1. Distinguish a contextual anomaly from a point anomaly.
  2. Why can accuracy be misleading in rare-event detection?
  3. What does a high Local Outlier Factor score generally indicate?
  4. State two reasons for scaling features before distance-based detection.
  5. What is concept drift, and why does it matter after deployment?

Long Answer Questions

  1. Compare statistical, distance-based, density-based, isolation-based, and one-class anomaly-detection methods.
  2. Explain Mahalanobis distance, including its assumptions, interpretation, and limitations.
  3. Discuss how precision, recall, F1 score, ROC analysis, and precision–recall analysis should be used for imbalanced detection.
  4. Design an end-to-end anomaly-detection workflow for predictive maintenance.
  5. Explain the ethical, privacy, fairness, and governance issues in automated fraud detection.

Analytical / Application Questions

  1. A detector produces TP = 80, FP = 20, and FN = 40. Calculate precision, recall, and F1 score, and interpret the results.
  2. A factory has many normal operating modes with unequal densities. Recommend a detection strategy and justify the roles of local density, contextual features, and threshold selection.
  3. A cybersecurity system’s alert rate doubles after a software upgrade, but confirmed attack labels are delayed by three weeks. Propose an evaluation and monitoring plan that distinguishes genuine threat increase from model or data drift.

Multiple Choice Questions with Answers

Question 1

Which type of anomaly is unusual only when considered together with time, location, or another context?

A. Point anomaly

B. Contextual anomaly

C. Collective anomaly

D. Sampling anomaly

Correct Answer: B. Contextual anomaly

Question 2

In the z-score formula z = (x − μ)/σ, what does σ represent?

A. The median

B. The covariance matrix

C. The standard deviation

D. The anomaly threshold

Correct Answer: C. The standard deviation

Question 3

A high Local Outlier Factor score generally means that an observation is:

A. In a region with density similar to its neighbours

B. In a much sparser region than its neighbours

C. The most frequent observation

D. Guaranteed to be fraudulent

Correct Answer: B. In a much sparser region than its neighbours

Question 4

Which method identifies anomalies using unusually short random partition paths?

A. Isolation Forest

B. k-means regression

C. Naive Bayes

D. Principal component classification

Correct Answer: A. Isolation Forest

Question 5

If TP = 80 and FP = 20, what is precision?

A. 0.20

B. 0.50

C. 0.80

D. 0. เต

Correct Answer: C. 0.80

Question 6

Why is accuracy often unsuitable for a detector where anomalies are extremely rare?

A. It cannot be calculated for binary outcomes

B. It may be very high even when all anomalies are missed

C. It always penalises false positives too strongly

D. It measures only model latency

Correct Answer: B. It may be very high even when all anomalies are missed

Question 7

Which validation design is usually most appropriate for a time-dependent fraud-detection problem?

A. Randomly mix all observations before splitting

B. Train on later data and test on earlier data

C. Train on earlier periods and test on later periods

D. Use only the training-set score

Correct Answer: C. Train on earlier periods and test on later periods

Question 8

What is the main purpose of a threshold in anomaly detection?

A. To convert an anomaly score into an operational decision

B. To guarantee that every anomaly is detected

C. To remove all missing values

D. To estimate the number of features

Correct Answer: A. To convert an anomaly score into an operational decision

Question 9

Which method learns a boundary around primarily normal observations using a kernel-based optimisation framework?

A. One-Class SVM

B. Apriori

C. Ordinary least squares

D. k-means centroid averaging

Correct Answer: A. One-Class SVM

Question 10

Which issue occurs when the data-generating process changes after deployment?

A. Concept drift

B. Dimensionality reduction

C. Data encryption

D. Unit consistency

Correct Answer: A. Concept drift

Data Mining
Chapter 9 of 15