The Kendall tau rank distance is a metric that counts the number of pairwise disagreements between two ranking lists. The larger the distance, the more dissimilar the two lists are. Kendall tau distance is also called bubblesort distance since it is equivalent to the number of swaps that the bubble sort algorithm would make to place one list in the same order as the other list. The Kendall tau distance was created by Maurice Kendall.
The Kendall tau ranking distance between two lists
L
1
and
L
2
is
K
(
τ
1
,
τ
2
)
=

{
(
i
,
j
)
:
i
<
j
,
(
τ
1
(
i
)
<
τ
1
(
j
)
∧
τ
2
(
i
)
>
τ
2
(
j
)
)
∨
(
τ
1
(
i
)
>
τ
1
(
j
)
∧
τ
2
(
i
)
<
τ
2
(
j
)
)
}

.
where
τ
1
(
i
)
and
τ
2
(
i
)
are the rankings of the element
i
in
L
1
and
L
2
respectively.
K
(
τ
1
,
τ
2
)
will be equal to 0 if the two lists are identical and
n
(
n
−
1
)
/
2
(where
n
is the list size) if one list is the reverse of the other. Often Kendall tau distance is normalized by dividing by
n
(
n
−
1
)
/
2
so a value of 1 indicates maximum disagreement. The normalized Kendall tau distance therefore lies in the interval [0,1].
Kendall tau distance may also be defined as
K
(
τ
1
,
τ
2
)
=
∑
{
i
,
j
}
∈
P
K
¯
i
,
j
(
τ
1
,
τ
2
)
where
P is the set of unordered pairs of distinct elements in
τ
1
and
τ
2
K
¯
i
,
j
(
τ
1
,
τ
2
)
= 0 if i and j are in the same order in
τ
1
and
τ
2
K
¯
i
,
j
(
τ
1
,
τ
2
)
= 1 if i and j are in the opposite order in
τ
1
and
τ
2
.
Kendall tau distance can also be defined as the total number of discordant pairs.
Kendall tau distance in Rankings: A permutation (or ranking) is an array of N integers where each of the integers between 0 and N1 appears exactly once. The Kendall tau distance between two rankings is the number of pairs that are in different order in the two rankings. For example, the Kendall tau distance between 0 3 1 6 2 5 4 and 1 0 3 6 4 2 5 is four because the pairs 01, 31, 24, 54 are in different order in the two rankings, but all other pairs are in the same order.
If Kendall tau function is performed as
K
(
L
1
,
L
2
)
instead of
K
(
τ
1
,
τ
2
)
(where
τ
1
and
τ
2
are the rankings of
L
1
and
L
2
elements respectively), then triangular inequality is not guaranteed. The triangular inequality fails in cases where there are repetitions in the lists. So then we are not any more dealing with a metric.
Suppose we rank a group of five people by height and by weight:
Here person A is tallest and thirdheaviest, and so on.
In order to calculate the Kendall tau distance, pair each person with every other person and count the number of times the values in list 1 are in the opposite order of the values in list 2.
Since there are 4 pairs whose values are in opposite order, the Kendall tau distance is 4. The normalized Kendall tau distance is
4
5
(
5
−
1
)
/
2
=
0.4.
A value of 0.4 indicates that 40% of pairs differ in ordering between the two lists.
Given two rankings
τ
1
,
τ
2
, it is possible to rename the items such that
τ
1
=
(
1
,
2
,
3
,
.
.
.
)
. Then, he problem of computing the Kendall tau distance reduces to computing the number of inversions in
τ
2
 the number of index pairs
i
,
j
such that
i
<
j
while
τ
2
(
i
)
>
τ
2
(
j
)
. There are several algorithms for calculating this number.
A simple algorithm based on merge sort requires time
O
(
n
log
n
)
.
A more advanced algorithm requires time
O
(
n
log
n
)
.
The Python scipy.stats library contains a function for calculating Kendall's tau which also handles ties in rankings.