parameters: (x, y, initial_lexsort=None, nan_policy='propagate')
return_statement: return KendalltauResult(tau, prob)
docstring: 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).
      nan_policy : {'propagate', 'raise', 'omit'}, optional
          Defines how to handle when input contains nan. 'propagate' returns nan,
          'raise' throws an error, 'omit' performs the calculations ignoring nan
          values. Default is 'propagate'.
  
      Returns
      -------
      correlation : float
         The tau statistic.
      pvalue : float
         The two-sided p-value for a hypothesis test whose null hypothesis is
         an absence of association, tau = 0.
  
      See also
      --------
      spearmanr : Calculates a Spearman rank-order correlation coefficient.
      theilslopes : Computes the Theil-Sen estimator for a set of points (x, y).
  
      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
      --------
      >>> from scipy import 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
docstring_summary: Calculates Kendall's tau, a correlation measure for ordinal data.
docstring_tokens: Calculates Kendall s tau a correlation measure for ordinal data .
function: def kendalltau(x, y, initial_lexsort=None, nan_policy='propagate'):
      """
      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).
      nan_policy : {'propagate', 'raise', 'omit'}, optional
          Defines how to handle when input contains nan. 'propagate' returns nan,
          'raise' throws an error, 'omit' performs the calculations ignoring nan
          values. Default is 'propagate'.
  
      Returns
      -------
      correlation : float
         The tau statistic.
      pvalue : float
         The two-sided p-value for a hypothesis test whose null hypothesis is
         an absence of association, tau = 0.
  
      See also
      --------
      spearmanr : Calculates a Spearman rank-order correlation coefficient.
      theilslopes : Computes the Theil-Sen estimator for a set of points (x, y).
  
      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
      --------
      >>> from scipy import 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
  
      """
      x = np.asarray(x).ravel()
      y = np.asarray(y).ravel()
  
      if x.size != y.size:
          raise ValueError("All inputs to `kendalltau` must be of the same size, "
                           "found x-size %s and y-size %s" % (x.size, y.size))
      elif not x.size or not y.size:
          return KendalltauResult(np.nan, np.nan)  # Return NaN if arrays are empty
  
      # check both x and y
      cnx, npx = _contains_nan(x, nan_policy)
      cny, npy = _contains_nan(y, nan_policy)
      contains_nan = cnx or cny
      if npx == 'omit' or npy == 'omit':
          nan_policy = 'omit'
  
      if contains_nan and nan_policy == 'propagate':
          return KendalltauResult(np.nan, np.nan)
  
      elif contains_nan and nan_policy == 'omit':
          x = ma.masked_invalid(x)
          y = ma.masked_invalid(y)
          return mstats_basic.kendalltau(x, y)
  
      if initial_lexsort is not None:  # deprecate to drop!
          warnings.warn('"initial_lexsort" is gone!')
  
      def count_rank_tie(ranks):
          cnt = np.bincount(ranks).astype('int64', copy=False)
          return (cnt * (cnt - 1) // 2).sum()
  
      size = x.size
      perm = np.argsort(y)  # sort on y and convert y to dense ranks
      x, y = x[perm], y[perm]
      y = np.r_[True, y[1:] != y[:-1]].cumsum(dtype=np.intp)
  
      # stable sort on x and convert x to dense ranks
      perm = np.argsort(x, kind='mergesort')
      x, y = x[perm], y[perm]
      x = np.r_[True, x[1:] != x[:-1]].cumsum(dtype=np.intp)
  
      con, dis = _kendall_condis(x, y)  # concordant & discordant pairs
  
      obs = np.r_[True, (x[1:] != x[:-1]) | (y[1:] != y[:-1]), True]
      cnt = np.diff(np.where(obs)[0]).astype('int64', copy=False)
  
      ntie = (cnt * (cnt - 1) // 2).sum()  # joint ties
      xtie = count_rank_tie(x) - ntie      # ties only in x
      ytie = count_rank_tie(y) - ntie      # ties only in y
  
      if con + dis + xtie == 0 or con + dis + ytie == 0:
          return KendalltauResult(np.nan, np.nan)
  
      tau = (con - dis) / np.sqrt(con + dis + xtie) / np.sqrt(con + dis + ytie)
  
      # what follows reproduces the ending of Gary Strangman's original
      # stats.kendalltau() in SciPy
      svar = (4.0 * size + 10.0) / (9.0 * size * (size - 1))
      z = tau / np.sqrt(svar)
      prob = special.erfc(np.abs(z) / 1.4142136)
  
      return KendalltauResult(tau, prob)
function_tokens: def kendalltau ( x , y , initial_lexsort = None , nan_policy = 'propagate' ) : x = np . asarray ( x ) . ravel ( ) y = np . asarray ( y ) . ravel ( ) if x . size != y . size : raise ValueError ( "All inputs to `kendalltau` must be of the same size, " "found x-size %s and y-size %s" % ( x . size , y . size ) ) elif not x . size or not y . size : return KendalltauResult ( np . nan , np . nan ) # Return NaN if arrays are empty # check both x and y cnx , npx = _contains_nan ( x , nan_policy ) cny , npy = _contains_nan ( y , nan_policy ) contains_nan = cnx or cny if npx == 'omit' or npy == 'omit' : nan_policy = 'omit' if contains_nan and nan_policy == 'propagate' : return KendalltauResult ( np . nan , np . nan ) elif contains_nan and nan_policy == 'omit' : x = ma . masked_invalid ( x ) y = ma . masked_invalid ( y ) return mstats_basic . kendalltau ( x , y ) if initial_lexsort is not None : # deprecate to drop! warnings . warn ( '"initial_lexsort" is gone!' ) def count_rank_tie ( ranks ) : cnt = np . bincount ( ranks ) . astype ( 'int64' , copy = False ) return ( cnt * ( cnt - 1 ) // 2 ) . sum ( ) size = x . size perm = np . argsort ( y ) # sort on y and convert y to dense ranks x , y = x [ perm ] , y [ perm ] y = np . r_ [ True , y [ 1 : ] != y [ : - 1 ] ] . cumsum ( dtype = np . intp ) # stable sort on x and convert x to dense ranks perm = np . argsort ( x , kind = 'mergesort' ) x , y = x [ perm ] , y [ perm ] x = np . r_ [ True , x [ 1 : ] != x [ : - 1 ] ] . cumsum ( dtype = np . intp ) con , dis = _kendall_condis ( x , y ) # concordant & discordant pairs obs = np . r_ [ True , ( x [ 1 : ] != x [ : - 1 ] ) | ( y [ 1 : ] != y [ : - 1 ] ) , True ] cnt = np . diff ( np . where ( obs ) [ 0 ] ) . astype ( 'int64' , copy = False ) ntie = ( cnt * ( cnt - 1 ) // 2 ) . sum ( ) # joint ties xtie = count_rank_tie ( x ) - ntie # ties only in x ytie = count_rank_tie ( y ) - ntie # ties only in y if con + dis + xtie == 0 or con + dis + ytie == 0 : return KendalltauResult ( np . nan , np . nan ) tau = ( con - dis ) / np . sqrt ( con + dis + xtie ) / np . sqrt ( con + dis + ytie ) # what follows reproduces the ending of Gary Strangman's original # stats.kendalltau() in SciPy svar = ( 4.0 * size + 10.0 ) / ( 9.0 * size * ( size - 1 ) ) z = tau / np . sqrt ( svar ) prob = special . erfc ( np . abs ( z ) / 1.4142136 ) return KendalltauResult ( tau , prob )