From 013b25427f91a68a99f2d0b1cdc871590dcf21fd Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Tue, 14 Nov 2023 22:28:26 +0530 Subject: [PATCH 01/17] Create spearman_rank_correlation_coefficient.py --- .../spearman_rank_correlation_coefficient.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 maths/spearman_rank_correlation_coefficient.py diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py new file mode 100644 index 000000000000..de04ff292b0a --- /dev/null +++ b/maths/spearman_rank_correlation_coefficient.py @@ -0,0 +1,70 @@ +def rankdata(arr): + """ + Assigns ranks to elements in the array. + """ + ranked = sorted((value, index) for index, value in enumerate(arr)) + ranks = [0] * len(arr) + + for pos, (value, index) in enumerate(ranked): + ranks[index] = pos + 1 + + return ranks + +def spearman_rank_correlation(x, y): + """ + Calculates Spearman's rank correlation coefficient. + + Example Usage : + + >>> x = [1, 2, 3, 4, 5] + >>> y = [5, 4, 3, 2, 1] + >>> spearman_rank_correlation(x, y) + -1.0 + + >>> x = [1, 2, 3, 4, 5] + >>> y = [2, 4, 6, 8, 10] + >>> spearman_rank_correlation(x, y) + 1.0 + + >>> x = [1, 2, 3, 4, 5] + >>> y = [5, 1, 2, 9, 5] + >>> spearman_rank_correlation(x, y) + 0.6 + + """ + n = len(x) + rank_x = rankdata(x) + rank_y = rankdata(y) + + # Calculate differences of ranks + d = [rx - ry for rx, ry in zip(rank_x, rank_y)] + + # 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: + x1 = [1, 2, 3, 4, 5] + y1 = [2, 4, 6, 8, 10] + rho1 = spearman_rank_correlation(x1, y1) + print(f"Spearman's rank correlation coefficient (Example 1): {rho1}") + + x2 = [1, 2, 3, 4, 5] + y2 = [5, 4, 3, 2, 1] + rho2 = spearman_rank_correlation(x2, y2) + print(f"Spearman's rank correlation coefficient (Example 2): {rho2}") + + x3 = [1, 2, 3, 4, 5] + y3 = [5, 1, 2, 9, 5] + rho3 = spearman_rank_correlation(x3, y3) + print(f"Spearman's rank correlation coefficient (Example 3): {rho3}") From b7eff0a1c2c40258ffa02b9ead2049a58e20e92b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:03:55 +0000 Subject: [PATCH 02/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/spearman_rank_correlation_coefficient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index de04ff292b0a..b51bcdb246c1 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -10,6 +10,7 @@ def rankdata(arr): return ranks + def spearman_rank_correlation(x, y): """ Calculates Spearman's rank correlation coefficient. @@ -40,7 +41,7 @@ def spearman_rank_correlation(x, y): d = [rx - ry for rx, ry in zip(rank_x, rank_y)] # Calculate the sum of squared differences - d_squared = sum(di ** 2 for di in d) + 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)) From f73bfd6cd8d613fc9de7f95b3c762abdd9f1c5fc Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Tue, 14 Nov 2023 22:39:43 +0530 Subject: [PATCH 03/17] Fixed Issues --- maths/spearman_rank_correlation_coefficient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index de04ff292b0a..33cbc509ec92 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -5,11 +5,12 @@ def rankdata(arr): ranked = sorted((value, index) for index, value in enumerate(arr)) ranks = [0] * len(arr) - for pos, (value, index) in enumerate(ranked): + for pos, (_, index) in enumerate(ranked): ranks[index] = pos + 1 return ranks + def spearman_rank_correlation(x, y): """ Calculates Spearman's rank correlation coefficient. From f86d9b0a0d3a4f53dcb9a5a1bc1b016620f25039 Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Tue, 14 Nov 2023 22:46:17 +0530 Subject: [PATCH 04/17] Added More Description --- maths/spearman_rank_correlation_coefficient.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 10ea3591c208..268ee53c865d 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -1,6 +1,17 @@ def rankdata(arr): """ Assigns ranks to elements in the array. + + :param arr: List of floats. + :return: List of ints representing the ranks. + + Example: + >>> rankdata([3.2, 1.5, 4.0, 2.7, 5.1]) + [3, 1, 4, 2, 5] + + >>> rankdata([10.5, 8.1, 12.4, 9.3, 11.0]) + [3, 1, 5, 2, 4] + """ ranked = sorted((value, index) for index, value in enumerate(arr)) ranks = [0] * len(arr) @@ -15,6 +26,10 @@ def spearman_rank_correlation(x, y): """ Calculates Spearman's rank correlation coefficient. + :param x: List of floats representing the first variable. + :param y: List of floats representing the second variable. + :return: Spearman's rank correlation coefficient. + Example Usage : >>> x = [1, 2, 3, 4, 5] @@ -41,7 +56,7 @@ def spearman_rank_correlation(x, y): d = [rx - ry for rx, ry in zip(rank_x, rank_y)] # Calculate the sum of squared differences - d_squared = sum(di**2 for di in d) + 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)) From 73da2808cd706c093b17b087f87e5ffd85f4ef83 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:16:55 +0000 Subject: [PATCH 05/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/spearman_rank_correlation_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 268ee53c865d..559b4ebe496e 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -56,7 +56,7 @@ def spearman_rank_correlation(x, y): d = [rx - ry for rx, ry in zip(rank_x, rank_y)] # Calculate the sum of squared differences - d_squared = sum(di ** 2 for di in d) + 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)) From 602bca66c589ca381252247847adca7111d27db3 Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:17:55 +0530 Subject: [PATCH 06/17] Fixed Issues --- .../spearman_rank_correlation_coefficient.py | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 559b4ebe496e..1a6cef55d41c 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -1,28 +1,29 @@ -def rankdata(arr): +from typing import List + +def assign_ranks(data: List[float]) -> List[int]: """ Assigns ranks to elements in the array. - :param arr: List of floats. + :param data: List of floats. :return: List of ints representing the ranks. Example: - >>> rankdata([3.2, 1.5, 4.0, 2.7, 5.1]) + >>> assign_ranks([3.2, 1.5, 4.0, 2.7, 5.1]) [3, 1, 4, 2, 5] - >>> rankdata([10.5, 8.1, 12.4, 9.3, 11.0]) + >>> assign_ranks([10.5, 8.1, 12.4, 9.3, 11.0]) [3, 1, 5, 2, 4] - """ - ranked = sorted((value, index) for index, value in enumerate(arr)) - ranks = [0] * len(arr) + ranked_data = sorted((value, index) for index, value in enumerate(data)) + ranks = [0] * len(data) - for pos, (_, index) in enumerate(ranked): - ranks[index] = pos + 1 + for position, (_, index) in enumerate(ranked_data): + ranks[index] = position + 1 return ranks -def spearman_rank_correlation(x, y): +def calculate_spearman_rank_correlation(x: List[float], y: List[float]) -> float: """ Calculates Spearman's rank correlation coefficient. @@ -30,33 +31,32 @@ def spearman_rank_correlation(x, y): :param y: List of floats representing the second variable. :return: Spearman's rank correlation coefficient. - Example Usage : + Example Usage: >>> x = [1, 2, 3, 4, 5] >>> y = [5, 4, 3, 2, 1] - >>> spearman_rank_correlation(x, y) + >>> calculate_spearman_rank_correlation(x, y) -1.0 >>> x = [1, 2, 3, 4, 5] >>> y = [2, 4, 6, 8, 10] - >>> spearman_rank_correlation(x, y) + >>> calculate_spearman_rank_correlation(x, y) 1.0 >>> x = [1, 2, 3, 4, 5] >>> y = [5, 1, 2, 9, 5] - >>> spearman_rank_correlation(x, y) + >>> calculate_spearman_rank_correlation(x, y) 0.6 - """ n = len(x) - rank_x = rankdata(x) - rank_y = rankdata(y) + rank_x = assign_ranks(x) + rank_y = assign_ranks(y) # Calculate differences of ranks d = [rx - ry for rx, ry in zip(rank_x, rank_y)] # Calculate the sum of squared differences - d_squared = sum(di**2 for di in d) + 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)) @@ -72,15 +72,15 @@ def spearman_rank_correlation(x, y): # Example usage: x1 = [1, 2, 3, 4, 5] y1 = [2, 4, 6, 8, 10] - rho1 = spearman_rank_correlation(x1, y1) + rho1 = calculate_spearman_rank_correlation(x1, y1) print(f"Spearman's rank correlation coefficient (Example 1): {rho1}") x2 = [1, 2, 3, 4, 5] y2 = [5, 4, 3, 2, 1] - rho2 = spearman_rank_correlation(x2, y2) + rho2 = calculate_spearman_rank_correlation(x2, y2) print(f"Spearman's rank correlation coefficient (Example 2): {rho2}") x3 = [1, 2, 3, 4, 5] y3 = [5, 1, 2, 9, 5] - rho3 = spearman_rank_correlation(x3, y3) + rho3 = calculate_spearman_rank_correlation(x3, y3) print(f"Spearman's rank correlation coefficient (Example 3): {rho3}") From b1911f0ea8c27c527b10b1b385c21463122d7576 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Nov 2023 10:49:24 +0000 Subject: [PATCH 07/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/spearman_rank_correlation_coefficient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 1a6cef55d41c..9dd5429ecfdb 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -1,5 +1,6 @@ from typing import List + def assign_ranks(data: List[float]) -> List[int]: """ Assigns ranks to elements in the array. @@ -56,7 +57,7 @@ def calculate_spearman_rank_correlation(x: List[float], y: List[float]) -> float d = [rx - ry for rx, ry in zip(rank_x, rank_y)] # Calculate the sum of squared differences - d_squared = sum(di ** 2 for di in d) + 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)) From 0f4da645e413507139da2dc3bcea5ca34bf7047f Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:23:06 +0530 Subject: [PATCH 08/17] Tried Fixing Issues --- maths/spearman_rank_correlation_coefficient.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 9dd5429ecfdb..fd7e62e357cc 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -1,7 +1,6 @@ from typing import List - -def assign_ranks(data: List[float]) -> List[int]: +def assign_ranks(data: list[float]) -> list[int]: """ Assigns ranks to elements in the array. @@ -24,7 +23,7 @@ def assign_ranks(data: List[float]) -> List[int]: return ranks -def calculate_spearman_rank_correlation(x: List[float], y: List[float]) -> float: +def calculate_spearman_rank_correlation(x: list[float], y: list[float]) -> float: """ Calculates Spearman's rank correlation coefficient. @@ -57,7 +56,7 @@ def calculate_spearman_rank_correlation(x: List[float], y: List[float]) -> float d = [rx - ry for rx, ry in zip(rank_x, rank_y)] # Calculate the sum of squared differences - d_squared = sum(di**2 for di in d) + 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)) From e41a7a3a404748fdfe45ca66fee5588222e10904 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Nov 2023 10:55:50 +0000 Subject: [PATCH 09/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/spearman_rank_correlation_coefficient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index fd7e62e357cc..f8648ff38410 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -1,5 +1,6 @@ from typing import List + def assign_ranks(data: list[float]) -> list[int]: """ Assigns ranks to elements in the array. @@ -56,7 +57,7 @@ def calculate_spearman_rank_correlation(x: list[float], y: list[float]) -> float d = [rx - ry for rx, ry in zip(rank_x, rank_y)] # Calculate the sum of squared differences - d_squared = sum(di ** 2 for di in d) + 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)) From 3cc9bed179640f27795cea04430978f9f6443b68 Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:30:14 +0530 Subject: [PATCH 10/17] Tried Fixing Issues --- .../spearman_rank_correlation_coefficient.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index f8648ff38410..73b740c100c7 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -1,6 +1,3 @@ -from typing import List - - def assign_ranks(data: list[float]) -> list[int]: """ Assigns ranks to elements in the array. @@ -24,12 +21,12 @@ def assign_ranks(data: list[float]) -> list[int]: return ranks -def calculate_spearman_rank_correlation(x: list[float], y: list[float]) -> float: +def calculate_spearman_rank_correlation(variable_1: list[float], variable_2: list[float]) -> float: """ Calculates Spearman's rank correlation coefficient. - :param x: List of floats representing the first variable. - :param y: List of floats representing the second variable. + :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: @@ -49,15 +46,15 @@ def calculate_spearman_rank_correlation(x: list[float], y: list[float]) -> float >>> calculate_spearman_rank_correlation(x, y) 0.6 """ - n = len(x) - rank_x = assign_ranks(x) - rank_y = assign_ranks(y) + 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_x, rank_y)] + 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) + 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)) From 45eb94db684e08b722c05afb7fb35dccff94ecd6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Nov 2023 11:00:53 +0000 Subject: [PATCH 11/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/spearman_rank_correlation_coefficient.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 73b740c100c7..bfab264d72e2 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -21,7 +21,9 @@ def assign_ranks(data: list[float]) -> list[int]: return ranks -def calculate_spearman_rank_correlation(variable_1: list[float], variable_2: list[float]) -> float: +def calculate_spearman_rank_correlation( + variable_1: list[float], variable_2: list[float] +) -> float: """ Calculates Spearman's rank correlation coefficient. @@ -54,7 +56,7 @@ def calculate_spearman_rank_correlation(variable_1: list[float], variable_2: lis 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) + 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)) From e7225e3723ce4e4e62a69044b6d45d22eed65fdc Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:40:59 +0530 Subject: [PATCH 12/17] Fixed Issues --- maths/spearman_rank_correlation_coefficient.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index bfab264d72e2..56ed3af54ea7 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -21,9 +21,7 @@ def assign_ranks(data: list[float]) -> list[int]: return ranks -def calculate_spearman_rank_correlation( - variable_1: list[float], variable_2: list[float] -) -> float: +def calculate_spearman_rank_correlation(variable_1: list[float], variable_2: list[float]) -> float: """ Calculates Spearman's rank correlation coefficient. @@ -44,7 +42,7 @@ def calculate_spearman_rank_correlation( 1.0 >>> x = [1, 2, 3, 4, 5] - >>> y = [5, 1, 2, 9, 5] + >>> y = [5, 1, 2, 6, 5] >>> calculate_spearman_rank_correlation(x, y) 0.6 """ @@ -56,7 +54,7 @@ def calculate_spearman_rank_correlation( 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) + 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)) @@ -81,6 +79,6 @@ def calculate_spearman_rank_correlation( print(f"Spearman's rank correlation coefficient (Example 2): {rho2}") x3 = [1, 2, 3, 4, 5] - y3 = [5, 1, 2, 9, 5] + y3 = [5, 1, 2, 6, 5] rho3 = calculate_spearman_rank_correlation(x3, y3) print(f"Spearman's rank correlation coefficient (Example 3): {rho3}") From d465b1106a9ca1c9ca1170d752459201204393dd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Nov 2023 11:11:36 +0000 Subject: [PATCH 13/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/spearman_rank_correlation_coefficient.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 56ed3af54ea7..6c794e731c18 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -21,7 +21,9 @@ def assign_ranks(data: list[float]) -> list[int]: return ranks -def calculate_spearman_rank_correlation(variable_1: list[float], variable_2: list[float]) -> float: +def calculate_spearman_rank_correlation( + variable_1: list[float], variable_2: list[float] +) -> float: """ Calculates Spearman's rank correlation coefficient. @@ -54,7 +56,7 @@ def calculate_spearman_rank_correlation(variable_1: list[float], variable_2: lis 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) + 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)) From 0a30ec56f3490993f2295fe95ffbf317e7b7aa3d Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Thu, 16 Nov 2023 19:30:47 +0530 Subject: [PATCH 14/17] Fixed Issues --- maths/spearman_rank_correlation_coefficient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 6c794e731c18..bfab264d72e2 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -44,7 +44,7 @@ def calculate_spearman_rank_correlation( 1.0 >>> x = [1, 2, 3, 4, 5] - >>> y = [5, 1, 2, 6, 5] + >>> y = [5, 1, 2, 9, 5] >>> calculate_spearman_rank_correlation(x, y) 0.6 """ @@ -81,6 +81,6 @@ def calculate_spearman_rank_correlation( print(f"Spearman's rank correlation coefficient (Example 2): {rho2}") x3 = [1, 2, 3, 4, 5] - y3 = [5, 1, 2, 6, 5] + y3 = [5, 1, 2, 9, 5] rho3 = calculate_spearman_rank_correlation(x3, y3) print(f"Spearman's rank correlation coefficient (Example 3): {rho3}") From d3dec3bf724341eb88de0576f5987670753d0d4f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 25 Nov 2023 15:05:37 +0100 Subject: [PATCH 15/17] Apply suggestions from code review --- .../spearman_rank_correlation_coefficient.py | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index bfab264d72e2..120bf8d58689 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -1,3 +1,6 @@ +from collections.abc import Sequence + + def assign_ranks(data: list[float]) -> list[int]: """ Assigns ranks to elements in the array. @@ -22,7 +25,7 @@ def assign_ranks(data: list[float]) -> list[int]: def calculate_spearman_rank_correlation( - variable_1: list[float], variable_2: list[float] + variable_1: Sequence[float], variable_2: Sequence[float] ) -> float: """ Calculates Spearman's rank correlation coefficient. @@ -70,17 +73,8 @@ def calculate_spearman_rank_correlation( doctest.testmod() # Example usage: - x1 = [1, 2, 3, 4, 5] - y1 = [2, 4, 6, 8, 10] - rho1 = calculate_spearman_rank_correlation(x1, y1) - print(f"Spearman's rank correlation coefficient (Example 1): {rho1}") - - x2 = [1, 2, 3, 4, 5] - y2 = [5, 4, 3, 2, 1] - rho2 = calculate_spearman_rank_correlation(x2, y2) - print(f"Spearman's rank correlation coefficient (Example 2): {rho2}") - - x3 = [1, 2, 3, 4, 5] - y3 = [5, 1, 2, 9, 5] - rho3 = calculate_spearman_rank_correlation(x3, y3) - print(f"Spearman's rank correlation coefficient (Example 3): {rho3}") + 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]) = }") From e6f24beaf6277a40152275f46d8ca92924790c0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 25 Nov 2023 14:06:06 +0000 Subject: [PATCH 16/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/spearman_rank_correlation_coefficient.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 120bf8d58689..67f68dd5dbbd 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -73,7 +73,9 @@ def calculate_spearman_rank_correlation( 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], [2, 4, 6, 8, 10]) = }" + ) print(f"{calculate_spearman_rank_correlation([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]) = }") From 32e448a8ad375702fefd45988d96a5573473bdc3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 25 Nov 2023 15:08:14 +0100 Subject: [PATCH 17/17] Update maths/spearman_rank_correlation_coefficient.py --- maths/spearman_rank_correlation_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/spearman_rank_correlation_coefficient.py b/maths/spearman_rank_correlation_coefficient.py index 67f68dd5dbbd..32ff6b9e3d71 100644 --- a/maths/spearman_rank_correlation_coefficient.py +++ b/maths/spearman_rank_correlation_coefficient.py @@ -1,7 +1,7 @@ from collections.abc import Sequence -def assign_ranks(data: list[float]) -> list[int]: +def assign_ranks(data: Sequence[float]) -> list[int]: """ Assigns ranks to elements in the array.