parameters: (x, y, use_ties=True, use_missing=False, method='auto')
return_statement: return KendalltauResult(tau, prob)
docstring: Computes Kendall's rank correlation tau on two variables *x* and *y*.
  
      Parameters
      ----------
      x : sequence
          First data list (for example, time).
      y : sequence
          Second data list.
      use_ties : {True, False}, optional
          Whether ties correction should be performed.
      use_missing : {False, True}, optional
          Whether missing data should be allocated a rank of 0 (False) or the
          average rank (True)
      method: {'auto', 'asymptotic', 'exact'}, optional
          Defines which method is used to calculate the p-value [1]_.
          'asymptotic' uses a normal approximation valid for large samples.
          'exact' computes the exact p-value, but can only be used if no ties
          are present. 'auto' is the default and selects the appropriate
          method based on a trade-off between speed and accuracy.
  
      Returns
      -------
      correlation : float
          Kendall tau
      pvalue : float
          Approximate 2-side p-value.
  
      References
      ----------
      .. [1] Maurice G. Kendall, "Rank Correlation Methods" (4th Edition),
             Charles Griffin & Co., 1970.
docstring_summary: Computes Kendall's rank correlation tau on two variables *x* and *y*.
docstring_tokens: Computes Kendall s rank correlation tau on two variables * x * and * y * .
function: def kendalltau(x, y, use_ties=True, use_missing=False, method='auto'):
      """
      Computes Kendall's rank correlation tau on two variables *x* and *y*.
  
      Parameters
      ----------
      x : sequence
          First data list (for example, time).
      y : sequence
          Second data list.
      use_ties : {True, False}, optional
          Whether ties correction should be performed.
      use_missing : {False, True}, optional
          Whether missing data should be allocated a rank of 0 (False) or the
          average rank (True)
      method: {'auto', 'asymptotic', 'exact'}, optional
          Defines which method is used to calculate the p-value [1]_.
          'asymptotic' uses a normal approximation valid for large samples.
          'exact' computes the exact p-value, but can only be used if no ties
          are present. 'auto' is the default and selects the appropriate
          method based on a trade-off between speed and accuracy.
  
      Returns
      -------
      correlation : float
          Kendall tau
      pvalue : float
          Approximate 2-side p-value.
  
      References
      ----------
      .. [1] Maurice G. Kendall, "Rank Correlation Methods" (4th Edition),
             Charles Griffin & Co., 1970.
  
      """
      (x, y, n) = _chk_size(x, y)
      (x, y) = (x.flatten(), y.flatten())
      m = ma.mask_or(ma.getmask(x), ma.getmask(y))
      if m is not nomask:
          x = ma.array(x, mask=m, copy=True)
          y = ma.array(y, mask=m, copy=True)
          # need int() here, otherwise numpy defaults to 32 bit
          # integer on all Windows architectures, causing overflow.
          # int() will keep it infinite precision.
          n -= int(m.sum())
  
      if n < 2:
          return KendalltauResult(np.nan, np.nan)
  
      rx = ma.masked_equal(rankdata(x, use_missing=use_missing), 0)
      ry = ma.masked_equal(rankdata(y, use_missing=use_missing), 0)
      idx = rx.argsort()
      (rx, ry) = (rx[idx], ry[idx])
      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)
      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)
      xties = count_tied_groups(x)
      yties = count_tied_groups(y)
      if use_ties:
          corr_x = np.sum([v*k*(k-1) for (k,v) in iteritems(xties)], dtype=float)
          corr_y = np.sum([v*k*(k-1) for (k,v) in iteritems(yties)], dtype=float)
          denom = ma.sqrt((n*(n-1)-corr_x)/2. * (n*(n-1)-corr_y)/2.)
      else:
          denom = n*(n-1)/2.
      tau = (C-D) / denom
  
      if method == 'exact' and (xties or yties):
          raise ValueError("Ties found, exact method cannot be used.")
  
      if method == 'auto':
          if (not xties and not yties) and (n <= 33 or min(C, n*(n-1)/2.0-C) <= 1):
              method = 'exact'
          else:
              method = 'asymptotic'
  
      if not xties and not yties and method == 'exact':
          # Exact p-value, see Maurice G. Kendall, "Rank Correlation Methods" (4th Edition), Charles Griffin & Co., 1970.
          c = int(min(C, (n*(n-1))/2-C))
          if n <= 0:
              raise ValueError
          elif c < 0 or 2*c > n*(n-1):
              raise ValueError
          elif n == 1:
              prob = 1.0
          elif n == 2:
              prob = 1.0
          elif c == 0:
              prob = 2.0/np.math.factorial(n)
          elif c == 1:
              prob = 2.0/np.math.factorial(n-1)
          else:
              old = [0.0]*(c+1)
              new = [0.0]*(c+1)
              new[0] = 1.0
              new[1] = 1.0
              for j in range(3,n+1):
                  old = new[:]
                  for k in range(1,min(j,c+1)):
                      new[k] += new[k-1]
                  for k in range(j,c+1):
                      new[k] += new[k-1] - old[k-j]
              prob = 2.0*sum(new)/np.math.factorial(n)
      elif method == 'asymptotic':
          var_s = n*(n-1)*(2*n+5)
          if use_ties:
              var_s -= np.sum([v*k*(k-1)*(2*k+5)*1. for (k,v) in iteritems(xties)])
              var_s -= np.sum([v*k*(k-1)*(2*k+5)*1. for (k,v) in iteritems(yties)])
              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)
              v1 /= 2.*n*(n-1)
              if n > 2:
                  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)
                  v2 /= 9.*n*(n-1)*(n-2)
              else:
                  v2 = 0
          else:
              v1 = v2 = 0
  
          var_s /= 18.
          var_s += (v1 + v2)
          z = (C-D)/np.sqrt(var_s)
          prob = special.erfc(abs(z)/np.sqrt(2))
      else:
          raise ValueError("Unknown method "+str(method)+" specified, please use auto, exact or asymptotic.")
  
      return KendalltauResult(tau, prob)
