<s> import unittest <EOL> import numpy as np <EOL> import numpy . testing as np_test <EOL> from pgmpy . inference import VariableElimination <EOL> from pgmpy . inference import BeliefPropagation <EOL> from pgmpy . models import BayesianModel , MarkovModel <EOL> from pgmpy . models import JunctionTree <EOL> from pgmpy . factors import TabularCPD <EOL> from pgmpy . factors import Factor <EOL> from pgmpy . extern . six . moves import range <EOL> class TestVariableElimination ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . bayesian_model = BayesianModel ( [ ( '<STR_LIT:A>' , '<STR_LIT>' ) , ( '<STR_LIT:R>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:L>' ) , ( '<STR_LIT>' , '<STR_LIT:L>' ) ] ) <EOL> cpd_a = TabularCPD ( '<STR_LIT:A>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> cpd_r = TabularCPD ( '<STR_LIT:R>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> cpd_j = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT:A>' , '<STR_LIT:R>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> cpd_q = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:L>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> cpd_g = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> self . bayesian_model . add_cpds ( cpd_a , cpd_g , cpd_j , cpd_l , cpd_q , cpd_r ) <EOL> self . bayesian_inference = VariableElimination ( self . bayesian_model ) <EOL> def test_query_single_variable ( self ) : <EOL> query_result = self . bayesian_inference . query ( [ '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_multiple_variable ( self ) : <EOL> query_result = self . bayesian_inference . query ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_single_variable_with_evidence ( self ) : <EOL> query_result = self . bayesian_inference . query ( variables = [ '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_multiple_variable_with_evidence ( self ) : <EOL> query_result = self . bayesian_inference . query ( variables = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_multiple_times ( self ) : <EOL> query_result = self . bayesian_inference . query ( [ '<STR_LIT>' ] ) <EOL> query_result = self . bayesian_inference . query ( [ '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> query_result = self . bayesian_inference . query ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> query_result = self . bayesian_inference . query ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> query_result = self . bayesian_inference . query ( variables = [ '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:1> } ) <EOL> query_result = self . bayesian_inference . query ( variables = [ '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> query_result = self . bayesian_inference . query ( variables = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:1> } ) <EOL> query_result = self . bayesian_inference . query ( variables = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_max_marginal ( self ) : <EOL> np_test . assert_almost_equal ( self . bayesian_inference . max_marginal ( ) , <NUM_LIT> , decimal = <NUM_LIT:4> ) <EOL> def test_max_marginal_var ( self ) : <EOL> np_test . assert_almost_equal ( self . bayesian_inference . max_marginal ( [ '<STR_LIT>' ] ) , <NUM_LIT> , decimal = <NUM_LIT:4> ) <EOL> def test_max_marginal_var1 ( self ) : <EOL> np_test . assert_almost_equal ( self . bayesian_inference . max_marginal ( [ '<STR_LIT>' , '<STR_LIT:R>' ] ) , <EOL> <NUM_LIT> , decimal = <NUM_LIT:4> ) <EOL> def test_max_marginal_var2 ( self ) : <EOL> np_test . assert_almost_equal ( self . bayesian_inference . max_marginal ( [ '<STR_LIT>' , '<STR_LIT:R>' , '<STR_LIT:A>' ] ) , <EOL> <NUM_LIT> , decimal = <NUM_LIT:4> ) <EOL> def test_map_query ( self ) : <EOL> map_query = self . bayesian_inference . map_query ( ) <EOL> self . assertDictEqual ( map_query , { '<STR_LIT:A>' : <NUM_LIT:1> , '<STR_LIT:R>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:L>' : <NUM_LIT:0> } ) <EOL> def test_map_query_with_evidence ( self ) : <EOL> map_query = self . bayesian_inference . map_query ( [ '<STR_LIT:A>' , '<STR_LIT:R>' , '<STR_LIT:L>' ] , <EOL> { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . assertDictEqual ( map_query , { '<STR_LIT:A>' : <NUM_LIT:1> , '<STR_LIT:R>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:0> } ) <EOL> def test_induced_graph ( self ) : <EOL> induced_graph = self . bayesian_inference . induced_graph ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:A>' , '<STR_LIT>' , '<STR_LIT:L>' , '<STR_LIT:R>' ] ) <EOL> result_edges = sorted ( [ sorted ( x ) for x in induced_graph . edges ( ) ] ) <EOL> self . assertEqual ( [ [ '<STR_LIT:A>' , '<STR_LIT>' ] , [ '<STR_LIT:A>' , '<STR_LIT:R>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT:L>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:L>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT:R>' ] , [ '<STR_LIT:L>' , '<STR_LIT:R>' ] ] , <EOL> result_edges ) <EOL> def test_induced_width ( self ) : <EOL> result_width = self . bayesian_inference . induced_width ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:A>' , '<STR_LIT>' , '<STR_LIT:L>' , '<STR_LIT:R>' ] ) <EOL> self . assertEqual ( <NUM_LIT:2> , result_width ) <EOL> def tearDown ( self ) : <EOL> del self . bayesian_inference <EOL> del self . bayesian_model <EOL> class TestVariableEliminationMarkov ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . markov_model = MarkovModel ( [ ( '<STR_LIT:A>' , '<STR_LIT>' ) , ( '<STR_LIT:R>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT:L>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:L>' ) , ( '<STR_LIT:A>' , '<STR_LIT:R>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> factor_a = TabularCPD ( '<STR_LIT:A>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) . to_factor ( ) <EOL> factor_r = TabularCPD ( '<STR_LIT:R>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) . to_factor ( ) <EOL> factor_j = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT:A>' , '<STR_LIT:R>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) . to_factor ( ) <EOL> factor_q = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> ] ) . to_factor ( ) <EOL> factor_l = TabularCPD ( '<STR_LIT:L>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) . to_factor ( ) <EOL> factor_g = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) . to_factor ( ) <EOL> self . markov_model . add_factors ( factor_a , factor_r , factor_j , factor_q , factor_l , factor_g ) <EOL> self . markov_inference = VariableElimination ( self . markov_model ) <EOL> def test_query_single_variable ( self ) : <EOL> query_result = self . markov_inference . query ( [ '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_multiple_variable ( self ) : <EOL> query_result = self . markov_inference . query ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_single_variable_with_evidence ( self ) : <EOL> query_result = self . markov_inference . query ( variables = [ '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_multiple_variable_with_evidence ( self ) : <EOL> query_result = self . markov_inference . query ( variables = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_multiple_times ( self ) : <EOL> query_result = self . markov_inference . query ( [ '<STR_LIT>' ] ) <EOL> query_result = self . markov_inference . query ( [ '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> query_result = self . markov_inference . query ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> query_result = self . markov_inference . query ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> query_result = self . markov_inference . query ( variables = [ '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:1> } ) <EOL> query_result = self . markov_inference . query ( variables = [ '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> query_result = self . markov_inference . query ( variables = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:1> } ) <EOL> query_result = self . markov_inference . query ( variables = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_max_marginal ( self ) : <EOL> np_test . assert_almost_equal ( self . markov_inference . max_marginal ( ) , <NUM_LIT> , decimal = <NUM_LIT:4> ) <EOL> def test_max_marginal_var ( self ) : <EOL> np_test . assert_almost_equal ( self . markov_inference . max_marginal ( [ '<STR_LIT>' ] ) , <NUM_LIT> , decimal = <NUM_LIT:4> ) <EOL> def test_max_marginal_var1 ( self ) : <EOL> np_test . assert_almost_equal ( self . markov_inference . max_marginal ( [ '<STR_LIT>' , '<STR_LIT:R>' ] ) , <EOL> <NUM_LIT> , decimal = <NUM_LIT:4> ) <EOL> def test_max_marginal_var2 ( self ) : <EOL> np_test . assert_almost_equal ( self . markov_inference . max_marginal ( [ '<STR_LIT>' , '<STR_LIT:R>' , '<STR_LIT:A>' ] ) , <EOL> <NUM_LIT> , decimal = <NUM_LIT:4> ) <EOL> def test_map_query ( self ) : <EOL> map_query = self . markov_inference . map_query ( ) <EOL> self . assertDictEqual ( map_query , { '<STR_LIT:A>' : <NUM_LIT:1> , '<STR_LIT:R>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:0> } ) <EOL> def test_map_query_with_evidence ( self ) : <EOL> map_query = self . markov_inference . map_query ( [ '<STR_LIT:A>' , '<STR_LIT:R>' , '<STR_LIT:L>' ] , <EOL> { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . assertDictEqual ( map_query , { '<STR_LIT:A>' : <NUM_LIT:1> , '<STR_LIT:R>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:0> } ) <EOL> def test_induced_graph ( self ) : <EOL> induced_graph = self . markov_inference . induced_graph ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:A>' , '<STR_LIT>' , '<STR_LIT:L>' , '<STR_LIT:R>' ] ) <EOL> result_edges = sorted ( [ sorted ( x ) for x in induced_graph . edges ( ) ] ) <EOL> self . assertEqual ( [ [ '<STR_LIT:A>' , '<STR_LIT>' ] , [ '<STR_LIT:A>' , '<STR_LIT:R>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT:L>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:L>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT:R>' ] , [ '<STR_LIT:L>' , '<STR_LIT:R>' ] ] , <EOL> result_edges ) <EOL> def test_induced_width ( self ) : <EOL> result_width = self . markov_inference . induced_width ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:A>' , '<STR_LIT>' , '<STR_LIT:L>' , '<STR_LIT:R>' ] ) <EOL> self . assertEqual ( <NUM_LIT:2> , result_width ) <EOL> def tearDown ( self ) : <EOL> del self . markov_inference <EOL> del self . markov_model <EOL> class TestBeliefPropagation ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . junction_tree = JunctionTree ( [ ( ( '<STR_LIT:A>' , '<STR_LIT:B>' ) , ( '<STR_LIT:B>' , '<STR_LIT:C>' ) ) , <EOL> ( ( '<STR_LIT:B>' , '<STR_LIT:C>' ) , ( '<STR_LIT:C>' , '<STR_LIT:D>' ) ) ] ) <EOL> phi1 = Factor ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] , range ( <NUM_LIT:6> ) ) <EOL> phi2 = Factor ( [ '<STR_LIT:B>' , '<STR_LIT:C>' ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] , range ( <NUM_LIT:6> ) ) <EOL> phi3 = Factor ( [ '<STR_LIT:C>' , '<STR_LIT:D>' ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , range ( <NUM_LIT:4> ) ) <EOL> self . junction_tree . add_factors ( phi1 , phi2 , phi3 ) <EOL> self . bayesian_model = BayesianModel ( [ ( '<STR_LIT:A>' , '<STR_LIT>' ) , ( '<STR_LIT:R>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:L>' ) , ( '<STR_LIT>' , '<STR_LIT:L>' ) ] ) <EOL> cpd_a = TabularCPD ( '<STR_LIT:A>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> cpd_r = TabularCPD ( '<STR_LIT:R>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> cpd_j = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT:A>' , '<STR_LIT:R>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> cpd_q = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:L>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> cpd_g = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> self . bayesian_model . add_cpds ( cpd_a , cpd_g , cpd_j , cpd_l , cpd_q , cpd_r ) <EOL> def test_calibrate_clique_belief ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . junction_tree ) <EOL> belief_propagation . calibrate ( ) <EOL> clique_belief = belief_propagation . get_clique_beliefs ( ) <EOL> phi1 = Factor ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] , range ( <NUM_LIT:6> ) ) <EOL> phi2 = Factor ( [ '<STR_LIT:B>' , '<STR_LIT:C>' ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] , range ( <NUM_LIT:6> ) ) <EOL> phi3 = Factor ( [ '<STR_LIT:C>' , '<STR_LIT:D>' ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , range ( <NUM_LIT:4> ) ) <EOL> b_A_B = phi1 * ( phi3 . marginalize ( [ '<STR_LIT:D>' ] , inplace = False ) * phi2 ) . marginalize ( [ '<STR_LIT:C>' ] , inplace = False ) <EOL> b_B_C = phi2 * ( phi1 . marginalize ( [ '<STR_LIT:A>' ] , inplace = False ) * phi3 . marginalize ( [ '<STR_LIT:D>' ] , inplace = False ) ) <EOL> b_C_D = phi3 * ( phi1 . marginalize ( [ '<STR_LIT:A>' ] , inplace = False ) * phi2 ) . marginalize ( [ '<STR_LIT:B>' ] , inplace = False ) <EOL> np_test . assert_array_almost_equal ( clique_belief [ ( '<STR_LIT:A>' , '<STR_LIT:B>' ) ] . values , b_A_B . values ) <EOL> np_test . assert_array_almost_equal ( clique_belief [ ( '<STR_LIT:B>' , '<STR_LIT:C>' ) ] . values , b_B_C . values ) <EOL> np_test . assert_array_almost_equal ( clique_belief [ ( '<STR_LIT:C>' , '<STR_LIT:D>' ) ] . values , b_C_D . values ) <EOL> def test_calibrate_sepset_belief ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . junction_tree ) <EOL> belief_propagation . calibrate ( ) <EOL> sepset_belief = belief_propagation . get_sepset_beliefs ( ) <EOL> phi1 = Factor ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] , range ( <NUM_LIT:6> ) ) <EOL> phi2 = Factor ( [ '<STR_LIT:B>' , '<STR_LIT:C>' ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] , range ( <NUM_LIT:6> ) ) <EOL> phi3 = Factor ( [ '<STR_LIT:C>' , '<STR_LIT:D>' ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , range ( <NUM_LIT:4> ) ) <EOL> b_B = ( phi1 * ( phi3 . marginalize ( [ '<STR_LIT:D>' ] , inplace = False ) * <EOL> phi2 ) . marginalize ( [ '<STR_LIT:C>' ] , inplace = False ) ) . marginalize ( [ '<STR_LIT:A>' ] , inplace = False ) <EOL> b_C = ( phi2 * ( phi1 . marginalize ( [ '<STR_LIT:A>' ] , inplace = False ) * <EOL> phi3 . marginalize ( [ '<STR_LIT:D>' ] , inplace = False ) ) ) . marginalize ( [ '<STR_LIT:B>' ] , inplace = False ) <EOL> np_test . assert_array_almost_equal ( sepset_belief [ frozenset ( ( ( '<STR_LIT:A>' , '<STR_LIT:B>' ) , ( '<STR_LIT:B>' , '<STR_LIT:C>' ) ) ) ] . values , b_B . values ) <EOL> np_test . assert_array_almost_equal ( sepset_belief [ frozenset ( ( ( '<STR_LIT:B>' , '<STR_LIT:C>' ) , ( '<STR_LIT:C>' , '<STR_LIT:D>' ) ) ) ] . values , b_C . values ) <EOL> def test_max_calibrate_clique_belief ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . junction_tree ) <EOL> belief_propagation . max_calibrate ( ) <EOL> clique_belief = belief_propagation . get_clique_beliefs ( ) <EOL> phi1 = Factor ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] , range ( <NUM_LIT:6> ) ) <EOL> phi2 = Factor ( [ '<STR_LIT:B>' , '<STR_LIT:C>' ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] , range ( <NUM_LIT:6> ) ) <EOL> phi3 = Factor ( [ '<STR_LIT:C>' , '<STR_LIT:D>' ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , range ( <NUM_LIT:4> ) ) <EOL> b_A_B = phi1 * ( phi3 . maximize ( [ '<STR_LIT:D>' ] , inplace = False ) * phi2 ) . maximize ( [ '<STR_LIT:C>' ] , inplace = False ) <EOL> b_B_C = phi2 * ( phi1 . maximize ( [ '<STR_LIT:A>' ] , inplace = False ) * phi3 . maximize ( [ '<STR_LIT:D>' ] , inplace = False ) ) <EOL> b_C_D = phi3 * ( phi1 . maximize ( [ '<STR_LIT:A>' ] , inplace = False ) * phi2 ) . maximize ( [ '<STR_LIT:B>' ] , inplace = False ) <EOL> np_test . assert_array_almost_equal ( clique_belief [ ( '<STR_LIT:A>' , '<STR_LIT:B>' ) ] . values , b_A_B . values ) <EOL> np_test . assert_array_almost_equal ( clique_belief [ ( '<STR_LIT:B>' , '<STR_LIT:C>' ) ] . values , b_B_C . values ) <EOL> np_test . assert_array_almost_equal ( clique_belief [ ( '<STR_LIT:C>' , '<STR_LIT:D>' ) ] . values , b_C_D . values ) <EOL> def test_max_calibrate_sepset_belief ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . junction_tree ) <EOL> belief_propagation . max_calibrate ( ) <EOL> sepset_belief = belief_propagation . get_sepset_beliefs ( ) <EOL> phi1 = Factor ( [ '<STR_LIT:A>' , '<STR_LIT:B>' ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] , range ( <NUM_LIT:6> ) ) <EOL> phi2 = Factor ( [ '<STR_LIT:B>' , '<STR_LIT:C>' ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] , range ( <NUM_LIT:6> ) ) <EOL> phi3 = Factor ( [ '<STR_LIT:C>' , '<STR_LIT:D>' ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] , range ( <NUM_LIT:4> ) ) <EOL> b_B = ( phi1 * ( phi3 . maximize ( [ '<STR_LIT:D>' ] , inplace = False ) * <EOL> phi2 ) . maximize ( [ '<STR_LIT:C>' ] , inplace = False ) ) . maximize ( [ '<STR_LIT:A>' ] , inplace = False ) <EOL> b_C = ( phi2 * ( phi1 . maximize ( [ '<STR_LIT:A>' ] , inplace = False ) * <EOL> phi3 . maximize ( [ '<STR_LIT:D>' ] , inplace = False ) ) ) . maximize ( [ '<STR_LIT:B>' ] , inplace = False ) <EOL> np_test . assert_array_almost_equal ( sepset_belief [ frozenset ( ( ( '<STR_LIT:A>' , '<STR_LIT:B>' ) , ( '<STR_LIT:B>' , '<STR_LIT:C>' ) ) ) ] . values , b_B . values ) <EOL> np_test . assert_array_almost_equal ( sepset_belief [ frozenset ( ( ( '<STR_LIT:B>' , '<STR_LIT:C>' ) , ( '<STR_LIT:C>' , '<STR_LIT:D>' ) ) ) ] . values , b_C . values ) <EOL> def test_query_single_variable ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . bayesian_model ) <EOL> query_result = belief_propagation . query ( [ '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_multiple_variable ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . bayesian_model ) <EOL> query_result = belief_propagation . query ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_single_variable_with_evidence ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . bayesian_model ) <EOL> query_result = belief_propagation . query ( variables = [ '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_query_multiple_variable_with_evidence ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . bayesian_model ) <EOL> query_result = belief_propagation . query ( variables = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> evidence = { '<STR_LIT:A>' : <NUM_LIT:0> , '<STR_LIT:R>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ '<STR_LIT>' ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_map_query ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . bayesian_model ) <EOL> map_query = belief_propagation . map_query ( ) <EOL> self . assertDictEqual ( map_query , { '<STR_LIT:A>' : <NUM_LIT:1> , '<STR_LIT:R>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:L>' : <NUM_LIT:0> } ) <EOL> def test_map_query_with_evidence ( self ) : <EOL> belief_propagation = BeliefPropagation ( self . bayesian_model ) <EOL> map_query = belief_propagation . map_query ( [ '<STR_LIT:A>' , '<STR_LIT:R>' , '<STR_LIT:L>' ] , <EOL> { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . assertDictEqual ( map_query , { '<STR_LIT:A>' : <NUM_LIT:1> , '<STR_LIT:R>' : <NUM_LIT:0> , '<STR_LIT:L>' : <NUM_LIT:0> } ) <EOL> def tearDown ( self ) : <EOL> del self . junction_tree <EOL> del self . bayesian_model </s>
