def build_bnet():
        """
        Builds QBnet called QuWetGrass with diamond shape
                Cloudy
                /    \
             Rain    Sprinkler
               \      /
               WetGrass
        All arrows pointing down

        """

        cl = BayesNode(0, name="Cloudy")
        sp = BayesNode(1, name="Sprinkler")
        ra = BayesNode(2, name="Rain")
        we = BayesNode(3, name="WetGrass")

        we.add_parent(sp)
        we.add_parent(ra)
        sp.add_parent(cl)
        ra.add_parent(cl)

        nodes = {cl, ra, sp, we}

        cl.potential = DiscreteUniPot(True, cl)  # P(a)
        sp.potential = DiscreteCondPot(True, [cl, sp])  # P(b| a)
        ra.potential = DiscreteCondPot(True, [cl, ra])
        we.potential = DiscreteCondPot(True, [sp, ra, we])

        # in general
        # DiscreteCondPot(True, [y1, y2, y3, x]) refers to A(x| y1, y2, y3)
        # off = 0
        # on = 1

        cl.potential.pot_arr[:] = [0.5 + 0.1j, 0.5]

        ra.potential.pot_arr[1, :] = [0.5 - 0.1j, 0.5 + 0.3j]
        ra.potential.pot_arr[0, :] = [0.4, 0.6 - 0.7j]

        sp.potential.pot_arr[1, :] = [0.7 + 3.0j, 0.3 - 1.0j]
        sp.potential.pot_arr[0, :] = [0.2 + 0.5j, 0.8]

        we.potential.pot_arr[1, 1, :] = [0.01 + 1j, 0.99]
        we.potential.pot_arr[1, 0, :] = [0.01 - 5.0j, 0.99]
        we.potential.pot_arr[0, 1, :] = [0.01, 0.99 + 2.3j]
        we.potential.pot_arr[0, 0, :] = [0.99, 0.01 - 0.01j]

        cl.potential.normalize_self()
        ra.potential.normalize_self()
        sp.potential.normalize_self()
        we.potential.normalize_self()

        return BayesNet(nodes)
    def build_bnet():
        """
        Builds QBnet called QuWetGrass with diamond shape
                Cloudy
                /    \
             Rain    Sprinkler
               \      /
               WetGrass
        All arrows pointing down

        """

        cl = BayesNode(0, name="Cloudy")
        sp = BayesNode(1, name="Sprinkler")
        ra = BayesNode(2, name="Rain")
        we = BayesNode(3, name="WetGrass")

        we.add_parent(sp)
        we.add_parent(ra)
        sp.add_parent(cl)
        ra.add_parent(cl)

        nodes = {cl, ra, sp, we}

        cl.potential = DiscreteUniPot(True, cl)  # P(a)
        sp.potential = DiscreteCondPot(True, [cl, sp])  # P(b| a)
        ra.potential = DiscreteCondPot(True, [cl, ra])
        we.potential = DiscreteCondPot(True, [sp, ra, we])

        # in general
        # DiscreteCondPot(True, [y1, y2, y3, x]) refers to A(x| y1, y2, y3)
        # off = 0
        # on = 1

        cl.potential.pot_arr[:] = [.5 + .1j, .5]

        ra.potential.pot_arr[1, :] = [.5 - .1j, .5 + .3j]
        ra.potential.pot_arr[0, :] = [.4, .6 - .7j]

        sp.potential.pot_arr[1, :] = [.7 + 3.j, .3 - 1.j]
        sp.potential.pot_arr[0, :] = [.2 + .5j, .8]

        we.potential.pot_arr[1, 1, :] = [.01 + 1j, .99]
        we.potential.pot_arr[1, 0, :] = [.01 - 5.j, .99]
        we.potential.pot_arr[0, 1, :] = [.01, .99 + 2.3j]
        we.potential.pot_arr[0, 0, :] = [.99, .01 - .01j]

        cl.potential.normalize_self()
        ra.potential.normalize_self()
        sp.potential.normalize_self()
        we.potential.normalize_self()

        return BayesNet(nodes)
