Ejemplo n.º 1
0
    def setUp(self):
        """Create all data required to instantiate the bnet object"""
        nodes = 4
        dag = np.zeros((nodes, nodes))
        C = 0
        S = 1
        R = 2
        W = 3
        dag[C, [R, S]] = 1
        dag[R, W] = 1
        dag[S, W] = 1
        ns = 2 * np.ones((1, nodes))

        """Instantiate the CPD for each node in the network"""
        node_cpds = [[], [], [], []]
        CPT = np.array([0.5, 0.5])
        node_cpds[C] = cpds.tabular_CPD(C, ns, dag, CPT)
        CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
        node_cpds[R] = cpds.tabular_CPD(R, ns, dag, CPT)
        CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
        node_cpds[S] = cpds.tabular_CPD(S, ns, dag, CPT)
        CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
        node_cpds[W] = cpds.tabular_CPD(W, ns, dag, CPT)

        """Instantiate the object"""
        self.net = models.bnet(dag, ns, node_cpds)
Ejemplo n.º 2
0
def test_bnet_mle():
    """EXAMPLE: MLE learning on a BNET"""
    """Create all data required to instantiate the bnet object"""
    nodes = 4
    dag = np.zeros((nodes, nodes))
    C = 0
    S = 1
    R = 2
    W = 3
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1
    ns = 2 * np.ones((1, nodes))
    """Instantiate the model"""
    net = models.bnet(dag, ns, [])
    """Learn the parameters"""
    samples = np.array(pylab.load('./Data/lawn_samples.txt')) - 1
    net.learn_params_mle(samples.copy())
    """Initialize the inference engine"""
    net.init_inference_engine(exact=True)
    """Create and enter evidence"""
    evidences = create_all_evidence(4, 2)
    mlcs = np.array([[0, 0, 0, 0]])
    for evidence in evidences:
        mlc = net.max_sum(evidence)
        mlcs = np.vstack((mlcs, mlc))
    """Read in expected values"""
    exp_mlcs = np.array(pylab.load('./Data/bnet_mle_exact_max_sum_res.txt'))
    """Assert that the output matched the expected values"""
    assert_array_equal(mlcs, exp_mlcs)
Ejemplo n.º 3
0
def test_bnet_exact_sum_product():
    """EXAMPLE: Junction tree sum-product on BNET"""
    """Create all data required to instantiate the bnet object"""
    nodes = 4
    dag = np.zeros((nodes, nodes))
    C = 0
    S = 1
    R = 2
    W = 3
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1
    ns = 2 * np.ones(nodes, dtype='int')
    """Instantiate the CPD for each node in the network"""
    node_cpds = [[], [], [], []]
    CPT = np.array([0.5, 0.5])
    node_cpds[C] = cpds.TabularCPD(CPT)
    CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
    node_cpds[R] = cpds.TabularCPD(CPT)
    CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
    node_cpds[S] = cpds.TabularCPD(CPT)
    CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
    node_cpds[W] = cpds.TabularCPD(CPT)
    """Instantiate the object"""
    net = models.bnet(dag, ns, node_cpds=node_cpds)
    net.init_inference_engine(exact=True)
    """Create and enter evidence"""
    evidences = create_all_evidence(4, 2)
    results = []
    for evidence in [evidences[0]]:
        net.enter_evidence(evidence)
        net.sum_product()
        result = []
        result.append(np.max(net.marginal_nodes([0]).T))
        result.append(np.max(net.marginal_nodes([1]).T))
        result.append(np.max(net.marginal_nodes([2]).T))
        result.append(np.max(net.marginal_nodes([3]).T))
        results.append(result)

    results = np.array(results)
    """Get the expected results"""
    exp_results = np.array(
        pylab.loadtxt('./Data/bnet_exact_sum_product_res.txt'))
    """Assert that the output matched the expected values"""
    assert_array_equal(results, exp_results)
