-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrelator.go
96 lines (83 loc) · 2.72 KB
/
correlator.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package anomalia
// CorrelationAlgorithm base interface for correlation algorithms.
type CorrelationAlgorithm interface {
Run() float64
sanityCheck() error
}
// CorrelationMethod type checker for correlation method
type CorrelationMethod int32
const (
// XCorr represents the Cross Correlation algorithm.
XCorr CorrelationMethod = iota
// SpearmanRank represents the Spearman Rank Correlation algorithm.
SpearmanRank
// Pearson represents the Pearson Correlation algorithm.
Pearson
)
// Correlator holds the correlator configuration.
type Correlator struct {
current, target *TimeSeries
algorithm CorrelationAlgorithm
useAnomalyScore bool
}
// NewCorrelator returns an instance of the correlation algorithm.
func NewCorrelator(current, target *TimeSeries) *Correlator {
if current == nil || target == nil {
panic("either current or target time series cannot be nil")
}
return &Correlator{
current: current,
target: target,
}
}
// CorrelationMethod specifies which correlation method to use (XCross or SpearmanRank).
func (c *Correlator) CorrelationMethod(method CorrelationMethod, options []float64) *Correlator {
c.algorithm = c.getCorrelationAlgorithmByMethod(method, options)
return c
}
// TimePeriod crops the current and target time series to specified range.
func (c *Correlator) TimePeriod(start, end float64) *Correlator {
c.current = c.current.Crop(start, end)
c.target = c.target.Crop(start, end)
return c
}
// UseAnomalyScore tells the correlator to calculate anomaly scores from both time series.
func (c *Correlator) UseAnomalyScore(use bool) *Correlator {
c.useAnomalyScore = use
return c
}
// Run runs the correlator.
func (c *Correlator) Run() float64 {
if err := c.algorithm.sanityCheck(); err != nil {
panic(err)
}
if c.useAnomalyScore {
c.current = getAnomalyScores(NewDetector(c.current))
c.target = getAnomalyScores(NewDetector(c.target))
}
return c.algorithm.Run()
}
func (c *Correlator) getCorrelationAlgorithmByMethod(method CorrelationMethod, options []float64) CorrelationAlgorithm {
var algorithm CorrelationAlgorithm
switch method {
case XCorr:
algorithm = NewCrossCorrelation(c.current, c.target)
if options != nil && len(options) > 0 {
algorithm = algorithm.(*CrossCorrelation).MaxShift(options[0]).Impact(options[1])
}
case SpearmanRank:
algorithm = NewSpearmanCorrelation(c.current, c.target)
case Pearson:
algorithm = NewPearsonCorrelation(c.current, c.target)
default:
panic("unsupported correlation method/algorithm")
}
return algorithm
}
func getAnomalyScores(detector *Detector) *TimeSeries {
scoreList := detector.GetScores()
if scoreList == nil {
panic("failed to calculate anomaly scores")
}
return &TimeSeries{scoreList.Timestamps, scoreList.Scores}
}