*
* @MannWhitney  x  y
*
* performs a Mann-Whitney(-Wilcoxon) non-parametric test for whether the
* observations from the vectors X and Y are drawn from the same distribution.
*
* Parameters:
*   x   y    VECTORS (not series). They don't have to be the same size
*
* Variables Defined:
*  %CDSTAT   Asymptotically N(0,1) test statistic (null=same distribution)
*  %SIGNIF   Two-tailed significance level. Positive value of %CDSTAT means
*            that elements of <<X>> tend to be bigger than elements of <<Y>>
*
* References:
*  Wilcoxon(1945), "Individual comparisons by ranking methods", Biometrics
*    Bulletin, vol 1, pp 80–83.
*  Mann & Whitney(1947), "On a test of whether one of two random variables is
*   stochastically larger than the other".  Annals of Mathematical Statistics,
*   vol18, pp 50–60.
*
* Example:
*   dec vect ss as
*   input(varying) ss
*    343 235 191 266 200 250 403 432
*   input(varying) as
*    700 317 399 643 631 586 571 549 748 558 557 666
*   @MannWhitney ss as
*
* Revision Schedule:
*  03/2009 Written by Tom Doan, Estima.
*  11/2010 Revised to use local report and report title
*
procedure MannWhitney x y
type vector x y
*
option switch print   1
*
local vector  w
local integer nx ny n1
local real    rx ry t1 z wilcoxon mw
local string  ltitle
local report  mreport
*
compute nx=%rows(x),ny=%rows(y)
*
compute w =%ranks(x~~y)
compute rx=%sum(%xsubvec(w,1,nx))
compute ry=%sum(%xsubvec(w,nx+1,nx+ny))
if nx<ny
   compute t1=rx,n1=nx
else
   compute t1=ry,n1=ny
compute wilcoxon=t1,mw=nx*ny+.5*n1*(n1+1)
compute z=(t1-.5*n1*(nx+ny+1))/sqrt(nx*ny*(nx+ny+1.0)/12.0)
compute %cdstat=z
compute %signif=%ztest(%cdstat)
*
if print==0
   return
compute ltitle="Mann-Whitney-Wilcoxon Test"
report(use=mreport,action=define)
report(use=mreport,atrow=1,atcol=1,span) ltitle
report(use=mreport,atrow=2,atcol=1) "Label" "N" "Avg Rank" "Sum Ranks"
report(use=mreport,atrow=3,atcol=1) %label(x) nx rx/nx rx
report(use=mreport,atrow=4,atcol=1) %label(y) ny ry/ny ry
report(use=mreport,atrow=6,atcol=1) "Mann-Whitney U" "" mw
report(use=mreport,atrow=7,atcol=1) "Wilcoxon W" "" wilcoxon
report(use=mreport,atrow=8,atcol=1) "Z-Score" "" z
report(use=mreport,atrow=9,atcol=1) "Signif Level" "" %signif
report(use=mreport,action=format,atcol=3,width=10)
report(use=mreport,action=show)
end