function_tokens: def kendalltau ( x , y , use_ties = True , use_missing = False , method = 'auto' ) : ( x , y , n ) = _chk_size ( x , y ) ( x , y ) = ( x . flatten ( ) , y . flatten ( ) ) m = ma . mask_or ( ma . getmask ( x ) , ma . getmask ( y ) ) if m is not nomask : x = ma . array ( x , mask = m , copy = True ) y = ma . array ( y , mask = m , copy = True ) # need int() here, otherwise numpy defaults to 32 bit # integer on all Windows architectures, causing overflow. # int() will keep it infinite precision. n -= int ( m . sum ( ) ) if n < 2 : return KendalltauResult ( np . nan , np . nan ) rx = ma . masked_equal ( rankdata ( x , use_missing = use_missing ) , 0 ) ry = ma . masked_equal ( rankdata ( y , use_missing = use_missing ) , 0 ) idx = rx . argsort ( ) ( rx , ry ) = ( rx [ idx ] , ry [ idx ] ) 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 ) 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 ) xties = count_tied_groups ( x ) yties = count_tied_groups ( y ) if use_ties : corr_x = np . sum ( [ v * k * ( k - 1 ) for ( k , v ) in iteritems ( xties ) ] , dtype = float ) corr_y = np . sum ( [ v * k * ( k - 1 ) for ( k , v ) in iteritems ( yties ) ] , dtype = float ) denom = ma . sqrt ( ( n * ( n - 1 ) - corr_x ) / 2. * ( n * ( n - 1 ) - corr_y ) / 2. ) else : denom = n * ( n - 1 ) / 2. tau = ( C - D ) / denom if method == 'exact' and ( xties or yties ) : raise ValueError ( "Ties found, exact method cannot be used." ) if method == 'auto' : if ( not xties and not yties ) and ( n <= 33 or min ( C , n * ( n - 1 ) / 2.0 - C ) <= 1 ) : method = 'exact' else : method = 'asymptotic' if not xties and not yties and method == 'exact' : # Exact p-value, see Maurice G. Kendall, "Rank Correlation Methods" (4th Edition), Charles Griffin & Co., 1970. c = int ( min ( C , ( n * ( n - 1 ) ) / 2 - C ) ) if n <= 0 : raise ValueError elif c < 0 or 2 * c > n * ( n - 1 ) : raise ValueError elif n == 1 : prob = 1.0 elif n == 2 : prob = 1.0 elif c == 0 : prob = 2.0 / np . math . factorial ( n ) elif c == 1 : prob = 2.0 / np . math . factorial ( n - 1 ) else : old = [ 0.0 ] * ( c + 1 ) new = [ 0.0 ] * ( c + 1 ) new [ 0 ] = 1.0 new [ 1 ] = 1.0 for j in range ( 3 , n + 1 ) : old = new [ : ] for k in range ( 1 , min ( j , c + 1 ) ) : new [ k ] += new [ k - 1 ] for k in range ( j , c + 1 ) : new [ k ] += new [ k - 1 ] - old [ k - j ] prob = 2.0 * sum ( new ) / np . math . factorial ( n ) elif method == 'asymptotic' : var_s = n * ( n - 1 ) * ( 2 * n + 5 ) if use_ties : var_s -= np . sum ( [ v * k * ( k - 1 ) * ( 2 * k + 5 ) * 1. for ( k , v ) in iteritems ( xties ) ] ) var_s -= np . sum ( [ v * k * ( k - 1 ) * ( 2 * k + 5 ) * 1. for ( k , v ) in iteritems ( yties ) ] ) 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 ) v1 /= 2. * n * ( n - 1 ) if n > 2 : 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 ) v2 /= 9. * n * ( n - 1 ) * ( n - 2 ) else : v2 = 0 else : v1 = v2 = 0 var_s /= 18. var_s += ( v1 + v2 ) z = ( C - D ) / np . sqrt ( var_s ) prob = special . erfc ( abs ( z ) / np . sqrt ( 2 ) ) else : raise ValueError ( "Unknown method " + str ( method ) + " specified, please use auto, exact or asymptotic." ) return KendalltauResult ( tau , prob )