-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathewma.go
More file actions
32 lines (25 loc) · 935 Bytes
/
Copy pathewma.go
File metadata and controls
32 lines (25 loc) · 935 Bytes
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
package stats
// EWMA calculates the exponentially weighted moving average of the input
// with smoothing factor alpha. The first output equals the first input and
// each subsequent entry is alpha*input[i] + (1-alpha)*output[i-1], so the
// result has the same length as the input. The alpha must satisfy
// 0 < alpha <= 1 or ErrBounds is returned. An empty input returns
// ErrEmptyInput.
func EWMA(input Float64Data, alpha float64) ([]float64, error) {
if input.Len() == 0 {
return nil, ErrEmptyInput
}
if alpha <= 0 || alpha > 1 {
return nil, ErrBounds
}
output := make([]float64, input.Len())
output[0] = input[0]
for i := 1; i < input.Len(); i++ {
output[i] = alpha*input[i] + (1-alpha)*output[i-1]
}
return output, nil
}
// EWMA returns the exponentially weighted moving average of the data with smoothing factor alpha
func (f Float64Data) EWMA(alpha float64) ([]float64, error) {
return EWMA(f, alpha)
}