Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Where the data rarely go
Imagine scattering a little sand on a table wherever a data point lies. Where the points are crowded, the sand piles up into dunes; where few points fall, the table stays nearly bare. A new point landing on bare table is suspicious. Kernel density estimation (KDE) builds exactly this sand landscape, a smooth estimate of how likely each location is, and density-based detection flags points that sit where the sand is thin. Unlike a single bell curve, the landscape can have several dunes with an empty valley between them.
Normal operation often occupies several distinct regimes, such as idle, climb and cruise, or forms curved shapes. A single mean and covariance then places the empty space between regimes right at its centre and treats it as most normal. A density estimate respects that structure, and its output, a likelihood, combines naturally with thresholds, alarm budgets and Bayesian reasoning.
- 1. KDE-based detection flags points with…
- 2. A single Gaussian model treats the gap between two clusters as…
- 3. The KDE bandwidth controls…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.The estimator by hand
Definition. With a Gaussian kernel of bandwidth h in d dimensions:
f̂(x) = (1/n) Σᵢ (2πh²)^(−d/2) exp(−‖x − xᵢ‖²/2h²).
Each bump integrates to 1, so f̂ integrates to 1.
- One training point at 0, evaluated at 0 with h = 1 (1-D): 1/√(2π) = 0.399.
- Two points at ±1, evaluated at 0: average of two bumps, each e^(−½)/√(2π) = 0.242.
Leave-one-out score. To score a training point, leave it out of its own sum. Otherwise every point gets a bump at its own location and even isolated anomalies look dense when h is small. The score is s(x) = −log f̂₋ᵢ(x).
Bandwidth rules.
- Scott: h = σ̂ · n^(−1/(d + 4)). For n = 1000: factor 0.251 in 1-D, 0.316 in 2-D, 0.611 in 10-D.
- Silverman (1-D): h = 0.9 · min(σ̂, IQR/1.34) · n^(−1/5). For 1000 standard normal values: about 0.226.
- Likelihood cross-validation: choose h to maximise Σᵢ log f̂₋ᵢ(xᵢ).
The steps in plain English:
- Standardise the features, so that one bandwidth suits all of them.
- Choose h by rule of thumb or cross-validation.
- Compute leave-one-out log densities for the training points, and log densities for new points.
- Flag points below the density quantile that matches the alarm budget, for example the lowest 1%.
- 1. Why score training points leave-one-out?
- 2. Scott's factor n^(−1/(d+4)) in high dimensions…
- 3. Likelihood cross-validation picks h that…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Bias, variance and the curse of dimensionality
Bias–variance trade-off. The KDE’s bias grows like h² times the curvature of the true density, because large h blurs peaks and fills valleys. Its variance grows like 1/(n hᵈ), because small h means few points per bump. The mean integrated squared error is minimised at h ∝ n^(−1/(d + 4)), the source of Scott’s rule.
For detection. Too large an h fills the valleys between clusters, so gap anomalies get high density: the KDE reverts to a single-blob detector. Too small an h gives every isolated normal point a deep hole, raising false alarms and making scores noisy.
Curse of dimensionality. The optimal error rate is n^(−4/(d + 4)): about n^(−2/3) in 2-D, but only n^(−2/7) in 10-D. Irrelevant noise dimensions add variation to every distance, washing out the low-density valleys in the relevant ones.
Exercise 1 (two clusters at ±3, with 15 anomalies spread through the gap):
- the KDE integrated to 1.0000;
- Scott’s bandwidth was 0.71 and the cross-validated one 0.50;
- AUC was 0.965 for the KDE against 0.608 for Mahalanobis distance from a single Gaussian.
from sklearn.neighbors import KernelDensity
kde = KernelDensity(kernel="gaussian", bandwidth=0.5).fit(X_train)
log_dens = kde.score_samples(X_new) # flag the lowest log densities- 1. KDE variance grows like…
- 2. Very large h makes KDE detection behave like…
- 3. The optimal KDE error rate in 10-D is about…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Consistency. If h → 0 and n hᵈ → ∞, the KDE converges to the true density at every continuity point. The kernel’s shape matters little (Epanechnikov is optimal in MISE, but only by a few per cent); the bandwidth matters a lot.
Variants for detection.
- Adaptive (variable) bandwidth: wider bumps where data are sparse, which reduces false alarms in the tails.
- Full bandwidth matrices: follow correlated directions.
- Mixtures of Gaussians: a parametric middle ground that is cheaper to evaluate.
- k-NN density: f ∝ k/(n · volume of the k-NN ball), linked to LOF and k-NN distance scores.
Cost. Exact evaluation costs O(n) per query. Tree-based or FFT-binned approximations and random Fourier features scale it up.
Interview questions.
- KDE detection or Isolation Forest? KDE in low dimension (up to roughly 5–10 standardised features) when calibrated likelihoods matter and the data are multimodal. Isolation Forest or LOF in higher dimensions or when n is large.
- How do you set the threshold? From an alarm budget, the quantile of the training log densities matching the acceptable false-alarm rate, or from labelled validation data. Revalidate it as the data drift.
- 1. For KDE consistency, h and n must satisfy…
- 2. The choice of kernel shape matters…
- 3. A variable bandwidth helps by…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Bandwidth and dimension
History.
- M. Rosenblatt (1956) and E. Parzen (1962) introduced kernel density estimation.
- D. Scott (1979, 1992) and B. Silverman (1986) introduced the bandwidth rules.
- KDE-based novelty detection became standard in condition monitoring (Tarassenko et al., 1995).
- Robust KDE (Kim and Scott, 2012) resists contamination of the training data.
Exercise 2. Two clusters at ±3 along the first coordinate, with 15 anomalies in the gap (x₁ ≈ 0), plus d − 1 standard-normal noise coordinates for everyone; 3 datasets. The table gives the AUC of the leave-one-out KDE at multiples of Scott’s bandwidth, and of single-Gaussian Mahalanobis distance.
| d | ×0.25 | ×0.5 | ×1 | ×2 | ×4 | Mahalanobis |
|---|---|---|---|---|---|---|
| 2 | 0.882 | 0.917 | 0.950 | 0.927 | 0.186 | 0.211 |
| 5 | 0.831 | 0.840 | 0.872 | 0.816 | 0.356 | 0.422 |
| 10 | 0.686 | 0.690 | 0.720 | 0.679 | 0.352 | 0.454 |
| 20 | 0.654 | 0.656 | 0.663 | 0.589 | 0.309 | 0.430 |
- Scott’s rule was the best of the five bandwidths in every dimension, although the cross-validated bandwidth in exercise 1 was smaller.
- At 4 × Scott, the KDE collapsed below chance (0.19 in 2-D), like the single Gaussian: the valley filled in, and the gap became the densest region.
- Irrelevant dimensions eroded the KDE’s advantage steadily: 0.95 in 2-D, 0.87 in 5-D, 0.72 in 10-D, 0.66 in 20-D, although the signal was the same throughout. Feature selection or dimension reduction before density estimation matters more than bandwidth fine-tuning.
- Single-Gaussian detection stayed below chance everywhere: for multimodal normal data it is the wrong model.
Density-based detection in AI, robotics and aerospace
- Novelty detection in condition monitoring: KDE of vibration features learned on healthy engines, flagging low-likelihood new runs (the classic jet-engine novelty-detection work).
- Regime-aware telemetry checks: multimodal operating envelopes where the gaps between regimes are themselves abnormal.
- Out-of-distribution detection: density of learned embeddings, with the caveat that high-dimensional density estimates need dimension reduction first.
References
- E. Parzen (1962). “On estimation of a probability density function and mode.” Annals of Mathematical Statistics 33(3).
- B. W. Silverman (1986). Density Estimation for Statistics and Data Analysis. Chapman and Hall.
- D. W. Scott (2015). Multivariate Density Estimation, 2nd ed. Wiley.
- L. Tarassenko, P. Hayton, N. Cerneaz, M. Brady (1995). “Novelty detection for the identification of masses in mammograms.” Proc. IEE Conference on Artificial Neural Networks.
- J. Kim, C. D. Scott (2012). “Robust kernel density estimation.” Journal of Machine Learning Research 13.