Sub topic 7
The anomaly score formula
This chapter assumes you know how an isolation tree is built, what path length means, and how a forest averages path lengths across trees. It turns those averages into a single number you can compare and threshold.
Why raw path length is not enough
A mean path length of 6 means different things depending on the sample size. In a tree built from 16 points, 6 is deep. In a tree built from 256 points, 6 is shallow. The score therefore divides the path length by a reference value that depends on the sample size.
The normalizing term c(n)
An isolation tree has the same shape as a binary search tree. The average depth at which an unsuccessful search ends in a binary search tree of n points is:
c(n) = 2 (ln(n − 1) + γ) − 2 (n − 1) / n
Here γ ≈ 0.5772 is the Euler–Mascheroni constant. Use c(n) = 1 for n = 2 and c(n) = 0 for n ≤ 1. The value c(n) is the typical path length of an unremarkable point in a sample of size n.
The score
For a point x and sample size n, the original paper defines:
s(x, n) = 2^(−E[h(x)] / c(n))
E[h(x)] is the mean path length of x over all trees. The formula behaves as follows:
| Mean path length E[h(x)] | Exponent | Score s | Reading |
|---|---|---|---|
| Close to 0 | Close to 0 | Close to 1 | Very likely an anomaly |
| c(n) / 2 | −0.5 | about 0.71 | Suspicious |
| c(n) | −1 | 0.5 | Unremarkable |
| 2 c(n) | −2 | 0.25 | Deep in dense data |
Scores fall in (0, 1]. If most points score near 0.5, the data has no clear anomalies. If most points score well below 0.5, the data is dense and homogeneous.
Computing it by hand
import numpy as np
EULER_GAMMA = 0.5772156649
def c(n):
"""Average path length of an unsuccessful search in a binary search tree of n points."""
if n <= 1:
return 0.0
if n == 2:
return 1.0
return 2.0 * (np.log(n - 1) + EULER_GAMMA) - 2.0 * (n - 1) / n
def anomaly_score(mean_path_length, n):
"""Score in (0, 1]; values near 1 are anomalous, around 0.5 are unremarkable."""
return 2.0 ** (-mean_path_length / c(n))
n = 256
print(round(c(n), 3)) # about 10.245
print(anomaly_score(c(n), n)) # 0.5
print(round(anomaly_score(3.0, n), 3)) # about 0.816Details that affect real results
- Trees stop growing at a depth limit of ceil(log2 ψ), where ψ is the subsample size. If a leaf still holds more than one point, the path length is the depth plus c(leaf size). This estimates the depth the tree would have reached.
- The normalizer uses the subsample size ψ, not the full dataset size. By default scikit-learn uses ψ = min(256, number of rows).
- Scikit-learn's
score_samplesreturns −s, the negated paper score. Its values lie in [−1, 0), and lower means more abnormal. decision_functionequalsscore_samplesminusoffset_, so negative values are outliers. Withcontamination="auto",offset_is −0.5, which matches the s = 0.5 line in the table.
Exercise
Using the formula in this chapter, a point has mean path length 5.12 and the subsample size is 256, where c(256) is about 10.245. What is its score s, and what score_samples value would scikit-learn report?
Show answer
The ratio is 5.12 / 10.245 ≈ 0.5, so s = 2^(−0.5) ≈ 0.71. Scikit-learn reports the negation, about −0.71.