SUBROUTINE RUNS(X,N)
C
C     PURPOSE--THIS SUBROUTINE PERFORMS A RUNS ANALYSIS
C              OF THE DATA IN THE INPUT VECTOR X. 
C              THE ANALYSIS CONSISTS OF FIRST DETERMINING
C              THE OBSERVED NUMBER OF RUNS FROM THE DATA,
C              AND THEN COMPUTING
C              THE EXPECTED NUMBER OF RUNS,
C              THE STANDARD DEVIATION OF THE NUMBER OF RUNS,
C              AND THE RESULTING STANDARDIZED STATISTIC
C              FOR THE NUMBER OF RUNS FOR RUNS OF VARIOUS
C              LENGTHS.
C              THIS IS DONE FOR RUNS UP, RUNS DOWN, AND
C              RUNS UP AND DOWN.
C              THIS RUNS ANSLYSIS IS A USEFUL DISTRIBUTION-FREE
C              TEST OF THE RANDOMNESS OF A DATA SET.
C     INPUT ARGUMENTS--X      = THE SINGLE PRECISION VECTOR OF
C                               (UNSORTED OR SORTED) OBSERVATIONS.
C                      N      = THE INTEGER NUMBER OF OBSERVATIONS
C                               IN THE VECTOR X.
C     OUTPUT--4 PAGES OF AUTOMATIC PRINTOUT
C             CONSISTING OF THE OBSERVED NUMBER,
C             EXPECTED NUMBER, STANDARD DEVIATION 
C             AND RESULTING STANDARDIZED STATISTIC
C             FOR RUNS OF VARIOUS LENGTHS.
C             AND THE CUMULATIVE FREQUENCY.
C     PRINTING--YES.
C     RESTRICTIONS--THE MAXIMUM ALLOWABLE VALUE OF N
C                   FOR THIS SUBROUTINE IS 15000. 
C     OTHER DATAPAC   SUBROUTINES NEEDED--NONE.
C     FORTRAN LIBRARY SUBROUTINES NEEDED--SQRT.
C     MODE OF INTERNAL OPERATIONS--SINGLE PRECISION.
C     LANGUAGE--ANSI FORTRAN. 
C     REFERENCES--LEVENE AND WOLFOWITZ, ANNALS OF MATHEMATICAL
C                 STATISTICS, 1944, PAGES 58-69;
C                 ESPECIALLY PAGES 60, 63, AND 64.
C     REFERENCES--BRADLEY, DISTRIBUTION-FREE STATISTICAL TESTS,
C                 1968, CHAPTER 12, PAGES 271-282.
C     WRITTEN BY--JAMES J. FILLIBEN
C                 STATISTICAL ENGINEERING LABORATORY (205.03)
C                 NATIONAL BUREAU OF STANDARDS
C                 WASHINGTON, D. C. 20234
C                 PHONE:  301-921-2315
C     ORIGINAL VERSION--JUNE      1972. 
C     UPDATED         --NOVEMBER  1975. 
C     UPDATED         --FEBRUARY  1976. 
C
C---------------------------------------------------------------------
C
      DIMENSION X(1)
      DIMENSION Y(15000)
      DIMENSION  NRUL(16), NRDL(16), NRTL(16), NRULG(16), NRDLG(16)
      DIMENSION  NRTLG(16)
      DIMENSION ENRUL(16),ENRTL(16),ENRULG(16),ENRTLG(16)
      DIMENSION SNRUL(16),SNRTL(16),SNRULG(16),SNRTLG(16)
      DIMENSION ZNRUL(16),ZNRDL(16),ZNRTL(16),ZNRULG(16),ZNRDLG(16)
      DIMENSION ZNRTLG(16)
      DIMENSION C1(15),C2(15),C3(15),C4(15)
      DIMENSION ANRUL(16),ANRDL(16),ANRTL(16)
      DIMENSION ANRULG(16),ANRDLG(16),ANRTLG(16)
      COMMON /BLOCK2/ WS(15000)
      EQUIVALENCE (Y(1),WS(1))
