Spearman Math ... an appendix to Spearman Correlation

The Spearman Correlation of two sets of n numbers, say xj and yj (where j goes from 1 to n) is defined as the Pearson Correlation of the Ranks.

If the numbers in each set are not repeated, then the Ranks for each set are just a reordering of the numbers 1, 2, 3, ... n.
For example: for the numbers 8, 17, 2, 5, 12 the Ranks are 3, 1, 5, 4, 2 which are just the numbers 1, 2, 3, 4, 5 reordered.

Because the ranks are just the numbers 1, 2, 3, ... n, then the Mean of the Ranks isjust:

[1]       M = (1/n)(1+2+3+...+n) = (1/2) [ n(n+1)/2 ] = (n+1)/2   using the magic formula: 1+2+3+...+n = n(n+1)/2
provided the ranks are not repeated.

For our n = 5 example, the numbers are 8, 17, 2, 5, 12 then their Ranks are 3, 1, 5, 4, 2 and the Mean of the Ranks is (5+1)/2 = 3.

Furthermore, the Standard Deviation, S, of the Ranks can calculated like so (provided the ranks are not repeated):

We know that S2 = (average of the squares) - (square of the average) ... see SD stuff,
so we can write:

[2]       S2 = (1/n)Σ k2 - M2 = (1/n)[ n(n+1)(2n+1)/6 ] - M2 = (n+1)(2n+1)/6 - [(n+1)/2]2 = (n2 - 1)/2
using the magic formula: 12+22+...+n2 = n(n+1)(2n+1)/6

The Pearson Correlation for two sets of numbers (say uj and vj) is R, defined by

[3]       SD[u] SD[v] R = (1/n)Σ (uj-M[u]) (vj-M[v])
where M[u], SD[u] are the Mean and Standard Deviation of the set of u's (and similarly for the v's).

However, if we're talking about Ranks, then both the u-set and v-set are just reordering of the same numbers 1, 2, 3, ... n (provided the ranks are not repeated!!) and that means that M[u] = M[v] = M and that SD[u] = SD[v] = S ... given by [2], above.

Then we may calculate R, the Pearson Correlation of Ranks (that's the Spearman Correlation!) from:

[4]       S2R = (1/n)Σ (uj-M) (vj-M) = (1/n)Σ ujvj - (M/n)Σ uj - (M/n)Σ vj + (1/n)Σ M2 = (1/n)Σ ujvj - M2
... since (1/n)Σuj = (1/n)Σvj = M.

Given the sequence of Ranks, we could use [4] to calculate the Spearman Correlation (provided the ranks are not repeated!!).
However, there's a somewhat simpler formula (where we find another expression for the annoying sum (1/n)Σ ujvj ):

Consider the average of the squares of the differences in the Ranks:

[5]       (1/n)Σ (uj - vj)2 = (1/n)Σ uj2 + (1/n)Σ vj2 - (2/n)Σ ujvj = (2/n)Σ k2 - (2/n)Σ ujvj

But (1/n)Σ k2 = S2 + M2 from [2], above and we have expressions for both S and S in terms of the number n ... so we can (eventually) write:
Pearson Correlation of Ranks = the Spearman Rank Correlation = R = 1 - 6 Σ dj2 / [n (n2 - 1)]

where d1, d2, ... dn are the differences between the Ranks

... provided the ranks are not repeated!!

P.S. If the ranks are repeated (meaning the original variables are not all different), then just use the Pearson Correlation of Ranks
... instead of the above neat formula