2 GTSAM Copyright 2010-2021, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
6 See LICENSE for the license information
8 Unit tests for Discrete Conditionals.
16 from gtsam
import DecisionTreeFactor, DiscreteConditional, DiscreteKeys
28 """Tests for Discrete Conditionals."""
35 actual0 = conditional.likelihood(0)
39 actual1 = conditional.likelihood(1)
43 actual = conditional.sample(2)
44 self.assertIsInstance(actual, int)
47 """Check calculation of joint P(A,B)"""
52 for actual
in [prior * conditional, conditional * prior]:
53 self.assertEqual(2, actual.nrFrontals())
54 for v, value
in actual.enumerate():
55 self.assertAlmostEqual(actual(v), conditional(v) *
prior(v))
58 """Check calculation of conditional joint P(A,B|C)"""
63 for actual
in [A_given_B * B_given_C, B_given_C * A_given_B]:
64 self.assertEqual(2, actual.nrFrontals())
65 self.assertEqual(1, actual.nrParents())
66 for v, value
in actual.enumerate():
67 self.assertAlmostEqual(actual(v), A_given_B(v) * B_given_C(v))
70 """Check calculation of joint P(A,B,C|D,E) = P(A,B|D) P(C|D,E)"""
73 AB_given_D = A_given_B * B_given_D
77 for actual
in [AB_given_D * C_given_DE, C_given_DE * AB_given_D]:
78 self.assertEqual(3, actual.nrFrontals())
79 self.assertEqual(2, actual.nrParents())
80 for v, value
in actual.enumerate():
81 self.assertAlmostEqual(
82 actual(v), AB_given_D(v) * C_given_DE(v))
87 pAB = prior * conditional
94 """Test whether the _repr_markdown_ method."""
104 "0/1 1/3 1/1 3/1 0/1 1/0")
105 expected =
" *P(A|B,C):*\n\n" \
107 "|:-:|:-:|:-:|:-:|\n" \
109 "|0|1|0.25|0.75|\n" \
111 "|1|0|0.75|0.25|\n" \
116 names = [
"C",
"B",
"A"]
119 actual = conditional._repr_markdown_(formatter)
120 self.assertEqual(actual, expected)
123 if __name__ ==
"__main__":