#NEWLINE# def kendalltau( x , y , use_ties = True , use_missing = False ) : #NEWLINE# #INDENT# "\n    Computes Kendall's rank correlation tau on two variables *x* and *y*.\n\n    Parameters\n    ----------\n    x : sequence\n        First data list (for example, time).\n    y : sequence\n        Second data list.\n    use_ties : {True, False}, optional\n        Whether ties correction should be performed.\n    use_missing : {False, True}, optional\n        Whether missing data should be allocated a rank of 0 (False) or the\n        average rank (True)\n\n    Returns\n    -------\n    correlation : float\n        Kendall tau\n    pvalue : float\n        Approximate 2-side p-value.\n\n    " #NEWLINE# ( x , y , n ) = _chk_size ( x , y ) #NEWLINE# ( x , y ) = ( x . flatten ( ) , y . flatten ( ) ) #NEWLINE# m = ma . mask_or ( ma . getmask ( x ) , ma . getmask ( y ) ) #NEWLINE# if ( m is not nomask ) : #NEWLINE# #INDENT# x = ma . array ( x , mask = m , copy = True ) #NEWLINE# y = ma . array ( y , mask = m , copy = True ) #NEWLINE# n -= m . sum ( ) #NEWLINE# #UNINDENT# if ( n < 2 ) : #NEWLINE# #INDENT# return KendalltauResult ( np . nan , np . nan ) #NEWLINE# #UNINDENT# rx = ma . masked_equal ( rankdata ( x , use_missing = use_missing ) , 0 ) #NEWLINE# ry = ma . masked_equal ( rankdata ( y , use_missing = use_missing ) , 0 ) #NEWLINE# idx = rx . argsort ( ) #NEWLINE# ( rx , ry ) = ( rx [ idx ] , ry [ idx ] ) #NEWLINE# C = np . sum ( [ ( ( ry [ ( i + 1 ) : ] > ry [ i ] ) * ( rx [ ( i + 1 ) : ] > rx [ i ] ) ) . filled ( 0 ) . sum ( ) for i in range ( ( len ( ry ) - 1 ) ) ] , dtype = float ) #NEWLINE# D = np . sum ( [ ( ( ry [ ( i + 1 ) : ] < ry [ i ] ) * ( rx [ ( i + 1 ) : ] > rx [ i ] ) ) . filled ( 0 ) . sum ( ) for i in range ( ( len ( ry ) - 1 ) ) ] , dtype = float ) #NEWLINE# if use_ties : #NEWLINE# #INDENT# xties = count_tied_groups ( x ) #NEWLINE# yties = count_tied_groups ( y ) #NEWLINE# corr_x = np . sum ( [ ( ( v * k ) * ( k - 1 ) ) for ( k , v ) in iteritems ( xties ) ] , dtype = float ) #NEWLINE# corr_y = np . sum ( [ ( ( v * k ) * ( k - 1 ) ) for ( k , v ) in iteritems ( yties ) ] , dtype = float ) #NEWLINE# denom = ma . sqrt ( ( ( ( ( ( n * ( n - 1 ) ) - corr_x ) / 2.0 ) * ( ( n * ( n - 1 ) ) - corr_y ) ) / 2.0 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# denom = ( ( n * ( n - 1 ) ) / 2.0 ) #NEWLINE# #UNINDENT# tau = ( ( C - D ) / denom ) #NEWLINE# var_s = ( ( n * ( n - 1 ) ) * ( ( 2 * n ) + 5 ) ) #NEWLINE# if use_ties : #NEWLINE# #INDENT# var_s -= np . sum ( ( ( ( ( ( v * k ) * ( k - 1 ) ) * ( ( 2 * k ) + 5 ) ) * 1.0 ) for ( k , v ) in iteritems ( xties ) ) ) #NEWLINE# var_s -= np . sum ( ( ( ( ( ( v * k ) * ( k - 1 ) ) * ( ( 2 * k ) + 5 ) ) * 1.0 ) for ( k , v ) in iteritems ( yties ) ) ) #NEWLINE# v1 = ( np . sum ( [ ( ( v * k ) * ( k - 1 ) ) for ( k , v ) in iteritems ( xties ) ] , dtype = float ) * np . sum ( [ ( ( v * k ) * ( k - 1 ) ) for ( k , v ) in iteritems ( yties ) ] , dtype = float ) ) #NEWLINE# v1 /= ( ( 2.0 * n ) * ( n - 1 ) ) #NEWLINE# if ( n > 2 ) : #NEWLINE# #INDENT# v2 = ( np . sum ( [ ( ( ( v * k ) * ( k - 1 ) ) * ( k - 2 ) ) for ( k , v ) in iteritems ( xties ) ] , dtype = float ) * np . sum ( [ ( ( ( v * k ) * ( k - 1 ) ) * ( k - 2 ) ) for ( k , v ) in iteritems ( yties ) ] , dtype = float ) ) #NEWLINE# v2 /= ( ( ( 9.0 * n ) * ( n - 1 ) ) * ( n - 2 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# v2 = 0 #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# v1 = v2 = 0 #NEWLINE# #UNINDENT# var_s /= 18.0 #NEWLINE# var_s += ( v1 + v2 ) #NEWLINE# z = ( ( C - D ) / np . sqrt ( var_s ) ) #NEWLINE# prob = special . erfc ( ( abs ( z ) / np . sqrt ( 2 ) ) ) #NEWLINE# return KendalltauResult ( tau , prob )