Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Odd compared with the neighbours
A house 200 m from its nearest neighbour is unremarkable in the countryside but very odd in a city centre. LOF judges every point the same way. It compares how crowded a point’s surroundings are with how crowded its neighbours’ surroundings are. A point in a sparse cluster is fine, because its neighbours are equally spread out. A point just outside a dense cluster stands out, because its neighbours are packed tightly while it is not.
Operational data mixes regimes of very different density: cruise is dense, transients are sparse, ground tests are rare. A single global distance threshold either floods the sparse regimes with alarms or misses subtle faults in the dense ones. LOF adapts to local density, which is why it remains a standard baseline in benchmarks and toolkits.
- 1. A point inside a sparse but uniform cluster has LOF about…
- 2. LOF compares a point's density with…
- 3. A tight group of 10 anomalies can fool LOF when k is…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.k-distance, reachability and the ratio by hand
Take the 1-D points 0, 1, 3, 7 with k = 2.
k-distance. The distance to the k-th nearest neighbour: point 0 → 3 (neighbours 1 and 3), point 1 → 2, point 3 → 3, point 7 → 6.
Reachability distance. reach-dist_k(p, o) = max(k-dist(o), d(p, o)). Distances to points inside o’s own neighbourhood are rounded up to k-dist(o), which smooths out random fluctuations in very close pairs.
Local reachability density. lrd_k(p) = 1 / mean over the k neighbours o of reach-dist_k(p, o).
- Point 0: reach-dist to 1 is max(2, 1) = 2, and to 3 is max(3, 3) = 3. The mean is 2.5, so lrd = 0.4.
- Point 7: reach-dist to 3 is max(3, 4) = 4, and to 1 is max(2, 6) = 6. The mean is 5, so lrd = 0.2.
LOF. LOF_k(p) = mean of lrd over p’s neighbours, divided by lrd(p). For point 7, the neighbours’ lrd are 0.4 and 1/3, so LOF = 0.367/0.2 = 1.83. The other three points score 0.92–1.2.
Reading the score. LOF ≈ 1 means p is as dense as its neighbours; LOF clearly above 1 means sparser than its neighbours. There is no universal threshold: values of 1.5–2 are often notable, but it depends on the data.
The steps in plain English:
- Standardise the features, choose k (10–50 is typical), and find each point’s k nearest neighbours, using a KD-tree or ball tree for large n.
- Compute k-distances, reachability distances and lrd.
- Compute LOF as the neighbour-to-own lrd ratio.
- Rank by LOF, or take the maximum LOF over a range of k, and review the top of the list.
- 1. reach-dist_k(p, o) equals…
- 2. lrd is…
- 3. LOF well above 1 means the point is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Local versus global, and the role of k
Why local beats global. A global k-NN distance score ranks every point in a sparse cluster above every point just outside a dense cluster, because absolute distances are larger in the sparse cluster. LOF normalises by the neighbours’ density, so the sparse cluster’s interior scores about 1.
Exercise 1:
- inside a uniform square, the median LOF was 1.000;
- with a dense cluster (s.d. 0.3), a sparse cluster (s.d. 2) and 5 isolated points 1.5 from the dense centre, LOF reached AUC 0.998 against 0.847 for the 10-NN distance;
- the local outliers scored a mean LOF of 3.63, against a median of 1.02 for sparse-cluster points.
Choosing k. k plays two roles. It is the neighbourhood size for density estimation: too small gives noisy lrd values. It is also the smallest group size that LOF can see as a cluster: any group of fewer than about k points is assessed against the surrounding data. Breunig et al. suggest k ≥ 10, at least the size of the smallest group that should count as a cluster, and taking each point’s maximum LOF over a range of k.
Novelty detection. Fit the neighbourhoods on clean training data and score new points against them (scikit-learn’s novelty=True). This is unlike outlier detection, which scores the training points themselves.
from sklearn.neighbors import LocalOutlierFactor
lof = LocalOutlierFactor(n_neighbors=20).fit(X); scores = -lof.negative_outlier_factor_
lof_nov = LocalOutlierFactor(n_neighbors=20, novelty=True).fit(X_clean); new_scores = -lof_nov.score_samples(X_new)- 1. A global k-NN distance score over-flags…
- 2. LOF cannot see a group as a cluster if the group has fewer than about…
- 3. Breunig et al.'s heuristic for k is to…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Bounds. For a point p deep inside a cluster, LOF(p) ≈ 1; Breunig et al. prove that the deviation from 1 is bounded by the ratio of the largest to smallest reachability distances within the neighbourhood. For a point outside a cluster, LOF is bounded between the ratio of its own reachability distances to its neighbours’ reachability distances, using the minimum and maximum. These bounds tighten when the neighbourhoods are homogeneous.
Why reachability distance? It caps how much a very close neighbour can inflate density. Without it, two points that happen to sit almost on top of each other would get enormous lrd and distort the ratios of their neighbours.
Family. LOF is the basis for many variants:
- LoOP turns it into a probability;
- COF uses chaining distances for line-like clusters;
- INFLO uses reverse neighbours;
- LDOF and k-NN-distance scores are simpler relatives.
In benchmarks (for example, Campos et al., 2016), simple k-NN distance and LOF often perform comparably on average, with LOF ahead when densities vary.
Interview questions.
- LOF or Isolation Forest? LOF for local anomalies in data with clusters of varying density, in moderate dimension and n. Isolation Forest for large n, high dimension and global anomalies. Both are cheap to try; compare them on labelled samples if you can.
- How should LOF be used in high dimensions? Distances concentrate, so reduce dimension first (PCA) and standardise. The k-NN search also becomes expensive and approximate.
- 1. Reachability distance prevents…
- 2. LoOP extends LOF by…
- 3. In high dimensions, before LOF you should…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.What k can and cannot see
History.
- M. Breunig, H.-P. Kriegel, R. Ng and J. Sander (2000) introduced LOF.
- J. Tang et al. (2002) introduced COF, H.-P. Kriegel et al. (2009) LoOP, and W. Jin et al. (2006) INFLO.
- G. Campos et al. (2016) benchmarked LOF and its relatives across many datasets.
Exercise 2. A dense cluster (300 points, s.d. 0.3) and a sparse cluster (300 points, s.d. 2) with either 10 scattered local outliers 1.5 from the dense centre, or a micro-cluster of 12 anomalies (s.d. 0.05) between the clusters; 3 datasets. The table gives AUC as LOF / k-NN distance, with the same k for both.
| k | scattered local outliers | micro-cluster of 12 |
|---|---|---|
| 3 | 0.843 / 0.904 | 0.575 / 0.198 |
| 5 | 0.969 / 0.895 | 0.478 / 0.198 |
| 10 | 0.992 / 0.853 | 0.273 / 0.288 |
| 20 | 0.995 / 0.762 | 0.481 / 0.910 |
| 40 | 0.992 / 0.612 | 0.858 / 0.951 |
| max LOF over k = 10–40 | 0.992 | 0.833 |
- For local outliers, LOF was excellent once k ≥ 10, while the global k-NN distance got steadily worse with k (0.90 down to 0.61), as the sparse cluster’s points came to dominate its ranking.
- The micro-cluster defeated LOF until k was far above its size: AUC 0.27 at k = 10, worse than chance, because its 12 members are each other’s neighbours, look perfectly dense, and score below many ordinary points. Even k = 20 was not enough (0.48); at k = 40, LOF reached 0.86.
- The plain k-NN distance found the micro-cluster easily at k ≥ 20 (0.91–0.95): once k exceeds 12, every member’s k-th neighbour lies far away.
- The max-over-k heuristic was a reasonable compromise (0.99 and 0.83), but no single setting won both cases. Know the size of the anomaly groups you want to detect, and set k above it.
LOF in AI, robotics and aerospace
- Multi-regime telemetry: flag samples that are unusual for their own flight phase without hand-crafting phase-specific thresholds.
- Quality control: detect parts whose measurements are unusual relative to similar parts, even when product variants form clusters of different spread.
- Dataset curation: find label-noise candidates that sit in thin regions next to dense class clusters.
References
- M. M. Breunig, H.-P. Kriegel, R. T. Ng, J. Sander (2000). “LOF: identifying density-based local outliers.” Proc. ACM SIGMOD.
- H.-P. Kriegel, P. Kröger, E. Schubert, A. Zimek (2009). “LoOP: local outlier probabilities.” Proc. ACM CIKM.
- G. O. Campos et al. (2016). “On the evaluation of unsupervised outlier detection: measures, datasets, and an empirical study.” Data Mining and Knowledge Discovery 30(4).
- E. Schubert, A. Zimek, H.-P. Kriegel (2014). “Local outlier detection reconsidered: a generalized view on locality with applications to spatial, video, and network outlier detection.” Data Mining and Knowledge Discovery 28(1).