Ejemplo n.º 1
0
 def test_prepare(self):
     d = Distribution(["A", "B", "C", "D"], [3, 3, -1, 0])
     d.prepare()
     self.assertEqual(d.getNormalizingConstant(), 6)
     self.assertEqual(d["A"], 0.5)
     self.assertEqual(d["B"], 0.5)
     self.assertEqual(d["C"], 0.0)
     self.assertEqual(d["D"], 0.0)
Ejemplo n.º 2
0
    def test_constructor(self):
        d = Distribution(["A", "B"], [0.3, 0.7])
        self.assertEqual(d["A"], 0.3)
        self.assertEqual(d["B"], 0.7)

        d = Distribution(dictionary={"A": 0.3, "B": 0.7})
        self.assertEqual(d["A"], 0.3)
        self.assertEqual(d["B"], 0.7)
Ejemplo n.º 3
0
 def test_isub(self):
     # TODO: Include bigger q in this test
     p = Distribution(["A", "B", "C"], [0.4, 0.3, 0.3])
     q = Distribution(["A", "C", "D"], [0.4, 0.2, 0.25])
     p -= q
     self.assertEqual(p["A"], 0.0)
     assert_allclose(p["B"], [0.3])
     assert_allclose(p["C"], [0.1])
Ejemplo n.º 4
0
 def test_prepare(self):
     d = Distribution(["A", "B", "C", "D"], [3, 3, -1, 0])
     d.prepare()
     self.assertEqual(d.getNormalizingConstant(), 6)
     self.assertEqual(d["A"], 0.5)
     self.assertEqual(d["B"], 0.5)
     self.assertEqual(d["C"], 0.0)
     self.assertEqual(d["D"], 0.0)
Ejemplo n.º 5
0
    def test_add(self):
        p = Distribution(["A", "B"], [0.2, 0.25])
        q = Distribution(["A", "C", "D"], [0.05, 0.25, 0.25])
        r = p + q

        self.assertEqual(r["A"], 0.25)
        self.assertEqual(r["B"], 0.25)
        self.assertEqual(r["C"], 0.25)
        self.assertEqual(r["D"], 0.25)
Ejemplo n.º 6
0
    def test_kld(self):
        p = Distribution(["A", "B"], [0.5, 0.5])
        q = Distribution(["A", "C"], [0.5, 0.5])
        assert_allclose(kld(p, q), [0])
        assert_allclose(kld(q, p), [0])

        p = Distribution(["A", "B"], [0.3, 0.7])
        q = Distribution(["A", "B"], [0.5, 0.5])
        self.assertNotEqual(kld(p, q), kld(q, p))
Ejemplo n.º 7
0
    def test_compute(self):
        p = Distribution(["A", "B"], [0.3, 0.7])
        q = Distribution(["A", "B", "C"], [0.3, 0.3, 0.4])
        r = Distribution(["D", "C"], [0.5, 0.5])
        l = [p, q, r]
        for w in compute(l, [2, 1, 3], 1, logOnScreen=False):
            pass

        for w in compute(l, [2, 1, 3], 3, logOnScreen=False):
            pass
Ejemplo n.º 8
0
    def test_indexD(self):
        p = Distribution(["A", "B"], [0.5, 0.5])
        q = Distribution(["A", "C"], [0.5, 0.5])
        r = Distribution(["D", "C"], [0.5, 0.5])

        assert_allclose(indexD(p, 5, q, 5), [0.5])
        assert_allclose(indexD(p, 5, p, 5), [0])
        assert_allclose(indexD(p, 5, p, 3), [0])
        assert_allclose(indexD(p, 1, p, 3), [0])
        assert_allclose(indexD(p, 5, r, 5), [1])
        assert_allclose(indexD(p, 5, r, 2), [1])
        assert_allclose(indexD(p, 5, r, 9), [1])
Ejemplo n.º 9
0
    def test_common(self):
        p = Distribution(["A", "B", "C"], [0.3, 0.3, 0.4])
        q = Distribution(["A", "B"], [0.3, 0.7])
        r = common(p, q)

        self.assertEqual(len(r), 2)
        self.assertEqual(r["A"], 0.5)
        self.assertEqual(r["B"], 0.5)

        r = common(q, p)

        self.assertEqual(len(r), 2)
        self.assertEqual(r["A"], 0.3)
        self.assertEqual(r["B"], 0.7)
Ejemplo n.º 10
0
    def test_getsetdel(self):
        d = Distribution()

        d["A"] = 0.2
        self.assertEqual(d["A"], 0.2)
        d["A"] = 0.3
        self.assertEqual(d["A"], 0.3)
        del d["A"]

        # with self.assertRaises(KeyError):
        #     d["A"]

        with self.assertRaises(TypeError):
            d[12]
Ejemplo n.º 11
0
    def test_iteration(self):
        d = Distribution(["A", "B"], [0.3, 0.7])

        it = iter(d)
        e, p = it.next()
        self.assertEqual(e, "A")
        self.assertEqual(p, 0.3)

        e, p = it.next()
        self.assertEqual(e, "B")
        self.assertEqual(p, 0.7)

        for e, p in d:
            pass

        it = iter(d)
        e, p = it.next()
        self.assertEqual(e, "A")
        self.assertEqual(p, 0.3)

        e, p = it.next()
        self.assertEqual(e, "B")
        self.assertEqual(p, 0.7)
