Local outlier factor (LOF) is an unsupervised, density-based algorithm for anomaly detection that assigns each data point a score expressing how much less dense its neighborhood is than the neighborhoods of its nearest neighbors. Introduced by Breunig, Kriegel, Ng and Sander in 2000, it was one of the first methods to treat outlierness as a local, relative property rather than a global yes-or-no label.
The key insight of LOF is that a point can be anomalous relative to a nearby dense cluster even when its absolute distance to other points looks ordinary. In artificial intelligence and data mining, this makes LOF well suited to datasets with clusters of different densities, where global distance thresholds or a single Gaussian model would either miss local anomalies or flag entire sparse clusters.
How the local outlier factor is computed
LOF builds its score in four steps from a single hyperparameter, the neighborhood size k, often called MinPts in the original paper. Each step refines the notion of local density so that the final score is robust to statistical fluctuations among close neighbors.
| Step | Quantity | Definition |
|---|---|---|
| 1 | k-distance | Distance from point p to its k-th nearest neighbor |
| 2 | Reachability distance | Maximum of the k-distance of o and the distance from p to o |
| 3 | Local reachability density | Inverse of the mean reachability distance from p to its neighbors |
| 4 | LOF score | Mean ratio of neighbor densities to the density of p |
The reachability distance smooths the measure: points that are very close to a neighbor are treated as if they were at that neighbor’s k-distance, which prevents tiny distances from producing extreme densities. The final LOF score is the average of the neighbors’ local reachability densities divided by the point’s own density.
Interpreting LOF scores
An LOF score close to 1 means the point has a density similar to its neighbors and is an inlier. Scores clearly above 1 indicate a point that sits in a sparser region than its neighbors, and the larger the value, the stronger the evidence for an anomaly. Scores slightly below 1 mark points in the dense core of a cluster.
There is no universal threshold. A score of 1.5 may be significant in one dataset and normal in another, because LOF values depend on dimensionality and the distribution. Practitioners therefore rank points by score or set a cutoff through an expected contamination rate, the assumed share of anomalies.
Raw LOF values are also hard to compare across models. Some workflows convert them into normalized outlier probabilities, for example with the LoOP variant, which maps local density deviations onto a range from 0 to 1. Such calibrated scores make thresholds easier to communicate to analysts and to combine with other detectors in an ensemble.
| LOF score | Interpretation | Typical action |
|---|---|---|
| Below 1 | Denser than neighbors | Treat as normal |
| About 1 | Similar density to neighbors | Treat as normal |
| 1.2 to 2 | Mildly sparser region | Review in context |
| Above 2 | Strongly isolated locally | Flag as likely anomaly |
Choosing the neighborhood size and distance metric
The choice of k controls the scale at which density is judged. A small k, such as 5, reacts to very local structure but produces noisy scores; a large k, such as 50, smooths the estimate but can absorb small anomalous groups into their surroundings. The original authors recommend a lower bound of about 10 to 20 and computing scores over a range of k, keeping the maximum per point.
Because LOF relies entirely on distances, feature scaling is mandatory. Standardizing features prevents a variable measured in thousands from dominating one measured in fractions. For mixed or specialized data, the metric can be replaced, for example by cosine distance for text embeddings or Mahalanobis distance for correlated features.
LOF compared with other anomaly detection methods
LOF belongs to the neighbor-based family of detectors. It shares the use of nearest neighbors with k-NN anomaly detection, but it adds the relative density comparison that k-NN distance scores lack. Isolation forests and one-class SVMs approach the problem through random partitioning and boundary estimation instead.
| Method | Core idea | Strength | Weakness |
|---|---|---|---|
| Local outlier factor | Relative local density | Handles varying densities | Quadratic cost without indexing |
| k-NN anomaly detection | Distance to k-th neighbor | Simple and fast | Misses local anomalies |
| Isolation forest | Path length in random trees | Scales to large datasets | Weaker on local density patterns |
| One-class SVM | Boundary around normal data | Flexible kernels | Sensitive to tuning |
| Elliptic envelope | Robust Gaussian fit | Interpretable | Assumes one elliptical cluster |
Outlier detection and novelty detection modes
LOF can run in two modes. In outlier detection, the model scores the same dataset it was fitted on, which is the classic batch use. In novelty detection, the model is fitted on clean training data and then scores new, unseen points against that reference. Libraries such as scikit-learn and PyOD expose both modes, and mixing them up is a frequent implementation error.
- Batch audits of historical records use outlier detection mode
- Streaming monitoring of new events uses novelty detection mode
- Incremental LOF variants update scores when points arrive or leave
Limitations and common pitfalls
The main practical limit is computational cost. A naive implementation needs all pairwise distances, giving quadratic time in the number of points; spatial indexes such as k-d trees or ball trees help in low dimensions but lose effectiveness above roughly 20 dimensions. In high-dimensional data, the concentration of distances also makes density contrasts less meaningful.
Other pitfalls include duplicate points, which can produce zero reachability distances and unstable scores, and large groups of anomalies that are denser than k, which LOF then treats as a normal cluster. Applying LOF to embeddings from a trained encoder or after dimensionality reduction often improves results on complex data.
In plain words
Local outlier factor looks for points that are lonely compared with their own neighbors. It does not ask whether a point is far from everything. It asks whether a point is in a much emptier spot than the points around it.
Picture houses on a map. A house in the countryside is normal, because all houses there are spread out. A single house in an empty lot inside a crowded city block stands out. LOF gives each point a number. A number near 1 means normal, and a much higher number means the point deserves a closer look.
A real-world example
A payments team at a mid-sized online retailer wants to find suspicious merchant accounts among about 60,000 sellers. Each seller is described by standardized features such as order volume, refund rate, average basket size and account age. Large sellers form a sparse cluster and small hobby sellers form a dense one, so a global distance rule flags too many large sellers.
The team applies LOF with k set to 30 and ranks sellers by score. The top 150 accounts, with scores above 2.3, go to manual review. Analysts confirm that about one third show signs of fraud or policy abuse, such as refund cycling, a hit rate far higher than the earlier rule. The team adds LOF as one signal in its fraud detection pipeline and recalibrates it every month.