function  [W, Z, p] = wsurt( data1, data2, alt )
% ------------------------------------------------------------------------------------
%   WILCOXON SUM RANK TEST
%   Input:  data1, data2 - first and second sample
%           alt - code for alternative hypothesis;
%               -1  mu1<m2; 0 mu1 ne m2; and 1 mu1>mu2
%   Output: W - sum of the ranks for the first sample. If
%               there is no ties, the standardization by ER = ... and
%               Var R =  ... ,       allows using standard normal quantiles as long as
%               sample sizes are larger than 15-20.
%           Z - standardized R but adjusted for the ties
%           p - p-value for testing equality of distributions (equality of locations)
%               against the alternative specified by input alt
%   Example of use:
%   >  dat1=[1 3 2 4 3 5 5 4 2 3 4 3 1 7 6 6 5 4 5 8 7 3 3 4];
%   >  dat2=[2 5 4 3 4 3 2 2 1 2 3 2 3 4 3 2 3 4 4 3 5];
%   >  [sumranks1, tstat, pval] = wsurt(dat1, dat2, 1)
%
%   Needs: M-FILE ranks.m (ranking procedure)
%-------------------------------------------------------------------------------------
data1 = data1(:)' ;                   % convert sample 1 to a row vector
  n1 = length( data1 );               % n1  is the size of the first sample, data1
data2 = data2(:)' ;                   % convert sample 2 to a row vector
  n2 = length( data2 );               % n2 is the size of the second sample, data2
  n=n1+ n2;                           % n is the total sample size
mergeboth = [ data1 data2 ];
rankall = ranks( mergeboth );       % ranks of merged observations
  W2 = sum( ( rankall.^2 ) );       % sum of all ranks squared needed to
% make adjustment for the ties; if no ties are present, this sum is equal
% to the sum of squares of the first n integers: n(n+1)(2 n+1)/6.
ranks_data1 = rankall( :, 1:n1);    % ranks of the first sample only
  W = sum( ranks_data1 );             % statistics for WMW, sum of the ranks for sample 1
%--------------------------------------------------------------------------------------
Z = (W - n1 * (n+1)/2 )/sqrt( n1*n2*W2/(n * (n-1)) - n1*n2*(n+1)^2/(4*(n-1)));
% T is approximately standard normal and approximation is quite good if n1,n2 > 15.
% Since R ranges over integers and half integers, a continuity correction, cc, may be
%  used for improving accuracy of p-values.
cc = 0.25/sqrt( n1*n2*W2/(n * (n-1)) - n1*n2*(n+1)^2/(4*(n-1)));
%-------------------------- alternatives ---------------------------------------
% alt == 0 for two sided;  alt == -1 for mu1 < mu2; alt == 1 for mu1 > mu2.
if      alt == 0
            p = 2*normcdf(- abs( Z ) - cc);
elseif  alt == -1
            p = normcdf( Z - cc );
elseif  alt == 1
            p = normcdf( - Z - cc );
else
        error('Wrong alternative code. Input "alt" should be either 0, -1, or 1.')
end
%-------------------------------------------------------------------------------------
% Copyright (C) 2002 by GaTech
% Ver 0.1   9/28/2002
