-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cc
306 lines (231 loc) · 7.95 KB
/
test.cc
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
304
305
// test.cc
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <map>
#include "test.h"
#include "Vose.h"
#include "VoseTree.h"
bool test::test1() {
std::cerr << "\nAdd several marbles, return correct weight and size.\n";
Vose vose;
std::cerr << "\tAdd m('red',5): ";
double red_score = vose.add("red", 5);
std::cerr << red_score << "\n";
assert(red_score == 1);
std::cerr << "\tAdd m('blue',10): ";
std::cerr << vose.add("blue", 10) << "\n";
std::cerr << "\tAdd m('yellow',20): ";
double yellow_score = vose.add("yellow", 20);
std::cerr << yellow_score << "\n";
assert(yellow_score == 20.0/(5+10+20));
return true;
}
bool test::test2() {
std::cerr << "Check that the vose data structure comes close to actual values.\n";
Vose vose;
vose.add("red", 5);
vose.add("blue", 10);
vose.add("yellow", 20);
vose.init();
std::cerr << "Vose Dist: ";
for (size_t i = 0; i != vose.scores.size(); ++i) {
std::cerr << "\t" << i << "-->" << vose.scores[i].weight << "\n";
}
std::cerr << "\n";
for (size_t i = 0; i != vose.probabilities().size(); ++i) {
std::cerr << "\t" << i << "-->" << vose.probabilities()[i] << "\n";
}
std::cerr << "\n";
std::cerr << "Create a histogram by sampling the vose structure.\n";
std::map<size_t,int> histogram;
auto hist_sum = 0L;
for (int i = 0; i != 50000; ++i) {
size_t idx = vose.rand();
if (histogram.find(idx) != histogram.end()) {
histogram[idx] += 1;
hist_sum += 1L;
}
else {
histogram.insert(std::make_pair(idx, 1));
hist_sum += 1L;
}
}
std::cerr << "Sampled Distribution:\n";
// Create the new dist
std::vector<double> new_dist;
for (auto& h : histogram) {
std::cerr << "\t" << h.first << "-->" << (double)h.second/hist_sum << "\n";
new_dist.push_back((double)h.second/hist_sum);
}
double kl_result = Vose::kl(vose.probabilities(), new_dist);
std::cerr << "kl(probabilities, new_dist): " << kl_result << "\n";
std::cerr << "kl(new_dist, probabilities): " << Vose::kl(new_dist, vose.probabilities()) << "\n";
return true;
}
bool test::test3() {
// Create a VoseTree structure
VoseTree tree;
tree.set_max_marbles(4);
// Create new marbles and add them to the Vose tree
std::cerr << tree.toString();
tree.add("red", 5L);
std::cerr << tree.toString();
tree.add("blue", 10L);
std::cerr << tree.toString();
tree.add("yellow", 20L);
std::cerr << tree.toString();
tree.add("red", 5L);
std::cerr << tree.toString();
tree.add("blue", 10L);
std::cerr << tree.toString();
tree.add("yellow", 20L);
std::cerr << tree.toString();
// Sample from the tree with and save the results to a histogram
std::map<std::string,double> histogram;
long hist_sum = 0L;
for (int i = 0; i != 50000; ++i) {
auto idx = tree.rand_marble();
if (histogram.find(idx.label) != histogram.end()) {
histogram[idx.label] += 1.0;
hist_sum += 1L;
}
else {
histogram.insert(std::make_pair(idx.label, 1.0));
hist_sum += 1L;
}
}
std::map<std::string,double> new_hist;
std::for_each(histogram.cbegin(), histogram.cend(),
[&new_hist,hist_sum] (std::pair<const std::string, double> a) {
//std::cerr << a.first << ", " << (double)a.second/hist_sum << "\n";
new_hist.insert( std::make_pair(a.first,(double)a.second/hist_sum));
});
std::map<std::string, double> exact_prob = tree.probabilities();
// Compare the results with kl divergence
double kl1 = VoseTree::kl(new_hist, exact_prob);
double kl2 = VoseTree::kl(exact_prob, new_hist);
std::cerr << "kl(histogram, exact_prob): " << kl1 << "\n";
std::cerr << "kl(exact_prob, histogram): " << kl2 << "\n";
return true;
}
bool test::test4() {
VoseTree tree;
tree.set_max_marbles(4);
tree.set_max_bags(5);
std::cerr << tree.toString();
for(size_t i = 0; i != 10; ++i) {
tree.add("red", 5L);
std::cerr << tree.toString();
tree.add("blue", 10L);
std::cerr << tree.toString();
tree.add("yellow", 20L);
std::cerr << tree.toString();
}
return true;
}
bool test::test5() {
VoseTree tree;
tree.set_max_marbles(4);
tree.set_max_bags(5);
// Generate a set of label names.
std::string labels[7] = {"red", "yellow", "pink", "green", "purple",
"orange", "blue"};
// Make a random number generator for marble labels
std::default_random_engine label_generator(VoseUtil::RANDOM_SEED);
std::uniform_int_distribution<size_t>
label_distribution(0, 7-1);
std::function<size_t()> random_label =
std::bind(label_distribution, label_generator);
// Make a random number generator for marble weights
std::default_random_engine weight_generator(VoseUtil::RANDOM_SEED);
std::uniform_int_distribution<long>
weight_distribution(1, 1000);
std::function<long()> random_weight =
std::bind(weight_distribution, weight_generator);
for(size_t i = 0; i != 50; ++i) {
// Get a random marble with random label and random weight.
size_t label_idx = random_label();
size_t weight = random_weight();
//std::cerr << "label_idx: " << label_idx << ", " << labels[label_idx] << "\n";
//std::cerr << " weight: " << weight << "\n";
tree.add(std::string(labels[label_idx]), weight);
std::cerr << tree.toString();
}
// Generate a histogram.
auto hist = VoseTree::histogram(tree, 50000L);
// Get the real probabilities
auto prob = tree.probabilities();
// Compare the results
double kl1 = VoseTree::kl(hist, prob);
double kl2 = VoseTree::kl(prob, hist);
std::cerr << "kl(hist, prob): " << kl1 << "\n";
std::cerr << "kl(prob, hist): " << kl2 << "\n";
return true;
}
bool test::test6() {
VoseTree tree;
tree.set_max_marbles(4);
tree.set_max_bags(5);
// Generate a set of label names.
std::string labels = "red yellow blue";
// Make a random number generator for marble weights
std::default_random_engine weight_generator(VoseUtil::RANDOM_SEED);
std::uniform_int_distribution<long>
weight_distribution(1, 1000);
std::function<long()> random_weight =
std::bind(weight_distribution, weight_generator);
// FIXME why does 280 node work? Where is the error, need more stats
for(size_t i = 0; i != 279; ++i) {
// Get a random marble with random label and random weight.
size_t weight = random_weight();
//std::cerr << "label_idx: " << label_idx << ", " << labels[label_idx] << "\n";
//std::cerr << " weight: " << weight << "\n";
std::random_shuffle(labels.begin(), labels.end());
tree.add(labels, weight);
//std::cerr << tree.toString();
}
// Generate a histogram.
auto hist = VoseTree::histogram(tree, 50000L);
// Get the real probabilities
auto prob = tree.probabilities();
// Compare the results
double kl1 = VoseTree::kl(hist, prob);
double kl2 = VoseTree::kl(prob, hist);
std::cerr << "kl(hist, prob): " << kl1 << "\n";
std::cerr << "kl(prob, hist): " << kl2 << "\n";
return true;
}
bool test::test7() {
std::cerr << "Need to make sure each branch is being selected.\n";
VoseTree tree;
tree.set_max_marbles(0);
tree.set_max_bags(4);
tree.add("red", 5);
tree.add("blue", 5);
tree.add("yellow", 5);
std::cerr << tree.toString();
for( size_t i = 0; i != 6; ++i) {
std::cerr << ">>> " << tree.rand_marble().label << "\n";
}
return true;
}
int main(int argc, char **argv) {
test t;
std::cout << "\nTest 1" << "----------------------------------------\n";
t.test1();
std::cout << "\nTest 2" << "----------------------------------------\n";
t.test2();
std::cout << "\nTest 3" << "----------------------------------------\n";
t.test3();
std::cout << "\nTest 4" << "----------------------------------------\n";
t.test4();
std::cout << "\nTest 5" << "----------------------------------------\n";
t.test5();
std::cout << "\nTest 6" << "----------------------------------------\n";
t.test6();
std::cout << "\nTest 7" << "----------------------------------------\n";
t.test7();
return 0;
}