-
-
Save itamarfaran/0cffce51cd0e3d78ddf509b72a9e702d to your computer and use it in GitHub Desktop.
This file contains 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
import numpy as np | |
from scipy.stats import rankdata, norm | |
def xicor(x, y, ties="auto"): | |
x = np.asarray(x).flatten() | |
y = np.asarray(y).flatten() | |
n = len(y) | |
if len(x) != n: | |
raise IndexError( | |
f"x, y length mismatch: {len(x)}, {len(y)}" | |
) | |
if ties == "auto": | |
ties = len(np.unique(y)) < n | |
elif not isinstance(ties, bool): | |
raise ValueError( | |
f"expected ties either \"auto\" or boolean, " | |
f"got {ties} ({type(ties)}) instead" | |
) | |
y = y[np.argsort(x)] | |
r = rankdata(y, method="ordinal") | |
nominator = np.sum(np.abs(np.diff(r))) | |
if ties: | |
l = rankdata(y, method="max") | |
denominator = 2 * np.sum(l * (n - l)) | |
nominator *= n | |
else: | |
denominator = np.power(n, 2) - 1 | |
nominator *= 3 | |
statistic = 1 - nominator / denominator # upper bound is (n - 2) / (n + 1) | |
p_value = norm.sf(statistic, scale=2 / 5 / np.sqrt(n)) | |
return statistic, p_value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment