Sub topic 4
Path length as a measure of abnormality
This chapter assumes you know how an isolation tree is built from random splits, and that you are comfortable with binary trees and averages. Here you turn a tree into a number you can compare across points.
Definition
The path length h(x) of a point x is the number of edges (splits) from the root of an isolation tree to the leaf that holds x. Each edge is one random split, so h(x) counts how many splits it took to separate x from the other points.
- A short path means x was separated after few splits. This is typical of an anomaly.
- A long path means x stayed grouped with other points through many splits. This is typical of a normal point.
Why anomalies get short paths
A random split picks a feature, then a value uniformly between that feature's minimum and maximum in the current node. Two effects favor isolating anomalies early:
- Anomalies sit in sparse regions. A random cut value has a good chance of falling between an anomaly and the dense group.
- Anomalies often have extreme values. The cut range is stretched by them, but most cut values still fall on the side that leaves the anomaly alone.
Points in a dense cluster need many cuts before each one is alone.
A single tree is noisy
Random splits mean one tree can get lucky or unlucky. A normal point may be isolated quickly by chance. So the useful quantity is the average path length E[h(x)] over many trees. The next chapter covers the forest. First, a one-dimensional simulation shows the effect.
import numpy as np
def path_length(x, data, rng, depth=0, max_depth=20):
"""Count random splits until x is alone (or limits are reached)."""
if len(data) <= 1 or depth >= max_depth:
return depth
lo, hi = data.min(), data.max()
if lo == hi:
return depth
split = rng.uniform(lo, hi)
side = data[data < split] if x < split else data[data >= split]
return path_length(x, side, rng, depth + 1, max_depth)
rng = np.random.RandomState(0)
data = np.r_[rng.normal(0.0, 1.0, 255), 8.0] # 255 normal points + 1 outlier
normal_point = data[0]
outlier = 8.0
normal_mean = np.mean([path_length(normal_point, data, rng) for _ in range(500)])
outlier_mean = np.mean([path_length(outlier, data, rng) for _ in range(500)])
print("mean path length, normal point:", round(normal_mean, 2))
print("mean path length, outlier: ", round(outlier_mean, 2))The outlier's mean path length comes out clearly smaller than the normal point's. Each call builds one random tree path, and the mean over 500 calls plays the role of the forest average.
Height limit and the leaf adjustment
Real implementations stop growing a tree at a maximum height, about ceil(log2(ψ)), where ψ is the subsample size. Anomalies are isolated early, so deep branches add little. The limit saves time and memory.
The limit creates a complication. A leaf may still hold several points when growth stops. For such a leaf, the path length is the depth reached plus c(size), an estimate of how many more splits a full tree would have needed. The function c(n) is the average path length of an unsuccessful search in a binary search tree. The next chapters define it and use it to normalize path lengths into a score.
Common errors
- Comparing raw path lengths across models. The typical path length grows with the subsample size. Raw values from models with different
max_samplesare not comparable. The score formula fixes this. - Trusting a single tree. One short path can be chance. Average over trees.
- Reading a short path as proof of an error. A short path means the point is easy to isolate. It does not say why, and rare but valid events also get short paths.
- Forgetting the sign. In scikit-learn, a lower
score_samplesvalue means a shorter average path and a more abnormal point.
Exercise
In the simulation above, you raise the number of trials from 500 to 5000. What changes in the two printed means, and what does not?
Show answer
Both means become more stable, because averaging over more random paths reduces noise. The outlier's mean stays clearly shorter than the normal point's. The gap reflects the data, not the number of trials.