def kendalltau(x, y, initial_lexsort=True):
x = np.asarray(x).ravel()<EOL>y = np.asarray(y).ravel()<EOL>if not x.size or not y.size:<EOL><INDENT>return (np.nan, np.nan)  <EOL><DEDENT>n = np.int64(len(x))<EOL>temp = list(range(n))  <EOL>def mergesort(offs, length):<EOL><INDENT>exchcnt = <NUM_LIT:0><EOL>if length == <NUM_LIT:1>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>if length == <NUM_LIT:2>:<EOL><INDENT>if y[perm[offs]] <= y[perm[offs+<NUM_LIT:1>]]:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>t = perm[offs]<EOL>perm[offs] = perm[offs+<NUM_LIT:1>]<EOL>perm[offs+<NUM_LIT:1>] = t<EOL>return <NUM_LIT:1><EOL><DEDENT>length0 = length // <NUM_LIT:2><EOL>length1 = length - length0<EOL>middle = offs + length0<EOL>exchcnt += mergesort(offs, length0)<EOL>exchcnt += mergesort(middle, length1)<EOL>if y[perm[middle - <NUM_LIT:1>]] < y[perm[middle]]:<EOL><INDENT>return exchcnt<EOL><DEDENT>i = j = k = <NUM_LIT:0><EOL>while j < length0 or k < length1:<EOL><INDENT>if k >= length1 or (j < length0 and y[perm[offs + j]] <=<EOL>y[perm[middle + k]]):<EOL><INDENT>temp[i] = perm[offs + j]<EOL>d = i - j<EOL>j += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>temp[i] = perm[middle + k]<EOL>d = (offs + i) - (middle + k)<EOL>k += <NUM_LIT:1><EOL><DEDENT>if d > <NUM_LIT:0>:<EOL><INDENT>exchcnt += d<EOL><DEDENT>i += <NUM_LIT:1><EOL><DEDENT>perm[offs:offs+length] = temp[<NUM_LIT:0>:length]<EOL>return exchcnt<EOL><DEDENT>if initial_lexsort:<EOL><INDENT>perm = np.lexsort((y, x))<EOL><DEDENT>else:<EOL><INDENT>perm = list(range(n))<EOL>perm.sort(key=lambda a: (x[a], y[a]))<EOL><DEDENT>first = <NUM_LIT:0><EOL>t = <NUM_LIT:0><EOL>for i in xrange(<NUM_LIT:1>, n):<EOL><INDENT>if x[perm[first]] != x[perm[i]] or y[perm[first]] != y[perm[i]]:<EOL><INDENT>t += ((i - first) * (i - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>first = i<EOL><DEDENT><DEDENT>t += ((n - first) * (n - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>first = <NUM_LIT:0><EOL>u = <NUM_LIT:0><EOL>for i in xrange(<NUM_LIT:1>,n):<EOL><INDENT>if x[perm[first]] != x[perm[i]]:<EOL><INDENT>u += ((i - first) * (i - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>first = i<EOL><DEDENT><DEDENT>u += ((n - first) * (n - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>exchanges = mergesort(<NUM_LIT:0>, n)<EOL>first = <NUM_LIT:0><EOL>v = <NUM_LIT:0><EOL>for i in xrange(<NUM_LIT:1>,n):<EOL><INDENT>if y[perm[first]] != y[perm[i]]:<EOL><INDENT>v += ((i - first) * (i - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>first = i<EOL><DEDENT><DEDENT>v += ((n - first) * (n - first - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>tot = (n * (n - <NUM_LIT:1>)) // <NUM_LIT:2><EOL>if tot == u or tot == v:<EOL><INDENT>return (np.nan, np.nan)    <EOL><DEDENT>denom = np.exp(<NUM_LIT:0.5> * (np.log(tot - u) + np.log(tot - v)))<EOL>tau = ((tot - (v + u - t)) - <NUM_LIT> * exchanges) / denom<EOL>svar = (<NUM_LIT> * n + <NUM_LIT>) / (<NUM_LIT> * n * (n - <NUM_LIT:1>))<EOL>z = tau / np.sqrt(svar)<EOL>prob = special.erfc(np.abs(z) / <NUM_LIT>)<EOL>return tau, prob<EOL>
Calculates Kendall's tau, a correlation measure for ordinal data.

Kendall's tau is a measure of the correspondence between two rankings.
Values close to 1 indicate strong agreement, values close to -1 indicate
strong disagreement.  This is the tau-b version of Kendall's tau which
accounts for ties.

Parameters
----------
x, y : array_like
    Arrays of rankings, of the same shape. If arrays are not 1-D, they will
    be flattened to 1-D.
initial_lexsort : bool, optional
    Whether to use lexsort or quicksort as the sorting method for the
    initial sort of the inputs. Default is lexsort (True), for which
    `kendalltau` is of complexity O(n log(n)). If False, the complexity is
    O(n^2), but with a smaller pre-factor (so quicksort may be faster for
    small arrays).

Returns
-------
Kendall's tau : float
   The tau statistic.
p-value : float
   The two-sided p-value for a hypothesis test whose null hypothesis is
   an absence of association, tau = 0.

Notes
-----
The definition of Kendall's tau that is used is::

  tau = (P - Q) / sqrt((P + Q + T) * (P + Q + U))

where P is the number of concordant pairs, Q the number of discordant
pairs, T the number of ties only in `x`, and U the number of ties only in
`y`.  If a tie occurs for the same pair in both `x` and `y`, it is not
added to either T or U.

References
----------
W.R. Knight, "A Computer Method for Calculating Kendall's Tau with
Ungrouped Data", Journal of the American Statistical Association, Vol. 61,
No. 314, Part 1, pp. 436-439, 1966.

Examples
--------
>>> import scipy.stats as stats
>>> x1 = [12, 2, 1, 12, 2]
>>> x2 = [1, 4, 7, 1, 0]
>>> tau, p_value = stats.kendalltau(x1, x2)
>>> tau
-0.47140452079103173
>>> p_value
0.24821309157521476