Skip to content

fix: use averaged ranks for ties in Spearman, guard n<2#14888

Closed
SAnnasJawed wants to merge 1 commit into
TheAlgorithms:masterfrom
SAnnasJawed:fix/spearman-tied-ranks-and-zero-division
Closed

fix: use averaged ranks for ties in Spearman, guard n<2#14888
SAnnasJawed wants to merge 1 commit into
TheAlgorithms:masterfrom
SAnnasJawed:fix/spearman-tied-ranks-and-zero-division

Conversation

@SAnnasJawed

@SAnnasJawed SAnnasJawed commented Jul 2, 2026

Copy link
Copy Markdown

Fixes #14887

Changes made:

  • Fixed assign_ranks() to use averaged ranks for tied values instead of sequential ranks (e.g. [1, 2, 2, 4] now correctly gives [1.0, 2.5, 2.5, 4.0] instead of [1.0, 2.0, 3.0, 4.0]).
  • Added ValueError guard for n<1 to prevent ZeroDivisionError if a single data point is passed.
  • Updated existing doctest for [5, 1, 2, 9, 5] from 0.6 to 0.4, the old value was itself incorrect, produced by the broken sequential ranking.
  • Updated return type annotation on assign_ranks() from list[int] to list[float] since averaged ranks like (2.5) are floats.
  • Added new doctests covering tied values and the n=1 edge case.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.

@algorithms-keeper

Copy link
Copy Markdown

Closing this pull request as invalid

@SAnnasJawed, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines.

If you're facing any problem on how to mark a checkbox, please read the following instructions:

  • Read a point one at a time and think if it is relevant to the pull request or not.
  • If it is, then mark it by putting a x between the square bracket like so: [x]

NOTE: Only [x] is supported so if you have put any other letter or symbol between the brackets, that will be marked as invalid. If that is the case then please open a new pull request with the appropriate changes.

@algorithms-keeper algorithms-keeper Bot closed this Jul 2, 2026
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed invalid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Spearman correlation incorrect for tied values, ZeroDivisionError on n=1

1 participant