bundles / scipy latest / scipy / stats / _correlation / spearmanrho
function
scipy.stats._correlation:spearmanrho
Signature
def spearmanrho ( x , y , / , alternative = two-sided , method = None , axis = 0 , nan_policy = propagate , keepdims = False ) Summary
Calculate a Spearman rho correlation coefficient with associated p-value.
Extended Summary
The Spearman rank-order correlation coefficient is a nonparametric measure of the monotonicity of the relationship between two datasets. Like other correlation coefficients, it varies between -1 and +1 with 0 implying no correlation. Coefficients of -1 or +1 are associated with an exact monotonic relationship. Positive correlations indicate that as x increases, so does y; negative correlations indicate that as x increases, y decreases. The p-value is the probability of an uncorrelated system producing datasets with a Spearman correlation at least as extreme as the one computed from the observed dataset.
Parameters
x, y: array-likeThe samples: corresponding observations of the independent and dependent variable. The (N-d) arrays must be broadcastable.
alternative: {'two-sided', 'less', 'greater'}, optionalDefines the alternative hypothesis. Default is 'two-sided'. The following options are available:
'two-sided': the correlation is nonzero
'less': the correlation is negative (less than zero)
'greater': the correlation is positive (greater than zero)
method: ResamplingMethod, optionalDefines the method used to compute the p-value. If
methodis an instance of PermutationMethod/MonteCarloMethod, the p-value is computed using scipy.stats.permutation_test/scipy.stats.monte_carlo_test with the provided configuration options and other appropriate settings. Otherwise, the p-value is computed using an asymptotic approximation of the null distribution.axis: int or None, default: 0If 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.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.
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
Warns
: `~scipy.stats.ConstantInputWarning`Raised if an input is a constant array. The correlation coefficient is not defined in this case, so
np.nanis returned.
Notes
spearmanrho was created to make improvements to SciPy's implementation of the Spearman correlation test without making backward-incompatible changes to spearmanr. Advantages of spearmanrho over spearmanr include:
spearmanrho follows standard array broadcasting rules.
spearmanrho is compatible with some non-NumPy arrays.
spearmanrho can compute exact p-values, even in the presence of ties, when an appropriate instance of PermutationMethod is provided via the
methodargument.
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
spearmanrho 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
Univariate samples, approximate p-value.import numpy as np from scipy import stats x = [1, 2, 3, 4, 5] y = [5, 6, 7, 8, 7] res = stats.spearmanrho(x, y) res.statistic✓
res.pvalue
✗res = stats.spearmanrho(x, y, method=stats.PermutationMethod()) res.statistic res.pvalue✓
rng = np.random.default_rng(98145152315484) x2 = rng.standard_normal((2, 100)) y2 = rng.standard_normal((2, 100)) res = stats.spearmanrho(x2, y2, axis=-1)✓
res.statistic res.pvalue✗
res = stats.spearmanrho(x2[np.newaxis, :], x2[:, np.newaxis], axis=-1)
✓res.statistic res.pvalue✗
Aliases
-
scipy.stats.spearmanrho