Ejemplo n.º 4
0
def test_bnet_EM():
    """EXAMPLE: EM learning on a BNET"""
    """Create all data required to instantiate the bnet object"""
    nodes = 4
    dag = np.zeros((nodes, nodes))
    C = 0
    S = 1
    R = 2
    W = 3
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1
    ns = 2 * np.ones((1, nodes))

    """Instantiate the model"""
    net = models.bnet(dag, ns, [])


    """
    Load the samples, and set one sample of one node to be unobserved, this
    should not effect the learnt parameter much, and will demonstrate that
    the algorithm can handle unobserved samples.
    """
    samples = (np.array(pylab.load('./Data/lawn_samples.txt')) - 1).tolist()
    samples[0][0] = []

    """Learn the parameters"""
    net.learn_params_EM(samples[:])
   
    """Initialize the inference engine"""
    net.init_inference_engine(exact=True)

    """Create and enter evidence"""
    evidences = create_all_evidence(4, 2)
    mlcs = np.array([[0, 0, 0, 0]])
    for evidence in evidences:
        mlc = net.max_sum(evidence)
        mlcs = np.vstack((mlcs, mlc))

    """Read in expected values"""
    exp_mlcs = np.array(pylab.load('./Data/bnet_mle_exact_max_sum_res.txt'))

    """Assert that the output matched the expected values"""
    assert_array_equal(mlcs, exp_mlcs)
 def setUp(self):
     """Create all data required to instantiate the bnet object"""
     nodes = 4
     dag = np.zeros((nodes, nodes))
     C = 0
     S = 1
     R = 2
     W = 3
     dag[C, [R, S]] = 1
     dag[R, W] = 1
     dag[S, W] = 1
     ns = 2 * np.ones((1, nodes))
     """Instantiate the CPD for each node in the network"""
     node_cpds = [[], [], [], []]
     CPT = np.array([0.5, 0.5])
     node_cpds[C] = cpds.tabular_CPD(C, ns, dag, CPT)
     CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
     node_cpds[R] = cpds.tabular_CPD(R, ns, dag, CPT)
     CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
     node_cpds[S] = cpds.tabular_CPD(S, ns, dag, CPT)
     CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
     node_cpds[W] = cpds.tabular_CPD(W, ns, dag, CPT)
     """Instantiate the object"""
     self.net = models.bnet(dag, ns, node_cpds)
Ejemplo n.º 6
0
def test_bnet_approx_max_sum():
    """EXAMPLE: Loopy belief max-sum on BNET"""
    """Create all data required to instantiate the bnet object"""
    nodes = 4
    dag = np.zeros((nodes, nodes))
    C = 0
    S = 1
    R = 2
    W = 3
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1
    ns = 2 * np.ones((1, nodes))
    """Instantiate the CPD for each node in the network"""
    node_cpds = [[], [], [], []]
    CPT = np.array([0.5, 0.5])
    node_cpds[C] = cpds.TabularCPD(CPT)
    CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
    node_cpds[R] = cpds.TabularCPD(CPT)
    CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
    node_cpds[S] = cpds.TabularCPD(CPT)
    CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
    node_cpds[W] = cpds.TabularCPD(CPT)
    """Instantiate the object"""
    net = models.bnet(dag, ns, node_cpds)
    net.init_inference_engine(exact=False)
    """Create and enter evidence"""
    evidences = create_all_evidence(4, 2)
    mlcs = np.array([0, 0, 0, 0])
    for evidence in evidences:
        mlc = net.max_sum(evidence)
        mlcs = np.vstack((mlcs, mlc))
    """Read in expected values"""
    exp_mlcs = np.array(pylab.load('./Data/bnet_approx_max_sum_res.txt'))
    """Assert that the output matched the expected values"""
    assert_array_equal(mlcs, exp_mlcs)
Ejemplo n.º 7
0
def test_bnet_mle():
    """EXAMPLE: MLE learning on a BNET"""
    """Create all data required to instantiate the bnet object"""
    nodes = 4
    dag = np.zeros((nodes, nodes))
    C = 0
    S = 1
    R = 2
    W = 3
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1
    ns = 2 * np.ones((1, nodes))

    """Instantiate the model"""
    net = models.bnet(dag, ns, [])

    """Learn the parameters"""
    samples = np.array(pylab.load('./Data/lawn_samples.txt')) - 1
    net.learn_params_mle(samples.copy())
   
    """Initialize the inference engine"""
    net.init_inference_engine(exact=True)

    """Create and enter evidence"""
    evidences = create_all_evidence(4, 2)
    mlcs = np.array([[0, 0, 0, 0]])
    for evidence in evidences:
        mlc = net.max_sum(evidence)
        mlcs = np.vstack((mlcs, mlc))

    """Read in expected values"""
    exp_mlcs = np.array(pylab.load('./Data/bnet_mle_exact_max_sum_res.txt'))

    """Assert that the output matched the expected values"""
    assert_array_equal(mlcs, exp_mlcs)
