Skip to content

Clustering, density estimation, and anomaly detection · Anomaly detection · UCL050 · Algorithm

Local Outlier Factor (LOF)

Detect observations whose local density is lower than that of nearby observations. It can identify local anomalies that are not globally distant from the entire dataset.

Runs in CodeSplash Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.lof. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Detect observations whose local density is lower than that of nearby observations. It can identify local anomalies that are not globally distant from the entire dataset.

Question / hypotheses

Which observations deviate from the chosen reference notion of normality, and at what operational alert rate? This entry defines an estimation or optimization objective, not an intrinsic null/alternative hypothesis test.

Architecture / computational structure

Neighbor search followed by reachability-density ratios.

Assumptions

Different neighborhood sizes detect different scales. Distance concentration, duplicate points, and contaminated neighborhoods can undermine scores. LOF is not a p-value.

Formula / computational definition

Reachability distance is max⁡{d(x,y),k-distance(y)}\max\{d(x,y),k\text{-distance}(y)\}. Local reachability density lrdk(x)lrd_k(x) is the reciprocal of mean reachability distance to neighbors; LOFk(x)=∣Nk(x)∣−1∑y∈Nk(x)lrdk(y)/lrdk(x)LOF_k(x)=|N_k(x)|^{-1}\sum_{y\in N_k(x)}lrd_k(y)/lrd_k(x). Values near one indicate comparable local density, while larger values indicate relative sparsity.

Parameters and notation

Learned or fitted quantities: Reference neighborhoods, local densities, and an optional decision threshold. User-set hyperparameters/design choices: Choose neighbor count, metric, scaling, and outlier-versus-novelty mode. Tune these against the stated objective and stability/validation criteria; do not select settings on the final evaluation set.

Fitting, fine-tuning, and adaptation

Neural fine-tuning is not applicable to this classical detector. Refit the reference model when its population changes, then recalibrate an alert threshold on a separate normal holdout. Tune sensitivity without using final-test anomaly labels repeatedly.

How to use and calibrate

Examine ranking stability across plausible kk, local explanations, and held-out operational detection when labels exist. Avoid fitting and scoring novelty data as if they were independent training cases.

Example application

Detect unusually sparse points within a dense local operating regime. Define the reference population, measurement units, and success criterion before fitting; investigate whether discovered groups or alerts reflect the intended phenomenon.

Interpretation / pitfalls

Watch out

The reported result is conditional on the representation and objective. An alert is a screening result, not a diagnosis or a calibrated posterior probability. Evaluate both ranking and the decision threshold.

Python and R code examples

Python LocalOutlierFactor distinguishes transductive fit_predict from novelty=True scoring of new points. R dbscan::lof supplies LOF scores. Confirm self-neighbor conventions and score orientation before cross-language comparison.

Component: LOF ratio from supplied local reachability densities. Python: executed successfully; R: not executed (Rscript unavailable). These small fixtures establish the arithmetic operation, not full-method accuracy or reproduction of paper results.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — LOF ratio from supplied local reachability densities. · needs numpy

import numpy as np
lrd_self=.4; neighbor_lrd=np.array([.8,1.,1.2])
result=np.mean(neighbor_lrd/lrd_self)
print(result)
R

Component demo, not a trained model — LOF ratio from supplied local reachability densities.

lrd_self=.4; neighbor_lrd=c(.8,1,1.2); result=mean(neighbor_lrd/lrd_self)
print(result)

Sources

From the CodeSplash reference, entry UCL050.