Local Outlier Factor
The local outlier factor scores anomalies by comparing a point density to the density of its neighbors.
Anomaly is relative to the neighborhood
A point is not anomalous in the abstract; it is anomalous relative to its surroundings. The local outlier factor (LOF) captures this by comparing how densely packed a point is with how densely packed its neighbors are. A point sitting in a sparse pocket while its neighbors sit in dense clusters gets a high LOF score even if, globally, the region is not the sparsest.
Reachability and local density
LOF first finds each point k nearest neighbors. It defines a reachability distance that smooths out the effect of very close points, then computes a local reachability density as the inverse of the average reachability distance to those neighbors. This density estimate is local, tied only to the immediate neighborhood.
The LOF score
The LOF of a point is the average ratio of its neighbors local densities to its own. A value near one means the point is as dense as its neighbors (normal). A value well above one means the point is far less dense than its neighbors expect (an outlier). The single parameter k sets the neighborhood scale and materially affects results.
- Detects local outliers that global methods miss
- Score near one is normal; larger is more anomalous
- Sensitive to the neighbor count k; try a range
- Costly on large data: needs pairwise or indexed neighbor search
When to use it
LOF shines when the data has clusters of differing density, where a single global threshold would either miss local outliers or flood the sparse clusters with false alarms. Its cost is the neighbor computation, which scales poorly without spatial indexing, making the linear-time isolation forest preferable on very large datasets. It remains a strong choice for moderate-size anomaly detection with heterogeneous density.