bundles / scipy latest / scipy / stats / _correlation / spearmanrho
function
scipy.stats._correlation:spearmanrho
source: build-install/usr/lib/python3.14/site-packages/scipy/stats/_correlation.py:250
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.
nan_policy='raise'andnan_policy='omit'are not supported by lazy backends, andnan_policy='omit'is not compatible with MArray input. For multidimensional input,nan_policy='omit'is supported only by the NumPy backend. Instead, mask NaN values using MArray; and use the defaultnan_policy. See Notes for support information.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
Spearman correlation coefficient
pvalue
The p-value - the probabilitiy of realizing such an extreme statistic value under the null hypothesis that two samples have no ordinal correlation. See
alternativeabove for alternative hypotheses.
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.
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✓
np.float64(0.8207826816681233)
res.pvalue
✗np.float64(0.08858700531354395)Univariate samples, exact p-value.
res = stats.spearmanrho(x, y, method=stats.PermutationMethod()) res.statistic res.pvalue✓
np.float64(0.13333333333333333)Batch of univariate samples, one vectorized call.
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✗
array([0.09912, 0.22847])Bivariate samples using standard broadcasting rules.
res = stats.spearmanrho(x2[np.newaxis, :], x2[:, np.newaxis], axis=-1)
✓res.statistic res.pvalue✗
array([[0. , 0.14526],
[0.14526, 0. ]])
Aliases
scipy.stats.spearmanrho