/
weighted_test.go
135 lines (108 loc) · 2.67 KB
/
weighted_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
package statea
import (
"testing"
"fmt"
"math"
"math/rand"
"time"
)
func Test_WeigthedSample_Update(t *testing.T) {
s := NewWeigthedSample(10)
if s.Count != 0 {
t.Fatalf("Uniform Sample count not zero. (got %d)", s.Count)
}
for i := 1; i < 21; i++ {
s.Update(1, 1)
if s.Count != i {
t.Fatalf("Uniform Sample count not %d. (got %d)", i, s.Count)
}
}
for i := 0; i < 10; i++ {
if s.Sample()[i] != 1 {
t.Fatalf("Uniform Sample at %d not 1. (got %d)", i, s.Count)
}
}
}
/* Use Kolmogorov-Smirnov test to validate that the sample is random */
func Test_WeigthedSample_Kolmogorov(t *testing.T) {
end := 1000
cdf := func(x float64) float64 {
px := x / float64(end)
return px * px
}
s := NewWeigthedSample(100)
for i := 0; i < end; i++ {
s.Update(float64(i), float64(i))
m := s.pq[0].priority
for n := range s.pq {
if s.pq[n].priority < m {
// we depend on this to do peek
t.Errorf("Minimum not in first position.")
}
}
}
sample := s.Sample()
D, pvalue := KolmogorovTest(sample, cdf)
if pvalue < 0.005 {
t.Errorf("KolmogorovTest(sample) == %f, D == %f", pvalue, D)
}
}
func Test_WeigthedSample_Rescale_Kolmogorov(t *testing.T) {
end := 1000
cdf := func(x float64) float64 {
px := x / float64(end)
return px * px
}
s := NewWeigthedSample(100)
for i := 0; i < 500; i++ {
s.Update(float64(i), float64(i))
}
s.Rescale(func(weight float64) float64 {
return weight * 2
})
for i := 0; i < 500; i++ {
s.Update(500.0 + float64(i), 2 * (500 + float64(i)))
}
sample := s.Sample()
D, pvalue := KolmogorovTest(sample, cdf)
if pvalue < 0.005 {
t.Errorf("KolmogorovTest(sample) == %f, D == %f", pvalue, D)
}
}
func Benchmark_WeigthedSample(t *testing.B) {
end := 1000000
s := NewWeigthedSample(1024)
for i := 0; i < end; i++ {
s.Update(float64(i), float64(i))
}
}
func Test_ExponentiallyDecayingSample_Kolmogorov(t *testing.T) {
rand.Seed(time.Now().UnixNano())
end := 10000000
scale := 100000.0
s := NewExponentiallyDecayingSample(1024, 0.3)
s.last_t = 0
for i := 0; i < end; i++ {
s.Update(float64(i)/scale, float64(i)/scale)
}
cdf := func(x float64) float64 {
w := math.Exp(0.3 * (x - s.last_t))
top_w := math.Exp(0.3 * (float64(end-1)/scale - s.last_t))
return w / top_w
}
sample := s.Sample()
D, pvalue := KolmogorovTest(sample, cdf)
if pvalue < 0.005 {
t.Errorf("KolmogorovTest(sample) == %f, D == %f", pvalue, D)
}
}
func Benchmark_ExponentiallyDecayingSample(t *testing.B) {
end := 100000
now := Now()
s := NewExponentiallyDecayingSample(1024, 0.3)
start := Now()
for i := 0; i < end; i++ {
s.Update(float64(i), start + float64(i) / 10000000)
}
fmt.Printf("duration: %f\n", Now() - now)
}