-
Notifications
You must be signed in to change notification settings - Fork 4
/
movingminmax_test.go
303 lines (261 loc) · 5.94 KB
/
movingminmax_test.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// movingminmax_test.go, jpad 2015
package movingminmax
import (
// "fmt"
"math/rand"
"testing"
"github.com/notnot/container/deque_int"
)
const (
N = 1000
W = 10
)
var (
values []float32 // input sample values
mmins, mmaxs []float32 // reference results
)
// init initializes the test values.
func init() {
values = make([]float32, N)
mmins = make([]float32, N)
mmaxs = make([]float32, N)
// generate random sample values in [0.0, 1.0)
rand.Seed(123)
for i := 0; i < N; i++ {
values[i] = rand.Float32()
}
/*
// display reference results
fmt.Printf("moving minmax, reference code (offline), W = %d:\n", W)
MovingMinMax_offline_a(W)
for i := 0; i < N; i++ {
fmt.Printf("value[%02d] %.3f : min %.3f, max %.3f\n",
i, values[i], mmins[i], mmaxs[i])
}
*/
}
//// tests /////////////////////////////////////////////////////////////////////
func TestMovingMinMax(t *testing.T) {
// test with various window widths
for w := uint(1); w < N; w++ {
minmax := NewMovingMinMax(w)
MovingMinMax_offline(int(w))
for i := range values[:len(values)-1] {
minmax.Update(values[i])
min := minmax.Min()
if min != mmins[i] {
t.Errorf("W %d: values[%d] Min() got: %.3f, want: %.3f",
w, i, min, mmins[i])
}
max := minmax.Max()
if max != mmaxs[i] {
t.Errorf("W %d: values[%d] Max() got: %.3f, want: %.3f",
w, i, max, mmaxs[i])
}
}
}
}
func TestMovingMinMax0(t *testing.T) {
// test with various window widths
for w := uint(1); w < N; w++ {
minmax := NewMovingMinMax0(w)
MovingMinMax_offline(int(w))
for i := range values[:len(values)-1] {
minmax.Update(values[i])
min := minmax.Min()
if min != mmins[i] {
t.Errorf("W %d: values[%d] Min() got: %.3f, want: %.3f",
w, i, min, mmins[i])
}
max := minmax.Max()
if max != mmaxs[i] {
t.Errorf("W %d: values[%d] Max() got: %.3f, want: %.3f",
w, i, max, mmaxs[i])
}
}
}
}
func TestMovingMin(t *testing.T) {
// test with various window widths
for w := uint(1); w < N; w++ {
mmin := NewMovingMin(w)
MovingMinMax_offline(int(w))
for i := range values[:len(values)-1] {
mmin.Update(values[i])
min := mmin.Min()
if min != mmins[i] {
t.Errorf("values[%d] Min() got: %.3f, want: %.3f",
i, min, mmins[i])
}
}
}
}
func TestMovingMax(t *testing.T) {
// test with various window widths
for w := uint(1); w < N; w++ {
mmax := NewMovingMax(w)
MovingMinMax_offline(int(w))
for i := range values[:len(values)-1] {
mmax.Update(values[i])
max := mmax.Max()
if max != mmaxs[i] {
t.Errorf("values[%d] Max() got: %.3f, want: %.3f",
i, max, mmaxs[i])
}
}
}
}
func TestMovingMean(t *testing.T) {
mmean := NewMovingMean(W)
for i := range values {
mmean.Update(values[i])
/*
fmt.Printf("values[%2d] %.3f: mean %.3f\n",
i, values[i], mmean.Mean())
*/
}
}
func TestDequePushPop(t *testing.T) {
deque := newDeque_IV(10)
iv1 := _IV{1, 1.0}
iv2 := _IV{2, 2.0}
deque.PushFront(iv1.i, iv1.v)
deque.PushBack(iv2.i, iv2.v)
item := deque.PopFront()
if *item != iv1 {
t.Errorf("got: %v, want: %v", item, iv1)
}
item = deque.PopBack()
if *item != iv2 {
t.Errorf("got: %v, want: %v", item, iv2)
}
}
func TestDequeFrontBackItem(t *testing.T) {
deque := newDeque_IV(10)
iv1 := _IV{1, 1.0}
iv2 := _IV{2, 2.0}
deque.PushFront(iv1.i, iv1.v)
deque.PushBack(iv2.i, iv2.v)
front := deque.FrontItem()
if *front != iv1 {
t.Errorf("got: %v, want: %v", front, iv1)
}
back := deque.BackItem()
if *back != iv2 {
t.Errorf("got: %v, want: %v", back, iv2)
}
}
//// benchmarks ////////////////////////////////////////////////////////////////
func BenchmarkReference(b *testing.B) {
for i := 0; i < b.N; i++ {
MovingMinMax_offline(W)
}
}
func BenchmarkMovingMinMax0(b *testing.B) {
minmax := NewMovingMinMax0(W)
for j := 0; j < b.N; j++ {
for i := range values {
minmax.Update(values[i])
}
}
}
func BenchmarkMovingMinMax(b *testing.B) {
minmax := NewMovingMinMax(W)
for j := 0; j < b.N; j++ {
for i := range values {
minmax.Update(values[i])
}
}
}
func BenchmarkMovingMin(b *testing.B) {
mmin := NewMovingMin(W)
for j := 0; j < b.N; j++ {
for i := range values {
mmin.Update(values[i])
}
}
}
func BenchmarkMovingMax(b *testing.B) {
mmax := NewMovingMax(W)
for j := 0; j < b.N; j++ {
for i := range values {
mmax.Update(values[i])
}
}
}
func BenchmarkMovingMean(b *testing.B) {
mmean := NewMovingMean(W)
for j := 0; j < b.N; j++ {
for i := range values {
mmean.Update(values[i])
}
}
}
//// reference code ////////////////////////////////////////////////////////////
func MovingMinMax_offline(w int) {
U := deque_int.New() // upper -> indices of maxima
L := deque_int.New() // lower -> indices of minima
// initial minimum and maximum
mmins[0] = values[0]
mmaxs[0] = values[0]
for i := 1; i < len(values); i++ {
if i < w {
// 'absolute' minimum and maximum
if values[i] < mmins[i-1] {
mmins[i] = values[i]
} else {
mmins[i] = mmins[i-1]
}
if values[i] > mmaxs[i-1] {
mmaxs[i] = values[i]
} else {
mmaxs[i] = mmaxs[i-1]
}
} else {
// 'moving' minimum and maximum
mmins[i-1] = values[iExtreme(L, i)]
mmaxs[i-1] = values[iExtreme(U, i)]
}
// update monotonic wedge
if values[i] > values[i-1] {
L.PushBack(i - 1)
if i == w+L.FrontItem() {
L.PopFront()
}
for U.Size() > 0 {
if values[i] <= values[U.BackItem()] {
if i == w+U.FrontItem() {
U.PopFront()
}
break
}
U.PopBack()
}
} else {
U.PushBack(i - 1)
if i == w+U.FrontItem() {
U.PopFront()
}
for L.Size() > 0 {
if values[i] >= values[L.BackItem()] {
if i == w+L.FrontItem() {
L.PopFront()
}
break
}
L.PopBack()
}
}
}
// final minimum and maximum
i := len(values) - 1
mmins[i] = values[iExtreme(L, i)]
mmaxs[i] = values[iExtreme(U, i)]
}
func iExtreme(d *deque_int.Deque, i int) int {
if d.Size() > 0 {
return d.FrontItem()
} else {
return i - 1
}
}