cdt15 / lingam

Python package for causal discovery based on LiNGAM.
https://sites.google.com/view/sshimizu06/lingam
MIT License
368 stars 56 forks source link

In ICALiNGAM, direction of causality is wrong when using data containing two variables #53

Closed kitahara-neut closed 1 year ago

kitahara-neut commented 1 year ago

Data I used contained two variables as follows:

np.random.seed(0)
size = 1000
x = np.random.uniform(size=size)
y = 2.0*x + np.random.uniform(size=size)
df_x_y = pd.DataFrame({'x': x, 'y': y})

Using ICALiNGAM, I got the following result:

model_x_y = lingam.ICALiNGAM()
model_x_y.fit(df_x_y)
model_x_y.adjacency_matrix_
# [[0.         0.        ]
#  [2.00618965 0.        ]]
model_x_y.causal_order_
# [0, 1]

Direction of causality is right, because estimated result is x->y. But when swapping x y, I got the following result:

df_y_x = pd.DataFrame({'y': df_x_y['y'], 'x': df_x_y['x']})
model_y_x = lingam.ICALiNGAM()
model_y_x.fit(df_y_x)
model_y_x.adjacency_matrix_
# [[0.         0.        ]
#  [0.39467697 0.        ]]
model_y_x.causal_order_
# [0, 1]

Estimated direction is y->x. I only swapped order of variables, but direction I got seemes wrong. I think this is bug. Please check this issue.

For your information, when using data containing three or more variables (e.g. x->y->z), this issue was not occurred.

ikeuchi-screen commented 1 year ago

Hi, @kitahara-neut. Thank you for your report. I have been able to confirm that it is a bug in ICALiNGAM. I will fix it right away.

kitahara-neut commented 1 year ago

Hi, @ikeuchi-screen. Thank you for your quick confirmation and reply. I am sorry to trouble you, but I appreciate your support.