File: test_beta.py

package info (click to toggle)
python-bayespy 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,132 kB
  • sloc: python: 22,402; makefile: 156
file content (110 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download | duplicates (3)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
################################################################################
# Copyright (C) 2014 Jaakko Luttinen
#
# This file is licensed under the MIT License.
################################################################################


"""
Unit tests for `beta` module.
"""

import numpy as np
from scipy import special

from bayespy.nodes import Beta

from bayespy.utils import random

from bayespy.utils.misc import TestCase

class TestBeta(TestCase):
    """
    Unit tests for Beta node
    """

    
    def test_init(self):
        """
        Test the creation of beta nodes.
        """

        # Some simple initializations
        p = Beta([1.5, 4.2])

        # Check that plates are correct
        p = Beta([2, 3], plates=(4,3))
        self.assertEqual(p.plates,
                         (4,3))
        p = Beta(np.ones((4,3,2)))
        self.assertEqual(p.plates,
                         (4,3))

        # Parent not a vector
        self.assertRaises(ValueError,
                          Beta,
                          4)
        
        # Parent vector has wrong shape
        self.assertRaises(ValueError,
                          Beta,
                          [4])
        self.assertRaises(ValueError,
                          Beta,
                          [4,4,4])

        # Parent vector has invalid values
        self.assertRaises(ValueError,
                          Beta,
                          [-2,3])

        # Plates inconsistent
        self.assertRaises(ValueError,
                          Beta,
                          np.ones((4,2)),
                          plates=(3,))

        # Explicit plates too small
        self.assertRaises(ValueError,
                          Beta,
                          np.ones((4,2)),
                          plates=(1,))

        pass

    
    def test_moments(self):
        """
        Test the moments of beta nodes.
        """

        p = Beta([2, 3])
        u = p._message_to_child()
        self.assertAllClose(u[0],
                            special.psi([2,3]) - special.psi(2+3))
        
        pass

    
    def test_random(self):
        """
        Test random sampling of beta nodes.
        """

        p = Beta([1e20, 3e20])
        x = p.random()
        self.assertAllClose(x,
                            0.25)

        p = Beta([[1e20, 3e20],
                  [1e20, 1e20]])
        x = p.random()
        self.assertAllClose(x,
                            [0.25, 0.5])

        p = Beta([1e20, 3e20], plates=(3,))
        x = p.random()
        self.assertAllClose(x,
                            [0.25, 0.25, 0.25])

        pass