Exemple #3
0
    def read_bif(path, is_quantum):
        """
        Reads a bif file using our stand-alone class BifTool and returns a
        BayesNet. bif and dot files complement each other. bif: graphical
        info No, pot info Yes. dot: graphical info Yes, pot info No. By pots
        I mean potentials, the transition matrices of the nodes. (aka CPTs,
        etc.)

        Parameters
        ----------
        path : str
        is_quantum : bool

        Returns
        -------
        BayesNet

        """

        bt = BifTool(is_quantum)
        bt.read_bif(path)
        nodes = set()
        name_to_nd = {}
        for k, nd_name in enumerate(bt.nd_sizes.keys()):
            node = BayesNode(k, nd_name)
            node.state_names = bt.states[nd_name]
            node.size = len(node.state_names)
            node.forget_all_evidence()
            nodes |= {node}
            name_to_nd[nd_name] = node
        for nd_name, pa_name_list in bt.parents.items():
            node = name_to_nd[nd_name]
            for pa_name in pa_name_list:
                pa = name_to_nd[pa_name]
                node.add_parent(pa)

        for nd_name, parent_names in bt.parents.items():
            node = name_to_nd[nd_name]
            num_pa = len(parent_names)
            parents = [name_to_nd[pa_name] for pa_name in parent_names]
            if num_pa == 0:
                node.potential = DiscreteUniPot(is_quantum, node)
            else:
                node.potential = DiscreteCondPot(
                    is_quantum, parents + [node])
            node.potential.pot_arr = bt.pot_arrays[nd_name]

        return BayesNet(nodes)
Exemple #4
0
    def build_bnet():
        """
        Builds CBnet called WetGrass with diamond shape
                Cloudy
                /    \
             Rain    Sprinkler
               \      /
               WetGrass
        All arrows pointing down
        """

        cl = BayesNode(0, name="Cloudy")
        sp = BayesNode(1, name="Sprinkler")
        ra = BayesNode(2, name="Rain")
        we = BayesNode(3, name="WetGrass")

        we.add_parent(sp)
        we.add_parent(ra)
        sp.add_parent(cl)
        ra.add_parent(cl)

        nodes = {cl, ra, sp, we}

        cl.potential = DiscreteUniPot(False, cl)  # P(a)
        sp.potential = DiscreteCondPot(False, [cl, sp])  # P(b| a)
        ra.potential = DiscreteCondPot(False, [cl, ra])
        we.potential = DiscreteCondPot(False, [sp, ra, we])

        # in general
        # DiscreteCondPot(False, [y1, y2, y3, x]) refers to P(x| y1, y2, y3)
        # off = 0
        # on = 1

        cl.potential.pot_arr[:] = [.5, .5]

        ra.potential.pot_arr[1, :] = [.5, .5]
        ra.potential.pot_arr[0, :] = [.4, .6]

        sp.potential.pot_arr[1, :] = [.7, .3]
        sp.potential.pot_arr[0, :] = [.2, .8]

        we.potential.pot_arr[1, 1, :] = [.01, .99]
        we.potential.pot_arr[1, 0, :] = [.01, .99]
        we.potential.pot_arr[0, 1, :] = [.01, .99]
        we.potential.pot_arr[0, 0, :] = [.99, .01]

        return BayesNet(nodes)
