Judging a Reading Against Its Neighbours
Different operating regimes can have different spreads. Local outlier factor compares a point’s density with the density around its neighbours.
13 sections · 4 questions · 2 coding challenges
The arrow keys move between sections too.
Outliers Relative to Their Surroundings
BOTIE
Our one-dimensional example avoids distance ties to expose the mechanism. The original neighbourhood definition includes ties at the kth-neighbour distance, which can make a neighbourhood contain more than k points.
Sources for this history
Can a Sparse Group Be Normal?
One pump runs under tightly controlled pressure, while another normally varies more. A single global distance threshold can flag the second pump’s ordinary readings and miss a gap inside the first pump’s tight group.
Let's compare each reading with the readings near it. Is it much more isolated than its neighbours are? We will start by finding those neighbours, then introduce reachability distance and use it to calculate a local density comparison.
Reachability distances and neighbour densities are supplied to expose the final comparison.
Step 1 of 3
Find neighbours excluding the point itself
For k=1, each point uses its nearest other point.
| Original point | Points | Nearest indices |
|---|---|---|
| Point 0 | 0 | 1 |
| Point 1 | 1 | 0 |
| Point 10 | 10 | 1 |
Point 0
- Points
- 0
- Nearest indices
- 1
Point 1
- Points
- 1
- Nearest indices
- 0
Point 10
- Points
- 10
- Nearest indices
- 1
Step 2 of 3
Calculate local reachability density
Use the larger of actual distance and the neighbour’s own kth distance.
| Original point | Local densities |
|---|---|
| Point 0 | 1 |
| Point 1 | 1 |
| Point 10 | 1/9 |
Point 0
- Local densities
- 1
Point 1
- Local densities
- 1
Point 10
- Local densities
- 1/9
Step 3 of 3
Compare with neighbour density
A much lower density than neighbours gives a larger ratio.
| Original point | LOF |
|---|---|
| Point 0 | 1 |
| Point 1 | 1 |
| Point 10 | 9 |
Point 0
- LOF
- 1
Point 1
- LOF
- 1
Point 10
- LOF
- 9
Local isolation depends on the neighbourhood's own density
The score compares density at the point with density nearby.
Is this reading isolated relative to its own neighbours?
A sparse area can be normal for the points that live there. Compare the point's density with its neighbours' densities, then change that local comparison.
BOTIE
Density ratio relative to the neighbours
2.25
Follow the operations
1. Neighbour local densities
- 00.5
- 11
2. Set a constant
1
3. Positive reachability distances
- 02
- 14
4. Mean
3
5. Divide
0.3333
6. Divide
- 01.5
- 13
7. Mean
2.25
8. Result
2.25
Inputs must come from the same neighbourhood rule. This stage does not construct neighbour sets.
1. Choose a Distance and Neighbourhood
Scale features using a training reference so units do not dominate distance. For each point, find its kth-neighbour distance after excluding itself. Include all neighbours within that distance when implementing the original tie convention.
The neighbourhood size sets the scale of comparison. Too small a neighbourhood can be unstable; too large one can mix different operating regimes.
The exercises use distinct nearest-neighbour distances to avoid an ambiguous tie rule.
2. Prevent Tiny Distances From Dominating
The reachability distance from a point to a neighbour is the maximum of their distance and the neighbour’s kth-distance. Notice whose kth-distance is used: the neighbour’s.
This lower bound prevents an extremely close pair from producing arbitrarily large local density just because their mutual distance is tiny. It does not guarantee a positive denominator when duplicates dominate.
reachability(p,o) = max(distance(p,o), kth_distance(o))
local_density(p) = 1 / mean(reachability(p, neighbours))Duplicate-only neighbourhoods need a declared numerical policy.
3. Compare Density Ratios
For each neighbour, divide its local density by the point’s density, then average. A value near one indicates density similar to the neighbourhood. A substantially larger value indicates the point is sparser than its neighbours.
Point 10 has local density 1/9
Its nearest neighbour at 1 has density 1
LOF = 1 / (1/9) = 9LOF is not an anomaly probability, and one is not a universal hard decision threshold.
Compare one local density with its neighbours
Suppose the reachability distances for this point are both two. Its two neighbours have the supplied local densities. The question is how their average density compares with this point's density.
import numpy as np
reachability = np.array([2., 2.])
neighbour_density = np.array([0.5, 1.0])
own_density = 1.0 / reachability.mean()
print(float(own_density))
print(float(np.mean(neighbour_density / own_density)))0.5
1.5The neighbours are denser on average. Practice starts earlier in the procedure, calculating neighbours and reachability from the point coordinates.
4. Separate Fitting From Scoring New Points
The classic training-set calculation includes each reference point’s neighbourhood. Scoring a new observation against a fixed reference is a related novelty-detection procedure and must avoid silently rebuilding the reference with the new point.
Inspect sensitivity to k, distance scaling, and contamination. The method can miss a group of anomalies that forms a dense group of its own.
The workshop combines supplied reachability distances and neighbour densities. The Python example computes them from raw points.
5. The Neighbour’s Radius Sets the Reachability Floor
For each reference point, find its distance to its kth neighbour. When comparing a query with a neighbour, the reachability distance is at least that neighbour's kth-distance. This prevents a tiny pairwise distance inside a dense group from dominating the density estimate.
Average the reachability distances, take their reciprocal to form local reachability density, then compare the query density with its neighbours' densities. Each stage has a separate role; an ordinary mean distance ratio is not automatically LOF.
6. Inspect the Reachability Distances before Their Reciprocal
The supplied neighbour radii and query distances below let us see the floor directly. A close query-neighbour pair can still receive a larger reachability distance because of the neighbour's surrounding spacing.
Neighbour selection, distance units and tie handling must be consistent throughout the calculation. The example supplies those neighbours rather than pretending to fit a complete local-density model.
Apply the neighbour-specific floor
There are two chosen neighbours. Their kth-distances are 0.5 and 0.7.
import numpy as np
distance = np.array([.1, 1.2])
neighbour_radius = np.array([.5, .7])
reachability = np.maximum(distance, neighbour_radius)
density = 1/reachability.mean()
print(reachability.tolist())
print(round(float(density), 4))[0.5, 1.2]
1.1765The first pair is floored while the second retains its actual distance. A final LOF ratio still requires the neighbours' own local densities.
7. Do Not Let a New Batch Quietly Become Its Own Reference
Novelty detection scores new observations against a fitted reference collection. Including all new observations in the neighbourhood fit can make a cluster of unusual incoming points support each other and appear ordinary.
Distinguish that task from detecting outliers inside one supplied dataset. Both can be legitimate, but their fitted neighbourhoods and score interpretations differ. Test duplicates, isolated points and varying density before trusting a numerical threshold.
Check yourself
Questions on Parts 1 to 7
Question 1 of 4
What is the mean density ratio?
Use the information below.
Point density=0.5; neighbour densities=[1,2].Try it with Botie
Build with Botie
Write and run Python