LOF Anomaly Detection
Detect anomalies using Local Outlier Factor. Uses Local Outlier Factor (LOF) to measure the local density deviation of data points relative to their neighbors.…
Uses Local Outlier Factor (LOF) to measure the local density deviation of data points relative to their neighbors. Points with substantially lower density are classified as anomalies.
What is Local Outlier Factor (LOF) and how does it detect anomalies?
Local Outlier Factor (LOF) is a density-based anomaly detection algorithm that identifies outliers by comparing the local density of a point to the densities of its neighbors. Unlike global methods, LOF can detect outliers in datasets with clusters of varying density — a point may be normal in a sparse cluster but anomalous near a dense cluster.
The algorithm computes the reachability distance and local reachability density (LRD) for each point based on its k-nearest neighbors. The LOF score is the ratio of a point's LRD to the average LRD of its neighbors. LOF ≈ 1 means similar density to neighbors (normal); LOF >> 1 means much lower density (anomaly).
The number of neighbors (k) controls the locality: small k focuses on very local structure, large k smooths over broader neighborhoods. The contamination ratio determines the fraction of points classified as anomalies based on the LOF score ranking.
Formula: reach-dist_k(A,B) = max(k-dist(B), dist(A,B)) LRD_k(A) = 1 / (Σ reach-dist_k(A,Bᵢ) / k) LOF_k(A) = (Σ LRD(Bᵢ) / k) / LRD(A) LOF ≈ 1: normal point LOF >> 1: outlier
Example Calculation
With k=5 neighbors: Point P₁ in a dense cluster has LRD=10, its 5 neighbors average LRD=9.5. LOF = 9.5/10 = 0.95 (normal). Point P₂ near the cluster edge has LRD=2, neighbors average LRD=8. LOF = 8/2 = 4.0 (clear outlier). With contamination=0.05, the top 5% highest LOF scores are flagged.
When to Use This Calculator
- When your data has clusters of varying density — LOF detects local anomalies that global methods like Z-score or Isolation Forest would miss
- During multivariate quality inspection when measurements from different test stations form natural clusters with different spreads
- When analyzing equipment vibration or sensor data where normal operating modes have different characteristic patterns and density
- When outlier detection results from Isolation Forest seem to miss anomalies near dense clusters — LOF's density-based approach handles this case
- During process troubleshooting to identify data points that are unusual relative to their local neighborhood, even if they fall within global ranges
Common Mistakes to Avoid
- Setting k (neighbors) too small, causing sensitivity to noise — very small k values make the algorithm unstable; start with k=20 and adjust based on results
- Using LOF on very high-dimensional data — LOF relies on distance metrics that become less meaningful in high dimensions (curse of dimensionality); consider dimensionality reduction first
- Treating all points with LOF > 1 as anomalies — LOF > 1 only means slightly lower density than neighbors; use the contamination threshold or a meaningful LOF cutoff (e.g., LOF > 2)
- Not scaling features before applying LOF — features with larger numerical ranges dominate the distance calculation; standardize all features to zero mean and unit variance
How to Interpret Results
- LOF ≈ 1.0 — the point has similar density to its neighbors; it is a normal member of its local cluster
- LOF 1.5-3.0 — the point is in a less dense area relative to its neighbors; it may be on the boundary of a cluster or a mild outlier
- LOF > 3.0 — the point has much lower density than its neighbors; it is a strong local outlier and should be investigated
- LOF < 1.0 — the point is in a denser area than its neighbors; this is common for points at the center of tight clusters
- If many points have high LOF scores, the data may not have well-defined clusters — consider whether LOF is the right method for this dataset
Related Standards & References
- Breunig, Kriegel, Ng & Sander (2000), "LOF: Identifying Density-Based Local Outliers", ACM SIGMOD — The original paper defining the LOF score computed here
- ISO 16269-4 — Statistical interpretation of data: detection and treatment of outliers, the standard governing how flagged observations should be handled
- ASTM E178 — Standard practice for dealing with outlying observations, the classical complement for univariate outlier decisions
- Aggarwal, Outlier Analysis (Springer) — Comprehensive reference comparing density-based methods like LOF with isolation- and distance-based alternatives
Frequently Asked Questions
How do I choose the number of neighbors (k)?
Start with k=20 as a reasonable default. Smaller k (5-10) detects very local anomalies but is sensitive to noise. Larger k (50+) provides smoother results but may miss localized anomalies. A practical approach: test multiple k values and check if the same points are consistently flagged — persistent outliers across different k values are more likely true anomalies.
When should I use LOF instead of Isolation Forest?
Use LOF when your data has clusters of different densities, as LOF evaluates each point relative to its local neighborhood. A point far from a dense cluster but close to a sparse cluster may be normal — LOF handles this correctly while global methods might flag it. Use Isolation Forest for high-dimensional data or very large datasets where LOF's O(n²) complexity becomes prohibitive.