EasyDeepLearn
Unsupervised Learning · section 6 of 9

Anomaly & outlier detection

26 interview questions on anomaly & outlier detection, each answered in full. Free to read, no account needed.

What are the main approaches to anomaly detection?

medium
  • (1) Statistical: fit a distribution and flag low-density points (z-score, Gaussian mixtures).
  • (2) Distance/density-based: LOF, DBSCAN.
  • (3) Isolation-based: Isolation Forest — random splits, anomalies are isolated with few splits.
  • (4) One-class SVM.
  • (5) Reconstruction-based: autoencoders — high reconstruction error means anomaly.
  • Choose based on data size, dimensionality, and whether you have any labels.
#anomaly-detectionPermalink & quiz →

Isolation Forest — how does it detect anomalies?

medium
  • Build many random trees splitting on random features and thresholds.
  • Anomalies are 'isolated' with fewer splits than inliers (shorter path length).
  • Score s(x, n) = 2^(-E[h(x)] / c(n)) where E[h] = average path length, c(n) = normalization.
  • Fast O(n log n), works well in high dims, no density estimation.
  • Standard first choice for tabular anomaly detection.
#anomaly-detectionPermalink & quiz →

One-class SVM — mechanism and pitfalls.

hard
  • Learns a decision boundary enclosing 'normal' data by maximizing margin from origin in RKHS (with RBF kernel typically).
  • Parameter ν upper-bounds fraction of outliers.
  • Sensitive to hyperparameters (ν, γ).
  • Doesn't scale beyond ~10k.
  • Modern alternative: Isolation Forest (faster, more robust).
  • Still useful when a semantic 'normal' class is well-defined and features are dense.
#anomaly-detectionPermalink & quiz →

Autoencoder for anomaly detection — how?

medium
  • Train AE on 'normal' data — it learns to reconstruct normal patterns well.
  • At inference, compute reconstruction error ||x - x̂||²; high error = anomaly.
  • Works well when normal data has low-dimensional manifold structure (images, time series).
  • Pitfalls: (1) if AE is too powerful, it reconstructs anomalies too — use small bottleneck; (2) contamination in training data → semi-supervised or filter first.
#anomaly-detection#deep-learningPermalink & quiz →

VAE-based anomaly detection — advantages.

hard
  • VAE learns probabilistic latent space with p(x).
  • Anomaly score: (1) reconstruction error, (2) negative ELBO, (3) KL between q(zx)q(z \mid x) and prior.
  • Advantages over AE: probabilistic, calibrated uncertainty, generative — can synthesize plausible normals.
  • Weakness: still needs threshold tuning; failure mode: 'likelihood > uniform' issue (in-distribution mismatch on OOD data — Nalisnick et al. 2019).
#anomaly-detection#deep-learningPermalink & quiz →

Deep SVDD — the core idea.

hard
  • Deep Support Vector Data Description (Ruff et al. 2018): train a neural encoder φ_θ to map all normal training data into a small hypersphere in feature space (center c, minimum radius).
  • Anomaly score: distance ||φ_θ(x) - c||.
  • End-to-end representation-plus-boundary learning.
  • Modern default for deep anomaly detection on images / time series.
#anomaly-detection#deep-learningPermalink & quiz →

Semi-supervised vs unsupervised anomaly detection.

medium
  • Unsupervised: no labels; assumes normals dominate.
  • Semi-supervised: only normal data labeled — train on clean normals, detect deviations (one-class SVM, deep SVDD, AE).
  • Supervised: rare — a few labeled anomalies (PU learning, imbalanced classification).
  • Rule: whenever you have even 20-50 confirmed anomaly examples, do semi-supervised evaluation or PU learning — it's a huge lift over pure unsupervised.
#anomaly-detectionPermalink & quiz →

Positive-Unlabeled (PU) learning — when useful?

