<s> import unittest <EOL> import numpy as np <EOL> import numpy . testing as np_test <EOL> from pgmpy . inference import DBNInference <EOL> from pgmpy . models import DynamicBayesianNetwork <EOL> from pgmpy . factors import TabularCPD <EOL> class TestDBNInference ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> dbn_1 = DynamicBayesianNetwork ( ) <EOL> dbn_1 . add_edges_from ( <EOL> [ ( ( '<STR_LIT>' , <NUM_LIT:0> ) , ( '<STR_LIT:X>' , <NUM_LIT:0> ) ) , ( ( '<STR_LIT>' , <NUM_LIT:0> ) , ( '<STR_LIT:Y>' , <NUM_LIT:0> ) ) , ( ( '<STR_LIT>' , <NUM_LIT:0> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ) ] ) <EOL> cpd_start_z_1 = TabularCPD ( ( '<STR_LIT>' , <NUM_LIT:0> ) , <NUM_LIT:2> , [ [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> cpd_x_1 = TabularCPD ( <EOL> ( '<STR_LIT:X>' , <NUM_LIT:0> ) , <NUM_LIT:2> , [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] ] , [ ( '<STR_LIT>' , <NUM_LIT:0> ) ] , <NUM_LIT:2> ) <EOL> cpd_y_1 = TabularCPD ( <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:0> ) , <NUM_LIT:2> , [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , [ ( '<STR_LIT>' , <NUM_LIT:0> ) ] , <NUM_LIT:2> ) <EOL> cpd_trans_z_1 = TabularCPD ( <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <NUM_LIT:2> , [ [ <NUM_LIT> , <NUM_LIT:0.1> ] , [ <NUM_LIT:0.1> , <NUM_LIT> ] ] , [ ( '<STR_LIT>' , <NUM_LIT:0> ) ] , <NUM_LIT:2> ) <EOL> dbn_1 . add_cpds ( cpd_start_z_1 , cpd_trans_z_1 , cpd_x_1 , cpd_y_1 ) <EOL> dbn_1 . initialize_initial_state ( ) <EOL> self . dbn_inference_1 = DBNInference ( dbn_1 ) <EOL> dbn_2 = DynamicBayesianNetwork ( ) <EOL> dbn_2 . add_edges_from ( [ ( ( '<STR_LIT>' , <NUM_LIT:0> ) , ( '<STR_LIT:X>' , <NUM_LIT:0> ) ) , ( ( '<STR_LIT:X>' , <NUM_LIT:0> ) , ( '<STR_LIT:Y>' , <NUM_LIT:0> ) ) , <EOL> ( ( '<STR_LIT>' , <NUM_LIT:0> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ) ] ) <EOL> cpd_start_z_2 = TabularCPD ( ( '<STR_LIT>' , <NUM_LIT:0> ) , <NUM_LIT:2> , [ [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ] ) <EOL> cpd_x_2 = TabularCPD ( <EOL> ( '<STR_LIT:X>' , <NUM_LIT:0> ) , <NUM_LIT:2> , [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT:0.1> ] ] , [ ( '<STR_LIT>' , <NUM_LIT:0> ) ] , <NUM_LIT:2> ) <EOL> cpd_y_2 = TabularCPD ( <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:0> ) , <NUM_LIT:2> , [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) ] , <NUM_LIT:2> ) <EOL> cpd_z_2 = TabularCPD ( <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <NUM_LIT:2> , [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , [ ( '<STR_LIT>' , <NUM_LIT:0> ) ] , <NUM_LIT:2> ) <EOL> dbn_2 . add_cpds ( cpd_x_2 , cpd_y_2 , cpd_z_2 , cpd_start_z_2 ) <EOL> dbn_2 . initialize_initial_state ( ) <EOL> self . dbn_inference_2 = DBNInference ( dbn_2 ) <EOL> def test_forward_inf_single_variable ( self ) : <EOL> query_result = self . dbn_inference_1 . forward_inference ( [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_forward_inf_multiple_variable ( self ) : <EOL> query_result = self . dbn_inference_1 . forward_inference ( <EOL> [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) , ( '<STR_LIT:Y>' , <NUM_LIT:0> ) ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:Y>' , <NUM_LIT:0> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_forward_inf_single_variable_with_evidence ( self ) : <EOL> query_result = self . dbn_inference_1 . forward_inference ( [ ( <EOL> '<STR_LIT>' , <NUM_LIT:1> ) ] , { ( '<STR_LIT:Y>' , <NUM_LIT:0> ) : <NUM_LIT:0> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:1> ) : <NUM_LIT:0> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> query_result = self . dbn_inference_2 . forward_inference ( [ ( <EOL> '<STR_LIT:X>' , <NUM_LIT:2> ) ] , { ( '<STR_LIT:Y>' , <NUM_LIT:0> ) : <NUM_LIT:1> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:1> ) : <NUM_LIT:0> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:2> ) : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:X>' , <NUM_LIT:2> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_forward_inf_multiple_variable_with_evidence ( self ) : <EOL> query_result = self . dbn_inference_1 . forward_inference ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( <EOL> '<STR_LIT:X>' , <NUM_LIT:1> ) ] , { ( '<STR_LIT:Y>' , <NUM_LIT:0> ) : <NUM_LIT:0> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:1> ) : <NUM_LIT:0> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:X>' , <NUM_LIT:1> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_backward_inf_single_variable ( self ) : <EOL> query_result = self . dbn_inference_2 . backward_inference ( [ ( '<STR_LIT:Y>' , <NUM_LIT:0> ) ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:Y>' , <NUM_LIT:0> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_backward_inf_multiple_variables ( self ) : <EOL> query_result = self . dbn_inference_2 . backward_inference ( [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:0> ) ] ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:Y>' , <NUM_LIT:0> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_backward_inf_single_variable_with_evidence ( self ) : <EOL> query_result = self . dbn_inference_2 . backward_inference ( [ ( <EOL> '<STR_LIT:X>' , <NUM_LIT:0> ) ] , { ( '<STR_LIT:Y>' , <NUM_LIT:0> ) : <NUM_LIT:0> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:1> ) : <NUM_LIT:1> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:2> ) : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> query_result = self . dbn_inference_1 . backward_inference ( [ ( <EOL> '<STR_LIT>' , <NUM_LIT:1> ) ] , { ( '<STR_LIT:Y>' , <NUM_LIT:0> ) : <NUM_LIT:0> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:1> ) : <NUM_LIT:0> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:2> ) : <NUM_LIT:0> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> def test_backward_inf_multiple_variables_with_evidence ( self ) : <EOL> query_result = self . dbn_inference_2 . backward_inference ( [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) , ( <EOL> '<STR_LIT:X>' , <NUM_LIT:1> ) ] , { ( '<STR_LIT:Y>' , <NUM_LIT:0> ) : <NUM_LIT:0> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:1> ) : <NUM_LIT:1> , <EOL> ( '<STR_LIT:Y>' , <NUM_LIT:2> ) : <NUM_LIT:1> } ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:X>' , <NUM_LIT:0> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> np_test . assert_array_almost_equal ( query_result [ ( '<STR_LIT:X>' , <NUM_LIT:1> ) ] . values , <EOL> np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) </s>
