Skip to content

Instantly share code, notes, and snippets.

@gabrielStanovsky
Last active September 18, 2024 06:48
Show Gist options
  • Save gabrielStanovsky/5b92bf389b6bdb6ee1129e0f181ae23e to your computer and use it in GitHub Desktop.
Save gabrielStanovsky/5b92bf389b6bdb6ee1129e0f181ae23e to your computer and use it in GitHub Desktop.
def is_concordant_strict(pair, ls1, ls2):
"""
Given a pair of indices, check if ls1 and ls2 agree
on their ranking, if either is a tie, return False.
"""
i1, i2 = pair
return ( (ls1[i1] > ls1[i2]) and (ls2[i1] > ls2[i2])) or \
( (ls1[i1] < ls1[i2]) and (ls2[i1] < ls2[i2])) or \
( (ls1[i1] == ls1[i2]) and (ls2[i1] == ls2[i2]))
def ktau_strict(ls1, ls2):
"""
A version of Kendall's tau where ties can be counted as disagreements.
"""
assert (len(ls1) == len(ls2))
num_of_items = len(ls1)
all_pairs = [(i, j)
for i in range(num_of_items)
for j in range(i+1, num_of_items)]
num_of_pairs = len(all_pairs)
# count concordant pairs
conc = sum([is_concordant_strict(pair, ls1, ls2) for pair in all_pairs])
# anything that isn't concordant is discordant
disc = num_of_pairs - conc
# compute tau
tau = (conc - disc) / num_of_pairs
return tau
x1 = [1,2,3,4,5]
x2 = [37,40,40,40,40]
print(ktau_strict(x1,x2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment