def sizes2count(a, n):
    return (np.inner(a, a) - n) // 2

def pair_counts_vec_nlogn(a, b):
    ab = a * b.max() + b
_, sizes = np.unique(ab, return_counts=True)
n = len(a)
n11 = sizes2count(sizes, n)
n10 = sizes2count(np.bincount(a), n) - n11
n01 = sizes2count(np.bincount(b), n) - n11
n00 = n ** 2 - n - n11 - n10 - n01
return n11, n10, n01, n00

def pair_counts_vec_linear(a, b):
    ab = a * b.max() + b
n = len(a)
n11 = sizes2count(np.bincount(ab), n)
n10 = sizes2count(np.bincount(a), n) - n11
n01 = sizes2count(np.bincount(b), n) - n11
n00 = n ** 2 - n - n11 - n10 - n01
return n11, n10, n01, n00
Высокопроизводительное всеобщее сравнение векторов в Python