Created
December 28, 2021 09:13
-
-
Save ili3p/f2b38b898f6eab0d87ec248ea39fde94 to your computer and use it in GitHub Desktop.
Fast Kendall Tau calculation with pytorch.
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 torch | |
import time | |
from scipy.stats import kendalltau | |
def kendall(x, y): | |
n = x.shape[0] | |
def sub_pairs(x): | |
return x.expand(n,n).T.sub(x).sign_() | |
return sub_pairs(x).mul_(sub_pairs(y)).sum().div(n*(n-1)) | |
d = torch.empty(10) | |
for i in range(10): | |
x, y = torch.randperm(4000), torch.randperm(4000) | |
t = time.time_ns() | |
m = kendall(x,y) | |
d[i] = (time.time_ns() - t)*1e-6 | |
print(f'{d[i]:.2f}ms') | |
print(f'{abs(kendalltau(x,y).correlation - m):.9f}') | |
print(f'AVG {d.mean():.2f}ms') |
There will be no version for TensorFlow.
By pure SciPy replacement you mean replace the current SciPy implementation with this one? If yes, then no.
Otherwise, I do not know what you mean.
Thank you for the link. I think the code at the link is better. I wrote this code 3 years ago, and back then the torch implementation was much faster than SciPy's implementation, at least in my runs. Now, when I run it, the torch implementation is much slower. I do not know what happened, so I suggest to use this code only for the educational values.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will there be a version for TensorFlow or pure SciPy replacement? See also https://github.com/pachadotdev/kendallknight