Ejemplo n.º 8
0
def test_bnet_EM():
    """EXAMPLE: EM learning on a BNET"""
    """Create all data required to instantiate the bnet object"""
    nodes = 4
    dag = np.zeros((nodes, nodes))
    C = 0
    S = 1
    R = 2
    W = 3
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1
    ns = 2 * np.ones((1, nodes))
    """Instantiate the model"""
    net = models.bnet(dag, ns, [])
    """
    Load the samples, and set one sample of one node to be unobserved, this
    should not effect the learnt parameter much, and will demonstrate that
    the algorithm can handle unobserved samples.
    """
    samples = (np.array(pylab.load('./Data/lawn_samples.txt')) - 1).tolist()
    samples[0][0] = []
    """Learn the parameters"""
    net.learn_params_EM(samples[:])
    """Initialize the inference engine"""
    net.init_inference_engine(exact=True)
    """Create and enter evidence"""
    evidences = create_all_evidence(4, 2)
    mlcs = np.array([[0, 0, 0, 0]])
    for evidence in evidences:
        mlc = net.max_sum(evidence)
        mlcs = np.vstack((mlcs, mlc))
    """Read in expected values"""
    exp_mlcs = np.array(pylab.load('./Data/bnet_mle_exact_max_sum_res.txt'))
    """Assert that the output matched the expected values"""
    assert_array_equal(mlcs, exp_mlcs)
def test_bnet_sumproduct():
    """
    Testing: SUM-PRODUCT on BNET
    This example is based on the lawn sprinkler example, and the Bayesian
    network has the following structure, with all edges directed downwards:

                            Cloudy - 0
                             /  \
                            /    \
                           /      \
                   Sprinkler - 1  Rainy - 2
                           \      /
                            \    /
                             \  /
                           Wet Grass -3                
    """
    """Assign a unique numerical identifier to each node"""
    C = 0
    S = 1
    R = 2
    W = 3

    """Assign the number of nodes in the graph"""
    nodes = 4

    """
    The graph structure is represented as a adjacency matrix, dag.
    If dag[i, j] = 1, then there exists a directed edge from node
    i and node j.
    """
    dag = np.zeros((nodes, nodes))
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1

    """
    Define the size of each node, which is the number of different values a
    node could observed at. For example, if a node is either True of False,
    it has only 2 possible values it could be, therefore its size is 2. All
    the nodes in this graph has a size 2.
    """
    node_sizes = 2 * np.ones(nodes)

    """
    We now need to assign a conditional probability distribution to each
    node.
    """
    node_cpds = [[], [], [], []]

    """Define the CPD for node 0"""
    CPT = np.array([0.5, 0.5])
    node_cpds[C] = cpds.TabularCPD(CPT)

    """Define the CPD for node 1"""
    CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
    node_cpds[R] = cpds.TabularCPD(CPT)

    """Define the CPD for node 2"""
    CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
    node_cpds[S] = cpds.TabularCPD(CPT)

    """Define the CPD for node 3"""
    CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
    node_cpds[W] = cpds.TabularCPD(CPT)

    """Create the Bayesian network"""
    net = models.bnet(dag, node_sizes, node_cpds=node_cpds)

    """
    Intialize the BNET's inference engine to use EXACT inference
    by setting exact=True.
    """
    net.init_inference_engine(exact=True)

    """Create and enter evidence ([] means that node is unobserved)"""
    all_ev = sprinkler_evidence();
    all_prob = sprinkler_probs();

    count = 0;
    errors = 0;
    for evidence in all_ev:
        """Execute the max-sum algorithm"""
        net.sum_product(evidence)

        ans = [1, 1, 1, 1]
        marginal = net.marginal_nodes([C])

        if evidence[C] is None:
            ans[C] = marginal.T[1]
            
        marginal = net.marginal_nodes([S])
        if evidence[S] is None:
            ans[S] = marginal.T[1]
            
        marginal = net.marginal_nodes([R])
        if evidence[R] is None:
            ans[R] = marginal.T[1]
            
        marginal = net.marginal_nodes([W])
        if evidence[W] is None:
            ans[W] = marginal.T[1]

        errors = errors +  \
                 np.round(np.sum(np.array(ans) - np.array(all_prob[count])), 3)
        count = count + 1

    assert errors == 0
