Example #1
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)
    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)
Example #3
0
    def __init__(self, is_quantum, states_df, vtx_to_states=None):
        """
        Constructor

        Parameters
        ----------
        is_quantum : bool
        states_df : pandas.DataFrame
        vtx_to_states : dict[str, list[str]]
            A dictionary mapping each node name to a list of its state names.
            This information will be stored in self.bnet. If
            vtx_to_states=None, constructor will learn vtx_to_states
            from states_df

        Returns
        -------

        """
        nd_names = states_df.columns
        ord_nodes = [BayesNode(k, nd_names[k]) for k in range(len(nd_names))]
        bnet = BayesNet(set(ord_nodes))
        self.is_quantum = is_quantum
        self.bnet = bnet
        self.states_df = states_df
        self.ord_nodes = ord_nodes
        if not vtx_to_states:
            bnet.learn_nd_state_names(states_df)
        else:
            bnet.import_nd_state_names(vtx_to_states)
Example #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)
Example #5
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)