Skip to content

ENH: add n_samples parameter to rich_club_coefficient for multi-sample averaging - #8721

Merged
dschult merged 10 commits into
networkx:mainfrom
magnuls:fix_rich_club_zero_div_error
Jul 22, 2026
Merged

dschult merged 10 commits into
networkx:mainfrom
magnuls:fix_rich_club_zero_div_error

Conversation

@magnuls

@magnuls magnuls commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #8485
Adds an n_samples parameter (defaults to 1) to rich_club_coefficient that generates multiple random graphs and averages their rich-club coefficients for normalization. I try to follow the same multi-sample averaging approach described by @dschult in #8485.

My Changes:

  • New n_samples parameter controls the number of random graphs used for normalization
  • Defaults to 1 for backwards compatibility (this was the OG case)
  • Raises NetworkXAlgorithmError, suggesting an increase in n_samples, instead of a raw ZeroDivisionError when the averaged rich-club coefficient is zero
  • Validates that n_samples >= 1 when normalized=True

Tests added:

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Thanks @magnuls for this.

My main concern is that we make sure to handle seed correctly here. I have other suggestions that are mostly nit picking... Push back or ask questions if anything doesn't make sense. :)

Comment thread networkx/algorithms/richclub.py Outdated
Comment on lines +41 to +43
`n_samples` reduces the likelyhood of a ZeroDivisionError for sparse
graphs with few high-degree nodes.
in the case that the average of their rich-club coefficients are all 0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check spelling and punctuation here.
Also can we state something like: Only relevant when normalized is True.
And the same phrase should go in the seed argument I think.

Comment thread networkx/algorithms/richclub.py Resolved
Comment thread networkx/algorithms/richclub.py Resolved
@magnuls

magnuls commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

These are all great suggestions. Will work on implementing this!

magnuls added 3 commits July 3, 2026 04:52
- Fixed spelling/grammar in docstring
- Made n_samples keyword-only
- Added py_random_state decorator
- Changed exception from Exception to ValueError
- Added test comment referencing networkxgh-8485
Comment thread networkx/algorithms/richclub.py Resolved

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the liberty of updating the doc_string Raises section and adjust the exception messages (and merge with main). Those are the only remaining open comments on this PR.

I approve this and think its ready. @amcandio can you verify that this is the adjustment to the Raises section you intended and if so, merge? Thanks!

Thanks @magnuls for this PR!

Comment thread networkx/algorithms/richclub.py Resolved
@dschult
dschult merged commit bf130bf into networkx:main Jul 22, 2026
43 checks passed
@dschult dschult added this to the 3.7 milestone Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Computing normalized rich-club coefficient may lead to ZeroDivisionError

4 participants