Ejemplo n.º 10
0
 """
 node_cpds = [[], [], [], []]
 """Define the CPD for node 0"""
 CPT = np.array([0.5, 0.5])
 node_cpds[C] = cpds.TabularCPD(CPT)
 """Define the CPD for node 1"""
 CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
 node_cpds[R] = cpds.TabularCPD(CPT)
 """Define the CPD for node 2"""
 CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
 node_cpds[S] = cpds.TabularCPD(CPT)
 """Define the CPD for node 3"""
 CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
 node_cpds[W] = cpds.TabularCPD(CPT)
 """Create the Bayesian network"""
 net = models.bnet(dag, node_sizes, node_cpds=node_cpds)
 """
 Intialize the BNET's inference engine to use EXACT inference
 by setting exact=True.
 """
 net.init_inference_engine(exact=True)
 """Define observed evidence ([] means that node is unobserved)"""
 evidence = [None, 0, None, None]
 """Execute the sum-product algorithm"""
 #    net.enter_evidence(evidence)
 mlc = net.max_sum(evidence)
 """
 mlc contains the most likely configuaration for all the nodes in the BNET
 based in the input evidence.
 """
 print 'Cloudy node:     ', bool(mlc[C])
Ejemplo n.º 11
0
 """
 node_cpds = [[], [], [], []]
 """Define the CPD for node 0"""
 CPT = np.array([0.5, 0.5])
 node_cpds[C] = cpds.tabular_CPD(C, ns, dag, CPT)
 """Define the CPD for node 1"""
 CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
 node_cpds[R] = cpds.tabular_CPD(R, ns, dag, CPT)
 """Define the CPD for node 2"""
 CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
 node_cpds[S] = cpds.tabular_CPD(S, ns, dag, CPT)
 """Define the CPD for node 3"""
 CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
 node_cpds[W] = cpds.tabular_CPD(W, ns, dag, CPT)
 """Create the Bayesian network"""
 net = models.bnet(dag, ns, node_cpds)
 """
 Intialize the BNET's inference engine to use EXACT inference, by
 setting exact=True.
 """
 net.init_inference_engine(exact=True)
 """Create and enter evidence ([] means that node is unobserved)"""
 evidence = [[], 0, [], []]
 """Execute the max-sum algorithm"""
 mlc = net.max_sum(evidence)
 """
 mlc contains the most likely configuaration for all the nodes in the BNET
 based in the input evidence.
 """
 print 'Cloudy node:     ', bool(mlc[C])
 print 'Sprinkler node:  ', bool(mlc[S])
Ejemplo n.º 12
0
    """
    dag = np.zeros((nodes, nodes))
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1

    """
    Define the size of each node, which is the number of different values a
    node could observed at. For example, if a node is either True of False,
    it has only 2 possible values it could be, therefore its size is 2. All
    the nodes in this graph has a size 2.
    """
    ns = 2 * np.ones(nodes)

    """Create the BNET"""
    net = models.bnet(dag, ns)

    """Define the samples to train the models parameters with"""
    samples = \
           [[0, 1, 0, 0],
            [0, 1, 0, 1],
            [1, 0, 1, 1],
            [1, 0, 1, 0],
            [0, 1, 0, 1],
            [1, 0, 1, 1],
            [0, 1, 1, 1],
            [0, 1, 0, 1],
            [1, 0, 1, 1],
            [0, 1, 0, 1],
            [0, 1, 0, 1],
            [1, 0, 1, 1],
    node_cpds[C] = cpds.TabularCPD(CPT)

    """Define the CPD for node 1"""
    CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
    node_cpds[R] = cpds.TabularCPD(CPT)

    """Define the CPD for node 2"""
    CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
    node_cpds[S] = cpds.TabularCPD(CPT)

    """Define the CPD for node 3"""
    CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
    node_cpds[W] = cpds.TabularCPD(CPT)

    """Create the Bayesian network"""
    net = models.bnet(dag, node_sizes, node_cpds=node_cpds)

    """
    Intialize the BNET's inference engine to use EXACT inference
    by setting exact=True.
    """
    net.init_inference_engine(exact=True)

    """Define observed evidence ([] means that node is unobserved)"""
    evidence = [None, 0, None, None]

    """Execute the sum-product algorithm"""
    net.enter_evidence(evidence)
    net.sum_product()

    """
