def spearmanr(x, y, use_ties=True):
(x, y, n) = _chk_size(x, y)<EOL>(x, y) = (x.ravel(), y.ravel())<EOL>m = ma.mask_or(ma.getmask(x), ma.getmask(y))<EOL>n -= m.sum()<EOL>if m is not nomask:<EOL><INDENT>x = ma.array(x, mask=m, copy=True)<EOL>y = ma.array(y, mask=m, copy=True)<EOL><DEDENT>df = n-<NUM_LIT:2><EOL>if df < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>rankx = rankdata(x)<EOL>ranky = rankdata(y)<EOL>dsq = np.add.reduce((rankx-ranky)**<NUM_LIT:2>)<EOL>if use_ties:<EOL><INDENT>xties = count_tied_groups(x)<EOL>yties = count_tied_groups(y)<EOL>corr_x = np.sum(v*k*(k**<NUM_LIT:2>-<NUM_LIT:1>) for (k,v) in iteritems(xties))/<NUM_LIT><EOL>corr_y = np.sum(v*k*(k**<NUM_LIT:2>-<NUM_LIT:1>) for (k,v) in iteritems(yties))/<NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>corr_x = corr_y = <NUM_LIT:0><EOL><DEDENT>denom = n*(n**<NUM_LIT:2> - <NUM_LIT:1>)/<NUM_LIT><EOL>if corr_x != <NUM_LIT:0> or corr_y != <NUM_LIT:0>:<EOL><INDENT>rho = denom - dsq - corr_x - corr_y<EOL>rho /= ma.sqrt((denom-<NUM_LIT:2>*corr_x)*(denom-<NUM_LIT:2>*corr_y))<EOL><DEDENT>else:<EOL><INDENT>rho = <NUM_LIT:1.> - dsq/denom<EOL><DEDENT>t = ma.sqrt(ma.divide(df,(rho+<NUM_LIT:1.0>)*(<NUM_LIT:1.0>-rho))) * rho<EOL>if t is masked:<EOL><INDENT>prob = <NUM_LIT:0.><EOL><DEDENT>else:<EOL><INDENT>prob = betai(<NUM_LIT:0.5>*df,<NUM_LIT:0.5>,df/(df+t*t))<EOL><DEDENT>return rho, prob<EOL>
Calculates a Spearman rank-order correlation coefficient and the p-value
to test for non-correlation.

The Spearman correlation is a nonparametric measure of the linear
relationship between two datasets. Unlike the Pearson correlation, the
Spearman correlation does not assume that both datasets are normally
distributed. Like other correlation coefficients, this one varies
between -1 and +1 with 0 implying no correlation. Correlations of -1 or
+1 imply an exact linear relationship. Positive correlations imply that
as `x` increases, so does `y`. Negative correlations imply that as `x`
increases, `y` decreases.

Missing values are discarded pair-wise: if a value is missing in `x`, the
corresponding value in `y` is masked.

The p-value roughly indicates the probability of an uncorrelated system
producing datasets that have a Spearman correlation at least as extreme
as the one computed from these datasets. The p-values are not entirely
reliable but are probably reasonable for datasets larger than 500 or so.

Parameters
----------
x : array_like
    The length of `x` must be > 2.
y : array_like
    The length of `y` must be > 2.
use_ties : bool, optional
    Whether the correction for ties should be computed.

Returns
-------
spearmanr : float
    Spearman correlation coefficient, 2-tailed p-value.

References
----------
[CRCProbStat2000] section 14.7