-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoseTree.cc
230 lines (183 loc) · 5.92 KB
/
VoseTree.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
#include <iostream>
#include <map>
#include <sstream>
#include "VoseTree.h"
#include "VoseUtil.h"
double VoseTree::add(std::string label, long weight) {
struct marble m;
m.weight = weight;
m.label = label; // TODO update with a proper copy() method
return VoseTree::add(m);
}
double VoseTree::add(marble m) {
// Update the new weight
total_weight += m.weight;
double contribution = (double) m.weight / total_weight;
// Add the marble to another branch
if(!branches.empty()) {
double new_total = branches.back() + m.weight;
branches.push_back(new_total);
}
else {
branches.push_back(m.weight);
}
// Add this marble to the free marble list
vosetreenode vtn;
vtn.isBag = false;
vtn.ball = m;
nodes.push_back(vtn);
// Shrink data structure if necessary
if (branches.size() - last_bag > max_marbles) {
// Compress the marbles to make a new bag
Vose v;
for(size_t i = last_bag; i != nodes.size(); ++i) {
v.add(nodes[i].ball);
}
v.init();
vosetreenode vtn2 (v);
nodes[last_bag] = vtn2;
nodes.resize(last_bag+1);
branches.resize(last_bag+1);
branches[last_bag] = total_weight;
++last_bag;
if (last_bag > max_bags) {
// Merge all the bags into one bag
Vose z;
z = merge_bags();
nodes.resize(1);
nodes[0] = z;
// Update the branch and bag locations
last_bag = 1;
total_weight = z.weight();
branches.resize(1);
branches[0] = total_weight;
}
}
// Update a random number generator to new range
std::default_random_engine generator(VoseUtil::RANDOM_SEED);
std::uniform_real_distribution<double>
real_distribution(0, total_weight);
random_branch = std::bind(real_distribution, generator);
return contribution;
}
marble VoseTree::rand_marble() {
// Select a random value in the range of [0, totalsize)
double rand_sz = random_branch();
// Binary search to find correct branch O(log n), n = branches.size()
//std::vector<double>::iterator branch
auto branch_idx
= std::lower_bound (branches.begin(), branches.end(), rand_sz);
size_t branch = branch_idx - branches.begin();
if (nodes[branch].isBag) {
// If a Vose class is at the node, we call rand() on the object.
return nodes[branch].bag.rand_marble(); // O(1)
}
else {
// If a marble is at the branch, return the marble
return nodes[branch].ball; // O(1)
}
}
Vose VoseTree::merge_bags() {
Vose v;
size_t i = 0;
for(; i != nodes.size(); ++i) {
if (!nodes[i].isBag) break;
v = v.merge(nodes[i].bag);
}
nodes.erase(nodes.begin(), nodes.begin()+i-1); // -1 because we have to replace it
nodes[0] = vosetreenode(v);
branches.erase(branches.begin(), branches.begin()+i-1);
branches[0] = v.weight();
v.init();
return v;
}
void VoseTree::init() {
// Init all the Vose bags
for (size_t i = 0; i != nodes.size(); ++i) {
if (!nodes[i].isBag) break;
nodes[i].bag.init();
}
}
std::string VoseTree::toString() {
std::stringstream ss;
ss << "[" << total_weight << "]\n";
std::for_each (nodes.cbegin(), nodes.cend(), [this,&ss] (vosetreenode v) {
ss << "----> " << v.toString() << "\n";
});
ss << "\n";
return ss.str();
}
std::map<std::string, long> VoseTree::distribution() {
std::map<std::string, long> map;
std::for_each(nodes.cbegin(), nodes.cend(), [&map] (vosetreenode vtn) {
if(vtn.isBag) {
for (auto m : vtn.bag.get_scores()) {
if(map.find(m.label) == map.end()) {
map.insert(std::pair<std::string, long>(m.label, m.weight));
}
else {
map[m.label] += m.weight;
}
}
}
else {
if(map.find(vtn.ball.label) == map.end()) {
map.insert(std::pair<std::string, long>(vtn.ball.label, vtn.ball.weight));
}
else {
map[vtn.ball.label] += vtn.ball.weight;
}
}
});
return map;
}
std::map<std::string, double> VoseTree::probabilities() {
auto h = distribution();
std::map<std::string, double> prob;
//map.resize(h.size());
std::for_each(h.begin(), h.end(), [&] (std::pair<std::string, long> m) {
//std::cerr << "p|" << m.first << "," << (double)m.second/total_weight << "\n";
prob.insert(std::make_pair(m.first, (double)m.second/total_weight));
});
return prob;
}
double VoseTree::kl(const std::map<std::string, double> &a, const std::map<std::string, double> &b) {
/*std::cerr<< "a: ";
std::for_each(a.begin(), a.end(), [] (std::pair<const std::string, double> z) {
std::cerr << "<" << z.first << "," << z.second << ">";
});
std::cerr << "\nb: ";
std::for_each(b.begin(), b.end(), [] (std::pair<const std::string, double> z) {
std::cerr << "<" << z.first << "," << z.second << ">";
});*/
std::vector<double> v;
v.resize(a.size());
std::transform(a.cbegin(), a.cend(), b.cbegin(), v.begin(), [&v] (std::pair<const std::string, double> a, std::pair<const std::string, double> b) {
//printf("a:%f, b:%f, log(a/b):%f\n", a.second, b.second, log(a.second/b.second));
return (double)log(a.second/b.second)*a.second;
});
return std::accumulate(v.cbegin(), v.cend(), 0.0, std::plus<double>());
}
std::map<std::string, double>
VoseTree::histogram(VoseTree tree, long samples) {
std::map<std::string,double> histogram;
long hist_sum = 0L;
for (int i = 0; i != samples; ++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));
});
return new_hist;
}