C
      DATA C1(1),C1(2),C1(3),C1(4),C1(5),C1(6),C1(7),C1(8),C1(9),C1(10),
     1C1(11),C1(12),C1(13),C1(14),C1(15)
     1/ .4236111111E+00,  .1126675485E+00,  .4191688713E-01,
     1  .1076912487E-01,  .2003959238E-02,  .3023235799E-03,
     1  .3911555473E-04,  .4459038843E-05,  .4551105210E-06,
     1  .4207466837E-07,  .3555930927E-08,  .2768273257E-09,
     1  .1997821524E-10,  .1343876568E-11,  .8465610177E-13/
      DATA C2(1),C2(2),C2(3),C2(4),C2(5),C2(6),C2(7),C2(8),C2(9),C2(10),
     1C2(11),C2(12),C2(13),C2(14),C2(15)
     1/-.4819444444E+00, -.1628284832E+00, -.9690696649E-01,
     1 -.3778106786E-01, -.9289228716E-02, -.1724429252E-02,
     1 -.2638557888E-03, -.3466965096E-04, -.4004129153E-05,
     1 -.4130382587E-06, -.3851876069E-07, -.3279103786E-08,
     1 -.2568491117E-09, -.1863433868E-10, -.1259220466E-11/
      DATA C3(1),C3(2),C3(3),C3(4),C3(5),C3(6),C3(7),C3(8),C3(9),C3(10),
     1C3(11),C3(12),C3(13),C3(14),C3(15)
     1/ .1777777778E+00,  .7916666667E-01,  .4738977072E-01,
     1  .1274801587E-01,  .2338606059E-02,  .3461358734E-03,
     1  .4407121770E-04,  .4960020603E-05,  .5010387575E-06,
     1  .4592883352E-07,  .3854170274E-08,  .2982393839E-09,
     1  .2141205844E-10,  .1433843200E-11,  .8996663214E-13/
      DATA C4(1),C4(2),C4(3),C4(4),C4(5),C4(6),C4(7),C4(8),C4(9),C4(10),
     1C4(11),C4(12),C4(13),C4(14),C4(15)
     1/-.3222222222E+00, -.5972222222E-01, -.1130268959E+00,
     1 -.4696428571E-01, -.1123273065E-01, -.2025170849E-02,
     1 -.3029410411E-03, -.3912824548E-04, -.4459234519E-05,
     1 -.4551128785E-06, -.4207469124E-07, -.3555931110E-08,
     1 -.2768273269E-09, -.1997821525E-10, -.1343876568E-11/
C
      IPR=6
      IUPPER=15000
C
C     CHECK THE INPUT ARGUMENTS FOR ERRORS
C
      IF(N.LT.1.OR.N.GT.IUPPER)GOTO50
      IF(N.EQ.1)GOTO55
      HOLD=X(1)
      DO60I=2,N
      IF(X(I).NE.HOLD)GOTO90
   60 CONTINUE
      WRITE(IPR, 9)HOLD
      GOTO90
   50 WRITE(IPR,17)IUPPER
      WRITE(IPR,47)N
      RETURN
   55 WRITE(IPR,18) 
      RETURN
   90 CONTINUE
    9 FORMAT(1H ,109H***** NON-FATAL DIAGNOSTIC--THE FIRST  INPUT ARGUME
     1NT (A VECTOR) TO THE RUNS   SUBROUTINE HAS ALL ELEMENTS = ,E15.8,6
     1H *****)
   17 FORMAT(1H , 98H***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO THE
     1 RUNS   SUBROUTINE IS OUTSIDE THE ALLOWABLE (1,,I6,16H) INTERVAL *
     1****)
   18 FORMAT(1H ,100H***** FATAL ERROR--         THE SECOND INPUT ARGUME
     1NT TO THE RUNS   SUBROUTINE HAS THE VALUE 1 *****)
   47 FORMAT(1H , 35H***** THE VALUE OF THE ARGUMENT IS ,I8   ,6H *****)
C
C-----START POINT-----------------------------------------------------
C
      AN=N
C
C     FORM THE SEQUENTIAL DIFFERENCE TABLE
C
      NM1=N-1
      DO100I=1,NM1
      IP1=I+1
      Y(I)=X(IP1)-X(I)
  100 CONTINUE
C
C     ZERO-OUT THE 6 'NUMBER OF RUNS' VECTORS
C
      DO200I=1,16
      NRUL(I)=0
      NRDL(I)=0
      NRTL(I)=0
      NRULG(I)=0
      NRDLG(I)=0
      NRTLG(I)=0
  200 CONTINUE
