Skip to content

72 pagerank tiny rows - #172

Open
eriknw wants to merge 1 commit into
python-graphblas:37-nonfinite-weightsfrom
eriknw:72-pagerank-tiny-rows
Open

eriknw wants to merge 1 commit into
python-graphblas:37-nonfinite-weightsfrom
eriknw:72-pagerank-tiny-rows

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Leave pagerank to NetworkX where values of both signs nearly cancel. NetworkX divides each node's out-weights by their sum, and nstart, personalization and dangling by 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 than N * tol between 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, personalization and dangling as NetworkX does, at the nodes, leaving sums of 0, inf or NaN to NetworkX, and divides by tiny sums as NetworkX does, in pagerank and in google_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_matrix NetworkX'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 of max_iter rounds, the l1 change of the ranks falls below N * 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.

    • The magnifications of every node's out-weights, and of nstart, personalization and dangling, are multiplied, as a path through the graph can meet each.
    • A call where the product passes sqrt(tol / 2**-52) / 100 is left to NetworkX: 671 for the default tol. Rounding to within 2**-52, magnified m times in the ranks and m times again in the change between rounds, then stays 10**4 times below tol.
    • A node with three or more out-weights of both signs is already left to NetworkX by 37 nan, infinite and nonpositive weights #136, as the order of adding them may decide whether they sum to 0 or to a small number. Two add to the same in either order, with or without zeros beside them, so a node whose two out-weights add to 0 is served as dangling, as in NetworkX.
    • A tol of 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 of tol:

      tol First disagreement (magnification)
      1e-4 1.0e7
      1e-8 7.8e4
      1e-10 9.7e3
      1e-12 1023

      Each 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, nstart or dangling values, 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:

    • the graph above;
    • one-row cancellation at every power of 2 from 1 to 2**52, served exactly up to a magnification of 671;
    • nested cancellation, served up to 671 for the product;
    • the bound at tol from 1e-4 to 1e-12 and 0, and a tol of -1 or NaN;
    • personalization, nstart and dangling that cancel;
    • three out-weights that add to 0, left to NetworkX by 37 nan, infinite and nonpositive weights #136's check, and two beside one or two zeros, which are served;
    • negative out-weights of one sign, which are served;
    • the magnification itself, including sums below 0 and a product too large for a float;
    • for the dicts: sums of 0 (empty, zeros, one zero, two or three values that cancel, a key not in the graph), sums of inf and NaN, and keys not in the graph beside nodes, for each dict of both names, on a graph with a dangling node and on one without;
    • out-weights whose sums are tiny, from 1e-320 to 1e-300, and dangling values with a tiny sum;
    • the ZeroDivisionError the kernel raises for a vector that sums to 0.

    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:

    • 15 changed versions of the cancelling check, one of them 37 nan, infinite and nonpositive weights #136's check of three or more out-weights removed. The unchanged code passes, as do two changes that give the same products: rows of one sign counted too, and vectors of negative values only, each of whose ratio is exactly 1. The other 13 fail the tests.
    • 10 changed versions of the dict rules, each of which fails the tests.
    • 8 changed versions of the division: 7 fail the tests, and one gives the same matrix (plus_rdiv for any_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 pagerank and 2 google_matrix calls 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, personalization and dangling at 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 the dangling values even where no node dangles. Where that sum is 0, inf or NaN this makes NaN, so its iteration fails to converge; for personalization it 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 {}).

    • Examples: a dangling of {0: 1, 1: -1}, of zeros, or {}, on a graph where every node has out-edges, which this answered; nstart of zeros or {}; personalization={}.
    • google_matrix follows the same rules, but reads dangling only where a node dangles, as NetworkX does. There NetworkX's matrix has rows or columns of NaN, or it raises ZeroDivisionError.
    • Finite values whose sum overflows, such as two of 1e308, give NetworkX zeros, and it answers; such a call is left to NetworkX as well.
  • 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_matrix now divides each row by its sum, where alpha / sum made the row inf, and both names divide the dangling values by their sum before taking alpha, where pagerank failed to converge and google_matrix gave NaN.

  • Measured (the dicts and tiny sums). 1104 calls in NetworkX dev, 3.7 and 3.6.1, the same in each.

    • The graphs: 4 small ones, directed and undirected, with a dangling node and without, and 8 whose out-weights are 1e-320, 5e-309, 5.5e-309 or 6e-309, on a cycle with a dangling node or as one edge of a cycle.
    • Each name was called with no dict, and with each of its dicts holding one of 18 kinds of value: empty, zeros, sums of 0 over one, two and three values, values that add to 0 over 1e16, a key not in the graph alone and beside nodes, NaN, inf, -inf, two of 1e308, subnormal values, and ordinary values of each sign.
    • On 37 nan, infinite and nonpositive weights #136, 646 of the calls differed from NetworkX: an answer for an exception, another exception or message, or other values. With this change, none do.
    • The answers agree with NetworkX's to within 8.2e-16, relative. 594 calls are left to NetworkX, 132 of which it answers: the 114 google_matrix calls whose matrix has NaN, and the 18 sums that overflow.
    • Cost (Indicative, load 4 to 6; best of 5, 37 nan, infinite and nonpositive weights #136 and this change interleaved): pagerank on 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_matrix on 1500 nodes, 2.3 to 2.6 ms either way.
  • Not changed. google_matrix where personalization or dangling holds 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 computes google_matrix NetworkX's way where a weight is negative.)

Stacked on #136.

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

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.48485% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.28%. Comparing base (804d15d) to head (22a9807).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant