C
C     ..................................................................
C
C        SUBROUTINE UTEST
C
C        PURPOSE
C           TEST WHETHER TWO INDEPENDENT GROUPS ARE FROM THE SAME
C           POPULATION BY MEANS OF MANN-WHITNEY U-TEST
C
C        USAGE
C           CALL UTEST(A,R,N1,N2,U,Z,IER)
C
C        DESCRIPTION OF PARAMETERS
C           A  - INPUT VECTOR OF CASES CONSISTING OF TWO INDEPENDENT
C                GROUPS . SMALLER GROUP PRECEDES LARGER GROUP. LENGTH
C                IS N1+N2.
C           R  - OUTPUT VECTOR OF RANKS. SMALLEST VALUE IS RANKED 1,
C                LARGEST IS RANKED N. TIES ARE ASSIGNED AVERAGE OF TIED
C                RANKS. LENGTH IS N1+N2.
C           N1 - NUMBER OF CASES IN SMALLER GROUP
C           N2 - NUMBER OF CASES IN LARGER GROUP
C           U  - STATISTIC USED TO TEST HOMOGENEITY OF THE TWO
C                GROUPS (OUTPUT)
C           Z  - MEASURE OF SIGNIFICANCE OF U IN TERMS OF NORMAL
C                DISTRIBUTION (OUTPUT)
C           IER- 0, IF NO ERROR.
C              - 1, IF ALL VALUES OF ONE GROUP ARE TIED.
C
C        REMARKS
C           Z IS SET TO ZERO IF N2 IS LESS THAN 20
C
C        SUBROUTINES AND FUNCTION SUBPROGRAMS REQUIRED
C           RANK
C           TIE
C
C        METHOD
C           DESCRIBED IN S. SIEGEL, 'NONPARAMETRIC STATISTICS FOR THE
C           BEHAVIORAL SCIENCES', MCGRAW-HILL, NEW YORK, 1956,
C           CHAPTER 6
C
C     ..................................................................
C
      SUBROUTINE UTEST(A,R,N1,N2,U,Z)
      DIMENSION A(1),R(1)
C
C        RANK SCORES FROM BOTH GROUP TOGETHER IN ASCENDING ORDER, AND
C        ASSIGN TIED OBSERVATIONS AVERAGE OF TIED RANKS
C
      N=N1+N2
      CALL RANK(A,R,N)
      Z=0.0
C
C        SUM RANKS IN LARGER GROUP
C
      R2=0.0
      NP=N1+1
      DO 10 I=NP,N
   10 R2=R2+R(I)
C
C        CALCULATE U
C
      FNX=N1*N2
      FN=N
      FN2=N2
      UP=FNX+FN2*((FN2+1.0)/2.0)-R2
      U=FNX-UP
      IF(UP-U) 20,30,30
   20 U=UP
C
C        TEST FOR N2 LESS THAN 20
C
   30 IF(N2-20) 80,40,40
C
C        COMPUTE STANDARD DEVIATION
C
   40 KT=1
      CALL TIE(R,N,KT,TS)
      IF(TS) 50,60,50
   50 IF (TS-(FN*FN*FN-FN)/12)52,51,52
   51 IER=1
      GO TO 80
   52 S=SQRT((FNX/(FN*(FN-1.0)))*(((FN*FN*FN-FN)/12.0)-TS))
      GO TO 70
   60 S=SQRT(FNX*(FN+1.0)/12.0)
C
C        COMPUTE Z
C
   70 Z=(U-FNX*0.5)/S
   80 RETURN
      END
