-
Notifications
You must be signed in to change notification settings - Fork 451
/
holt_winters.go
140 lines (116 loc) · 4.16 KB
/
holt_winters.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package temporal
import (
"fmt"
"math"
"time"
"github.com/m3db/m3/src/query/executor/transform"
)
const (
// HoltWintersType produces a smoothed value for time series based on the specified interval.
// The algorithm used comes from https://en.wikipedia.org/wiki/Exponential_smoothing#Double_exponential_smoothing.
// Holt-Winters should only be used with gauges.
HoltWintersType = "holt_winters"
)
// NewHoltWintersOp creates a new base Holt-Winters transform with a specified node.
func NewHoltWintersOp(args []interface{}) (transform.Params, error) {
// todo(braskin): move this logic to the parser.
if len(args) != 3 {
return emptyOp, fmt.Errorf("invalid number of args for %s: %d", HoltWintersType, len(args))
}
duration, ok := args[0].(time.Duration)
if !ok {
return emptyOp, fmt.Errorf("unable to cast to scalar argument: %v for %s", args[0], HoltWintersType)
}
sf, ok := args[1].(float64)
if !ok {
return emptyOp, fmt.Errorf("unable to cast to scalar argument: %v for %s", args[1], HoltWintersType)
}
tf, ok := args[2].(float64)
if !ok {
return emptyOp, fmt.Errorf("unable to cast to scalar argument: %v for %s", args[2], HoltWintersType)
}
// Sanity check the input.
if sf <= 0 || sf >= 1 {
return emptyOp, fmt.Errorf("invalid smoothing factor. Expected: 0 < sf < 1, got: %f", sf)
}
if tf <= 0 || tf >= 1 {
return emptyOp, fmt.Errorf("invalid trend factor. Expected: 0 < tf < 1, got: %f", tf)
}
aggregationFunc := makeHoltWintersFn(sf, tf)
a := aggProcessor{
aggFunc: aggregationFunc,
}
return newBaseOp(duration, HoltWintersType, a)
}
func makeHoltWintersFn(sf, tf float64) aggFunc {
return func(vals []float64) float64 {
var (
foundFirst, foundSecond bool
secondVal float64
trendVal float64
scaledSmoothVal, scaledTrendVal float64
prev, curr float64
idx int
)
for _, val := range vals {
if math.IsNaN(val) {
continue
}
if !foundFirst {
foundFirst = true
curr = val
idx++
continue
}
if !foundSecond {
foundSecond = true
secondVal = val
trendVal = secondVal - curr
}
// scale the raw value against the smoothing factor.
scaledSmoothVal = sf * val
// scale the last smoothed value with the trend at this point.
trendVal = calcTrendValue(idx-1, sf, tf, prev, curr, trendVal)
scaledTrendVal = (1 - sf) * (curr + trendVal)
prev, curr = curr, scaledSmoothVal+scaledTrendVal
idx++
}
// need at least two values to apply a smoothing operation.
if !foundSecond {
return math.NaN()
}
return curr
}
}
// Calculate the trend value at the given index i in raw data d.
// This is somewhat analogous to the slope of the trend at the given index.
// The argument "s" is the set of computed smoothed values.
// The argument "b" is the set of computed trend factors.
// The argument "d" is the set of raw input values.
func calcTrendValue(i int, sf, tf, s0, s1, b float64) float64 {
if i == 0 {
return b
}
x := tf * (s1 - s0)
y := (1 - tf) * b
return x + y
}