-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforest.go
77 lines (64 loc) · 1.97 KB
/
forest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package eif
import "math"
const (
numberOfTreesDefault = 10
)
func cFactor(n float64) float64 {
return 2.0*(math.Log(n-1)+0.5772156649) - (2.0 * (n - 1.) / (n * 1.0))
}
// Forest is a group of trees
type Forest struct {
trees []*Node
c float64
}
// Score calculates the anomaloussnes score (between 0.0 and 1.0) of a given point.
// Higher scores imply more anomaloussnes. Note that scores are affected by maximum tree depth.
func (f *Forest) Score(p []float64) float64 {
var totalDepth int
for _, t := range f.trees {
totalDepth += t.Depth(p)
}
avg := float64(totalDepth) / float64(len(f.trees))
return math.Pow(2, -avg/f.c)
}
// ForestParams is the set of metadata used to construct a forest
type forestParams struct {
numberOfTrees int
maxTreeDepth int
}
// ForestOpt is an option to adapt ForestParams
type ForestOpt func(*forestParams)
// WithTrees is a construction option, setting the amount of trees to generate
func WithTrees(n int) ForestOpt {
return func(f *forestParams) {
f.numberOfTrees = n
}
}
// WithMaxTreeDepth is a construction option, which determines how deep trees are constructed
// In general, running with smaller values is faster, but loses fidelity in anomaly scores
func WithMaxTreeDepth(n int) ForestOpt {
return func(f *forestParams) {
f.maxTreeDepth = n
}
}
// NewForest creates a new forest, with optional construction parameters.
// By default 10 trees are constructed, and a maximum depth determined by the expected depth
// of an unsuccesful binary tree search with the given data size.
func NewForest(data [][]float64, opts ...ForestOpt) *Forest {
sampleSize := float64(len(data))
params := forestParams{
numberOfTrees: numberOfTreesDefault,
maxTreeDepth: int(math.Ceil(math.Log2(sampleSize))),
}
for _, fn := range opts {
fn(¶ms)
}
trees := make([]*Node, params.numberOfTrees)
for i := range trees {
trees[i] = NewTree(data, params.maxTreeDepth)
}
return &Forest{
trees: trees,
c: cFactor(sampleSize),
}
}