- 
                Notifications
    You must be signed in to change notification settings 
- Fork 82
Closed
Labels
bugfunctionality that: returns invalid, erroneous, or meaningless results; or doesn't work at all.functionality that: returns invalid, erroneous, or meaningless results; or doesn't work at all.
Description
I encountered super strange behaviour of higher_order, both in old W and new Graph implementation.
g = graph.Graph.from_arrays([0, 1, 2, 3, 3, 4, 4], [0, 3, 4, 1, 4, 2, 3], [0, 1, 1, 1, 1, 1, 1])
g.adjacency
focal  neighbor
0      0           0
1      3           1
2      4           1
3      1           1
       4           1
4      2           1
       3           1
Name: weight, dtype: int64
g.higher_order(k=2, lower_order=True).adjacency
focal  neighbor
0      0           0
1      4           1
2      3           1
3      2           1
4      1           1
Name: weight, dtype: int64As you can see, this includes only neighbors of second order, not the first one as it should. The reason is that the underlying matrix power computation does not include them.
g.sparse.todense()
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],
       [0., 1., 0., 0., 1.],
       [0., 0., 1., 1., 0.]])
sparse.linalg.matrix_power(int_g.sparse, 2).todense()
array([[0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 1.],
       [0., 0., 1., 1., 0.],
       [0., 0., 1., 2., 0.],
       [0., 1., 0., 0., 2.]])
The solution is to sum the original sparse with the new one. It seems to be an edge case resulting from extremely sparse matrix but could be real nevertheless.
I think that the solution is to sum the result of matrix power(s) with the original sparse to ensure a value is present. Here
libpysal/libpysal/graph/base.py
Lines 1607 to 1608 in 01c1f80
| if lower_order: | |
| wk = sum(sparse.linalg.matrix_power(sp, x) for x in range(2, k + 1)) | 
Metadata
Metadata
Assignees
Labels
bugfunctionality that: returns invalid, erroneous, or meaningless results; or doesn't work at all.functionality that: returns invalid, erroneous, or meaningless results; or doesn't work at all.