-
Notifications
You must be signed in to change notification settings - Fork 38
/
anomalyze_test.go
68 lines (55 loc) · 1.69 KB
/
anomalyze_test.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
package anomalyzer
import (
"fmt"
"math/rand"
"testing"
"github.com/bmizerany/assert"
"github.com/drewlanenga/govector"
)
// Generates a random walk given the number of steps desired, a starting point,
// and the desired standard deviation.
func randomWalk(nsteps int, start float64, sd float64) (govector.Vector, error) {
walk := make(govector.Vector, nsteps)
walk[0] = float64(start)
i := 1
for i < nsteps {
step := rand.NormFloat64() * sd
walk[i] = cap(walk[i-1]+step, 0.0, 1.0)
i++
}
return walk, nil
}
func TestAnomalyzer(t *testing.T) {
conf := &AnomalyzerConf{
Sensitivity: 0.1,
UpperBound: 5,
LowerBound: 0,
ActiveSize: 1,
NSeasons: 4,
Methods: []string{"cdf", "fence", "highrank", "lowrank", "magnitude"},
}
// initialize with empty data or an actual slice of floats
data := []float64{0.1, 2.05, 1.5, 2.5, 2.6, 2.55}
anomalyzer, err := NewAnomalyzer(conf, data)
assert.Equal(t, nil, err, "Error initializing new anomalyzer")
prob := anomalyzer.Push(8.0)
assert.Tf(t, prob > 0.5, "Anomalyzer returned a probability that was too small")
}
func Example() {
conf := &AnomalyzerConf{
Sensitivity: 0.1,
UpperBound: 5,
LowerBound: NA, // ignore the lower bound
ActiveSize: 1,
NSeasons: 4,
Methods: []string{"diff", "fence", "highrank", "lowrank", "magnitude"},
}
// initialize with empty data or an actual slice of floats
data := []float64{0.1, 2.05, 1.5, 2.5, 2.6, 2.55}
anom, _ := NewAnomalyzer(conf, data)
// the push method automatically triggers a recalcuation of the
// anomaly probability. The recalculation can also be triggered
// by a call to the Eval method.
prob := anom.Push(8.0)
fmt.Println("Anomalous Probability:", prob)
}