C
C     DETERMINE THE NUMBER OF RUNS UP OF LENGTH EXACTLY I
C     AND THE NUMBER OF RUNS DOWN OF LENGTH EXACTLY I
C     DETERMINE THE LENGTH OF THE LONGEST RUN UP
C     AND THE LENGTH OF THE LONGEST RUN DOWN
C
      LENUP=0
      LENDN=0
      MAXLNU=0
      MAXLND=0
      DO300I=1,NM1
      IF(Y(I).EQ.0.0.AND.LENUP.GE.1)LENUP=LENUP+1 
      IF(Y(I).EQ.0.0.AND.LENDN.GE.1)LENDN=LENDN+1 
      IF(Y(I).EQ.0.0.AND.LENUP.EQ.0.AND.LENDN.EQ.0)LENUP=LENUP+1
      IF(Y(I).GT.0.0.AND.LENDN.GE.1.AND.LENDN.LE.15)NRDL(LENDN)=NRDL(LEN
     1DN)+1
      IF(Y(I).GT.0.0.AND.LENDN.GE.1.AND.LENDN.GE.16)NRDL(16)=NRDL(16)+1
      IF(Y(I).GT.0.0)LENDN=0
      IF(Y(I).GT.0.0)LENUP=LENUP+1
      IF(Y(I).LT.0.0.AND.LENUP.GE.1.AND.LENUP.LE.15)NRUL(LENUP)=NRUL(LEN
     1UP)+1
      IF(Y(I).LT.0.0.AND.LENUP.GE.1.AND.LENUP.GE.16)NRUL(16)=NRUL(16)+1
      IF(Y(I).LT.0.0)LENUP=0
      IF(Y(I).LT.0.0)LENDN=LENDN+1
      IF(I.EQ.NM1.   AND.LENDN.GE.1.AND.LENDN.LE.15)NRDL(LENDN)=NRDL(LEN
     1DN)+1
      IF(I.EQ.NM1.   AND.LENDN.GE.1.AND.LENDN.GE.16)NRDL(16)=NRDL(16)+1
      IF(I.EQ.NM1.   AND.LENUP.GE.1.AND.LENUP.LE.15)NRUL(LENUP)=NRUL(LEN
     1UP)+1
      IF(I.EQ.NM1.   AND.LENUP.GE.1.AND.LENUP.GE.16)NRUL(16)=NRUL(16)+1
      IF(LENUP.GT.MAXLNU)MAXLNU=LENUP
      IF(LENDN.GT.MAXLND)MAXLND=LENDN
  300 CONTINUE
C
C     DETERMINE THE NUMBER OF RUNS TOTAL OF LENGTH EXACTLY I
C     AND THE LENGTH OF THE LONGEST RUN UP OR DOWN
C
      DO400I=1,16
      NRTL(I)=NRUL(I)+NRDL(I) 
  400 CONTINUE
      MAXLNT=MAXLNU 
      IF(MAXLND.GT.MAXLNU)MAXLNT=MAXLND 
C
C     DETERMINE THE NUMBER OF RUNS UP OF LENGTH I OR MORE
C     AND THE NUMBER OF RUNS DOWN OF LENGTH I OR MORE
C     AND THE NUMBER OF RUNS TOTAL OF LENGTH I OR MORE
C
      NRULG(16)=NRUL(16)
      NRDLG(16)=NRDL(16)
      NRTLG(16)=NRTL(16)
      DO500I=1,15
      J=16-I
      JP1=J+1
      NRULG(J)=NRULG(JP1)+NRUL(J)
      NRDLG(J)=NRDLG(JP1)+NRDL(J)
      NRTLG(J)=NRTLG(JP1)+NRTL(J)
  500 CONTINUE
C
C     DETERMINE THE NUMBER OF POSITIVE, ZERO, AND NEGATIVE ENTRIES
C     IN THE DIFFERENCE TABLE.  IF RANDOM, THE NUMBER OF POSITIVE SHOULD BE
C     APPROXIMATELY EQUAL TO THE NUMBER OF NEGATIVE
C
      NNEG=0
      NZER=0
      NPOS=0
      DO800I=1,NM1
      IF(Y(I).LT.0.0)NNEG=NNEG+1
      IF(Y(I).EQ.0.0)NZER=NZER+1
      IF(Y(I).GT.0.0)NPOS=NPOS+1
  800 CONTINUE
