forked from go-gota/gota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolling_window.go
52 lines (43 loc) · 1005 Bytes
/
rolling_window.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
package series
// RollingWindow is used for rolling window calculations.
type RollingWindow struct {
window int
series Series
}
// Rolling creates new RollingWindow
func (s Series) Rolling(window int) RollingWindow {
return RollingWindow{
window: window,
series: s,
}
}
// Mean returns the rolling mean.
func (r RollingWindow) Mean() (s Series) {
s = New([]float64{}, Float, "Mean")
for _, block := range r.getBlocks() {
s.Append(block.Mean())
}
return
}
// StdDev returns the rolling mean.
func (r RollingWindow) StdDev() (s Series) {
s = New([]float64{}, Float, "StdDev")
for _, block := range r.getBlocks() {
s.Append(block.StdDev())
}
return
}
func (r RollingWindow) getBlocks() (blocks []Series) {
for i := 1; i <= r.series.Len(); i++ {
if i < r.window {
blocks = append(blocks, r.series.Empty())
continue
}
index := []int{}
for j := i - r.window; j < i; j++ {
index = append(index, j)
}
blocks = append(blocks, r.series.Subset(index))
}
return
}