-
Notifications
You must be signed in to change notification settings - Fork 8
/
params_test.go
172 lines (164 loc) · 2.76 KB
/
params_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
package bayesopt
import "testing"
func TestParams(t *testing.T) {
t.Parallel()
cases := []struct {
p Param
name string
max, min float64
sampleMax float64
}{
{
p: UniformParam{
Name: "uniform",
Max: 10,
Min: 1,
},
name: "uniform",
max: 10,
min: 1,
},
{
p: NormalParam{
Name: "normal",
Max: 10,
Min: -10,
Mean: 0,
StdDev: 10,
},
name: "normal",
max: 10,
min: -10,
},
{
p: NormalParam{
Name: "normal",
Max: 10,
Min: 0,
Mean: 1,
StdDev: 5,
},
name: "normal",
max: 10,
min: 0,
},
{
p: ExponentialParam{
Name: "exponential",
Max: 100,
Min: 10,
Rate: 10,
},
name: "exponential",
max: 100,
min: 10,
},
{
p: RejectionParam{
Param: UniformParam{
Name: "rejection uniform",
Max: 100,
Min: 10,
},
F: func(x float64) float64 {
if x > 50 {
return 0
}
return 1
},
},
name: "rejection uniform",
max: 100,
min: 10,
sampleMax: 50,
},
}
for i, c := range cases {
{
out := c.p.GetName()
want := c.name
if out != want {
t.Errorf("%d. %+v.GetName() = %q; wanted %q", i, c.p, out, want)
}
}
{
out := c.p.GetMax()
want := c.max
if out != want {
t.Errorf("%d. %+v.GetMax() = %v; wanted %v", i, c.p, out, want)
}
}
{
out := c.p.GetMin()
want := c.min
if out != want {
t.Errorf("%d. %+v.GetMin() = %v; wanted %v", i, c.p, out, want)
}
}
for j := 0; j < 1000; j++ {
sample := c.p.Sample()
if sample < c.min || sample > c.max {
t.Errorf("%d. %+v.Sample() = %v; outside bounds", i, c.p, sample)
}
if c.sampleMax != 0 && sample > c.sampleMax {
t.Errorf("%d. %+v.Sample() = %v; outside sample max %f", i, c.p, sample, c.sampleMax)
}
}
}
}
func TestTruncateSample(t *testing.T) {
t.Parallel()
var count int
cases := []struct {
p Param
f func() float64
want float64
count int
}{
{
p: UniformParam{"", 10, 5},
f: func() float64 {
return 0
},
want: 5,
count: 1000,
},
{
p: UniformParam{"", 10, 5},
f: func() float64 {
return 100
},
want: 10,
count: 1000,
},
{
p: UniformParam{"", 10, 5},
f: func() float64 {
return 7
},
want: 7,
count: 1,
},
{
p: UniformParam{"", 10, 5},
f: func() float64 {
return float64(count)
},
want: 5,
count: 5,
},
}
for i, c := range cases {
count = 0
out := truncateSample(c.p, func() float64 {
count++
return c.f()
})
if out != c.want {
t.Errorf("%d. truncateSample(%+v, ...) = %f; not %f", i, c.p, out, c.want)
}
if count != c.count {
t.Errorf("%d. count = %d; not %d", i, count, c.count)
}
}
}