# How is the signed rank statistic defined in Matlab?

1. Given a sequence of sample points $X_1, \dots, X_n$, I am trying to find a Matlab function to

• compute the signed rank statistic defined as $$\sum_{i=1}^n sign(X_i) r_i$$ where $r_i$ is the rank of $|X_i|$ in $|X_1|, \dots, |X_n|$, and $sign(X_i)$ returns $1$ if $X_i >0$ and $-1$ if $X_i <0$.

• and also to compute the p-value of this signed rank statistic.

2. The function I suspect to be relevant is signrank in Matlab. Matlab signrank function takes the minimum between the sum of the ranks of positive differences and the sum of the ranks of negative differences. In the implementation of signrank function

% Compute signed rank statistic (most extreme version)
w = sum(tierank(neg));
w = min(w, n*(n+1)/2-w);


Questions:

1. I was wondering why Matlab signrank function takes the minimum between the sum of the ranks of positive differences and the sum of the ranks of negative differences? I checked the following references listed in the source file of signrank.m, but haven't found them taking the minimum as the signed rank statistic.

%   References:
%       Hollander, M. and D. A. Wolfe.  Nonparametric Statistical
%          Methods. Wiley, 1973.
%       Gibbons, J.D.  Nonparametric Statistical Inference,
%          2nd ed.  M. Dekker, 1985.

2. Is the p value provided by Matlab signrank function, same as the p value I want?

Thanks and regards!

You are right that the MATLAB signrank function returns something other than what you defined. It returns the sum of the ranks for the points with positive sign. Presumably this would have the same p-value as your statistic, since one is a function of the other (and the sample size).
You could use the tiedrank and sign functions to compute your statistic directly.
• Thanks! (1) "It returns the sum of the ranks for the points with positive sign", so if flipping the signs of the points, the signed rank statistic value may change. But I found that [p,h,stats] = signrank(x) and [p,h,stats] = signrank(-x) return the same signed rank statistic value. (2) "You could use the tiedrank and sign functions to compute your statistic directly." How shall I compute the p-value next? – Tim Apr 18 '13 at 17:14
[r p]=corr(X,Y,'type','spearman') will calculate a p-value by comparing ranks between X and Y.