-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
PERF: cythonize kendall correlation #39132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
72427e6
7ded648
ef1dc87
b2384bf
26f4771
e6fca9b
85a33c8
012b5a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,6 +393,77 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1) -> ndarr | |
return result | ||
|
||
|
||
# ---------------------------------------------------------------------- | ||
# Kendall correlation | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def nancorr_kendall(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1) -> ndarray: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we usually dont mix-and-match cython-vs-python style annotations. i know this is tough bc the python-style won't allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now, the function signature for this function is the same as the one for nancorr_spearman. However, I notice that other functions have left out the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right but we are currently not checking the .pyx right so this is moot for now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yah its not a big deal |
||
cdef: | ||
Py_ssize_t i, j, xi, yi, N, K | ||
ndarray[float64_t, ndim=2] result | ||
ndarray[float64_t, ndim=2] ranked_mat | ||
ndarray[uint8_t, ndim=2] mask | ||
float64_t currj | ||
ndarray[uint8_t, ndim=1] valid | ||
ndarray[float64_t, ndim=2] valid_cols | ||
ndarray[float64_t, ndim=1] col | ||
int64_t n_concordant | ||
int64_t total_concordant = 0 | ||
int64_t total_discordant = 0 | ||
float64_t kendall_tau | ||
int64_t n_obs | ||
const int64_t[:] labels_n | ||
|
||
N, K = (<object>mat).shape | ||
|
||
result = np.empty((K, K), dtype=np.float64) | ||
mask = np.isfinite(mat).view(np.uint8) | ||
|
||
ranked_mat = np.empty((N, K), dtype=np.float64) | ||
# For compatibility when calling rank_1d | ||
labels_n = np.zeros(N, dtype=np.int64) | ||
|
||
for i in range(K): | ||
ranked_mat[:, i] = rank_1d(mat[:, i], labels_n) | ||
|
||
for xi in range(K): | ||
for yi in range(xi + 1, K): | ||
valid = mask[:, xi] & mask[:, yi] | ||
if valid.sum() < minp: | ||
result[xi, yi] = NaN | ||
result[yi, xi] = NaN | ||
else: | ||
# Get columns and order second column using 1st column ranks | ||
if not valid.all(): | ||
valid_cols = ranked_mat[valid.nonzero()][:, [xi, yi]] | ||
else: | ||
valid_cols = ranked_mat[:, [xi, yi]] | ||
# Unfortunately we have to sort here, since we can have tied indices | ||
col = valid_cols[:, 1][valid_cols[:, 0].argsort()] | ||
lithomas1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
n_obs = valid_cols.shape[0] | ||
total_concordant = 0 | ||
total_discordant = 0 | ||
for j in range(n_obs - 1): | ||
currj = col[j] | ||
# Count num concordant and discordant pairs | ||
n_concordant = np.sum(col[j+1:]>=currj) | ||
total_concordant += n_concordant | ||
total_discordant += (n_obs-1-j-n_concordant) | ||
lithomas1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
kendall_tau = (total_concordant - total_discordant) / \ | ||
(total_concordant + total_discordant) | ||
result[xi, yi] = kendall_tau | ||
result[yi, xi] = kendall_tau | ||
|
||
if mask[:, xi].sum() > minp: | ||
result[xi, xi] = 1 | ||
else: | ||
result[xi, xi] = NaN | ||
|
||
return result | ||
|
||
|
||
# ---------------------------------------------------------------------- | ||
|
||
ctypedef fused algos_t: | ||
|
Uh oh!
There was an error while loading. Please reload this page.