-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlearnbayes.cpp
executable file
·230 lines (206 loc) · 7.09 KB
/
learnbayes.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
#include "learnbayes.h"
#include "bayesnet.h"
#include "utility.h"
#include <iostream>
#include <fstream>
#include <algorithm>
LearnBayes::LearnBayes(BayesNet *bn, const string &learnFile)
{
mBayesNet = bn;
loadFromTextFile(learnFile);
}
LearnBayes::LearnBayes(BayesNet *bn, double prior, double cptWeight)
{
mBayesNet = bn;
initCaseCounterBasedOnPriorCpts(prior, cptWeight);
}
/**
* @brief LearnBayes::learnParams
* learn CPTs for each node from complete sample data
* @param samples
*/
void LearnBayes::learn(const vector<vector<uint>> & samples)
{
if (mCaseCounter.size() != mBayesNet->mListJointProbabilities.size())
initCaseCounterBasedOnPriorCpts();
/// count the number of times each case appears
uint index;
vector<uint> maxValues;
for (const auto & sample : samples) {
if (std::find(sample.begin(), sample.end(), NONE_NODE_INDEX) == sample.end()) {
/// complete data
index = Utility::vectorToNumber(mBayesNet->mMapNodeSizes, sample);
mCaseCounter[index] += 1;
} else {
/// EM
vector<uint> mapEvidences, postValues, indexPreValues, maxPreValues;
uint size = 1;
postValues = sample;
for (uint i=0; i < sample.size(); ++i) {
if (sample[i] != NONE_NODE_INDEX) {
mapEvidences.push_back(i);
} else {
size *= mBayesNet->mMapNodeSizes[i];
maxPreValues.push_back(mBayesNet->mMapNodeSizes[i]);
indexPreValues.push_back(i);
}
}
for (uint i = 0; i < size; ++i) {
auto v = Utility::numberToVector(maxPreValues, i);
for (uint j = 0; j < v.size(); ++j) {
postValues[indexPreValues[j]] = v[j];
}
auto p = mBayesNet->getConditionalProbability(mapEvidences, postValues);
///@
// vector<uint> eIndice;
// vector<int> eVals;
// for(uint j=0; j<postValues.size()-1; j++)
// {
// eIndice.push_back(j);
// eVals.push_back(postValues.at(j));
// }
// mBayesNet->updateMul(eIndice, eVals);
///@
index = Utility::vectorToNumber(mBayesNet->mMapNodeSizes, postValues);
mCaseCounter[index] += p;
}
}
}
///
/// count the number of time a group of values related to a node appears
vector<vector<double>> nodeCounter;
vector<vector<double>> parentCounter;
/// init counters if not defined
uint size;
for (uint nodeId = 0; nodeId < mBayesNet->mNumNodes; ++nodeId) {
size = 1;
for (const auto & paNode : mBayesNet->mMapParents[nodeId]) {
size *= mBayesNet->mMapNodeSizes[paNode];
}
nodeCounter.push_back(vector<double>(size*mBayesNet->mMapNodeSizes[nodeId], 0));
parentCounter.push_back(vector<double>(size, 0));
}
for (uint nodeId = 0; nodeId < mBayesNet->mNumNodes; ++nodeId) {
maxValues.clear();
for (const auto & paNode : mBayesNet->mMapParents[nodeId]) {
maxValues.push_back(mBayesNet->mMapNodeSizes[paNode]);
}
maxValues.push_back(mBayesNet->mMapNodeSizes[nodeId]);
for (uint i = 0; i < mCaseCounter.size(); ++i) {
vector<uint> values;
for (const auto & paNode : mBayesNet->mMapParents[nodeId]) {
values.push_back(mMapIndex2Case[i][paNode]);
}
values.push_back(mMapIndex2Case[i][nodeId]);
index = Utility::vectorToNumber(maxValues, values);
nodeCounter[nodeId][index] += mCaseCounter[i];
parentCounter[nodeId][ index / mBayesNet->mMapNodeSizes[nodeId] ] += mCaseCounter[i];
}
}
///
/// setup CPT for BayesNet
vector<vector<double>> cpt;
for (uint nodeId = 0; nodeId < mBayesNet->mNumNodes; ++nodeId) {
cpt.push_back(vector<double>());
size = nodeCounter[nodeId].size();
for (uint i = 0; i < size; ++i) {
index = i / mBayesNet->mMapNodeSizes[nodeId];
cpt[nodeId].push_back( nodeCounter[nodeId][i] / parentCounter[nodeId][index] );
}
}
mBayesNet->setCpt(cpt);
}
/**
* @brief LearnBayes::learn
* EM learning
* @param datafile
* @param iter - the likelihood must increase after each iteration
*/
void LearnBayes::learn(const string &datafile, uint iter)
{
auto samples = Utility::readTsvUintData(datafile);
while (iter--)
learn(samples);
}
void LearnBayes::save(const string &learnFile) const
{
ofstream of;
of.open(learnFile, ios::binary | ios::out);
if (!of.is_open()) {
cout << "Write file error!" << "\n";
return;
}
uint size = mCaseCounter.size(); /// caseCounter
of.write((char*)&size, sizeof(size));
for (const auto & p : mCaseCounter) {
of.write((char*)&p, sizeof(p));
}
of.close();
cout << "Save " << learnFile << " successfully."<< "\n";
}
string LearnBayes::toString() const
{
stringstream content;
content << mCaseCounter.size() << "\n";
for (const auto & c : mCaseCounter) {
content << c << "\n";
}
return content.str();
}
void LearnBayes::loadFromTextFile(const string &learnFile)
{
ifstream stream(learnFile);
if (!stream.is_open()) {
cout << "Open file error!" << "\n";
return;
}
double dValue;
uint size = 0;
stream >> size;
if (size > 0) {
/// node counter
for (uint i = 0; i < size; ++i) {
stream >> dValue;
mCaseCounter.push_back(dValue);
mMapIndex2Case.push_back(Utility::numberToVector(mBayesNet->mMapNodeSizes, i));
}
}
stream.close();
cout << "Read " << learnFile << " successfully."<< "\n";
}
void LearnBayes::loadFromBinFile(const string &learnFile)
{
ifstream inf;
inf.open(learnFile, ios::binary | ios::in);
if (!inf.is_open()) {
cout << "Open file error!" << "\n";
return;
}
/// read caseCounter
uint size = 0;
double dValue;
inf.read((char*)&size, sizeof(size));
if (size > 0) {
/// node counter
for (uint i = 0; i < size; ++i) {
inf.read((char*)&dValue, sizeof(dValue));
mCaseCounter.push_back(dValue);
mMapIndex2Case.push_back(Utility::numberToVector(mBayesNet->mMapNodeSizes, i));
}
}
inf.close();
cout << "Read " << learnFile << " successfully."<< "\n";
}
void LearnBayes::initCaseCounterBasedOnPriorCpts(double prior, double cptWeight)
{
if (mBayesNet->mListJointProbabilities.empty())
mBayesNet->setJointDistribution();
auto size = mBayesNet->mListJointProbabilities.size();
for (uint i = 0; i < size; ++i) {
mMapIndex2Case.push_back(Utility::numberToVector(mBayesNet->mMapNodeSizes, i));
mCaseCounter.push_back(0.0);
mCaseCounter[i] += prior / size;
mCaseCounter[i] += cptWeight * mBayesNet->mListJointProbabilities[i] * size;
}
cout << "init prior CPTs ok";
}