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");
48  DiscreteDistribution prior(B, "1/2");
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 /* ************************************************************************* */
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
static int runAllTests(TestResult &result)
signatures for conditional densities
Point2 prior(const Point2 &x)
Prior on a single pose.
Definition: simulated2D.h:88
Matrix expected
Definition: testMatrix.cpp:971
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
size_t nrFrontals() const
Definition: Conditional.h:129
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
size_t sample(const DiscreteValues &parentsValues) const
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
std::vector< double > pmf() const
We also want to keep the Base version, taking DiscreteValues:
static const DiscreteKey X(0, 2)
#define EXPECT(condition)
Definition: Test.h:150
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
traits
Definition: chartTesting.h:28
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
size_t argmax() const
Return assignment that maximizes distribution.
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
TEST(SmartFactorBase, Pinhole)
size_t nrParents() const
Definition: Conditional.h:132


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:38:01