30 using namespace gtsam;
31 static constexpr
bool debug =
false;
46 for (
int i = 0;
i < 15;
i++) {
48 keys.push_back(key_i);
87 self.bayesNet.saveGraph(
"/tmp/discreteBayesNet.dot");
93 self.bayesTree->saveGraph(
"/tmp/discreteBayesTree.dot");
97 for (
size_t i : {13, 14, 9, 3, 2, 8, 1, 0, 10, 5, 4}) {
98 auto clique_i = (*
self.bayesTree)[
i];
102 for (
const auto&
x :
self.assignments) {
103 double expected =
self.bayesNet.evaluate(
x);
104 double actual =
self.bayesTree->evaluate(
x);
115 double marginal_14 = 0, joint_8_12 = 0;
116 for (
auto&
x :
self.assignments) {
117 double px =
self.bayesTree->evaluate(
x);
118 if (
x[8] &&
x[12]) joint_8_12 +=
px;
119 if (
x[14]) marginal_14 +=
px;
124 auto clique = (*
self.bayesTree)[0];
130 clique = (*
self.bayesTree)[9];
136 clique = (*
self.bayesTree)[11];
148 double joint_11_13 = 0, joint_11_13_14 = 0, joint_11_12_13_14 = 0,
149 joint_9_11_12_13 = 0, joint_8_11_12_13 = 0;
150 for (
auto&
x :
self.assignments) {
151 double px =
self.bayesTree->evaluate(
x);
152 if (
x[11] &&
x[13]) {
154 if (
x[8] &&
x[12]) joint_8_11_12_13 +=
px;
155 if (
x[9] &&
x[12]) joint_9_11_12_13 +=
px;
157 joint_11_13_14 +=
px;
159 joint_11_12_13_14 +=
px;
166 auto R =
self.bayesTree->roots().front();
169 auto clique = (*
self.bayesTree)[9];
175 clique = (*
self.bayesTree)[8];
180 clique = (*
self.bayesTree)[2];
185 clique = (*
self.bayesTree)[0];
191 for (
auto clique : cliques) {
194 clique.second->conditional_->printSignature();
195 shortcut.
print(
"shortcut:");
206 for (
size_t i = 0;
i <
self.assignments.size(); ++
i) {
208 double px =
self.bayesTree->evaluate(
x);
209 for (
size_t i = 0;
i < 15;
i++)
215 for (
size_t i = 0;
i < 15;
i++) {
217 double actual = (*marginalFactor)(all1);
229 double joint_12_14 = 0, joint_9_12_14 = 0, joint_8_12_14 = 0, joint82 = 0,
230 joint12 = 0, joint24 = 0, joint45 = 0, joint46 = 0, joint_4_11 = 0;
231 for (
size_t i = 0;
i <
self.assignments.size(); ++
i) {
233 double px =
self.bayesTree->evaluate(
x);
234 for (
size_t i = 0;
i < 15;
i++)
236 if (
x[12] &&
x[14]) {
238 if (
x[9]) joint_9_12_14 +=
px;
239 if (
x[8]) joint_8_12_14 +=
px;
242 if (
x[8]) joint82 +=
px;
243 if (
x[1]) joint12 +=
px;
246 if (
x[2]) joint24 +=
px;
247 if (
x[5]) joint45 +=
px;
248 if (
x[6]) joint46 +=
px;
249 if (
x[11]) joint_4_11 +=
px;
289 std::string actual =
self.
bayesTree->dot();
292 "0[label=\"13, 11, 6, 7\"];\n"
294 "1[label=\"14 : 11, 13\"];\n"
296 "2[label=\"9, 12 : 14\"];\n"
298 "3[label=\"3 : 9, 12\"];\n"
300 "4[label=\"2 : 9, 12\"];\n"
302 "5[label=\"8 : 12, 14\"];\n"
304 "6[label=\"1 : 8, 12\"];\n"
306 "7[label=\"0 : 8, 12\"];\n"
308 "8[label=\"10 : 13, 14\"];\n"
310 "9[label=\"5 : 10, 13\"];\n"
312 "10[label=\"4 : 10, 13\"];\n"
334 std::vector<double>
table{
348 auto lookup_x1_a1_x2 = (*lookup)[
X(1)]->conditional();
356 auto lookup_a2_x3 = (*lookup)[
X(3)]->conditional();
358 auto sum_x2 = lookup_a2_x3->sum(2);