hard
  • You have some confirmed positives (frauds, anomalies) and a large unlabeled pool that mixes positives + negatives.
  • Standard classification is biased.
  • PU methods: (1) treat unlabeled as noisy negatives and correct via propensity, (2) two-step: identify reliable negatives then train classifier, (3) unbiased PU risk estimator (Kiryo et al. 2017).
  • Uses: fraud (few labeled frauds, most 'clean' actually clean), rare-disease diagnosis.
#anomaly-detectionPermalink & quiz →

Anomaly detection in time series — what changes?

hard
  • Temporal context matters.
  • Options: (1) statistical: rolling mean/std + z-score, ARIMA residuals, STL decomposition + IQR on residuals.
  • (2) Prophet / SARIMA residual monitoring.
  • (3) LSTM-AE / Transformer-AE — reconstruction error on windows.
  • (4) Anomaly Transformer / TimesNet.
  • (5) Change-point detection (Bayesian, CUSUM).
  • Consider: point anomalies, contextual anomalies, collective anomalies.
  • Pipe with alerting + human review.
#anomaly-detection#applicationsPermalink & quiz →

How is drift detection an unsupervised problem?

medium
  • Compare current feature distributions to training / baseline distributions without labels.
  • Methods: (1) KS test per feature (univariate).
  • (2) Population Stability Index (PSI).
  • (3) Multivariate: MMD (Maximum Mean Discrepancy), classifier drift test (train binary classifier reference vs current — AUC > 0.7 = drift).
  • (4) Wasserstein / Jensen-Shannon divergence.
  • Standard in production ML monitoring — Datadog, Arize, WhyLabs, Fiddler all implement variants.
#anomaly-detection#applicationsPermalink & quiz →

Maximum Mean Discrepancy (MMD) — what is it?

hard
  • Distance between two distributions in RKHS: MMD(P, Q) = ||μP    μQ{\mu}_{P}\; - \;{\mu}_{Q}||_H where μP{\mu}_{P} is the mean embedding.
  • Estimated from samples using kernel k (Gaussian typical): MMD2  =  E[k(x,  x)]  +  E[k(y,  y)]    2\mathrm{MMD}^{2}\; = \;E[k(x, \;x)]\; + \;E[k(y, \;y)]\; - \;2 E[k(x, y)].
  • Uses: two-sample tests (do P and Q differ?), drift detection, generative model evaluation.
  • More powerful than univariate tests in multivariate settings.
#anomaly-detection#theoryPermalink & quiz →

Wasserstein distance — intuition.

hard
  • 'Earth mover's distance': minimum cost of transporting mass from P to Q where cost = distance × mass.
  • W1(P,  Q)  =  infW_{1}(P, \;Q)\; = \;\operatorname{inf}_γ E_γ[x    y][ \mid \mid x\; - \;y \mid \mid ] over couplings γ.
  • Metrically meaningful even for disjoint distributions (unlike KL, which is infinite).
  • Uses: WGAN training stability, drift detection, optimal transport for domain adaptation, distribution comparison in imaging.
#anomaly-detection#theoryPermalink & quiz →

Covariate drift vs concept drift vs label drift — the differences.

medium
  • Covariate drift: P(X) changes, P(Y    X)P(Y\; \mid \;X) same — model still valid, just used on different inputs (retrain if severe).
  • Concept drift: P(Y    X)P(Y\; \mid \;X) changes — model relationship broken, must retrain.
  • Label drift: P(Y) shifts (imbalance changes) — matters most for calibration.
  • Rule: monitor all three.
  • Concept drift is the hardest to detect without labels (needs delayed ground truth) → proxy via prediction confidence drift.
#anomaly-detection#applicationsPermalink & quiz →

Out-of-distribution (OOD) detection — approaches.