C
C     COMPUTE THE EXPECTED NUMBER OF RUNS UP OF LENGTH EXACTLY I =
C     THE EXPECTED NUMBER OF RUNS DOWN OF LENGTH EXACTLY I =
C     ONE HALF THE EXPECTED NUMBER OF RUNS TOTAL OF LENGTH EXACTLY I
C
      DEN=6.0
      DO2000I=1,15
      AI=I
      ENRUL(I)=AN*(AI*AI+3.0*AI+1.0)-(AI*AI*AI+3.0*AI*AI-AI-4.0)
      DEN=DEN*(AI+3.0)
      ENRUL(I)=ENRUL(I)/DEN
      ENRTL(I)=2.0*ENRUL(I)
 2000 CONTINUE
C
C     COMPUTE THE EXPECTED NUMBER OF RUNS UP OF LENGTH I OR MORE =
C     THE EXPECTED NUMBER OF RUNS DOWN OF LENGTH I OR MORE =
C     ONE HALF THE EXPECTED NUMBER OF RUNS TOTAL OF LENGTH I OR MORE
C
      DEN=2.0
      DO2100I=1,15
      AI=I
      ENRULG(I)=AN*(AI+1.0)-(AI*AI+AI-1.0)
      DEN=DEN*(AI+2.0)
      ENRULG(I)=ENRULG(I)/DEN 
      ENRTLG(I)=2.0*ENRULG(I) 
 2100 CONTINUE
C
C     COMPUTE THE STANDARD DEV. OF THE NUMBER OF RUNS UP OF LENGTH EXACTLY I =
C     THE STANDARD DEV. OF THE NUMBER OF RUNS DOWN OF LENGTH EXACTLY I =
C     SQRT(0.5)* THE STAND. DEV. OF THE NUMBER OF RUNS TOTAL OF LENGTH EXACTLY I
C
      DO2500I=1,15
      SNRTL(I)=SQRT(C1(I)*AN+C2(I))
      SNRUL(I)=SQRT(0.5)*SNRTL(I)
 2500 CONTINUE
C
C     COMPUTE THE STAND. DEV. OF THE NUMBER OF RUNS UP OF LENGTH I OR MORE =
C     THE STAND. DEV. OF THE NUMBER OF RUNS DOWN OF LENGTH I OR MORE =
C     SQRT(0.5)* THE STAND. DEV. OF THE NUMBER OF RUNS TOTAL OF LENGTH I OR MORE
C
      DO2600I=1,15
      SNRTLG(I)=SQRT(C3(I)*AN+C4(I))
      SNRULG(I)=SQRT(0.5)*SNRTLG(I)
 2600 CONTINUE
C
C     FORM Z STATISTICS
C
      DO3100I=1,15
      STAT=NRUL(I)
      ZNRUL(I)=(STAT   -ENRUL(I))/SNRUL(I)
      STAT=NRDL(I)
      ZNRDL(I)=(STAT   -ENRUL(I))/SNRUL(I)
      STAT=NRTL(I)
      ZNRTL(I)=(STAT   -ENRTL(I))/SNRTL(I)
      STAT=NRULG(I) 
      ZNRULG(I)=(STAT    -ENRULG(I))/SNRULG(I)
      STAT=NRDLG(I) 
      ZNRDLG(I)=(STAT    -ENRULG(I))/SNRULG(I)
      STAT=NRTLG(I) 
      ZNRTLG(I)=(STAT    -ENRTLG(I))/SNRTLG(I)
 3100 CONTINUE
C
      DO3200I=1,15
      ANRUL(I)=NRUL(I)
      ANRDL(I)=NRDL(I)
      ANRTL(I)=NRTL(I)
      ANRULG(I)=NRULG(I)
      ANRDLG(I)=NRDLG(I)
      ANRTLG(I)=NRTLG(I)
 3200 CONTINUE
