-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderivative.go
72 lines (59 loc) · 1.89 KB
/
derivative.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
package anomalia
import "math"
// Derivative holds the derivative algorithm configuration.
// It uses the derivative of the current value as anomaly score.
type Derivative struct {
smoothingFactor float64
}
// NewDerivative return Derivative instance
func NewDerivative() *Derivative {
return &Derivative{0.2}
}
// SmoothingFactor sets the smoothing factor.
func (d *Derivative) SmoothingFactor(factor float64) *Derivative {
d.smoothingFactor = factor
return d
}
// Run runs the derivative algorithm over the time series
func (d *Derivative) Run(timeSeries *TimeSeries) *ScoreList {
scoreList, _ := d.computeScores(timeSeries)
return scoreList
}
func (d *Derivative) computeScores(timeSeries *TimeSeries) (*ScoreList, error) {
derivatives := d.computeDerivatives(timeSeries)
derivativesEma := Ema(derivatives, d.smoothingFactor)
scores := mapSliceWithIndex(timeSeries.Values, func(i int, value float64) float64 {
return math.Abs(derivatives[i] - derivativesEma[i])
})
stdev := Stdev(scores)
if stdev != 0.0 {
scores = mapSlice(scores, func(score float64) float64 {
return score / stdev
})
}
scoreList := (&ScoreList{timeSeries.Timestamps, scores}).Denoise()
return scoreList, nil
}
func (d *Derivative) computeDerivatives(timeSeries *TimeSeries) []float64 {
zippedSeries := timeSeries.Zip()
derivatives := make([]float64, 0, len(zippedSeries))
for i, timestamp := range timeSeries.Timestamps {
if i > 0 {
preTimestamp := timeSeries.Timestamps[i-1]
preValue := zippedSeries[preTimestamp]
currentValue := zippedSeries[timestamp]
delta := timestamp - preTimestamp
derivative := 0.0
if delta != 0 {
derivative = (currentValue - preValue) / delta
} else {
derivative = currentValue - preValue
}
derivatives = append(derivatives, math.Abs(derivative))
}
}
if len(derivatives) != 0 {
derivatives = insertAt(derivatives, 0, derivatives[0])
}
return derivatives
}