spearman
The spearman operator computes a rank-based measure of association called Spearman's rho. Available only in the Enterprise Edition.
Synopsis
spearman(A,B);
Library
The spearman operator resides in the Linear Algebra library. Run the following query to load this library:
AFL% load_library('linear_algebra');
Summary
Spearman's rho, , or the Spearman rank correlation coefficient, is a non-parametric measure of dependence between two variables. Spearman's is defined as the Pearson correlation coefficient of the ranks of the two variables. For a sample population of size and two arrays and let :
where ties in rank are defined as the mean of the tied ranks had they been assigned sequentially, and
Then Spearman's is defined as
which reduces to
Example
Calculate Spearman's rho for the wikipedia example of 10 people's IQ's and the corresponding number of hours watching TV:
Create a vector of 10 IQ's:
AFL% store(build(<iq:double>[i=1:10; j=0:0],'[[106], [86],[100],[101],[99],[103],[97],[113],[112],[110]]',true),IQ);
The output is:{i,j} iq {1,0} 106 {2,0} 86 {3,0} 100 {4,0} 101 {5,0} 99 {6,0} 103 {7,0} 97 {8,0} 113 {9,0} 112 {10,0} 110
Create the coresponding vector of hours-per-week:
AFL% store(build(<hours:double>[i=1:10; j=0:0],'[[7],[0],[27],[50],[28],[29],[20],[12],[6],[17]]',true),HOURS);
The output is:{i,j} hours {1,0} 7 {2,0} 0 {3,0} 27 {4,0} 50 {5,0} 28 {6,0} 29 {7,0} 20 {8,0} 12 {9,0} 6 {10,0} 17
Calculate Spearman's rho between 'iq' and 'hours':
AFL% spearman(IQ, HOURS);
The output is:{j1,j2} spearman {0,0} -0.175758
This result suggests that the relationship between IQ and hours-of-TV/week is weak and should it exist, would be negatively correlated.