py-why / dowhy

DoWhy is a Python library for causal inference that supports explicit modeling and testing of causal assumptions. DoWhy is based on a unified language for causal inference, combining causal graphical models and potential outcomes frameworks.
https://www.pywhy.org/dowhy
MIT License
6.88k stars 916 forks source link

Fix incorrect edges in `adjacency_matrix_to_graph` #1202

Open rahulbshrestha opened 3 weeks ago

rahulbshrestha commented 3 weeks ago

This PR fixes the issue of incorrect edges in adjacency_matrix_to_graph when the adjacency matrix is of type np.matrix by converting the matrix into a numpy array.

Related issue: https://github.com/py-why/dowhy/issues/887

rahulbshrestha commented 3 weeks ago

(Copying from another comment) I was a bit confused about the direction of the edges. If the adjacency matrix is

np.matrix(
    [[0, 0, 1, 0, 0],
     [0, 0, 1, 0, 0],
     [0, 0, 0, 1, 1],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0]]
)

the graph is currently,

image

isn't it supposed to be:

image

because the typical interpretation of adjacency matrices is rows represent source nodes and columns represent destination nodes. Is this a bug or a conscious decision?

This comes from the for-loop,

    for to, from_, coef in zip(dirs[0], dirs[1], adjacency_matrix[idx]):
        d.edge(names[from_], names[to], label=str(coef))
rahulbshrestha commented 2 weeks ago

@amit-sharma If the design choice for the direction of the edges (adjacency matrix -> graph) is intentional and supposed to be the way it is, then the PR is ready to be merged!