def lspearmanr ( x , y ) : TINY = 1e-30 if len ( x ) != len ( y ) : raise ValueError ( 'Input values not paired in spearmanr.  Aborting.' ) n = len ( x ) rankx = rankdata ( x ) ranky = rankdata ( y ) dsq = sumdiffsquared ( rankx , ranky ) rs = 1 - 6 * dsq / float ( n * ( n ** 2 - 1 ) ) t = rs * math . sqrt ( ( n - 2 ) / ( ( rs + 1.0 ) * ( 1.0 - rs ) ) ) df = n - 2 probrs = betai ( 0.5 * df , 0.5 , df / ( df + t * t ) ) # t already a float # probability values for rs are from part 2 of the spearman function in # Numerical Recipies, p.510.  They are close to tables, but not exact. (?) return rs , probrs
spearman rank correlation python
Calculates a Spearman rank - order correlation coefficient . Taken from Heiman s Basic Statistics for the Behav . Sci ( 1st ) p . 192 .
def lspearmanr(x,y):
    """
Calculates a Spearman rank-order correlation coefficient.  Taken
from Heiman's Basic Statistics for the Behav. Sci (1st), p.192.

Usage:   lspearmanr(x,y)      where x and y are equal-length lists
Returns: Spearman's r, two-tailed p-value
"""
    TINY = 1e-30
    if len(x) != len(y):
        raise ValueError('Input values not paired in spearmanr.  Aborting.')
    n = len(x)
    rankx = rankdata(x)
    ranky = rankdata(y)
    dsq = sumdiffsquared(rankx,ranky)
    rs = 1 - 6*dsq / float(n*(n**2-1))
    t = rs * math.sqrt((n-2) / ((rs+1.0)*(1.0-rs)))
    df = n-2
    probrs = betai(0.5*df,0.5,df/(df+t*t))  # t already a float
# probability values for rs are from part 2 of the spearman function in
# Numerical Recipies, p.510.  They are close to tables, but not exact. (?)
    return rs, probrs