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.
18 from gtsam
import DecisionTreeFactor, DiscreteConditional, DiscreteKeys
29 """Tests for Discrete Conditionals."""
36 actual0 = conditional.likelihood(0)
40 actual1 = conditional.likelihood(1)
44 actual = conditional.sample(2)
45 self.assertIsInstance(actual, int)
48 """Check calculation of joint P(A,B)"""
53 for actual
in [prior * conditional, conditional * prior]:
54 self.assertEqual(2, actual.nrFrontals())
55 for v, value
in actual.enumerate():
56 self.assertAlmostEqual(actual(v), conditional(v) *
prior(v))
59 """Check calculation of conditional joint P(A,B|C)"""
64 for actual
in [A_given_B * B_given_C, B_given_C * A_given_B]:
65 self.assertEqual(2, actual.nrFrontals())
66 self.assertEqual(1, actual.nrParents())
67 for v, value
in actual.enumerate():
68 self.assertAlmostEqual(actual(v), A_given_B(v) * B_given_C(v))
71 """Check calculation of joint P(A,B,C|D,E) = P(A,B|D) P(C|D,E)"""
74 AB_given_D = A_given_B * B_given_D
78 for actual
in [AB_given_D * C_given_DE, C_given_DE * AB_given_D]:
79 self.assertEqual(3, actual.nrFrontals())
80 self.assertEqual(2, actual.nrParents())
81 for v, value
in actual.enumerate():
82 self.assertAlmostEqual(
83 actual(v), AB_given_D(v) * C_given_DE(v))
88 pAB = prior * conditional
95 """Test whether the _repr_markdown_ method."""
105 "0/1 1/3 1/1 3/1 0/1 1/0")
106 expected =
" *P(A|B,C):*\n\n" \
108 "|:-:|:-:|:-:|:-:|\n" \
110 "|0|1|0.25|0.75|\n" \
112 "|1|0|0.75|0.25|\n" \
117 names = [
"C",
"B",
"A"]
120 actual = conditional._repr_markdown_(formatter)
121 self.assertEqual(actual, expected)
124 if __name__ ==
"__main__":