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

Common Mistakes to Avoid

How to Interpret Results

Related Standards & References

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.