Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rolling window Mean and StdDev #117

Merged
merged 2 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions series/rolling_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
85 changes: 85 additions & 0 deletions series/rolling_window_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package series

import (
"math"
"strings"
"testing"
)

func TestSeries_RollingMean(t *testing.T) {
tests := []struct {
window int
series Series
expected Series
}{
{
3,
Ints([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
Floats([]float64{math.NaN(), math.NaN(), 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}),
},
{
2,
Floats([]float64{1.0, 2.0, 3.0}),
Floats([]float64{math.NaN(), 1.5, 2.5}),
},
{
0,
Floats([]float64{}),
Floats([]float64{}),
},
}

for testnum, test := range tests {
expected := test.expected
received := test.series.Rolling(test.window).Mean()

for i := 0; i < expected.Len(); i++ {
if strings.Compare(expected.Elem(i).String(),
received.Elem(i).String()) != 0 {
t.Errorf(
"Test:%v\nExpected:\n%v\nReceived:\n%v",
testnum, expected, received,
)
}
}
}
}

func TestSeries_RollingStdDev(t *testing.T) {
tests := []struct {
window int
series Series
expected Series
}{
{
3,
Ints([]int{5, 5, 6, 7, 5, 5, 5}),
Floats([]float64{math.NaN(), math.NaN(), 0.5773502691896257, 1.0, 1.0, 1.1547005383792515, 0.0}),
},
{
2,
Floats([]float64{1.0, 2.0, 3.0}),
Floats([]float64{math.NaN(), 0.7071067811865476, 0.7071067811865476}),
},
{
0,
Floats([]float64{}),
Floats([]float64{}),
},
}

for testnum, test := range tests {
expected := test.expected
received := test.series.Rolling(test.window).StdDev()

for i := 0; i < expected.Len(); i++ {
if strings.Compare(expected.Elem(i).String(),
received.Elem(i).String()) != 0 {
t.Errorf(
"Test:%v\nExpected:\n%v\nReceived:\n%v",
testnum, expected, received,
)
}
}
}
}