maths.spearman_rank_correlation_coefficient

Functions

assign_ranks(→ list[float])

Assigns ranks to elements in the array.

calculate_spearman_rank_correlation(→ float)

Calculates Spearman's rank correlation coefficient.

Module Contents

maths.spearman_rank_correlation_coefficient.assign_ranks(data: collections.abc.Sequence[float]) list[float]

Assigns ranks to elements in the array.

Parameters:

data – List of floats.

Returns:

List of floats representing the ranks.

Example: >>> assign_ranks([3.2, 1.5, 4.0, 2.7, 5.1]) [3.0, 1.0, 4.0, 2.0, 5.0]

>>> assign_ranks([10.5, 8.1, 12.4, 9.3, 11.0])
[3.0, 1.0, 5.0, 2.0, 4.0]
>>> assign_ranks([1, 1, 1, 1])
[2.5, 2.5, 2.5, 2.5]
maths.spearman_rank_correlation_coefficient.calculate_spearman_rank_correlation(variable_1: collections.abc.Sequence[float], variable_2: collections.abc.Sequence[float]) float

Calculates Spearman’s rank correlation coefficient.

Parameters:
  • variable_1 – List of floats representing the first variable.

  • variable_2 – List of floats representing the second variable.

Returns:

Spearman’s rank correlation coefficient.

Raises:

ValueError – If less than 2 data points are provided.

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.4
>>> x = [5]
>>> y = [9]
>>> calculate_spearman_rank_correlation(x, y)
Traceback (most recent call last):
    ...
ValueError: Need at least 2 data points to calculate correlation