Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/pingouin/nonparametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,14 @@ def wilcoxon(x, y=None, alternative="two-sided", **kwargs):
# according to the formula given in Vargha and Delaney 2000 which
# works with ordinal data.
if y is not None:
diff = x[:, None] - y
# cles = max((diff < 0).sum(), (diff > 0).sum()) / diff.size
# Counting via binary search on sorted y avoids building the O(n^2) difference matrix
# (same approach as pingouin.compute_effsize(..., eftype="cles")).
# alternative = 'greater', with ties set to 0.5
# Note that alternative = 'two-sided' gives same output as alternative = 'greater'
cles = np.where(diff == 0, 0.5, diff > 0).mean()
ys = np.sort(y)
n_less = np.searchsorted(ys, x, side="left")
n_ties = np.searchsorted(ys, x, side="right") - n_less
cles = (n_less.sum() + 0.5 * n_ties.sum()) / (x.size * y.size)
cles = 1 - cles if alternative == "less" else cles
else:
# CLES cannot be computed if y is None
Expand Down
9 changes: 9 additions & 0 deletions tests/test_nonparametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def test_wilcoxon():
assert round(wc_pg.at["Wilcoxon", "CLES"], 3) == 0.396
assert round(wc_pg_less.at["Wilcoxon", "CLES"], 3) == 0.604
assert round(wc_pg_greater.at["Wilcoxon", "CLES"], 3) == 0.396
# Equivalence of CLES with ties across all alternatives
x_ties = np.array([1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 5.0, 5.0])
y_ties = np.array([2.0, 2.0, 3.0, 4.0, 4.0, 1.0, 5.0, 3.0])
diff = x_ties[:, None] - y_ties
expected_greater = np.where(diff == 0, 0.5, diff > 0).mean()
for alt in ("two-sided", "greater", "less"):
expected = 1 - expected_greater if alt == "less" else expected_greater
res = wilcoxon(x_ties, y_ties, alternative=alt, method="asymptotic")
assert np.isclose(res.at["Wilcoxon", "CLES"], expected, atol=1e-15)
# Without continuity correction
wc_scp_nocorr = scipy.stats.wilcoxon(x2, y2, correction=False, method="asymptotic")
wc_pg_nocorr = wilcoxon(x2, y2, correction=False, method="asymptotic")
Expand Down
Loading