Commit bc8dc88
fix: Depth Calculation for iForest importer (#617)
* fix: Depth Calculation for iForest importer
Corrected the path depth calculation for `IsolationForest` model conversion
to match sklearn's implementation.
In Isolation Forest, the anomaly score is computed as:
s(x) = 2^(-E[h(x)] / c(n))
Where:
- E[h(x)] is the expected path length for sample x
- c(n) = 2 * H(n - 1) - (2 * (n - 1)) / n
- H(n) is the nth harmonic number
Previously, Treelite used the digamma-based formulation for the harmonic number:
H(n) = ψ(n + 1) + γ
and computed c(n) as:
c(n) = float(2 * (harmonic(n) - 1))
While mathematically valid, this differs from sklearn’s implementation
and the original paper. sklearn approximates H(n) as:
H(n) ≈ ln(n) + γ
and computes:
c(n) = 2 * H(n - 1) - (2 * (n - 1)) / n
This PR changes Treelite’s calculation to match sklearn's implementation,
ensuring consistency in path length and anomaly score computations,
especially for small n where numerical differences may affect results.
* Fix formatting
* Update pytest
---------
Co-authored-by: Philip Hyunsu Cho <chohyu01@cs.washington.edu>
Co-authored-by: Hyunsu Cho <phcho@nvidia.com>1 parent a78b6cf commit bc8dc88
File tree
3 files changed
+4
-5
lines changed- python/treelite/sklearn
- tests/python
3 files changed
+4
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | | - | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | | - | |
| 17 | + | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
| 195 | + | |
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| |||
0 commit comments