-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create Spearman's rank correlation coefficient #11155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
013b254
Create spearman_rank_correlation_coefficient.py
cyrixninja b7eff0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f73bfd6
Fixed Issues
cyrixninja 9663952
Merge branch 'master' of https://github.com/cyrixninja/Python
cyrixninja f86d9b0
Added More Description
cyrixninja 73da280
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 602bca6
Fixed Issues
cyrixninja b1911f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0f4da64
Tried Fixing Issues
cyrixninja e41a7a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3cc9bed
Tried Fixing Issues
cyrixninja 45eb94d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e7225e3
Fixed Issues
cyrixninja d465b11
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6d896fb
Merge branch 'TheAlgorithms:master' into master
cyrixninja 0a30ec5
Fixed Issues
cyrixninja d3dec3b
Apply suggestions from code review
cclauss e6f24be
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 32e448a
Update maths/spearman_rank_correlation_coefficient.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from collections.abc import Sequence | ||
|
||
|
||
def assign_ranks(data: list[float]) -> list[int]: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Assigns ranks to elements in the array. | ||
|
||
:param data: List of floats. | ||
:return: List of ints representing the ranks. | ||
|
||
Example: | ||
>>> assign_ranks([3.2, 1.5, 4.0, 2.7, 5.1]) | ||
[3, 1, 4, 2, 5] | ||
|
||
>>> assign_ranks([10.5, 8.1, 12.4, 9.3, 11.0]) | ||
[3, 1, 5, 2, 4] | ||
""" | ||
ranked_data = sorted((value, index) for index, value in enumerate(data)) | ||
ranks = [0] * len(data) | ||
|
||
for position, (_, index) in enumerate(ranked_data): | ||
ranks[index] = position + 1 | ||
|
||
return ranks | ||
|
||
|
||
def calculate_spearman_rank_correlation( | ||
variable_1: Sequence[float], variable_2: Sequence[float] | ||
) -> float: | ||
""" | ||
Calculates Spearman's rank correlation coefficient. | ||
|
||
:param variable_1: List of floats representing the first variable. | ||
:param variable_2: List of floats representing the second variable. | ||
:return: Spearman's rank correlation coefficient. | ||
|
||
Example Usage: | ||
|
||
>>> x = [1, 2, 3, 4, 5] | ||
>>> y = [5, 4, 3, 2, 1] | ||
>>> calculate_spearman_rank_correlation(x, y) | ||
-1.0 | ||
|
||
>>> x = [1, 2, 3, 4, 5] | ||
>>> y = [2, 4, 6, 8, 10] | ||
>>> calculate_spearman_rank_correlation(x, y) | ||
1.0 | ||
|
||
>>> x = [1, 2, 3, 4, 5] | ||
>>> y = [5, 1, 2, 9, 5] | ||
>>> calculate_spearman_rank_correlation(x, y) | ||
0.6 | ||
""" | ||
n = len(variable_1) | ||
rank_var1 = assign_ranks(variable_1) | ||
rank_var2 = assign_ranks(variable_2) | ||
|
||
# Calculate differences of ranks | ||
d = [rx - ry for rx, ry in zip(rank_var1, rank_var2)] | ||
|
||
# Calculate the sum of squared differences | ||
d_squared = sum(di**2 for di in d) | ||
|
||
# Calculate the Spearman's rank correlation coefficient | ||
rho = 1 - (6 * d_squared) / (n * (n**2 - 1)) | ||
|
||
return rho | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
# Example usage: | ||
print( | ||
f"{calculate_spearman_rank_correlation([1, 2, 3, 4, 5], [2, 4, 6, 8, 10]) = }" | ||
) | ||
|
||
print(f"{calculate_spearman_rank_correlation([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]) = }") | ||
|
||
print(f"{calculate_spearman_rank_correlation([1, 2, 3, 4, 5], [5, 1, 2, 9, 5]) = }") |
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.
Uh oh!
There was an error while loading. Please reload this page.