-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest_probability.py
64 lines (50 loc) · 2.46 KB
/
test_probability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from unittest import TestCase
from probability import *
class TestProbability(TestCase):
def test_is_probability(self):
self.assertTrue(is_probability([1.0, 0.0]))
self.assertTrue(is_probability([0.5, 0.5]))
self.assertFalse(is_probability([0.0, 0.0]))
self.assertFalse(is_probability([-1.0, 2.0]))
def test_marginalize(self):
self.assertEqual(marginalize([[0.1, 0.4], [0.3, 0.2]], axis=1), [0.5, 0.5])
self.assertEqual(marginalize([[0.1, 0.4], [0.3, 0.2]], axis=0), [0.4, 0.6000000000000001])
def test_condition(self):
self.assertEqual(condition([[0.1, 0.4], [0.3, 0.2]]), [[0.2, 0.8], [0.6, 0.4]])
def test_is_independent(self):
self.assertTrue(is_independent([[0.1, 0.4], [0.1, 0.4]]))
self.assertTrue(is_independent([[0.2, 0.2], [0.3, 0.3]]))
self.assertFalse(is_independent([[0.1, 0.4], [0.4, 0.1]]))
def test_expectation(self):
self.assertEqual(expectation([0.1, 0.9], lambda x: x), 0.9)
def test_uniform(self):
self.assertEqual(uniform(2)(0), 0.5)
def test_bernoulli(self):
self.assertEqual(bernoulli(0.1)(0), 0.9)
def test_multinoulli(self):
self.assertEqual(multinoulli([0.1, 0.9])(1), 0.9)
def test_gaussian(self):
N = gaussian(0.0, 1.0)
self.assertGreater(N(0.0), N(0.1))
self.assertGreater(N(0.0), N(-0.1))
self.assertEqual(N(0.1), N(-0.1))
def test_mixture(self):
G1 = gaussian(1.0, 1.0)
G2 = gaussian(-1.0, 1.0)
M = mixture([G1, G2], [0.6, 0.4])
self.assertGreater(M(1.0), M(-1.0))
self.assertGreater(M(1.0), M(0.0))
self.assertLess(M(-10.0), M(0.0))
def test_entropy(self):
self.assertEqual(entropy([1.0, 0.0]), 0.0)
self.assertEqual(entropy([0.5, 0.5]), -math.log(0.5))
self.assertLess(entropy([0.1, 0.9]), entropy([0.5, 0.5]))
self.assertGreater(entropy([0.1, 0.9]), 0.0)
def test_kl_divergence(self):
self.assertEqual(kl_divergence([1.0, 0.0], [1.0, 0.0]), 0.0)
self.assertLess(kl_divergence([1.0, 0.0], [0.9, 0.1]), kl_divergence([1.0, 0.0], [0.1, 0.9]))
self.assertGreater(kl_divergence([1.0, 0.0], [0.9, 0.1]), 0.0)
def test_cross_entropy(self):
self.assertEqual(cross_entropy([1.0, 0.0], [1.0, 0.0]), 0.0)
self.assertGreater(cross_entropy([1.0, 0.0], [0.9, 0.1]), 0.0)
self.assertLess(cross_entropy([1.0, 0.0], [0.9, 0.1]), cross_entropy([1.0, 0.0], [0.5, 0.5]))