bundles / scipy 1.17.1 / scipy / stats / _stats_py / kendalltau
function
scipy.stats._stats_py:kendalltau
source: /scipy/stats/_stats_py.py :5530
Signature
def kendalltau ( x , y , * , nan_policy = propagate , method = auto , variant = b , alternative = two-sided , axis = None , keepdims = False ) Summary
Calculate Kendall's tau, a correlation measure for ordinal data.
Extended Summary
Kendall's tau is a measure of the correspondence between two rankings. Values close to 1 indicate strong agreement, and values close to -1 indicate strong disagreement. This implements two variants of Kendall's tau: tau-b (the default) and tau-c (also known as Stuart's tau-c). These differ only in how they are normalized to lie within the range -1 to 1; the hypothesis tests (their p-values) are identical. Kendall's original tau-a is not implemented separately because both tau-b and tau-c reduce to tau-a in the absence of ties.
Although a naive implementation has O(n^2) complexity, this implementation uses a Fenwick tree to do the computation in O(n log(n)) complexity.
Parameters
x, y: array_likeArrays of rankings, of the same shape. If arrays are not 1-D, they will be flattened to 1-D.
nan_policy: {'propagate', 'omit', 'raise'}Defines how to handle input NaNs.
propagate: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.omit: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.raise: if a NaN is present, aValueErrorwill be raised.
method: {'auto', 'asymptotic', 'exact'}, optionalDefines which method is used to calculate the p-value [5]. The following options are available (default is 'auto'):
'auto': selects the appropriate method based on a trade-off between speed and accuracy
'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. As the sample size increases, the 'exact' computation time may grow and the result may lose some precision.
variant: {'b', 'c'}, optionalDefines which variant of Kendall's tau is returned. Default is 'b'.
alternative: {'two-sided', 'less', 'greater'}, optionalDefines the alternative hypothesis. Default is 'two-sided'. The following options are available:
'two-sided': the rank correlation is nonzero
'less': the rank correlation is negative (less than zero)
'greater': the rank correlation is positive (greater than zero)
axis: int or None, default: NoneIf an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If
None, the input will be raveled before computing the statistic.keepdims: bool, default: FalseIf this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
Returns
res: SignificanceResultAn object containing attributes:
statistic
statistic
pvalue
pvalue
Raises
: ValueErrorIf
nan_policyis 'omit' andvariantis not 'b' or ifmethodis 'exact' and there are ties betweenxandy.
Notes
The definition of Kendall's tau that is used is [2]
tau_b = (P - Q) / sqrt((P + Q + T) * (P + Q + U)) tau_c = 2 (P - Q) / (n**2 * (m - 1) / m)
where P is the number of concordant pairs, Q the number of discordant pairs, T the number of tied pairs only in x, and U the number of tied pairs 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. n is the total number of samples, and m is the number of unique values in either x or y, whichever is smaller.
Beginning in SciPy 1.9, np.matrix inputs (not recommended for new code) are converted to np.ndarray before the calculation is performed. In this case, the output will be a scalar or np.ndarray of appropriate shape rather than a 2D np.matrix. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar or np.ndarray rather than a masked array with mask=False.
Array API Standard Support
kendalltau has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ⛔ ⛔ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
from scipy import stats x1 = [12, 2, 1, 12, 2] x2 = [1, 4, 7, 1, 0] res = stats.kendalltau(x1, x2)✓
res.statistic res.pvalue✗
See also
- hypothesis_kendalltau
Extended example
- spearmanr
Calculates a Spearman rank-order correlation coefficient.
- theilslopes
Computes the Theil-Sen estimator for a set of points (x, y).
- weightedtau
Computes a weighted version of Kendall's tau.
Aliases
-
scipy.stats.kendalltau