1212
1313from causallearn .search .Granger .Granger import Granger
1414
15+ ######################################### Test Notes ###########################################
16+ # All the benchmark results (p_value_matrix_truth, adj_matrix_truth, coeff_truth) #
17+ # are obtained from the code of causal-learn as of commit #
18+ # https://github.com/cmu-phil/causal-learn/commit/b49980d046607baaaa66ff8dc0ceb98452ab8616 #
19+ # (b49980d). #
20+ # #
21+ # We are not sure if the results are completely "correct" (reflect ground truth graph) or not. #
22+ # So if you find your tests failed, it means that your modified code is logically inconsistent #
23+ # with the code as of b49980d, but not necessarily means that your code is "wrong". #
24+ # If you are sure that your modification is "correct" (e.g. fixed some bugs in b49980d), #
25+ # please report it to us. We will then modify these benchmark results accordingly. Thanks :) #
26+ ######################################### Test Notes ###########################################
27+
1528
1629class TestGranger (unittest .TestCase ):
1730 # simulate data from a VAR model
1831 def syn_data_3d (self ):
1932 # generate transition matrix, time lag 2
2033 np .random .seed (0 )
2134 A = 0.2 * np .random .rand (3 ,6 )
22- print ('True matrix is \n {}' .format (A ))
2335 # generate time series
2436 T = 1000
2537 data = np .random .rand (3 , T )
@@ -35,7 +47,6 @@ def syn_data_2d(self):
3547 A = 0.5 * np .random .rand (2 ,4 )
3648 A [0 ,1 ] = 0
3749 A [0 ,3 ] = 0
38- print ('True matrix is \n {}' .format (A ))
3950 # generate time series
4051 T = 100
4152 data = np .random .rand (2 , T )
@@ -54,8 +65,10 @@ def test_granger_test(self):
5465 dataset = self .syn_data_2d ()
5566 G = Granger ()
5667 p_value_matrix , adj_matrix = G .granger_test_2d (data = dataset )
57- print ('P-value matrix is \n {}' .format (p_value_matrix ))
58- print ('Adjacency matrix is \n {}' .format (adj_matrix ))
68+ p_value_matrix_truth = np .array ([[0 , 0.5989 , 0 , 0.5397 ], [0.0006 , 0 , 0.0014 , 0 ]])
69+ adj_matrix_truth = np .array ([[1 , 0 , 1 , 0 ], [1 , 1 , 1 , 1 ]])
70+ self .assertEqual ((np .round (p_value_matrix , 4 ) - p_value_matrix_truth ).all (), 0 )
71+ self .assertEqual ((adj_matrix - adj_matrix_truth ).all (), 0 )
5972
6073 # example2
6174 # for data with multi-dimensional variables, granger lasso regression.
@@ -66,10 +79,8 @@ def test_granger_lasso(self):
6679 dataset = self .syn_data_3d ()
6780 G = Granger ()
6881 coeff = G .granger_lasso (data = dataset )
69- print ('Estimated matrix is \n {}' .format (coeff ))
70-
82+ coeff_truth = np .array ([[0.09 , 0.1101 , 0.1527 , 0.1127 , 0.0226 , 0.1538 ],
83+ [0.1004 , 0.15 , 0.1757 , 0.1037 , 0.1612 , 0.0987 ],
84+ [0.1155 , 0.1485 , 0 , 0.039 , - 0. , 0.1085 ]])
85+ self .assertEqual ((np .round (coeff , 4 ) - coeff_truth ).all (), 0 )
7186
72- if __name__ == '__main__' :
73- test = TestGranger ()
74- test .test_granger_test ()
75- test .test_granger_lasso ()
0 commit comments