ranksumcdf.m

function p = ranksumcdf(k, n, m)
% computes the cdf needed for the Wilcoxon rank-sum test
% given two samples of size n and m with equal distribution
% and R1 := rank-sum of the first sample in the union of both samples,
%   ranksumcdf(k,n,m) = P(R1 <= k)
if n == 1 && m == 0
  if k <= 0 
    p = 0;
  else
    p = 1;
  end      
elseif n == 0 && m == 1
  if k < 0 
    p = 0;
  else
    p = 1;
  end      
elseif n == 0
  p = m/(n+m)*ranksumcdf(k, n, m-1);
elseif m == 0
  p = n/(n+m)*ranksumcdf(k-n-m, n-1, m);
else
  p = n/(n+m)*ranksumcdf(k-n-m, n-1, m) + m/(n+m)*ranksumcdf(k, n, m-1);
end