C
C     WRITE EVERYTHING OUT
C
      IMAX=15
      WRITE(IPR,998)
      WRITE(IPR,4002)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4004)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4006)
      WRITE(IPR,999)
      DO4050I=1,IMAX
      WRITE(IPR,4060)I,ANRUL(I),ENRUL(I),SNRUL(I),ZNRUL(I)
 4050 CONTINUE
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4064)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4006)
      WRITE(IPR,999)
      DO4070I=1,IMAX
      WRITE(IPR,4060)I,ANRULG(I),ENRULG(I),SNRULG(I),ZNRULG(I)
 4070 CONTINUE
      WRITE(IPR,998)
      WRITE(IPR,4102)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4104)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4006)
      WRITE(IPR,999)
      DO4150I=1,IMAX
      WRITE(IPR,4060)I,ANRDL(I),ENRUL(I),SNRUL(I),ZNRDL(I)
 4150 CONTINUE
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4164)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4006)
      WRITE(IPR,999)
      DO4170I=1,IMAX
      WRITE(IPR,4060)I,ANRDLG(I),ENRULG(I),SNRULG(I),ZNRDLG(I)
 4170 CONTINUE
      WRITE(IPR,998)
      WRITE(IPR,4202)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4204)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4006)
      WRITE(IPR,999)
      DO4250I=1,IMAX
      WRITE(IPR,4060)I,ANRTL(I),ENRTL(I),SNRTL(I),ZNRTL(I)
 4250 CONTINUE
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4264)
      WRITE(6,999)
      WRITE(6,999)
      WRITE(IPR,4006)
      WRITE(IPR,999)
      DO4270I=1,IMAX
      WRITE(IPR,4060)I,ANRTLG(I),ENRTLG(I),SNRTLG(I),ZNRTLG(I)
 4270 CONTINUE
      WRITE(IPR,998)
      WRITE(IPR,4601)MAXLNU
      WRITE(IPR,4602)MAXLND
      WRITE(IPR,4603)MAXLNT
      WRITE(IPR,999)
      WRITE(IPR,4611)NPOS
      WRITE(IPR,4612)NNEG
      WRITE(IPR,4613)NZER
 4002 FORMAT(1H ,48X,7HRUNS UP)
 4102 FORMAT(1H ,48X,9HRUNS DOWN)
 4202 FORMAT(1H ,40X,32HRUNS TOTAL = RUNS UP + RUNS DOWN)
 4004 FORMAT(1H ,27X,52HSTATISTIC = NUMBER OF RUNS UP    OF LENGTH EXACT
     1LY I)
 4104 FORMAT(1H ,27X,52HSTATISTIC = NUMBER OF RUNS DOWN  OF LENGTH EXACT
     1LY I)
 4204 FORMAT(1H ,27X,52HSTATISTIC = NUMBER OF RUNS TOTAL OF LENGTH EXACT
     1LY I)
 4064 FORMAT(1H ,27X,52HSTATISTIC = NUMBER OF RUNS UP    OF LENGTH I OR
     1MORE)
 4164 FORMAT(1H ,27X,52HSTATISTIC = NUMBER OF RUNS DOWN  OF LENGTH I OR
     1MORE)
 4264 FORMAT(1H ,27X,52HSTATISTIC = NUMBER OF RUNS TOTAL OF LENGTH I OR
     1MORE)
 4006 FORMAT(1H ,105HI = LENGTH OF RUN         VALUE OF STAT        EXP(
     1STAT)            SD(STAT)    (STAT-EXP(STAT))/SD(STAT))
 4060 FORMAT(1H ,4X,I4,13X,6X,F7.1,13X,F8.4,12X,F8.4,11X,F8.2)
 4601 FORMAT(1H ,39HLENGTH OF THE LONGEST RUN UP         = ,I5)
 4602 FORMAT(1H ,39HLENGTH OF THE LONGEST RUN DOWN       = ,I5)
 4603 FORMAT(1H ,39HLENGTH OF THE LONGEST RUN UP OR DOWN = ,I5)
 4611 FORMAT(1H ,33HNUMBER OF POSITIVE DIFFERENCES = ,I5)
 4612 FORMAT(1H ,33HNUMBER OF NEGATIVE DIFFERENCES = ,I5)
 4613 FORMAT(1H ,33HNUMBER OF ZERO     DIFFERENCES = ,I5)
 4010 FORMAT(1H ,2(I4,2X,F7.1,2X,F8.4,2X,F8.4,2X,F8.2,8X))
 4605 FORMAT(1H ,I6,2X,I6,2X,I6)
  998 FORMAT(1H1)
  999 FORMAT(1H )
      RETURN
      END