#NEWLINE# def kendalltau_seasonal( x ) : #NEWLINE# #INDENT# "\n    Computes a multivariate Kendall's rank correlation tau, for seasonal data.\n\n    Parameters\n    ----------\n    x : 2-D ndarray\n        Array of seasonal data, with seasons in columns.\n\n    " #NEWLINE# x = ma . array ( x , subok = True , copy = False , ndmin = 2 ) #NEWLINE# ( n , m ) = x . shape #NEWLINE# n_p = x . count ( 0 ) #NEWLINE# S_szn = np . sum ( ( msign ( ( x [ i : ] - x [ i ] ) ) . sum ( 0 ) for i in range ( n ) ) ) #NEWLINE# S_tot = S_szn . sum ( ) #NEWLINE# n_tot = x . count ( ) #NEWLINE# ties = count_tied_groups ( x . compressed ( ) ) #NEWLINE# corr_ties = np . sum ( ( ( ( v * k ) * ( k - 1 ) ) for ( k , v ) in iteritems ( ties ) ) ) #NEWLINE# denom_tot = ( ma . sqrt ( ( ( ( 1.0 * n_tot ) * ( n_tot - 1 ) ) * ( ( n_tot * ( n_tot - 1 ) ) - corr_ties ) ) ) / 2.0 ) #NEWLINE# R = rankdata ( x , axis = 0 , use_missing = True ) #NEWLINE# K = ma . empty ( ( m , m ) , dtype = int ) #NEWLINE# covmat = ma . empty ( ( m , m ) , dtype = float ) #NEWLINE# denom_szn = ma . empty ( m , dtype = float ) #NEWLINE# for j in range ( m ) : #NEWLINE# #INDENT# ties_j = count_tied_groups ( x [ : , j ] . compressed ( ) ) #NEWLINE# corr_j = np . sum ( ( ( ( v * k ) * ( k - 1 ) ) for ( k , v ) in iteritems ( ties_j ) ) ) #NEWLINE# cmb = ( n_p [ j ] * ( n_p [ j ] - 1 ) ) #NEWLINE# for k in range ( j , m , 1 ) : #NEWLINE# #INDENT# K [ ( j , k ) ] = np . sum ( ( msign ( ( ( x [ i : , j ] - x [ ( i , j ) ] ) * ( x [ i : , k ] - x [ ( i , k ) ] ) ) ) . sum ( ) for i in range ( n ) ) ) #NEWLINE# covmat [ ( j , k ) ] = ( ( ( K [ ( j , k ) ] + ( 4 * ( R [ : , j ] * R [ : , k ] ) . sum ( ) ) ) - ( ( n * ( n_p [ j ] + 1 ) ) * ( n_p [ k ] + 1 ) ) ) / 3.0 ) #NEWLINE# K [ ( k , j ) ] = K [ ( j , k ) ] #NEWLINE# covmat [ ( k , j ) ] = covmat [ ( j , k ) ] #NEWLINE# #UNINDENT# denom_szn [ j ] = ( ma . sqrt ( ( cmb * ( cmb - corr_j ) ) ) / 2.0 ) #NEWLINE# #UNINDENT# var_szn = covmat . diagonal ( ) #NEWLINE# z_szn = ( ( msign ( S_szn ) * ( abs ( S_szn ) - 1 ) ) / ma . sqrt ( var_szn ) ) #NEWLINE# z_tot_ind = ( ( msign ( S_tot ) * ( abs ( S_tot ) - 1 ) ) / ma . sqrt ( var_szn . sum ( ) ) ) #NEWLINE# z_tot_dep = ( ( msign ( S_tot ) * ( abs ( S_tot ) - 1 ) ) / ma . sqrt ( covmat . sum ( ) ) ) #NEWLINE# prob_szn = special . erfc ( ( abs ( z_szn ) / np . sqrt ( 2 ) ) ) #NEWLINE# prob_tot_ind = special . erfc ( ( abs ( z_tot_ind ) / np . sqrt ( 2 ) ) ) #NEWLINE# prob_tot_dep = special . erfc ( ( abs ( z_tot_dep ) / np . sqrt ( 2 ) ) ) #NEWLINE# chi2_tot = ( z_szn * z_szn ) . sum ( ) #NEWLINE# chi2_trd = ( m * ( z_szn . mean ( ) ** 2 ) ) #NEWLINE# output = { #NEWLINE# #INDENT# 'seasonal tau' : ( S_szn / denom_szn ) , #NEWLINE# 'global tau' : ( S_tot / denom_tot ) , #NEWLINE# 'global tau (alt)' : ( S_tot / denom_szn . sum ( ) ) , #NEWLINE# 'seasonal p-value' : prob_szn , #NEWLINE# 'global p-value (indep)' : prob_tot_ind , #NEWLINE# 'global p-value (dep)' : prob_tot_dep , #NEWLINE# 'chi2 total' : chi2_tot , #NEWLINE# 'chi2 trend' : chi2_trd , #NEWLINE# #UNINDENT# } #NEWLINE# return output