Skip to content

Commit f810b3e

Browse files
authored
rank fixes (#195)
1 parent 4d04ccc commit f810b3e

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/obscure_stats/association/association.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,10 @@ def rank_minrelation_coefficient(x: np.ndarray, y: np.ndarray) -> float:
549549
x, y = _prep_arrays(x, y)
550550
if _check_arrays(x, y):
551551
return np.nan
552-
n_sq = len(x) ** 2
553-
rank_x_inc = (np.argsort(x) + 1) ** 2 / n_sq - 0.5
554-
rank_y_inc = (np.argsort(y) + 1) ** 2 / n_sq - 0.5
555-
rank_y_dec = -((np.argsort(-y) + 1) ** 2) / n_sq + 0.5
552+
n_sq = len(x) ** 2 + 1
553+
rank_x_inc = (stats.rankdata(x) ** 2) / n_sq - 0.5
554+
rank_y_inc = (stats.rankdata(y) ** 2) / n_sq - 0.5
555+
rank_y_dec = -(stats.rankdata(-y) ** 2) / n_sq + 0.5
556556
lower = np.sum((-rank_x_inc < rank_y_inc) * (rank_x_inc + rank_y_inc) ** 2)
557557
higher = np.sum((rank_x_inc > rank_y_dec) * (rank_x_inc - rank_y_dec) ** 2)
558558
return float((lower - higher) / (lower + higher))
@@ -637,12 +637,12 @@ def gaussain_rank_correlation(x: np.ndarray, y: np.ndarray) -> float:
637637
return np.nan
638638
n = len(x)
639639
norm_factor = 1 / (n + 1)
640-
x_ranks_norm = (np.argsort(x) + 1) * norm_factor
641-
y_ranks_norm = (np.argsort(y) + 1) * norm_factor
640+
x_ranks_norm = stats.rankdata(x) * norm_factor
641+
y_ranks_norm = stats.rankdata(y) * norm_factor
642642
coef = np.sum(stats.norm.ppf(x_ranks_norm) * stats.norm.ppf(y_ranks_norm)) / np.sum(
643643
stats.norm.ppf(np.arange(1, n + 1) * norm_factor) ** 2
644644
)
645-
return float((coef - 0.5) * 2)
645+
return float(max(min(coef, 1.0), -1.0))
646646

647647

648648
def quantile_correlation(x: np.ndarray, y: np.ndarray, q: float = 0.5) -> float:

0 commit comments

Comments
 (0)