Repository navigation
Conversation
Where a node's out-weights, or the values of nstart, personalization or dangling, hold both signs and nearly cancel, dividing by their sum magnifies its rounding, and rounding then decides whether NetworkX's power iteration stops and with which ranks. NetworkX adds and multiplies in its own order, so on such a graph it could fail to converge where this answered with huge ranks, or the other way round. The magnifications are multiplied, and past sqrt(tol / 2**-52) / 100 the call is left to NetworkX. A node with three or more out-weights of both signs already goes to NetworkX; two add to 0 in either order. NetworkX reads nstart, personalization and dangling at each node, so a key that is not a node is now left out, where this raised KeyError. It divides each by its sum, and the dangling values even where no node dangles: a sum of 0, inf or NaN makes NaN and a failure to converge (ZeroDivisionError for personalization), where this answered, or raised ZeroDivisionError or ValueError. Such a call is now left to NetworkX, in pagerank and in google_matrix, which reads the dangling values only where a node dangles. NetworkX takes 1 / sum of each node's out-weights, which passes the largest float below about 5.56e-309; its ranks then turn inf, and it fails to converge. This took alpha / sum, finite down to 4.72e-309, so between the two it answered. It now takes 1 / sum, then alpha. Elsewhere NetworkX divides by the sum itself, so its result stays finite: google_matrix now divides each row by its sum, and both names divide the dangling values by their sum, where pagerank failed to converge and google_matrix gave inf or NaN.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 37-nonfinite-weights #172 +/- ##
========================================================
+ Coverage 93.12% 93.28% +0.16%
========================================================
Files 183 183
Lines 11685 11935 +250
Branches 1326 1344 +18
========================================================
+ Hits 10882 11134 +252
+ Misses 550 549 -1
+ Partials 253 252 -1 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Leave
pagerankto NetworkX where values of both signs nearly cancel. NetworkX divides each node's out-weights by their sum, andnstart,personalizationanddanglingby theirs. Where the values cancel, the division magnifies the rounding of the sum: a node whose out-weights are 1 and -1 + 2**-51 sends 2.25e15 times its rank to one head and minus as much to the other. NetworkX stops when the ranks change by less thanN * tolbetween rounds, and with ranks that large, whether that ever happens is decided by rounding, which differs between NetworkX's NumPy and SciPy arithmetic and the GraphBLAS arithmetic here. On that graph NetworkX raises PowerIterationFailedConvergence, and this answered{0: 0.375, 1: 7.2e14, 2: -7.2e14}. With -1 + 2**-50 it was the other way round.It also reads
nstart,personalizationanddanglingas NetworkX does, at the nodes, leaving sums of 0, inf or NaN to NetworkX, and divides by tiny sums as NetworkX does, inpagerankand ingoogle_matrix.This builds on #136's pagerank checks: #136 leaves to NetworkX a node whose out-weights sum to inf or -inf, or hold both signs, three or more of them, and computes
google_matrixNetworkX's way where a weight is negative.NetworkX's condition.
pagerank(_pagerank_scipy) is the same in 3.6.1, 3.7 and dev, except for how it adds up the ranks of dangling nodes. It raises PowerIterationFailedConvergence when, in none ofmax_iterrounds, the l1 change of the ranks falls belowN * tol. It takes a node as dangling where its out-weights sum to exactly 0. Where the sums cancel, nothing short of NetworkX's own arithmetic says which rounds those are. So a call is left to NetworkX wherever rounding may decide it.The rule. For values of both signs, the sum of their sizes over the size of their sum is how many times dividing by that sum magnifies its rounding. For values of one sign it is 1.
nstart,personalizationanddangling, are multiplied, as a path through the graph can meet each.sqrt(tol / 2**-52) / 100is left to NetworkX: 671 for the defaulttol. Rounding to within 2**-52, magnifiedmtimes in the ranks andmtimes again in the change between rounds, then stays 10**4 times belowtol.tolof 0 or less, or NaN, never stops NetworkX. Such a call is left to it where anything cancels, and served otherwise, raising PowerIterationFailedConvergence as NetworkX does.Measured. These were run on graphs built to cancel, in NetworkX dev, 3.7 and 3.6.1, on 37 nan, infinite and nonpositive weights #136 and with this change; the counts are the same in each. On 37 nan, infinite and nonpositive weights #136, NetworkX and this backend disagreed on some of them; with this change, none of the calls still served differ.
Cancelling graphs, one row: a node whose two out-weights sum to 1/K of their size, both heads leading back to it, alone or in a random graph.
Cancelling graphs, nested: the same with each head again such a node, so the magnification is (2K - 1)**3.
The sweep covered K from 1 to 2**52, alpha 0.5, 0.85 and 0.95, and 3135 calls.
With the default
tol, the backend on 37 nan, infinite and nonpositive weights #136 disagreed with NetworkX on 216 calls: one raised and the other answered. Where both answered, the ranks differed by up to 0.655 of the largest. 37 nan, infinite and nonpositive weights #136 left 1220 calls to NetworkX, those with three or more out-weights of both signs.With this change, 2899 of those calls are left to NetworkX. The rest agree with NetworkX, to within 3.3e-14 of the largest rank.
The first disagreement came at a magnification of 1.0e6 for the default
tol. For other values oftol:tolEach is 1100 to 1600 times the bound for that
tol, with no disagreement left after the change.Random graphs: a second sweep covered 2388 calls on random graphs of 5 to 30 nodes, directed and undirected, where some nodes' out-weights, or the
personalization,nstartordanglingvalues, sum to 1/K of their size. On 37 nan, infinite and nonpositive weights #136, 24 calls disagreed and ranks differed by up to the largest rank, with 488 left to NetworkX. With this change, none disagree, the ranks are within 2.5e-13, and 1708 calls are left to NetworkX.Where nothing cancels, the check costs 0.09 ms on 1e6 edges, against 12 ms for the call (Indicative, load 4 to 6).
Tests. The tests cover:
tolfrom 1e-4 to 1e-12 and 0, and atolof -1 or NaN;personalization,nstartanddanglingthat cancel;danglingvalues with a tiny sum;Each case runs through NetworkX with graphblas first, and its outcome is compared with NetworkX's, exceptions with their messages. Mutants, in NetworkX dev, 3.7 and 3.6.1:
plus_rdivforany_rdiv).NetworkX's tests through the backend in dev, 3.7 and 3.6.1. The link-analysis tests (61, 61 and 56) and the whole suite (9569, 9430 and 6815 passed) give the same results on 37 nan, infinite and nonpositive weights #136 and with this change. No pagerank call there nearly cancels. With the dicts read as NetworkX reads them, 2
pagerankand 2google_matrixcalls there are left to NetworkX: its tests of a personalization of zeros, which expect its ZeroDivisionError.The dicts, read as NetworkX reads them. NetworkX reads
nstart,personalizationanddanglingat each node, so a key that is not a node is now left out, where this raised KeyError. NetworkX divides each by its sum, and thedanglingvalues even where no node dangles. Where that sum is 0, inf or NaN this makes NaN, so its iteration fails to converge; forpersonalizationit raises ZeroDivisionError, with no message, before that. Such a call is now left to NetworkX, where this answered, or raised ZeroDivisionError ("nstart sums to 0") or ValueError (for{}).danglingof{0: 1, 1: -1}, of zeros, or{}, on a graph where every node has out-edges, which this answered;nstartof zeros or{};personalization={}.google_matrixfollows the same rules, but readsdanglingonly where a node dangles, as NetworkX does. There NetworkX's matrix has rows or columns of NaN, or it raises ZeroDivisionError.Tiny sums. NetworkX takes 1 / sum of each node's out-weights, which passes the largest float below about 5.56e-309, so its ranks turn inf and it fails to converge. This took alpha / sum, finite down to 4.72e-309, and answered between the two; it now takes 1 / sum, then alpha. Elsewhere NetworkX divides by the sum itself, so its result stays finite:
google_matrixnow divides each row by its sum, where alpha / sum made the row inf, and both names divide thedanglingvalues by their sum before taking alpha, wherepagerankfailed to converge andgoogle_matrixgave NaN.Measured (the dicts and tiny sums). 1104 calls in NetworkX dev, 3.7 and 3.6.1, the same in each.
google_matrixcalls whose matrix has NaN, and the 18 sums that overflow.pagerankon 1e6 edges, 11.9 to 12.1 ms on 37 nan, infinite and nonpositive weights #136 and 12.0 to 12.2 ms with this change; with all three dicts over its 1e5 nodes, 50.1 to 50.7 ms and 51.1 to 51.6 ms.google_matrixon 1500 nodes, 2.3 to 2.6 ms either way.Not changed.
google_matrixwherepersonalizationordanglingholds values of both signs that nearly cancel: the order of adding may decide their sums there too, and it keeps its present behaviour. (37 nan, infinite and nonpositive weights #136 already computesgoogle_matrixNetworkX's way where a weight is negative.)Stacked on #136.