LocalOutlierFactor<T>

Models & Types · AiDotNet.AnomalyDetection.DistanceBased

Implements the Local Outlier Factor (LOF) algorithm for density-based anomaly detection.

Inheritance: ModelBase<T, Matrix<T>, Vector<T>> → AnomalyDetectorBase<T> → LocalOutlierFactor<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

LOF detects anomalies by comparing the local density of a point to the local densities of its neighbors. Points in low-density regions (relative to their neighbors) are considered anomalies.

How It Works

The algorithm works by:

  1. Finding the k-nearest neighbors for each point
  2. Computing the local reachability density (LRD) for each point
  3. Comparing each point’s LRD to its neighbors’ LRDs to get the LOF score
  4. Points with LOF score significantly greater than 1 are anomalies

When to use: LOF is particularly effective for:

  • Detecting local anomalies (points that are anomalies relative to their neighborhood)
  • Data with varying densities across different regions
  • When you want interpretable density-based detection

Industry Standard Defaults:

  • Number of neighbors (k): 20 (good balance between local and global detection)
  • Contamination: 0.1 (10%)

Reference: Breunig, M. M., Kriegel, H. P., Ng, R. T., and Sander, J. (2000). “LOF: Identifying Density-Based Local Outliers.” ACM SIGMOD Record.

Constructors

Constructor Summary
LocalOutlierFactor(int numNeighbors, double contamination, int randomSeed) Creates a new Local Outlier Factor detector.

Properties

Property Summary
int NumNeighbors { get; } Gets the number of neighbors used for LOF computation.

Methods

Method Summary
Vector<T> ScoreAnomalies(Matrix<T> X)
void Fit(Matrix<T> X)