test_DiscreteDistribution.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2021, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 Unit tests for Discrete Priors.
9 Author: Frank Dellaert
10 """
11 
12 # pylint: disable=no-name-in-module, invalid-name
13 
14 import unittest
15 
16 import numpy as np
17 from gtsam import DecisionTreeFactor, DiscreteKeys, DiscreteDistribution
18 from gtsam.utils.test_case import GtsamTestCase
19 
20 X = 0, 2
21 
22 
24  """Tests for Discrete Priors."""
25 
26  def test_constructor(self):
27  """Test various constructors."""
28  keys = DiscreteKeys()
29  keys.push_back(X)
30  f = DecisionTreeFactor(keys, "0.4 0.6")
31  expected = DiscreteDistribution(f)
32 
33  actual = DiscreteDistribution(X, "2/3")
34  self.gtsamAssertEquals(actual, expected)
35 
36  actual2 = DiscreteDistribution(X, [0.4, 0.6])
37  self.gtsamAssertEquals(actual2, expected)
38 
39  def test_operator(self):
40  prior = DiscreteDistribution(X, "2/3")
41  self.assertAlmostEqual(prior(0), 0.4)
42  self.assertAlmostEqual(prior(1), 0.6)
43 
44  def test_pmf(self):
45  prior = DiscreteDistribution(X, "2/3")
46  expected = np.array([0.4, 0.6])
47  np.testing.assert_allclose(expected, prior.pmf())
48 
49  def test_sample(self):
50  prior = DiscreteDistribution(X, "2/3")
51  actual = prior.sample()
52  self.assertIsInstance(actual, int)
53 
54  def test_markdown(self):
55  """Test the _repr_markdown_ method."""
56 
57  prior = DiscreteDistribution(X, "2/3")
58  expected = " *P(0):*\n\n" \
59  "|0|value|\n" \
60  "|:-:|:-:|\n" \
61  "|0|0.4|\n" \
62  "|1|0.6|\n" \
63 
64  actual = prior._repr_markdown_()
65  self.assertEqual(actual, expected)
66 
67 
68 if __name__ == "__main__":
69  unittest.main()
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
test_DiscreteDistribution.TestDiscreteDistribution.test_operator
def test_operator(self)
Definition: test_DiscreteDistribution.py:39
test_DiscreteDistribution.TestDiscreteDistribution.test_sample
def test_sample(self)
Definition: test_DiscreteDistribution.py:49
test_DiscreteDistribution.TestDiscreteDistribution
Definition: test_DiscreteDistribution.py:23
gtsam::DiscreteDistribution
Definition: DiscreteDistribution.h:33
test_DiscreteDistribution.TestDiscreteDistribution.test_pmf
def test_pmf(self)
Definition: test_DiscreteDistribution.py:44
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
gtsam::utils.test_case.GtsamTestCase.gtsamAssertEquals
def gtsamAssertEquals(self, actual, expected, tol=1e-9)
Definition: test_case.py:18
test_DiscreteDistribution.TestDiscreteDistribution.test_constructor
def test_constructor(self)
Definition: test_DiscreteDistribution.py:26
gtsam::utils.test_case
Definition: test_case.py:1
gtsam::utils.test_case.GtsamTestCase
Definition: test_case.py:15
different_sigmas::prior
const auto prior
Definition: testHybridBayesNet.cpp:238
test_DiscreteDistribution.TestDiscreteDistribution.test_markdown
def test_markdown(self)
Definition: test_DiscreteDistribution.py:54


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:05