This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaggregator_test.go
189 lines (149 loc) · 4.52 KB
/
aggregator_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
package netmap
import (
"math"
"math/rand"
"sort"
"testing"
"github.com/stretchr/testify/require"
)
const (
eps float64 = 0.001
)
func initTestBucket(t *testing.T, b *Bucket) {
require.Nil(t, b.AddBucket("/opt:first", Nodes{{0, 1, 2}, {2, 3, 2}}))
require.Nil(t, b.AddBucket("/opt:second/sub:1", Nodes{{1, 2, 3}, {10, 6, 1}}))
b.fillNodes()
}
func TestNewWeightFunc(t *testing.T) {
var b Bucket
initTestBucket(t, &b)
meanCap := b.Traverse(new(meanAgg), CapWeightFunc).Compute()
capNorm := NewSigmoidNorm(meanCap)
minPrice := b.Traverse(new(minAgg), PriceWeightFunc).Compute()
priceNorm := NewReverseMinNorm(minPrice)
wf := NewWeightFunc(capNorm, priceNorm)
nodes := make(Nodes, len(b.nodes))
copy(nodes, b.nodes)
expected := Nodes{
{10, 6, 1},
{2, 3, 2},
{1, 2, 3},
{0, 1, 2},
}
sort.Slice(nodes, func(i, j int) bool { return wf(nodes[i]) > wf(nodes[j]) })
require.Equal(t, expected, nodes)
}
func TestAggregator_Compute(t *testing.T) {
var (
b Bucket
a Aggregator
)
initTestBucket(t, &b)
a = NewMeanAgg()
b.Traverse(a, CapWeightFunc)
require.InEpsilon(t, 3.0, a.Compute(), eps)
a = NewMeanSumAgg()
b.Traverse(a, CapWeightFunc)
require.InEpsilon(t, 3.0, a.Compute(), eps)
a = NewMinAgg()
b.Traverse(a, PriceWeightFunc)
require.InEpsilon(t, 1.0, a.Compute(), eps)
a = NewMaxAgg()
b.Traverse(a, PriceWeightFunc)
require.InEpsilon(t, 3.0, a.Compute(), eps)
a = NewMeanIQRAgg()
b.Traverse(a, PriceWeightFunc)
require.InEpsilon(t, 2.0, a.Compute(), eps)
mp := new(meanIQRAgg)
nodes := []Node{{P: 1}, {P: 1}, {P: 10}, {P: 3}, {P: 5}, {P: 5}, {P: 1}, {P: 100}}
for i := range nodes {
mp.Add(PriceWeightFunc(nodes[i]))
}
mp.k = 0.5
require.InEpsilon(t, 2.666, mp.Compute(), eps)
mp.k = 1.5
require.InEpsilon(t, 3.714, mp.Compute(), eps)
mp.k = 23.0
require.InEpsilon(t, 3.714, mp.Compute(), eps)
mp.k = 24.0
require.InEpsilon(t, 15.75, mp.Compute(), eps)
mp.Clear()
mp.Add(1)
mp.Add(101)
require.InEpsilon(t, 51.0, mp.Compute(), eps)
}
func TestSigmoidNorm_Normalize(t *testing.T) {
t.Run("sigmoid norm must equal to 1/2 at `scale`", func(t *testing.T) {
norm := NewSigmoidNorm(1)
require.InEpsilon(t, 0.5, norm.Normalize(1), eps)
norm = NewSigmoidNorm(10)
require.InEpsilon(t, 0.5, norm.Normalize(10), eps)
})
t.Run("sigmoid norm must be less than 1", func(t *testing.T) {
norm := NewSigmoidNorm(2)
require.True(t, norm.Normalize(100) < 1)
require.True(t, norm.Normalize(math.MaxFloat64) <= 1)
})
t.Run("sigmoid norm must be monotonic", func(t *testing.T) {
norm := NewSigmoidNorm(5)
for i := 0; i < 5; i++ {
a, b := rand.Float64(), rand.Float64()
if b < a {
a, b = b, a
}
require.True(t, norm.Normalize(a) <= norm.Normalize(b))
}
})
}
func TestReverseMinNorm_Normalize(t *testing.T) {
t.Run("reverseMin norm should not panic", func(t *testing.T) {
norm := NewReverseMinNorm(0)
require.NotPanics(t, func() { norm.Normalize(0) })
norm = NewReverseMinNorm(1)
require.NotPanics(t, func() { norm.Normalize(0) })
})
t.Run("reverseMin norm should equal 1 at min value", func(t *testing.T) {
norm := NewReverseMinNorm(10)
require.InEpsilon(t, 1.0, norm.Normalize(10), eps)
})
}
func TestMaxNorm_Normalize(t *testing.T) {
t.Run("max norm should not panic", func(t *testing.T) {
norm := NewMaxNorm(0)
require.NotPanics(t, func() { norm.Normalize(1) })
norm = NewMaxNorm(1)
require.NotPanics(t, func() { norm.Normalize(0) })
})
t.Run("max norm should equal 1 at max value", func(t *testing.T) {
norm := NewMaxNorm(10)
require.InEpsilon(t, 1.0, norm.Normalize(10), eps)
})
}
func TestBucket_TraverseTree(t *testing.T) {
var (
meanAF = AggregatorFactory{New: func() Aggregator { return new(meanAgg) }}
minAF = AggregatorFactory{New: func() Aggregator { return new(minAgg) }}
)
b := &Bucket{
children: []Bucket{
{nodes: Nodes{{0, 1, 2}, {2, 3, 2}}},
{
children: []Bucket{
{nodes: Nodes{{1, 2, 3}, {10, 6, 1}}},
{nodes: Nodes{{12, 3, 4}, {2, 3, 4}}},
},
},
},
}
b.fillNodes()
b.TraverseTree(meanAF, CapWeightFunc)
require.InEpsilon(t, 2, b.children[0].weight, eps)
require.InEpsilon(t, 3.5, b.children[1].weight, eps)
require.InEpsilon(t, 4, b.children[1].children[0].weight, eps)
require.InEpsilon(t, 3, b.children[1].children[1].weight, eps)
b.TraverseTree(minAF, PriceWeightFunc)
require.InEpsilon(t, 2, b.children[0].weight, eps)
require.InEpsilon(t, 1, b.children[1].weight, eps)
require.InEpsilon(t, 1, b.children[1].children[0].weight, eps)
require.InEpsilon(t, 4, b.children[1].children[1].weight, eps)
}