Exemple #5
0
    def build_bnet():
        """
        Builds CBnet called WetGrass with diamond shape
                Cloudy
                /    \
             Rain    Sprinkler
               \      /
               WetGrass
        All arrows pointing down
        """

        cl = BayesNode(0, name="Cloudy")
        sp = BayesNode(1, name="Sprinkler")
        ra = BayesNode(2, name="Rain")
        we = BayesNode(3, name="WetGrass")

        we.add_parent(sp)
        we.add_parent(ra)
        sp.add_parent(cl)
        ra.add_parent(cl)

        nodes = {cl, ra, sp, we}

        cl.potential = DiscreteUniPot(False, cl)  # P(a)
        sp.potential = DiscreteCondPot(False, [cl, sp])  # P(b| a)
        ra.potential = DiscreteCondPot(False, [cl, ra])
        we.potential = DiscreteCondPot(False, [sp, ra, we])

        # in general
        # DiscreteCondPot(False, [y1, y2, y3, x]) refers to P(x| y1, y2, y3)
        # off = 0
        # on = 1

        cl.potential.pot_arr[:] = [.5, .5]

        ra.potential.pot_arr[1, :] = [.5, .5]
        ra.potential.pot_arr[0, :] = [.4, .6]

        sp.potential.pot_arr[1, :] = [.7, .3]
        sp.potential.pot_arr[0, :] = [.2, .8]

        we.potential.pot_arr[1, 1, :] = [.01, .99]
        we.potential.pot_arr[1, 0, :] = [.01, .99]
        we.potential.pot_arr[0, 1, :] = [.01, .99]
        we.potential.pot_arr[0, 0, :] = [.99, .01]

        return BayesNet(nodes)
    def build_bnet():
        """
        Builds simple 7 node binary tree
                     a0
                   /    \
                  b0    b1
                 /  \  /  \
                c0  c1,c2 c3

        All arrows pointing down
        """

        a0 = BayesNode(0, name="a0")

        b0 = BayesNode(1, name="b0")
        b1 = BayesNode(2, name="b1")

        c0 = BayesNode(3, name="c0")
        c1 = BayesNode(4, name="c1")
        c2 = BayesNode(5, name="c2")
        c3 = BayesNode(6, name="c3")

        a0.add_children([b0, b1])
        b0.add_children([c0, c1])
        b1.add_children([c2, c3])

        nodes = {a0, b0, b1, c0, c1, c2, c3}

        a0.potential = DiscreteUniPot(False, a0)  # P(a)

        b0.potential = DiscreteCondPot(False, [a0, b0])  # P(b| a)
        b1.potential = DiscreteCondPot(False, [a0, b1])

        c0.potential = DiscreteCondPot(False, [b0, c0])
        c1.potential = DiscreteCondPot(False, [b0, c1])
        c2.potential = DiscreteCondPot(False, [b1, c2])
        c3.potential = DiscreteCondPot(False, [b1, c3])

        # in general
        # DiscreteCondPot(False, [y1, y2, y3, x]) refers to P(x| y1, y2, y3)
        # off = 0
        # on = 1

        a0.potential.pot_arr = np.array([.7, .3])

        b0.potential.pot_arr = np.array([[.5, .5], [.4, .6]])
        b1.potential.pot_arr = np.array([[.2, .8], [.4, .6]])

        c0.potential.pot_arr = np.array([[.3, .7], [.4, .6]])
        c1.potential.pot_arr = np.array([[.5, .5], [.9, .1]])
        c2.potential.pot_arr = np.array([[.8, .2], [.4, .6]])
        c3.potential.pot_arr = np.array([[.5, .5], [.6, .4]])

        return BayesNet(nodes)
