The IMSL_RANDOMNESS_TEST function performs a test for randomness.

This routine requires an IDL Advanced Math and Stats license. For more information, contact your sales or technical support representative.

Runs Up Test

The IMSL_RANDOMNESS_TEST function performs one of four different tests for randomness. Input keyword Runs_Counts computes statistics for the runs up test. Runs tests are used to test for cyclical trend in sequences of random numbers. If the runs down test is desired, each observation should first be multiplied by –1 to change its sign, and Runs_Counts used with the modified vector of observations.

Runs_Counts first tallies the number of runs up (increasing sequences) of each desired length. For i = 1, ..., r – 1, where r = N_Run, RUNS_COUNTS(i) contains the number of runs of length i. RUNS_COUNTS(N_Run) contains the number of runs of length N_Run 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_COUNTS computes the expected values and the covariances of the counts according to methods given by Knuth (1981, pages 65(67). Let R denote a vector of length N_Run 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 N_Run degrees of freedom is given as:

In general, the larger the value of each element of µR, the better the chi-squared approximation.

Pairs Test

Pairs_Counts computes the pairs test (or the Good’s serial test) on a hypothesized sequence of uniform (0,1) pseudorandom numbers. The test proceeds as follows. Subsequent pairs (X(i), X(i + Pairs_Lag)) are tallied into a k x k matrix, where k = N_Run. In this tally, element (j, m) of the matrix is incremented, where:

where l = Pairs_Lag, and the notation represents the greatest integer function, Y is the greatest integer less than or equal to Y, where Y is a real number. If l = 1, then i = 1, 3, 5, ..., n – 1. If l > 1, then i = 1, 2, 3, ..., n – l, where n is the total number of pseudorandom numbers input on the current usage of Pairs_Counts (i.e., n = N_ELEMENTS(x)).

Given the tally matrix in Pairs_Counts, chi-squared is computed as:

where e = Σoij/k2, and oij is the observed count in cell (i, j) (oij = Pairs_Counts (i, j)). Because pair statistics for the trailing observations are not tallied on any call, You should use Pairs_Counts with N_ELEMENTS(x) as large as possible. For Pairs_Lag < 20 and N_ELEMENTS(x) = 2000, little power is lost.

d2 Test

DSQUARE_COUNTS computes the d2 test for succeeding quadruples of hypothesized pseudorandom uniform (0, 1) deviates. The d2 test is performed as follows. Let X1, X2, X3, and X4 denote four pseudorandom uniform deviates, and consider:

D2 = (X3 – X1)2 + (X4 – X2)2

The probability distribution of D2 is given as:

when D2 ≤1, where π denotes the value of pi. If D2 > 1, this probability is given as:

See Gruenberger and Mark (1951) for a derivation of this distribution.

 

For each succeeding set of 4 pseudorandom uniform numbers input in x, d2 and the cumulative probability of d2 (Pr(D2 ≤ d2)) are computed. The resulting probability is tallied into one of k = N_Run equally spaced intervals.

Let n denote the number of sets of four random numbers input (n = the total number of observations/4). Then, under the null hypothesis that the numbers input are random uniform (0, 1) numbers, the expected value for each element in DSQUARE_COUNTS is e = n/k. An approximate chi-squared statistic is computed as:

where oi = DSQUARE_COUNTS(i) is the observed count. Thus, χ2 has k – 1 degrees of freedom, and the null hypothesis of pseudorandom uniform (0, 1) deviates is rejected if χ2 is too large. As n increases, the chi-squared approximation becomes better. A useful generalization is that e > 5 yields a good chi-squared approximation.

Triplets Test

DCUBE_COUNTS computes the triplets test on a sequence of hypothesized pseudorandom uniform(0, 1) deviates. The triplets test is computed as follows: Each set of three successive deviates, X1, X2, and X3, is tallied into one of m3 equal sized

cubes, where m = N_Run. Let i = [mX1] + 1, j = [mX2] + 1, and k = [mX3] + 1. For the

triplet (X1, X2, X3), DCUBE_COUNTS(i, j, k) is incremented.

Under the null hypothesis of pseudorandom uniform(0, 1) deviates, the m3 cells are equally probable and each has expected value e = n/m3, where n is the number of triplets tallied. An approximate chi-squared statistic is computed as:

where oijk = DCUBE_COUNTS(i, j, k).

The computed chi-squared has m3– 1 degrees of freedom, and the null hypothesis of pseudorandom uniform (0, 1) deviates is rejected if χ2 is too large.

Examples


Example 1

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 use of RUNS_COUNTS. 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.

.RUN
PRO print_results, n_run, num, rc, re, cov, chisq, df, p
  PRINT, '  runs_count' 
  PRINT, num + 1, FORMAT = '(6I5)' 
  PRINT, rc, FORMAT = '(6I5)'
  PRINT
  PRINT, '    runs_expect' 
  PRINT, num + 1, FORMAT = '(6I7)' 
  PRINT, re, FORMAT = '(6F7.1)'
  PRINT
  PRINT, '    covariances' 
  PRINT, num + 1, FORMAT = '(7X, 6I8)'
  FOR i	=	0, n_run - 1 DO $
  PRINT, num(i) + 1, cov(i, *), FORMAT = '(I8, 6F8.1)' PRINT
  PRINT, 'chisq	=', chisq PRINT, 'df	=', df PRINT, 'pvalue =', p
END
 
nran	=	10000 n_run	 =		6
num	=	INDGEN(n_run)
IMSL_RANDOMOPT, set	=	123457
x	=	IMSL_RANDOM(nran, /Uniform)
p	=	IMSL_RANDOMNESS_TEST(x, n_run, Runs_Counts = rc, $
  Covariances = cov, Chisq = chisq, Df = df, Runs_Expect = re)
print_results, n_run, num, rc,re,cov,chisq, df, p

Result:

Example 2

The following example illustrates the calculations of the Pairs_Counts statistics when a random sample of size 104 is used and the Pairs_Lag is 1. The results are not significant.

.RUN
PRO print_results, n_run, num, pc, expect, chisq, df, p
  PRINT, '     pairs_count'
  PRINT, num + 1, FORMAT = '(5X, 10I5)'
  FOR i	=	0, n_run - 1 DO $
    PRINT, num(i) + 1, pc(i, *), FORMAT = '(I5, 10I5)' PRINT
  PRINT, 'expect	=', expect
  PRINT, 'chisq	=', chisq
  PRINT, 'df	=', df
  PRINT, 'pvalue	=', p
END
 
nran	=	10000 n_run	 =		10
num	=	INDGEN(n_run)
lag	=	5
IMSL_RANDOMOPT, set	=	123467
x	=	IMSL_RANDOM(nran, /Uniform)
p	=	IMSL_RANDOMNESS_TEST(x, n_run, Pairs_Counts = pc, $
  Pairs_Lag = lag, Chisq = chisq, $
  Df = df, Expect = expect)
print_results, n_run, num, pc, expect, chisq, df, p

Result:

Example 3

In the following example, 2000 observations generated using the routine IMSL_RANDOM are input to DSQUARE_COUNTS in one call. In the example, the null hypothesis of a uniform distribution is not rejected.

.RUN
PRO print_results, n_run, num, dc, expect, chisq, df, p
  PRINT, '     dsquare_counts'
  PRINT, num + 1, FORMAT = '(6I5)'
  PRINT, dc, FORMAT = '(6I5)'
  PRINT
  PRINT, 'expect	=', expect
  PRINT, 'chisq	=', chisq
  PRINT, 'df	=', df
  PRINT, 'pvalue	=', p
END
 
nran	=	2000 n_run	 =		6
num	=	INDGEN(n_run) IMSL_RANDOMOPT, set	=	123457
x	=	IMSL_RANDOM(nran, /Uniform)
p	=	IMSL_RANDOMNESS_TEST(x, n_run, Chisq = chisq, Df = df, $
  Expect = expect, Dsquare_Counts = dc)
print_results, n_run, num, dc, expect, chisq, df, p

Result:

Example 4

In the following example, 2001 deviates generated by the routine IMSL_RANDOM are input to DCUBE_COUNTS, and tabulated in 27 equally sized cubes. In the example, the null hypothesis is not rejected.

.RUN
PRO print_results, n_run, num, dc, expect, chisq, df, p
  FOR j	=	0, n_run - 1 DO BEGIN
    PRINT, ' dcube_counts'
    PRINT, num + 1, FORMAT = '(5X, 3I5)'
  FOR i	=	0, n_run - 1 DO $
    PRINT, num(i) + 1, dc(j, i, *), FORMAT = '(I5, 3I5)'
      PRINT
  ENDFOR
  PRINT, 'expect =', expect PRINT, 'chisq	=', chisq PRINT, 'df	=', df PRINT, 'pvalue =', p
END
 
nran	=	2001 n_run	 =		3
num	=	INDGEN(n_run)
IMSL_RANDOMOPT, set	=	123457
x	=	IMSL_RANDOM(nran, /Uniform)
p	=	IMSL_RANDOMNESS_TEST(x, n_run, Chisq = chisq, Df = df, $
  Expect = expect, Dcube_Counts = dc)
print_results, n_run, num, dc, expect, chisq, df, p

Result:

Syntax


Result = IMSL_RANDOMNESS_TEST(X, N_Run [, COVARIANCES=variable] [, DCUBE_COUNTS=variable] [, /DOUBLE] [, DSQUARE_COUNTS=variable] [, EXPECT=variable] [, PAIRS_COUNTS=variable] [, PAIRS_LAG=value] [, RUNS_COUNTS=variable] [, RUNS_EXPECT=variable])

Return Value


The probability of a larger chi-squared statistic for testing the null hypothesis of a uniform distribution.

Arguments


N_Run

Length of longest run for which tabulation is desired. For keywords PAIRS_COUNTS, DSQUARE_COUNTS, and DCUBE_COUNTS, N_Run stands for the number of equiprobable cells into which the statistics are to be tabulated.

X

One-dimensional array containing the data.

Keywords


COVARIANCES (optional)

Named variable into which an array of size N_ELEMENTS(x) by N_ELEMENTS(x) containing the variances and covariances of the counts is stored. The keywords RUNS_COUNTS and COVARIANCES must be used together.

Use one of the options listed below to specify which test is to perform.

Keyword

Test to Perform

RUNS_COUNTS with COVARIANCES

Runs Test

PAIRS_COUNTS with PAIRS_LAG

Pairs Test

DSQUARE_COUNTS

d2 Test

DCUBE_COUNTS

Triplets Test

DCUBE_COUNTS (optional)

Named variable into which an array of length N_Run by N_Run by N_Run containing the tabulations for the triplets test is stored. The keywords RUNS_COUNTS, PAIRS_COUNTS, DSQUARE_COUNTS, and DCUBE_COUNTS cannot be used together.

Chisq: Named variable into which the Chi-squared statistic for testing the null hypothesis of a uniform distribution is stored.

Df: Named variable into which the degrees of freedom for chi-squared is stored.

Use one of the options listed below to specify which test is to perform.

Keyword

Test to Perform

RUNS_COUNTS with COVARIANCES

Runs Test

PAIRS_COUNTS with PAIRS_LAG

Pairs Test

DSQUARE_COUNTS

d2 Test

DCUBE_COUNTS

Triplets Test

DOUBLE (optional)

If present and nonzero, double precision is used.

DSQUARE_COUNTS (optional)

Named variable into which an array of length N_Run containing the tabulations for the d2 test is stored. The keywords DSQUARE_COUNTS, RUNS_COUNTS, PAIRS_COUNTS, and DCUBE_COUNTS cannot be used together.

Use one of the options listed below to specify which test is to perform.

Keyword

Test to Perform

RUNS_COUNTS with COVARIANCES

Runs Test

PAIRS_COUNTS with PAIRS_LAG

Pairs Test

DSQUARE_COUNTS

d2 Test

DCUBE_COUNTS

Triplets Test

EXPECT (optional)

Named variable into which the expected number of counts for each cell is stored. This keyword is optional only if one of the keywords PAIRS_COUNTS, DSQUARE_COUNTS, or DCUBE_COUNTS is used. The keywords RUNS_COUNTS and EXPECT cannot be used together.

PAIRS_COUNTS (optional)

Named variable into which an array of size N_Run by N_Run containing the count of the number of pairs in each cell is stored. The lag to be used in computing the pairs statistic is stored in PAIRS_LAG. PAIRS (X(i), X(i + PAIRS_LAG)) for i = 0, ..., N – PAIRS_LAG – 1 are tabulated, where N is the total sample size. The keywords RUNS_COUNTS, PAIRS_COUNTS, DSQUARE_COUNTS, and DCUBE_COUNTS cannot be used together.

Use one of the options listed below to specify which test is to perform.

Keyword

Test to Perform

RUNS_COUNTS with COVARIANCES

Runs Test

PAIRS_COUNTS with PAIRS_LAG

Pairs Test

DSQUARE_COUNTS

d2 Test

DCUBE_COUNTS

Triplets Test

PAIRS_LAG (optional)

The lag to be used in computing the pairs statistic. The keywords PAIRS_LAG and PAIRS_COUNTS must be used together.

RUNS_COUNTS (optional)

Named variable into which an array of size N_ELEMENTS(x) containing the counts of the number of runs up each length is stored. The Runs Test is the default test to return the counts and covariances; the RUNS_COUNTS keyword must be used, and RUNS_COUNTS and COVARIANCES must be used together. The keywords RUNS_COUNTS, PAIRS_COUNTS, DSQUARE_COUNTS, and DCUBE_COUNTS cannot be used together.

Use one of the options listed below to specify which test is to perform.

Keyword

Test to Perform

RUNS_COUNTS with COVARIANCES

Runs Test

PAIRS_COUNTS with PAIRS_LAG

Pairs Test

DSQUARE_COUNTS

d2 Test

DCUBE_COUNTS

Triplets Test

RUNS_EXPECT (optional)

Named variable into which an array of length N_Run containing the expected number of runs of each length is expected is stored. This keyword is optional if RUNS_COUNTS is used.

Version History


6.4

Introduced

See Also


IMSL_RANDOM