RUNS
Performs a runs up test.
Required Arguments
X — Vector of length NRAN containing the data elements to be added to the test on this invocation. (Input)
COUNT — Vector of length NRUN containing the counts of the number of runs up of each length. (Output, if IDO = 0 or 1; Input/Output, if IDO = 2 or 3)
EXPECT — Vector of length NRUN containing the expected number of runs of each length. (Output, if IDO = 0 or 3; not referenced otherwise)
COVARNRUN by NRUN matrix containing the variances and covariances of the counts (Output, if IDO = 0 or 3; not referenced otherwise)
CHISQ — Chi‑squared statistic for testing the null hypothesis of a uniform distribution. (Output, if IDO = 0 or 3; not referenced otherwise)
DF — Degrees of freedom for chi‑squared. (Output, if IDO = 0 or 3; not referenced otherwise)
PROB — Probability of a larger chi‑squared. (Output, if IDO = 0 or 3; not referenced otherwise)
Optional Arguments
IDO — Processing option. (Input)
Default: IDO = 0.
IDO
Action
0
This is the only invocation of RUNS, and all the data are input at once.
1
This is the first invocation of RUNS, and additional calls will be made. Initialization and updating for the NRAN data elements are performed.
2
This is an intermediate invocation of RUNS, and updating for the NRAN data elements is performed.
3
This is the final invocation of RUNS for this data. Updating for the NRAN data elements is performed, followed by the wrap‑up computations.
NRAN — Number of data points currently input in X. (Input)
NRAN may be positive or zero on any invocation of RUNS.
Default: NRAN = size (X,1).
NRUN — Length of the longest run for which tabulation is desired. (Input)
Runs of length 1, 2, K, NRUN  1 are counted in COUNT(1)  COUNT(NRUN  1). COUNT(NRUN) contains the number of runs of length NRUN or greater. NRUN must be greater than or equal to one.
Default: NRUN = size (COUNT,1).
LDCOVA — Leading dimension of COVAR exactly as specified in the dimension statement in the calling program. (Input)
Default: LDCOVA = size (COVAR,1).
FORTRAN 90 Interface
Generic: CALL RUNS (X, COUNT, EXPECT, COVAR, CHISQ, DF, PROB [])
Specific: The specific interface names are S_RUNS and D_RUNS.
FORTRAN 77 Interface
Single: CALL RUNS (IDO, NRAN, X, NRUN, COUNT, EXPECT, COVAR, LDCOVA, CHISQ, DF, PROB)
Double: The double precision name is DRUNS.
Description
Routine RUNS computes statistics for the runs up test. Runs tests are used to test for cyclical trend in sequences of random numbers. Routine RUNS may be called once (IDO = 0) or several times (IDO = 1, 2, and 3). If all of the data will not fit into memory, the second mode of operation must be used. If the data fit into memory, then the first mode of operation is slightly more efficient. If the runs down test is desired, each observation should first be multiplied by 1 to change its sign, and RUNS called with the modified vector of observations.
Routine RUNS first tallies the number of runs up (increasing sequences) of each desired length. For i = 1, K, r  1, where r = NRUN, COUNT(i) contains the number of runs of length i. COUNT(NRUN) contains the number of runs of length NRUN or greater. As an example of how runs are counted, the sequence (1, 2, 3, 1) contains 1 run up of length 3, and one run up of length 1.
After tallying the number of runs up of each length, RUNS computes the expected values and the covariances of the counts according to methods given by Knuth (1981, pages 6567). Let R denote a vector of length NRUN containing the number of runs of each length so that the i‑th element of R, ri, contains the count of the runs of length i. Let ΣR denote the covariance matrix of R under the null hypothesis of randomness, and let μR denote the vector of expected values for R under this null hypothesis. Then, an approximate chi‑squared statistic with NRUN degrees of freedom is given as
In general, the larger the value of each element of μR, the better the chi‑squared approximation.
Comments
1. Workspace may be explicitly provided, if desired, by use of R2NS/DR2NS. The reference is:
CALL R2NS (IDO, NRAN, X, NRUN, COUNT, EXPECT, COVAR, LDCOVA, CHISQ, DF, PROB, RWK, CWK, LRUN, NOBS, XLAST)
The additional arguments are as follows:
RWK — Work vector of length NRUN.
CWK — Work vector of length NRUN * NRUN.
LRUN — Scalar used to keep track of number of last runs. (Output, if IDO = 0 or 1; input/output, otherwise)
LRUN should not be changed between calls with the same data set.
NOBS — Scalar used to keep track of total number of observations. (Output, if
IDO = 0 or 1; input/output, otherwise)
NOBS should not be changed between calls with the same data set.
XLAST — Scalar used to keep track of last run. (Output, if IDO = 0 or 1; input/output, otherwise)
XLAST should not be changed between calls with the same data set.
2. Informational errors
Type
Code
Description
3
1
At least one tie is detected in X.
4
2
The covariance matrix of the runs score is not positive definite. Use a smaller value of NRUN.
Example
The following example illustrates the use of the runs test on 104 pseudo‑random uniform deviates. In the example, 2000 deviates are generated for each call to RUNS. The IDO parameter is set to 1 on the first call to RUNS, 2 on the second, third, and fourth calls, and 3 on the last call. Since the probability of a larger chi‑squared statistic is 0.1872, there is no strong evidence to support rejection of this null hypothesis of randomness.
 
USE IMSL_LIBRARIES
 
IMPLICIT NONE
INTEGER LDCOVA, NRAN, NRUN
PARAMETER (LDCOVA=6, NRAN=2000, NRUN=6)
!
INTEGER I, IDO, NOUT
REAL CHISQ, COUNT(NRUN), COVAR(LDCOVA,NRUN), DF, &
EXPECT(NRUN), PROB, X(NRAN)
!
CALL RNSET (123457)
!
DO 10 I=1, 5
! Set IDO
IF (I .EQ. 1) THEN
IDO = 1
ELSE IF (I .EQ. 5) THEN
IDO = 3
ELSE
IDO = 2
END IF
! Generate the random numbers
CALL RNUN (X)
!
CALL RUNS (X, COUNT, EXPECT, COVAR, CHISQ, DF, PROB, IDO=IDO)
10 CONTINUE
!
CALL WRRRN ('COUNT', COUNT, 1, NRUN, 1)
CALL WRRRN ('EXPECT', EXPECT, 1, NRUN, 1)
CALL WRRRN ('COVAR', COVAR)
CALL UMACH (2, NOUT)
WRITE (NOUT,*) ' CHISQ = ', CHISQ
WRITE (NOUT,*) ' DF = ', DF
WRITE (NOUT,*) ' PROB = ', PROB
END
Output
 
COUNT
1 2 3 4 5 6
1709.0 2046.0 953.0 260.0 55.0 4.0
 
EXPECT
1 2 3 4 5 6
1667.3 2083.4 916.5 263.8 57.5 11.9
 
COVAR
1 2 3 4 5 6
1 1278.2 -194.6 -148.9 -71.6 -22.9 -6.7
2 -194.6 1410.1 -490.6 -197.2 -55.2 -14.4
3 -148.9 -490.6 601.4 -117.4 -31.2 -7.8
4 -71.6 -197.2 -117.4 222.1 -10.8 -2.6
5 -22.9 -55.2 -31.2 -10.8 54.8 -0.6
6 -6.7 -14.4 -7.8 -2.6 -0.6 11.7
CHISQ = 8.76514
DF = 6.00000
PROB = 0.187225
Published date: 03/19/2020
Last modified date: 03/19/2020