Exemple #7
0
    def build_bnet():
        """
        Builds simple 7 node binary tree
                     a0
                   /    \
                  b0    b1
                 /  \  /  \
                c0  c1,c2 c3

        All arrows pointing down
        """

        a0 = BayesNode(0, name="a0")

        b0 = BayesNode(1, name="b0")
        b1 = BayesNode(2, name="b1")

        c0 = BayesNode(3, name="c0")
        c1 = BayesNode(4, name="c1")
        c2 = BayesNode(5, name="c2")
        c3 = BayesNode(6, name="c3")

        a0.add_children([b0, b1])
        b0.add_children([c0, c1])
        b1.add_children([c2, c3])

        nodes = {a0, b0, b1, c0, c1, c2, c3}

        a0.potential = DiscreteUniPot(False, a0)  # P(a)

        b0.potential = DiscreteCondPot(False, [a0, b0])  # P(b| a)
        b1.potential = DiscreteCondPot(False, [a0, b1])

        c0.potential = DiscreteCondPot(False, [b0, c0])
        c1.potential = DiscreteCondPot(False, [b0, c1])
        c2.potential = DiscreteCondPot(False, [b1, c2])
        c3.potential = DiscreteCondPot(False, [b1, c3])

        # in general
        # DiscreteCondPot(False, [y1, y2, y3, x]) refers to P(x| y1, y2, y3)
        # off = 0
        # on = 1

        a0.potential.pot_arr = np.array([.7, .3])

        b0.potential.pot_arr = np.array([[.5, .5], [.4, .6]])
        b1.potential.pot_arr = np.array([[.2, .8], [.4, .6]])

        c0.potential.pot_arr = np.array([[.3, .7], [.4, .6]])
        c1.potential.pot_arr = np.array([[.5, .5], [.9, .1]])
        c2.potential.pot_arr = np.array([[.8, .2], [.4, .6]])
        c3.potential.pot_arr = np.array([[.5, .5], [.6, .4]])

        return BayesNet(nodes)
Exemple #8
0
    def build_bnet():
        """
        Builds CBnet in accompanying gif : bnet_HuangDarwiche.gif

        From "Inference Belief Networks: A Procedural Guide", by C.Huang and
        A. Darwiche

        """

        a_node = BayesNode(0, name="A")
        b_node = BayesNode(1, name="B")
        c_node = BayesNode(2, name="C")
        d_node = BayesNode(3, name="D")
        e_node = BayesNode(4, name="E")
        f_node = BayesNode(5, name="F")
        g_node = BayesNode(6, name="G")
        h_node = BayesNode(7, name="H")

        b_node.add_parent(a_node)
        c_node.add_parent(a_node)
        d_node.add_parent(b_node)
        e_node.add_parent(c_node)
        f_node.add_parent(d_node)
        f_node.add_parent(e_node)
        g_node.add_parent(c_node)
        h_node.add_parent(e_node)
        h_node.add_parent(g_node)

        nodes = {
            a_node, b_node, c_node, d_node, e_node, f_node, g_node, h_node
        }

        a_node.potential = DiscreteUniPot(False, a_node)  # P(a)
        b_node.potential = DiscreteCondPot(False, [a_node, b_node])  # P(b| a)
        c_node.potential = DiscreteCondPot(False, [a_node, c_node])
        d_node.potential = DiscreteCondPot(False, [b_node, d_node])
        e_node.potential = DiscreteCondPot(False, [c_node, e_node])

        # P(f|d, e)
        f_node.potential = DiscreteCondPot(False, [d_node, e_node, f_node])

        g_node.potential = DiscreteCondPot(False, [c_node, g_node])
        h_node.potential = DiscreteCondPot(False, [e_node, g_node, h_node])

        # in general
        # DiscreteCondPot(False, [y1, y2, y3, x]) refers to P(x| y1, y2, y3)
        # off = 0
        # on = 1

        a_node.potential.pot_arr[:] = [.5, .5]

        b_node.potential.pot_arr[1, :] = [.5, .5]
        b_node.potential.pot_arr[0, :] = [.4, .6]

        c_node.potential.pot_arr[1, :] = [.7, .3]
        c_node.potential.pot_arr[0, :] = [.2, .8]

        d_node.potential.pot_arr[1, :] = [.9, .1]
        d_node.potential.pot_arr[0, :] = [.5, .5]

        e_node.potential.pot_arr[1, :] = [.3, .7]
        e_node.potential.pot_arr[0, :] = [.6, .4]

        f_node.potential.pot_arr[1, 1, :] = [.01, .99]
        f_node.potential.pot_arr[1, 0, :] = [.01, .99]
        f_node.potential.pot_arr[0, 1, :] = [.01, .99]
        f_node.potential.pot_arr[0, 0, :] = [.99, .01]

        g_node.potential.pot_arr[1, :] = [.8, .2]
        g_node.potential.pot_arr[0, :] = [.1, .9]

        h_node.potential.pot_arr[1, 1, :] = [.05, .95]
        h_node.potential.pot_arr[1, 0, :] = [.95, .05]
        h_node.potential.pot_arr[0, 1, :] = [.95, .05]
        h_node.potential.pot_arr[0, 0, :] = [.95, .05]

        return BayesNet(nodes)