hard
  • Softmax confidence is unreliable (over-confident on OOD).
  • Better: (1) MSP with temperature scaling, (2) energy score (LeCun et al.), (3) ODIN (temperature + input perturbation), (4) Mahalanobis distance in feature space, (5) deep generative likelihood (with Nalisnick's caveat), (6) contrastive OOD detectors.
  • Modern default: energy score or Mahalanobis on penultimate features.
  • Critical in safety-critical deployment (medical, autonomous).
#anomaly-detection#deep-learningPermalink & quiz →

Conformal prediction for anomaly detection.

hard
  • Given calibration set of normal points, use non-conformity score to produce p-values with guaranteed (1-α) coverage on inliers under exchangeability. p-value < α → outlier at level α.
  • Distribution-free, model-agnostic → wraps any anomaly detector for calibrated alarms.
  • Standard in modern high-assurance production monitoring; foundational in the CP framework (Vovk, Shafer).
#anomaly-detection#theoryPermalink & quiz →

Scan statistics — when do you use them?

hard
  • Detect unusual clusters in space-time (disease outbreaks, crime hotspots, network intrusion).
  • Slide a window over spatial / temporal regions, compare observed vs expected counts (usually Poisson).
  • Kulldorff's spatial scan is standard.
  • Uses: public health surveillance (BioSense, Prodrome), retail hotspot detection.
  • Corrects for multiple testing across regions via Monte Carlo simulation of the null.
#anomaly-detection#applicationsPermalink & quiz →

CUSUM change-point detection — how does it work?

hard
  • Cumulative sum of deviations from a reference mean: St  =  max(0,  St1  +  (xt    μ)    k)S_{t}\; = \;\operatorname{max}(0, \;S_{t - 1}\; + \;(x_{t}\; - \;{\mu})\; - \;k).
  • Alarm when St  >  thresholdS_{t}\; > \;\mathrm{threshold} h.
  • Quickly detects small persistent shifts.
  • Bidirectional variant.
  • Tune k, h to control false-alarm rate + detection latency.
  • Standard in manufacturing SPC, network monitoring, sensor drift.
  • Bayesian alternatives (BOCPD) provide posterior over change points.
#anomaly-detection#applicationsPermalink & quiz →

How do you handle 99.9% normal / 0.1% anomaly training data?

hard
  • (1) Semi-supervised: train only on normal (assume clean).
  • (2) Isolation Forest / LOF: designed for exactly this ratio.
  • (3) If you have SOME labeled anomalies (>50), oversample them + SMOTE + supervised (imbalanced XGBoost).
  • (4) PU learning if unlabeled is 'mostly normal'.
  • (5) Cost-sensitive: assign high FN cost.
  • Avoid: naive balanced accuracy, single-threshold optimization — use PR AUC.
#anomaly-detection#applicationsPermalink & quiz →

How do you explain why a point is anomalous?

medium
  • (1) Per-feature contribution: which feature's z-score / reconstruction error / SHAP value drove the anomaly.
  • (2) Nearest-normals: 'this looks unusual because normally X, Y differ by …'.
  • (3) Rule-based post-hoc: fit decision tree to Isolation Forest scores.
  • (4) Counterfactual: what minimal change makes it normal?
  • Standard in production ops so on-call engineers can validate alerts.
#anomaly-detection#applicationsPermalink & quiz →

Interview: 'design an unsupervised fraud detection system'.

hard
  • (1) Feature engineering: transaction amount, velocity, device / IP fingerprints, merchant risk, time-of-day, historical account patterns.
  • (2) Stack detectors: (a) Isolation Forest on tabular features, (b) autoencoder on account behavior sequences, (c) graph-based (transaction network) for money-laundering rings.
  • (3) Ensemble scores.
  • (4) Human-in-the-loop: score → analyst → labeled anomalies → semi-supervised uplift.
  • (5) Monitor drift + retrain weekly.
  • (6) Guardrails: false-positive rate SLO.
#anomaly-detection#interview#applicationsPermalink & quiz →

How would you detect anomalies in multi-modal data (image + tabular)?

hard
  • (1) Fuse: (a) tabular through MLP encoder, (b) image through CNN encoder → concat embeddings → joint autoencoder or joint density model.
  • (2) Score per modality then combine (max or weighted sum).
  • (3) Cross-modal consistency: does the image match tabular metadata?
  • (Contrastive score.) Standard in medical (image + EHR), manufacturing (sensor + camera), autonomous vehicles (camera + LiDAR).
#anomaly-detection#applicationsPermalink & quiz →

How would you detect fraud rings (colluding accounts)?

hard
  • (1) Build interaction graph: accounts as nodes, shared devices/IPs/transactions as edges.
  • (2) Community detection (Louvain, Leiden, HDBSCAN on node embeddings).
  • (3) Node2Vec / GraphSAGE embeddings → cluster.
  • (4) Anomalous dense subgraphs / motifs.
  • (5) Combine with per-account anomaly scores.
  • Standard in banking / crypto / marketplace fraud (Uber, Airbnb).
  • GNN-based methods (GraphSAGE + supervised head where labels exist) common.
#applications#anomaly-detectionPermalink & quiz →

Interview: production fraud detector — daily volume 10M, current FN too high.

hard
  • (1) Investigate: what patterns are FNs?
  • Feature gaps?
  • Concept drift?
  • (2) Enrich features: velocity ratios, device / IP fingerprints, graph features.
  • (3) Ensemble models: existing rules + Isolation Forest + supervised XGBoost on labeled + AE on sequences.
  • (4) Add HITL: analyst-labeled cases feed back weekly.
  • (5) Score threshold tuning by cost-sensitive PR curve.
  • (6) Monitor precision + recall + latency + drift dashboards.
  • (7) Guardrail: never lower FP-rate SLA.
  • (8) Iterate.
#interview#anomaly-detection#applicationsPermalink & quiz →

Interview: model trained in region A must now serve region B — approach?

hard
  • (1) Diagnose: KS + PSI feature-by-feature; MMD/classifier-drift for multivariate.
  • (2) If severe drift, retrain on B data (labeled ideally).
  • (3) Domain adaptation: (a) importance weighting via density ratio, (b) adversarial (DANN), (c) CORAL alignment of covariances, (d) fine-tune on small B-labeled set.
  • (4) Semi-supervised: use B unlabeled + A labeled.
  • (5) Test-time adaptation (BN statistics update on B).
  • (6) Monitor performance in B before full rollout.
#interview#applications#anomaly-detectionPermalink & quiz →

Interview: 'design anomaly detection for a factory sensor with 200 signals.'

hard
  • (1) EDA: distribution + autocorrelation per signal; note skew / seasonality.
  • (2) Feature engineering: rolling stats + FFT / wavelet features + inter-sensor correlations.
  • (3) Ensemble: (a) univariate rolling z-score / STL, (b) Isolation Forest on features, (c) multivariate autoencoder on signal windows.
  • (4) Score → alerts with severity levels.
  • (5) HITL: operator confirms anomalies → label + retrain.
  • (6) Explainability: feature contribution per alert.
  • (7) Monitor: false alert rate + MTTR.
#interview#anomaly-detection#applicationsPermalink & quiz →

Isolation forest or autoencoder for anomaly detection?

medium
  • Isolation forest for tabular data, as a first attempt in nearly every case: it is fast, needs almost no tuning, handles mixed scales tolerably, and its notion of an anomaly as a point that is easy to isolate is easy to explain to a stakeholder.
  • Autoencoders earn their complexity when anomalies are defined by structure a tree cannot see, such as images, spectrograms, or long sequences where the relationship between dimensions is the signal.
  • They need enough clean normal data, careful capacity control since an over-large autoencoder reconstructs anomalies too, and real tuning effort.
  • The honest default is to run isolation forest, measure it against reviewed alerts, and only reach for reconstruction error when the data is high-dimensional and structured.
#anomaly-detection#representation-learningPermalink & quiz →

Practise Unsupervised Learning