-
Notifications
You must be signed in to change notification settings - Fork 0
/
BayesNet.cpp
397 lines (334 loc) · 12.6 KB
/
BayesNet.cpp
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <cmath>
#include <sstream>
#include <set>
#include "BayesNet.hpp"
void CPT0::buildTable(const vector<Instance*>& instances) {
int rangeY = (int)table.size();
vector<int> YOccurance;
YOccurance.resize(rangeY);
for (int i = 0; i < instances.size(); ++i) {
Instance* inst = instances[i];
int valY = (int)round(inst->classLabel);
YOccurance[valY]++;
}
int total = (int)instances.size();
for (int valY = 0; valY < rangeY; ++valY) {
table[valY] = (YOccurance[valY] + 1.0) / (total + rangeY);
}
}
double CPT0::computeCondProb(const Instance* instance) const {
int valSelf = (int)round(instance->classLabel);
return table[valSelf];
}
string CPT0::toString() const {
stringstream ss;
ss << "CPT of attribute " << self << endl;
ss.setf(ios::fixed, ios::floatfield);
ss.precision(PRECISION);
for (int i = 0; i < table.size(); ++i)
ss << "Pr(" << self << " = " << i << ") = " << table[i] << endl;
return ss.str();
}
void CPT1::buildTable(const vector<Instance*>& instances) {
int rangeX = (int)table.size();
int rangeY = (int)table[0].size();
vector<int> YOccurance;
YOccurance.resize(rangeY);
vector<vector<int> > XYOccurance;
XYOccurance.resize(rangeX);
for (int valX = 0; valX < rangeX; ++valX)
XYOccurance[valX].resize(rangeY);
for (int i = 0; i < instances.size(); ++i) {
Instance* inst = instances[i];
int valX = (int)round(inst->featureVector[self]);
int valY = (int)round(inst->classLabel);
YOccurance[valY]++;
XYOccurance[valX][valY]++;
}
for (int valX = 0; valX < rangeX; ++valX)
for (int valY = 0; valY < rangeY; ++valY)
table[valX][valY] = (XYOccurance[valX][valY] + 1.0) / (YOccurance[valY] + rangeX);
}
double CPT1::computeCondProb(const Instance* instance) const {
int valSelf = (int)round(instance->featureVector[self]);
int valParent = (int)round(instance->classLabel);
return table[valSelf][valParent];
}
string CPT1::toString() const {
stringstream ss;
ss << "CPT of attribute " << self << endl;
ss.setf(ios::fixed, ios::floatfield);
ss.precision(PRECISION);
for (int j = 0; j < table[0].size(); ++j)
for (int i = 0; i < table.size(); ++i)
ss << "Pr(" << self << " = " << i << " | " << parents[0] << " = " << j << ") = " << table[i][j] << endl;
return ss.str();
}
void CPT2::buildTable(const vector<Instance*>& instances) {
int rangeX = (int)table.size();
int rangeZ = (int)table[0].size();
int rangeY = (int)table[0][0].size();
vector<vector<int> > ZYOccurance;
ZYOccurance.resize(rangeZ);
for (int valZ = 0; valZ < rangeZ; ++valZ)
ZYOccurance[valZ].resize(rangeY);
vector<vector<vector<int> > > XZYOccurance;
XZYOccurance.resize(rangeX);
for (int valX = 0; valX < rangeX; ++valX) {
XZYOccurance[valX].resize(rangeZ);
for (int valZ = 0; valZ < rangeZ; ++valZ)
XZYOccurance[valX][valZ].resize(rangeY);
}
for (int i = 0; i < instances.size(); ++i) {
Instance* inst = instances[i];
int valX = (int)round(inst->featureVector[self]);
int valZ = (int)round(inst->featureVector[parents[0]]);
int valY = (int)round(inst->classLabel);
ZYOccurance[valZ][valY]++;
XZYOccurance[valX][valZ][valY]++;
}
for (int valX = 0; valX < rangeX; ++valX)
for (int valZ = 0; valZ < rangeZ; ++valZ)
for (int valY = 0; valY < rangeY; ++valY)
table[valX][valZ][valY] = (XZYOccurance[valX][valZ][valY] + 1.0) / (ZYOccurance[valZ][valY] + rangeX);
}
double CPT2::computeCondProb(const Instance* instance) const {
int valSelf = (int)round(instance->featureVector[self]);
int valParent0 = (int)round(instance->featureVector[parents[0]]);
int valParent1 = (int)round(instance->classLabel);
return table[valSelf][valParent0][valParent1];
}
string CPT2::toString() const {
stringstream ss;
ss << "CPT of attribute " << self << endl;
ss.setf(ios::fixed, ios::floatfield);
ss.precision(PRECISION);
for (int k = 0; k < table[0][0].size(); ++k)
for (int j = 0; j < table[0].size(); ++j)
for (int i = 0; i < table.size(); ++i)
ss << "Pr(" << self << " = " << i << " | " << parents[0] << " = " << j << ", " <<
parents[1] << " = " << k << ") = " << table[i][j][k] << endl;
return ss.str();
}
BayesNet::BayesNet(const DatasetMetadata* metadata, const vector<Instance*>& instances, bool treeAugmented) :
metadata(metadata), instances(instances), treeAugmented(treeAugmented) {
if (treeAugmented) {
createMutualInfoTable();
createMaximalSpanningTree();
}
createBayesNet();
createProbabilityTables();
}
double BayesNet::computeMutualInfo(int featureIdxI, int featureIdxJ) const {
Feature* Y = metadata->classVariable;
Feature* Xi = metadata->featureList[featureIdxI];
Feature* Xj = metadata->featureList[featureIdxJ];
int rangeY = Y->getRange();
int rangeXi = Xi->getRange();
int rangeXj = Xj->getRange();
vector<int> YOccurance;
YOccurance.resize(rangeY);
vector<vector<int> > YXiOccurance;
YXiOccurance.resize(rangeY);
for (int valY = 0; valY < rangeY; ++valY)
YXiOccurance[valY].resize(rangeXi);
vector<vector<int> > YXjOccurance;
YXjOccurance.resize(rangeY);
for (int valY = 0; valY < rangeY; ++valY)
YXjOccurance[valY].resize(rangeXj);
vector<vector<vector<int> > > YXiXjOccurance;
YXiXjOccurance.resize(rangeY);
for (int valY = 0; valY < rangeY; ++valY) {
YXiXjOccurance[valY].resize(rangeXi);
for (int valXi = 0; valXi < rangeXi; ++valXi)
YXiXjOccurance[valY][valXi].resize(rangeXj);
}
for (int i = 0; i < instances.size(); ++i) {
Instance* inst = instances[i];
int valY = (int)round(inst->classLabel);
int valXi = (int)round(inst->featureVector[featureIdxI]);
int valXj = (int)round(inst->featureVector[featureIdxJ]);
YOccurance[valY]++;
YXiOccurance[valY][valXi]++;
YXjOccurance[valY][valXj]++;
YXiXjOccurance[valY][valXi][valXj]++;
}
int total = (int)instances.size();
double mutualInfo = 0.0;
for (int valY = 0; valY < rangeY; ++valY) {
for (int valXi = 0; valXi < rangeXi; ++valXi) {
for (int valXj = 0; valXj < rangeXj; ++valXj) {
double pXiXjY = (YXiXjOccurance[valY][valXi][valXj] + 1.0) /
(total + rangeXi * rangeXj * rangeY);
double pXiXj_Y = (YXiXjOccurance[valY][valXi][valXj] + 1.0) /
(YOccurance[valY] + rangeXi * rangeXj);
double pXi_Y = (YXiOccurance[valY][valXi] + 1.0) /
(YOccurance[valY] + rangeXi);
double pXj_Y = (YXjOccurance[valY][valXj] + 1.0) /
(YOccurance[valY] + rangeXj);
mutualInfo += pXiXjY * log2(pXiXj_Y / (pXi_Y * pXj_Y));
}
}
}
return mutualInfo;
}
void BayesNet::createMutualInfoTable() {
int numOfFeatures = metadata->numOfFeatures;
mutualInfoTable.resize(numOfFeatures);
for (int i = 0; i < numOfFeatures; ++i)
mutualInfoTable[i].resize(numOfFeatures);
for (int i = 0; i < numOfFeatures; ++i) {
mutualInfoTable[i][i] = -1.0;
for (int j = i + 1; j < numOfFeatures; ++j) {
double mutualInfo = computeMutualInfo(i, j);
mutualInfoTable[i][j] = mutualInfo;
mutualInfoTable[j][i] = mutualInfo;
}
}
}
string BayesNet::getMutualInfoTable() const {
stringstream ss;
ss << "<Conditional Mutual Information Table>" << endl;
if (treeAugmented) {
ss.setf(ios::fixed, ios::floatfield);
ss.precision(PRECISION);
for (int i = 0; i < mutualInfoTable.size(); ++i) {
for (int j = 0; j < mutualInfoTable[i].size(); ++j) {
if (j != 0) ss << DELIMITER;
ss << mutualInfoTable[i][j];
}
ss << endl;
}
} else {
ss << "Not applicable" << endl;
}
return ss.str();
}
void BayesNet::createMaximalSpanningTree() {
int numOfFeatures = metadata->numOfFeatures;
set<int> nodesInTree;
set<int> nodesNotInTree;
nodesInTree.insert(0);
for (int i = 1; i < numOfFeatures; ++i)
nodesNotInTree.insert(i);
while (nodesInTree.size() < numOfFeatures) {
double maxWeight = -1.0;
int maxI = -1;
int maxJ = -1;
for (set<int>::iterator itI = nodesInTree.begin(); itI != nodesInTree.end(); ++itI) {
int i = *itI;
for (set<int>::iterator itJ = nodesNotInTree.begin(); itJ != nodesNotInTree.end(); ++itJ) {
int j = *itJ;
if (mutualInfoTable[i][j] > maxWeight) {
maxWeight = mutualInfoTable[i][j];
maxI = i;
maxJ = j;
}
}
}
nodesInTree.insert(maxJ);
nodesNotInTree.erase(maxJ);
maximalSpanningTree.push_back(pair<int, int>(maxI, maxJ));
}
}
string BayesNet::getMaximalSpanningTree() const {
stringstream ss;
ss << "<Maximal Spanning Tree>" << endl;
if (treeAugmented) {
ss << "{";
for (int i = 0; i < maximalSpanningTree.size(); ++i) {
if (i != 0) ss << ", ";
ss << "(" << maximalSpanningTree[i].first << ", " << maximalSpanningTree[i].second << ")";
}
ss << "}" << endl;
} else {
ss << "Not applicable" << endl;
}
return ss.str();
}
void BayesNet::createBayesNet() {
int numOfFeatures = metadata->numOfFeatures;
bayesNet.resize(numOfFeatures);
for (int i = 0; i < maximalSpanningTree.size(); ++i) {
pair<int, int> edge = maximalSpanningTree[i];
bayesNet[edge.second].push_back(edge.first);
}
for (int i = 0; i < numOfFeatures; ++i)
bayesNet[i].push_back(numOfFeatures);
}
string BayesNet::getBayesNet() const {
stringstream ss;
ss << "<Bayesian Network Structure>" << endl;
for (int i = 0; i < bayesNet.size(); ++i) {
ss << metadata->featureList[i]->getName();
for (int j = 0; j < bayesNet[i].size(); ++j) {
ss << DELIMITER;
int featureIdx = bayesNet[i][j];
if (featureIdx < metadata->numOfFeatures)
ss << metadata->featureList[featureIdx]->getName();
else
ss << metadata->classVariable->getName();
}
ss << endl;
}
return ss.str();
}
CPT* BayesNet::computeCPT(int self, const vector<int>& parents) const {
CPT* cpt = 0;
switch (parents.size()) {
case 0:
cpt = new CPT0(metadata, self, parents);
break;
case 1:
cpt = new CPT1(metadata, self, parents);
break;
case 2:
cpt = new CPT2(metadata, self, parents);
break;
default:
break;
}
if (cpt) cpt->buildTable(instances);
return cpt;
}
void BayesNet::createProbabilityTables() {
int numOfFeatures = metadata->numOfFeatures;
probabilityTables.resize(numOfFeatures + 1);
for (int i = 0; i < numOfFeatures; ++i)
probabilityTables[i] = computeCPT(i, bayesNet[i]);
probabilityTables[numOfFeatures] = computeCPT(numOfFeatures, vector<int>());
}
string BayesNet::getProbabilityTables() const {
stringstream ss;
ss << "<Conditional Probability Tables>" << endl;
for (int i = 0; i < probabilityTables.size(); ++i)
ss << probabilityTables[i]->toString();
return ss.str();
}
string BayesNet::predict(const Instance* instance, double* probability) const {
int numOfClasses = metadata->numOfClasses;
int numOfFeatures = metadata->numOfFeatures;
Instance inst = *instance;
double probSum = 0.0;
vector<double> probs(numOfClasses);
for (int y = 0; y < numOfClasses; ++y) {
inst.classLabel = y;
probs[y] = probabilityTables.back()->computeCondProb(&inst);
for (int x = 0; x < numOfFeatures; ++x) {
probs[y] *= probabilityTables[x]->computeCondProb(&inst);
}
probSum += probs[y];
}
double maxProb = -1.0;
int maxClass = -1;
for (int y = 0; y < numOfClasses; ++y) {
probs[y] /= probSum;
if (probs[y] > maxProb) {
maxProb = probs[y];
maxClass = y;
}
}
if (probability)
*probability = maxProb;
return metadata->classVariable->convertInternalToValue(maxClass);
}