testDiscreteDistribution.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
12 /*
13  * @file testDiscreteDistribution.cpp
14  * @brief unit tests for DiscreteDistribution
15  * @author Frank dellaert
16  * @date December 2021
17  */
18 
22 
23 using namespace gtsam;
24 
25 static const DiscreteKey X(0, 2);
26 
27 /* ************************************************************************* */
28 TEST(DiscreteDistribution, constructors) {
29  DecisionTreeFactor f(X, "0.4 0.6");
31 
32  DiscreteDistribution actual(X % "2/3");
33  EXPECT_LONGS_EQUAL(1, actual.nrFrontals());
34  EXPECT_LONGS_EQUAL(0, actual.nrParents());
35  EXPECT(assert_equal(expected, actual, 1e-9));
36 
37  const std::vector<double> pmf{0.4, 0.6};
38  DiscreteDistribution actual2(X, pmf);
39  EXPECT_LONGS_EQUAL(1, actual2.nrFrontals());
40  EXPECT_LONGS_EQUAL(0, actual2.nrParents());
41  EXPECT(assert_equal(expected, actual2, 1e-9));
42 }
43 
44 /* ************************************************************************* */
46  DiscreteKey A(0, 2), B(1, 2);
47  DiscreteConditional conditional(A | B = "1/2 2/1");
49  DiscreteConditional actual = prior * conditional; // P(A|B) * P(B)
50 
51  EXPECT_LONGS_EQUAL(2, actual.nrFrontals()); // = P(A,B)
52  DecisionTreeFactor factor(A & B, "1 4 2 2");
53  DiscreteConditional expected(2, factor);
54  EXPECT(assert_equal(expected, actual, 1e-5));
55 }
56 
57 /* ************************************************************************* */
59  DiscreteDistribution prior(X % "2/3");
60  EXPECT_DOUBLES_EQUAL(prior(0), 0.4, 1e-9);
61  EXPECT_DOUBLES_EQUAL(prior(1), 0.6, 1e-9);
62 }
63 
64 /* ************************************************************************* */
66  DiscreteDistribution prior(X % "2/3");
67  std::vector<double> expected{0.4, 0.6};
68  EXPECT(prior.pmf() == expected);
69 }
70 
71 /* ************************************************************************* */
73  DiscreteDistribution prior(X % "2/3");
74  prior.sample();
75 }
76 
77 /* ************************************************************************* */
79  DiscreteDistribution prior(X % "2/3");
80  EXPECT_LONGS_EQUAL(prior.argmax(), 1);
81 }
82 
83 /* ************************************************************************* */
84 int main() {
85  TestResult tr;
86  return TestRegistry::runAllTests(tr);
87 }
88 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
gtsam::DiscreteDistribution
Definition: DiscreteDistribution.h:33
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
Signature.h
signatures for conditional densities
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
EXPECT_DOUBLES_EQUAL
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
TestResult
Definition: TestResult.h:26
gtsam::Conditional::nrFrontals
size_t nrFrontals() const
Definition: Conditional.h:129
X
static const DiscreteKey X(0, 2)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
simulated2D::prior
Point2 prior(const Point2 &x)
Prior on a single pose.
Definition: simulated2D.h:88
gtsam::DiscreteConditional
Definition: DiscreteConditional.h:37
gtsam
traits
Definition: chartTesting.h:28
gtsam::TEST
TEST(SmartFactorBase, Pinhole)
Definition: testSmartFactorBase.cpp:38
DiscreteDistribution.h
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
gtsam::Conditional::nrParents
size_t nrParents() const
Definition: Conditional.h:132
main
int main()
Definition: testDiscreteDistribution.cpp:84


gtsam
Author(s):
autogenerated on Mon Jul 1 2024 03:06:18