Ejemplo n.º 12
0
    def test_isEmpty(self):
        d = Distribution(["A", "B"], [0.3, 0.7])
        self.assertFalse(d.isEmpty())

        d = Distribution()
        self.assertTrue(d.isEmpty())
Ejemplo n.º 13
0
 def test_plot(self):
     p = Distribution(["A", "B", "C"], [0.3, 0.3, 0.4])
     q = Distribution(["A", "B"], [0.3, 0.7])
     plot([p, q])
Ejemplo n.º 14
0
 def test_direction(self):
     p = Distribution(["A", "B", "C"], [0.3, 0.3, 0.4])
     q = Distribution(["A", "B"], [0.3, 0.7])
     self.assertEqual(direction(p, q), -direction(q, p))
     self.assertEqual(direction(p, p), 0)
Ejemplo n.º 15
0
 def test_insert(self):
     d = Distribution()
     self.assertEqual(d.size(), 0)
     d.insert("A", 0.5)
     d.insert("B", 0.5)
     self.assertEqual(d.size(), 2)
Ejemplo n.º 16
0
 def test_append(self):
     p = Distribution(["A", "B", "C"], [0.2, 0.25, 0.55])
     p.append("A", 0.3)
     self.assertEqual(p["A"], 0.5)
Ejemplo n.º 17
0
 def test_normalize(self):
     d = Distribution(["A", "B"], [3, 3])
     self.assertEqual(d.normalize(), 6)
     self.assertEqual(d.getNormalizingConstant(), 6)
     self.assertEqual(d["A"], 0.5)
Ejemplo n.º 18
0
 def test_plot(self):
     d = Distribution(["A", "B"], [0.3, 0.7])
     d.plot(show=False)
Ejemplo n.º 19
0
 def test_getData(self):
     d = Distribution(["A", "B", "C"], [0.4, 0.3, 0.2])
     keys, values = d.getData()
     self.assertEqual(keys, ["A", "B", "C"])
     self.assertEqual(values, [0.4, 0.3, 0.2])
Ejemplo n.º 20
0
 def test_append(self):
     p = Distribution(["A", "B", "C"], [0.2, 0.25, 0.55])
     p.append("A", 0.3)
     self.assertEqual(p["A"], 0.5)
Ejemplo n.º 21
0
 def test_str(self):
     d = Distribution(["A", "B"], [0.3, 0.7])
     self.assertEqual(str(d), '{"A": 0.3, "B": 0.7}')
Ejemplo n.º 22
0
 def test_plot(self):
     d = Distribution(["A", "B"], [0.3, 0.7])
     d.plot(show=False)
Ejemplo n.º 23
0
 def test_entropy(self):
     d = Distribution(["A", "B"], [0.5, 0.5])
     assert_allclose(d.entropy(), [1])
Ejemplo n.º 24
0
 def test_getData(self):
     d = Distribution(["A", "B", "C"], [0.4, 0.3, 0.2])
     keys, values = d.getData()
     self.assertEqual(keys, ["A", "B", "C"])
     self.assertEqual(values, [0.4, 0.3, 0.2])
Ejemplo n.º 25
0
 def test_insert(self):
     d = Distribution()
     self.assertEqual(d.size(), 0)
     d.insert("A", 0.5)
     d.insert("B", 0.5)
     self.assertEqual(d.size(), 2)
Ejemplo n.º 26
0
    def test_jsd(self):
        p = Distribution(["A", "B"], [0.5, 0.5])
        q = Distribution(["A", "C"], [0.5, 0.5])

        assert_allclose(jsd(p, 0.5, q, 0.5), [0.5])
Ejemplo n.º 27
0
 def test_entropy(self):
     d = Distribution(["A", "B"], [0.5, 0.5])
     assert_allclose(d.entropy(), [1])
Ejemplo n.º 28
0
 def test_normalize(self):
     d = Distribution(["A", "B"], [3, 3])
     self.assertEqual(d.normalize(), 6)
     self.assertEqual(d.getNormalizingConstant(), 6)
     self.assertEqual(d["A"], 0.5)
Ejemplo n.º 29
0
 def test_contains(self):
     d = Distribution(["A", "B"], [0.3, 0.7])
     self.assertTrue(d.contains("A"))
     self.assertFalse(d.contains("C"))
Ejemplo n.º 30
0
    def test_isEmpty(self):
        d = Distribution(["A", "B"], [0.3, 0.7])
        self.assertFalse(d.isEmpty())

        d = Distribution()
        self.assertTrue(d.isEmpty())
Ejemplo n.º 31
0
 def test_contains(self):
     d = Distribution(["A", "B"], [0.3, 0.7])
     self.assertTrue(d.contains("A"))
     self.assertFalse(d.contains("C"))
Ejemplo n.º 32
0
 def test_iadd(self):
     p = Distribution(["A", "B", "C"], [0.2, 0.25, 0.55])
     q = Distribution(["A"], [0.2])
     p += q
     self.assertEqual(p["A"], 0.4)