-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtests.js
254 lines (175 loc) · 5.33 KB
/
tests.js
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
var testCase = require('nodeunit').testCase
, Bandit = require('./bayesian-bandit.js').Bandit
module.exports = testCase({
'Arm': testCase({
'reward should increase the count': function(test) {
var arm = new Bandit.Arm()
arm.reward()
arm.reward()
test.equal(arm.count, 2)
test.done()
},
'reward should increase the sum': function(test) {
var arm = new Bandit.Arm()
arm.reward(0)
arm.reward(1)
arm.reward(1)
test.equal(arm.sum, 2)
test.done()
},
'rewardMultiple should increase the sum and count': function(test) {
var arm = new Bandit.Arm()
arm.rewardMultiple(10, 2)
arm.rewardMultiple(15, 5)
test.equal(arm.count, 25)
test.equal(arm.sum, 7)
test.done()
},
'sample should sample from the rbeta distribution': function(test) {
var arm = new Bandit.Arm()
arm.rbeta = function() { return 33 }
var sample = arm.sample()
test.equal(sample, 33)
test.done()
},
'sample should pass 1+sum as the 1st rbeta param': function(test) {
var arm = new Bandit.Arm()
, passed
arm.rbeta = function() { passed = arguments }
arm.sum = 33
arm.sample()
test.equal(passed[0], 34) // 1 + 33
test.done()
},
'sample should pass 1+ct-sum as the 2nd rbeta param': function(test) {
var arm = new Bandit.Arm()
, passed
arm.rbeta = function() { passed = arguments }
arm.count = 15
arm.sum = 10
arm.sample()
test.equal(passed[1], 6) // 1 + 15 - 10
test.done()
},
'rbeta': testCase({
'a=11, b=16, two random draws': function(test) {
var sample = rbetaTest({
a: 11,
b: 16,
fakeRandomNumbers: [0.1, 0.2]
})
test.equal(sample, 0.56)
test.done()
},
'a=1, b=1, two random draws': function(test) {
var sample = rbetaTest({
a: 1,
b: 1,
fakeRandomNumbers: [0.1, 0.2]
})
test.equal(sample, 0.9)
test.done()
},
'a=10, b=10, two random draws': function(test) {
var sample = rbetaTest({
a: 10,
b: 10,
fakeRandomNumbers: [0.1, 0.2]
})
test.equal(sample, 0.67)
test.done()
},
'a=10, b=10, four random draws': function(test) {
var sample = rbetaTest({
a: 10,
b: 10,
fakeRandomNumbers: [0.99, 0.99, 0.1, 0.1]
})
test.equal(sample, 0.67)
test.done()
}
}),
}),
'Bandit': testCase({
'new Bandit should create the specified number of arms': function(test) {
var bandit = new Bandit({ numberOfArms: 3 })
test.equal(bandit.arms.length, 3)
test.done()
},
'when nothing is passed new Bandit should create 0 arms': function(test) {
var bandit = new Bandit()
test.equal(bandit.arms.length, 0)
test.done()
},
'new Bandit with options.arms should initialized arms properly':
function(test) {
var bandit = new Bandit({
arms:[
{count: 100, sum: 20},
{count: 50, sum: 30},
{count: 70, sum: 10}
]
})
test.equal(bandit.arms.length, 3)
test.equal(bandit.arms[0].count, 100)
test.equal(bandit.arms[0].sum, 20)
test.equal(bandit.arms[1].count, 50)
test.equal(bandit.arms[1].sum, 30)
test.equal(bandit.arms[2].count, 70)
test.equal(bandit.arms[2].sum, 10)
test.done()
},
'selectArm should return the arm with the largest sample': function(test) {
var bandit = new Bandit({ numberOfArms: 3 })
bandit.arms[0].sample = function() { return 0.2; }
bandit.arms[1].sample = function() { return 0.9; }
bandit.arms[2].sample = function() { return 0.3; }
var selected = bandit.selectArm()
test.equal(selected, 1)
test.done()
},
'should explore all arms': function(test) {
var bandit = new Bandit({ numberOfArms: 2 })
bandit.arms[0].probability = 0.3
bandit.arms[0].probability = 0.8
pullArms({ bandit: bandit, times: 100 })
test.ok(bandit.arms[0].count > 1)
test.ok(bandit.arms[1].count > 1)
test.done()
},
'should converge on the optimal arm': function(test) {
var bandit = new Bandit({ numberOfArms: 2 })
bandit.arms[0].probability = 0.1
bandit.arms[1].probability = 0.9
pullArms({ bandit: bandit, times: 100 })
test.ok(bandit.arms[0].count < 10)
test.ok(bandit.arms[1].count > 90)
test.done()
}
})
})
function fakeRandom(numbers) {
return function getNextFakeRandomNumber() {
if(!numbers.length) {
console.log('unexpected call to getNextFakeRandomNumber')
throw 'unexpected call to getNextFakeRandomNumber'
}
return numbers.shift()
}
}
function rbetaTest(options) {
var arm = new Bandit.Arm()
arm.random = fakeRandom(options.fakeRandomNumbers)
var sample = arm.rbeta(options.a, options.b)
, rounded = Math.round(sample * 100) / 100
return rounded
}
function pullArms(options) {
var bandit = options.bandit
, times = options.times
for(var i = 0; i < times; i++) {
var selected = bandit.selectArm()
, arm = bandit.arms[selected]
arm.reward(Math.random() < arm.probability ? 1 : 0)
}
}