@wikipedia


\rho_s(x,y) = 1 - \frac{6}{n(n^2-1)} \sum_i \big( rg(x_i)-rg(y_i) \big)^2

where  and  are the ranks of elements  and ,  i.e. index number in a low-to-high sorted array:  

\{ x \} = \{ x_1, x_2, x_3 ... x_n \} \rightarrow \{ rg(x) \} =

 \{ x_{i_1}, x_{i_2}, x_{i_3} ... x_{i_n} \}, \, {\rm where} \, x_{i_1} \leq x_{i_2} \leq x_{i_3} ...  \leq x_{i_n}


\{ y \} = \{ y_1, y_2, y_3 ... y_n \} \rightarrow \{ rg(y) \} =

 \{ y_{i_1}, y_{i_2}, x_{i_3} ... y_{i_n} \}, \, {\rm where} \, y_{i_1} \leq y_{i_2} \leq y_{i_3} ...  \leq y_{i_n}