Skip to content

Commit 966aa4c

Browse files
committed
Add test to GrangerCausality
1 parent 7539522 commit 966aa4c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

tests/TestGranger.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def syn_data_3d(self):
1919
# generate transition matrix, time lag 2
2020
np.random.seed(0)
2121
A = 0.2 * np.random.rand(3,6)
22-
print('True matrix is \n {}'.format(A))
2322
# generate time series
2423
T = 1000
2524
data = np.random.rand(3, T)
@@ -35,7 +34,6 @@ def syn_data_2d(self):
3534
A = 0.5*np.random.rand(2,4)
3635
A[0,1] = 0
3736
A[0,3] = 0
38-
print('True matrix is \n {}'.format(A))
3937
# generate time series
4038
T = 100
4139
data = np.random.rand(2, T)
@@ -54,8 +52,10 @@ def test_granger_test(self):
5452
dataset = self.syn_data_2d()
5553
G = Granger()
5654
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))
55+
p_value_matrix_truth = np.array([[0, 0.5989, 0, 0.5397], [0.0006, 0, 0.0014, 0]])
56+
adj_matrix_truth = np.array([[1, 0, 1, 0], [1, 1, 1, 1]])
57+
self.assertEqual((np.round(p_value_matrix, 4) - p_value_matrix_truth).all(), 0)
58+
self.assertEqual((adj_matrix - adj_matrix_truth).all(), 0)
5959

6060
# example2
6161
# for data with multi-dimensional variables, granger lasso regression.
@@ -66,7 +66,10 @@ def test_granger_lasso(self):
6666
dataset = self.syn_data_3d()
6767
G = Granger()
6868
coeff = G.granger_lasso(data=dataset)
69-
print('Estimated matrix is \n {}'.format(coeff))
69+
coeff_truth = np.array([[0.09, 0.1101, 0.1527, 0.1127, 0.0226, 0.1538],
70+
[0.1004, 0.15, 0.1757, 0.1037, 0.1612, 0.0987],
71+
[0.1155, 0.1485, 0, 0.039, -0., 0.1085]])
72+
self.assertEqual((np.round(coeff, 4) - coeff_truth).all(), 0)
7073

7174

7275
if __name__ == '__main__':

0 commit comments

Comments
 (0)