forked from gerardcanal/rddl_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobability_distribution.cc
43 lines (36 loc) · 1.22 KB
/
probability_distribution.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
#include "probability_distribution.h"
#include <cassert>
using namespace std;
inline int DiscretePD::getNumberOfOutcomes() const {
assert(isWellDefined());
return values.size();
}
bool DiscretePD::isWellDefined() const {
// Only use this funciton in assertions, it's quite inefficient!
// Each value must have a probability
if (isUndefined() || (values.size() != probabilities.size())) {
return false;
}
// The sum of the probabilities must be 1, each individual probability must
// not be 0.0 and the values must be ordered and unique
double probSum = probabilities[0];
double lastVal = values[0];
for (unsigned int i = 1; i < probabilities.size(); ++i) {
if (MathUtils::doubleIsEqual(probabilities[i], 0.0)) {
return false;
}
probSum += probabilities[i];
if (!MathUtils::doubleIsGreater(values[i], lastVal)) {
return false;
}
lastVal = values[i];
}
return MathUtils::doubleIsEqual(probSum, 1.0);
}
void DiscretePD::print(ostream& out) const {
out << "[ ";
for (unsigned int i = 0; i < values.size(); ++i) {
out << values[i] << ":" << probabilities[i] << " ";
}
out << "]" << endl;
}