C
C     ..................................................................
C
C        SUBROUTINE KRANK
C
C        PURPOSE
C           TEST CORRELATION BETWEEN TWO VARIABLES BY MEANS OF KENDALL
C           RANK CORRELATION COEFFICIENT
C
C        USAGE
C           CALL KRANK(A,B,R,N,TAU,SD,Z,NR)
C
C        DESCRIPTION OF PARAMETERS
C           A   - INPUT VECTOR OF N OBSERVATIONS FOR FIRST VARIABLE
C           B   - INPUT VECTOR OF N OBSERVATIONS FOR SECOND VARIABLE
C           R   - OUTPUT VECTOR OF RANKED DATA OF LENGTH 2*N. SMALLEST
C                 OBSERVATION IS RANKED 1, LARGEST IS RANKED N. TIES
C                 ARE ASSIGNED AVERAGE OF TIED RANKS.
C           N   - NUMBER OF OBSERVATIONS
C           TAU - KENDALL RANK CORRELATION COEFFICIENT (OUTPUT)
C           SD  - STANDARD DEVIATION (OUTPUT)
C           Z   - TEST OF SIGNIFICANCE OF TAU IN TERMS OF NORMAL
C                 DISTRIBUTION (OUTPUT)
C           NR  - CODE, 0 FOR UNRANKED DATA IN A AND B, 1 FOR RANKED
C                 DATA IN A AND B (INPUT)
C
C        REMARKS
C           SD AND Z ARE SET TO ZERO IF N IS LESS THAN TEN
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 9
C
C     ..................................................................
C
      SUBROUTINE KRANK(A,B,R,N,TAU,SD,Z,NR)
      DIMENSION A(1),B(1),R(1)
C
      SD=0.0
      Z=0.0
      FN=N
      FN1=N*(N-1)
C
C        DETERMINE WHETHER DATA IS RANKED
C
      IF(NR-1) 5, 10, 5
C
C        RANK DATA IN A AND B VECTORS AND ASSIGN TIED OBSERVATIONS
C        AVERAGE OF TIED RANKS
C
    5 CALL RANK (A,R,N)
      CALL RANK (B,R(N+1),N)
      GO TO 40
C
C        MOVE RANKED DATA TO R VECTOR
C
   10 DO 20 I=1,N
   20 R(I)=A(I)
      DO 30 I=1,N
      J=I+N
   30 R(J)=B(I)
C
C        SORT RANK VECTOR R IN SEQUENCE OF VARIABLE A
C
   40 ISORT=0
      DO 50 I=2,N
      IF(R(I)-R(I-1)) 45,50,50
   45 ISORT=ISORT+1
      RSAVE=R(I)
      R(I)=R(I-1)
      R(I-1)=RSAVE
      I2=I+N
      SAVER=R(I2)
      R(I2)=R(I2-1)
      R(I2-1)=SAVER
   50 CONTINUE
      IF(ISORT) 40,55,40
C
C        COMPUTE S ON VARIABLE B. STARTING WITH THE FIRST RANK, ADD 1
C        TO S FOR EACH LARGER RANK TO ITS RIGHT AND SUBTRACT 1 FOR EACH
C        SMALLER RANK.  REPEAT FOR ALL RANKS.
C
   55 S=0.0
      NM=N-1
      DO 60 I=1,NM
      J=N+I
      DO 60 L=I,N
      K=N+L
      IF(R(K)-R(J)) 56,60,57
   56 S=S-1.0
      GO TO 60
   57 S=S+1.0
   60 CONTINUE
C
C        COMPUTE TIED SCORE INDEX FOR BOTH VARIABLES
C
      KT=2
      CALL TIE(R,N,KT,TA)
      CALL TIE(R(N+1),N,KT,TB)
C
C        COMPUTE TAU
C
      IF(TA) 70,65,70
   65 IF(TB) 70,67,70
   67 TAU=S/(0.5*FN1)
      GO TO 80
   70 TAU=S/((SQRT(0.5*FN1-TA))*(SQRT(0.5*FN1-TB)))
C
C     COMPUTE STANDARD DEVIATION AND Z IF N IS 10 OR LARGER
C
   80 IF(N-10) 90,85,85
   85 SD=(SQRT((2.0*(FN+FN+5.0))/(9.0*FN1)))
      Z=TAU/SD
   90 RETURN
      END
