spearman_r

HydroErr.HydroErr.spearman_r(simulated_array: ndarray[tuple[Any, ...], dtype[floating | integer]] | Sequence[int | float], observed_array: ndarray[tuple[Any, ...], dtype[floating | integer]] | Sequence[int | float], replace_nan: float | None = None, replace_inf: float | None = None, remove_neg: bool = False, remove_zero: bool = False) floating[Any]

Compute the spearman rank correlation coefficient.

\[R_{Spearman}=\frac{\frac{1}{n}\sum_{i=1}^{n}(R(O_i)-\overline{R(O)})(R(S_i)-\overline{R(S)})}{\sqrt{\frac{1}{n}\sum_{i=1}^{n}(R(O_i)-\overline{R(O)})^2}\sqrt{\frac{1}{n}\sum_{i=1}^{n}(R(S_i)-\overline{R(S)})^2}}\]

Range: -1 ≤ R (Pearson) ≤ 1. 1 indicates perfect postive correlation, 0 indicates complete randomness, -1 indicate perfect negative correlation.

Notes: The spearman r coefficient measures the monotonic relation between simulated and observed data. Because it uses a nonparametric measure of rank correlation, it is less sensitive to outliers compared to the Pearson correlation coefficient.

Parameters:
  • simulated_array – An array of simulated data from the time series.

  • observed_array – An array of observed data from the time series.

  • replace_nan – If given, indicates which value to replace NaN values with in the two arrays. If None, when a NaN value is found at the i-th position in the observed OR simulated array, the i-th value of the observed and simulated array are removed before the computation.

  • replace_inf – If given, indicates which value to replace Inf values with in the two arrays. If None, when an inf value is found at the i-th position in the observed OR simulated array, the i-th value of the observed and simulated array are removed before the computation.

  • remove_neg – If True, when a negative value is found at the i-th position in the observed OR simulated array, the i-th value of the observed AND simulated array are removed before the computation.

  • remove_zero – If true, when a zero value is found at the i-th position in the observed OR simulated array, the i-th value of the observed AND simulated array are removed before the computation.

Return type:

The Spearman rank correlation coefficient.

Examples

>>> import HydroErr as he
>>> import numpy as np
>>> sim = np.array([5, 7, 9, 2, 4.5, 6.7])
>>> obs = np.array([4.7, 6, 10, 2.5, 4, 7])
>>> he.spearman_r(sim, obs)
0.942857142857143

References

  • Spearman C (1904). “The proof and measurement of association between two things”. American Journal of Psychology. 15: 72-101. doi:10.2307/1412159