def test_bnet_sumproduct():
    """
    Testing: SUM-PRODUCT on BNET
    This example is based on the lawn sprinkler example, and the Bayesian
    network has the following structure, with all edges directed downwards:

                            Cloudy - 0
                             /  \
                            /    \
                           /      \
                   Sprinkler - 1  Rainy - 2
                           \      /
                            \    /
                             \  /
                           Wet Grass -3                
    """
    """Assign a unique numerical identifier to each node"""
    C = 0
    S = 1
    R = 2
    W = 3
    """Assign the number of nodes in the graph"""
    nodes = 4
    """
    The graph structure is represented as a adjacency matrix, dag.
    If dag[i, j] = 1, then there exists a directed edge from node
    i and node j.
    """
    dag = np.zeros((nodes, nodes))
    dag[C, [R, S]] = 1
    dag[R, W] = 1
    dag[S, W] = 1
    """
    Define the size of each node, which is the number of different values a
    node could observed at. For example, if a node is either True of False,
    it has only 2 possible values it could be, therefore its size is 2. All
    the nodes in this graph has a size 2.
    """
    node_sizes = 2 * np.ones(nodes)
    """
    We now need to assign a conditional probability distribution to each
    node.
    """
    node_cpds = [[], [], [], []]
    """Define the CPD for node 0"""
    CPT = np.array([0.5, 0.5])
    node_cpds[C] = cpds.TabularCPD(CPT)
    """Define the CPD for node 1"""
    CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
    node_cpds[R] = cpds.TabularCPD(CPT)
    """Define the CPD for node 2"""
    CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
    node_cpds[S] = cpds.TabularCPD(CPT)
    """Define the CPD for node 3"""
    CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
    node_cpds[W] = cpds.TabularCPD(CPT)
    """Create the Bayesian network"""
    net = models.bnet(dag, node_sizes, node_cpds=node_cpds)
    """
    Intialize the BNET's inference engine to use EXACT inference
    by setting exact=True.
    """
    net.init_inference_engine(exact=True)
    """Create and enter evidence ([] means that node is unobserved)"""
    all_ev = sprinkler_evidence()
    all_prob = sprinkler_probs()

    count = 0
    errors = 0
    for evidence in all_ev:
        """Execute the max-sum algorithm"""
        net.sum_product(evidence)

        ans = [1, 1, 1, 1]
        marginal = net.marginal_nodes([C])

        if evidence[C] is None:
            ans[C] = marginal.T[1]

        marginal = net.marginal_nodes([S])
        if evidence[S] is None:
            ans[S] = marginal.T[1]

        marginal = net.marginal_nodes([R])
        if evidence[R] is None:
            ans[R] = marginal.T[1]

        marginal = net.marginal_nodes([W])
        if evidence[W] is None:
            ans[W] = marginal.T[1]

        errors = errors +  \
                 np.round(np.sum(np.array(ans) - np.array(all_prob[count])), 3)
        count = count + 1

    assert errors == 0
 If dag[i, j] = 1, then there exists a directed edge from node
 i and node j.
 """
 dag = np.zeros((nodes, nodes))
 dag[C, [R, S]] = 1
 dag[R, W] = 1
 dag[S, W] = 1
 """
 Define the size of each node, which is the number of different values a
 node could observed at. For example, if a node is either True of False,
 it has only 2 possible values it could be, therefore its size is 2. All
 the nodes in this graph has a size 2.
 """
 ns = 2 * np.ones(nodes)
 """Create the BNET"""
 net = models.bnet(dag, ns)
 """Define the samples to train the models parameters with"""
 samples = \
        [[0, 1, 0, 0],
         [0, 1, 0, 1],
         [1, 0, 1, 1],
         [1, 0, 1, 0],
         [0, 1, 0, 1],
         [1, 0, 1, 1],
         [0, 1, 1, 1],
         [0, 1, 0, 1],
         [1, 0, 1, 1],
         [0, 1, 0, 1],
         [0, 1, 0, 1],
         [1, 0, 1, 1],
         [0, 1, 0, 1],
    node_cpds[C] = cpds.TabularCPD(CPT)

    """Define the CPD for node 1"""
    CPT = np.array([[0.8, 0.2], [0.2, 0.8]])
    node_cpds[R] = cpds.TabularCPD(CPT)

    """Define the CPD for node 2"""
    CPT = np.array([[0.5, 0.5], [0.9, 0.1]])
    node_cpds[S] = cpds.TabularCPD(CPT)

    """Define the CPD for node 3"""
    CPT = np.array([[[1, 0], [0.1, 0.9]], [[0.1, 0.9], [0.01, 0.99]]])
    node_cpds[W] = cpds.TabularCPD(CPT)

    """Create the Bayesian network"""
    net = models.bnet(dag, ns, node_cpds=node_cpds)

    """
    Intialize the BNET's inference engine to use approximate inference, by
    setting exact=False.
    """
    net.init_inference_engine(exact=False)

    """Create and enter evidence ([] means that node is unobserved)"""
    evidence = [None, 0, None, None]

    """Execute the max-sum algorithm"""
    mlc = net.max_sum(evidence)

    """
    mlc contains the most likely configuaration for all the nodes in the BNET