Exemple #9
0
    def build_bnet():
        """
        Builds CBnet in accompanying gif : bnet_HuangDarwiche.gif

        From "Inference Belief Networks: A Procedural Guide", by C.Huang and
        A. Darwiche

        """

        a_node = BayesNode(0, name="A")
        b_node = BayesNode(1, name="B")
        c_node = BayesNode(2, name="C")
        d_node = BayesNode(3, name="D")
        e_node = BayesNode(4, name="E")
        f_node = BayesNode(5, name="F")
        g_node = BayesNode(6, name="G")
        h_node = BayesNode(7, name="H")

        b_node.add_parent(a_node)
        c_node.add_parent(a_node)
        d_node.add_parent(b_node)
        e_node.add_parent(c_node)
        f_node.add_parent(d_node)
        f_node.add_parent(e_node)
        g_node.add_parent(c_node)
        h_node.add_parent(e_node)
        h_node.add_parent(g_node)

        nodes = {a_node, b_node, c_node, d_node, e_node,
                f_node, g_node, h_node}

        a_node.potential = DiscreteUniPot(False, a_node)  # P(a)
        b_node.potential = DiscreteCondPot(False, [a_node, b_node])  # P(b| a)
        c_node.potential = DiscreteCondPot(False, [a_node, c_node])
        d_node.potential = DiscreteCondPot(False, [b_node, d_node])
        e_node.potential = DiscreteCondPot(False, [c_node, e_node])

        # P(f|d, e)
        f_node.potential = DiscreteCondPot(False, [d_node, e_node, f_node])

        g_node.potential = DiscreteCondPot(False, [c_node, g_node])
        h_node.potential = DiscreteCondPot(False, [e_node, g_node, h_node])

        # in general
        # DiscreteCondPot(False, [y1, y2, y3, x]) refers to P(x| y1, y2, y3)
        # off = 0
        # on = 1

        a_node.potential.pot_arr[:] = [.5, .5]

        b_node.potential.pot_arr[1, :] = [.5, .5]
        b_node.potential.pot_arr[0, :] = [.4, .6]

        c_node.potential.pot_arr[1, :] = [.7, .3]
        c_node.potential.pot_arr[0, :] = [.2, .8]

        d_node.potential.pot_arr[1, :] = [.9, .1]
        d_node.potential.pot_arr[0, :] = [.5, .5]

        e_node.potential.pot_arr[1, :] = [.3, .7]
        e_node.potential.pot_arr[0, :] = [.6, .4]

        f_node.potential.pot_arr[1, 1, :] = [.01, .99]
        f_node.potential.pot_arr[1, 0, :] = [.01, .99]
        f_node.potential.pot_arr[0, 1, :] = [.01, .99]
        f_node.potential.pot_arr[0, 0, :] = [.99, .01]

        g_node.potential.pot_arr[1, :] = [.8, .2]
        g_node.potential.pot_arr[0, :] = [.1, .9]

        h_node.potential.pot_arr[1, 1, :] = [.05, .95]
        h_node.potential.pot_arr[1, 0, :] = [.95, .05]
        h_node.potential.pot_arr[0, 1, :] = [.95, .05]
        h_node.potential.pot_arr[0, 0, :] = [.95, .05]

        return BayesNet(nodes)