NAG Library Routine Document

g08eaf (randtest_runs)

1
Purpose

g08eaf performs a runs up (or a runs down) test on a sequence of observations.

2
Specification

Fortran Interface
Subroutine g08eaf ( cl, n, x, m, maxr, nruns, ncount, ex, cov, ldcov, chi, df, prob, wrk, lwrk, ifail)
Integer, Intent (In):: n, m, maxr, ldcov, lwrk
Integer, Intent (Inout):: nruns, ncount(maxr), ifail
Real (Kind=nag_wp), Intent (In):: x(n)
Real (Kind=nag_wp), Intent (Inout):: cov(ldcov,maxr)
Real (Kind=nag_wp), Intent (Out):: ex(maxr), chi, df, prob, wrk(lwrk)
Character (1), Intent (In):: cl
C Header Interface
#include <nagmk26.h>
void  g08eaf_ (const char *cl, const Integer *n, const double x[], const Integer *m, const Integer *maxr, Integer *nruns, Integer ncount[], double ex[], double cov[], const Integer *ldcov, double *chi, double *df, double *prob, double wrk[], const Integer *lwrk, Integer *ifail, const Charlen length_cl)

3
Description

Runs tests may be used to investigate for trends in a sequence of observations. g08eaf computes statistics for the runs up test. If the runs down test is desired then each observation must be multiplied by -1 before g08eaf is called with the modified vector of observations. g08eaf may be used in two different modes:
(i) a single call to g08eaf which computes all test statistics after counting the runs;
(ii) multiple calls to g08eaf with the final test statistics only being computed in the last call.
The second mode is necessary if all the data do not fit into the memory. See argument cl in Section 5 for details on how to invoke each mode.
A run up is a sequence of numbers in increasing order. A run up ends at xk when xk>xk+1 and the new run then begins at xk+1. g08eaf counts the number of runs up of different lengths. Let ci denote the number of runs of length i, for i=1,2,,r-1. The number of runs of length r or greater is then denoted by cr.
An unfinished run at the end of a sequence is not counted unless the sequence is part of an initial or intermediate call to g08eaf (i.e., unless there is another call to g08eaf to follow) in which case the unfinished run is used together with the beginning of the next sequence of numbers input to g08eaf in the next call. The following is a trivial example.
Suppose we called g08eaf twice with the following two sequences:
Then after the second call g08eaf would have counted the runs up of the following lengths:
When the counting of runs is complete g08eaf computes the expected values and covariances of the counts, ci. For the details of the method used see Knuth (1981). An approximate χ2 statistic with r degrees of freedom is computed, where
X2= c-μcT Σ c -1 c-μc ,  
where
The use of the χ2-distribution as an approximation to the exact distribution of the test statistic, X2, improves as the length of the sequence relative to m increases and hence the expected value, e, increases.
You may specify the total number of runs to be found. If the specified number of runs is found before the end of a sequence g08eaf will exit before counting any further runs. The number of runs actually counted and used to compute the test statistic is returned via nruns.

4
References

Dagpunar J (1988) Principles of Random Variate Generation Oxford University Press
Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley
Morgan B J T (1984) Elements of Simulation Chapman and Hall
Ripley B D (1987) Stochastic Simulation Wiley

5
Arguments

1:     cl – Character(1)Input
On entry: must specify the type of call to g08eaf.
cl='S'
This is the one and only call to g08eaf (single call mode). All data are to be input at once. All test statistics are computed after the counting of runs is complete.
cl='F'
This is the first call to the routine. All initializations are carried out and the counting of runs begins. The final test statistics are not computed since further calls will be made to g08eaf.
cl='I'
This is an intermediate call during which the counts of runs are updated. The final test statistics are not computed since further calls will be made to g08eaf.
cl='L'
This is the last call to g08eaf. The test statistics are computed after the final counting of runs is completed.
Constraint: cl='S', 'F', 'I' or 'L'.
2:     n – IntegerInput
On entry: n, the length of the current sequence of observations.
Constraints:
  • if cl='S', n3;
  • otherwise n1.
3:     xn – Real (Kind=nag_wp) arrayInput
On entry: the sequence of observations.
4:     m – IntegerInput
On entry: the maximum number of runs to be sought. If m0 then no limit is placed on the number of runs that are found.
m must not be changed between calls to g08eaf.
Constraint: if mn, cl='S'.
5:     maxr – IntegerInput
On entry: r, the length of the longest run for which tabulation is desired. That is, all runs with length greater than or equal to r are counted together.
maxr must not be changed between calls to g08eaf.
Constraint: maxr1 and if cl='S', maxr<n.
6:     nruns – IntegerInput/Output
On entry: if cl='S' or 'F', nruns need not be set.
If cl='I' or 'L', nruns must contain the value returned by the previous call to g08eaf.
On exit: the number of runs actually found.
7:     ncountmaxr – Integer arrayInput/Output
On entry: if cl='S' or 'F', ncount need not be set.
If cl='I' or 'L', ncount must contain the values returned by the previous call to g08eaf.
On exit: the counts of runs of the different lengths, ci, for i=1,2,,r.
8:     exmaxr – Real (Kind=nag_wp) arrayOutput
On exit: if cl='S' or 'L', (i.e., if it is the final exit) then ex contains the expected values of the counts, ei, for i=1,2,,r.
Otherwise the elements of ex are not set.
9:     covldcovmaxr – Real (Kind=nag_wp) arrayOutput
On exit: if cl='S' or 'L' (i.e., if it is the final exit) then cov contains the covariance matrix of the counts, Σc.
Otherwise the elements of cov are not set.
10:   ldcov – IntegerInput
On entry: the first dimension of the array cov as declared in the (sub)program from which g08eaf is called.
Constraint: ldcovmaxr.
11:   chi – Real (Kind=nag_wp)Output
On exit: if cl='S' or 'L' (i.e., if it is the final exit), chi contains the approximate χ2 test statistic, X2.
Otherwise chi is not set.
12:   df – Real (Kind=nag_wp)Output
On exit: if cl='S' or 'L' (i.e., if it is the final exit) then df contains the degrees of freedom of the χ2 statistic.
Otherwise df is not set.
13:   prob – Real (Kind=nag_wp)Output
On exit: if cl='S' or 'L', (i.e., if it is the final exit) then prob contains the upper tail probability corresponding to the χ2 test statistic, i.e., the significance level.
Otherwise prob is not set.
14:   wrklwrk – Real (Kind=nag_wp) arrayWorkspace
15:   lwrk – IntegerInput
On entry: the dimension of the array wrk as declared in the (sub)program from which g08eaf is called.
Constraint: lwrkmaxr×maxr+52+1.
16:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1 or 1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1 or 1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if ifail0 on exit, the recommended value is -1. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6
Error Indicators and Warnings

If on entry ifail=0 or -1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Note: g08eaf may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
ifail=1
On entry, cl=value.
Constraint: cl='S', 'F', 'I' or 'L'.
ifail=2
On entry, n=value.
Constraint: if cl='S', n3, otherwise n1.
ifail=3
On entry, m=value and n=value.
Constraint: if cl='S', mn.
ifail=4
On entry, maxr=value.
Constraint: maxr1.
On entry, maxr=value and n=value.
Constraint: if cl='S', maxr<n.
ifail=5
On entry, ldcov=value and maxr=value.
Constraint: ldcovmaxr.
ifail=6
On entry, lwrk=value.
Constraint: lwrkmaxr×maxr+5/2+1=value.
ifail=7
There is a tie in the sequence of observations.
ifail=8
The total length of the runs found is less than maxr.
maxr=value whereas the total length of all runs is value.
ifail=9
The covariance matrix stored in cov is not positive definite, thus the approximate χ2 test statistic cannot be computed.
This may be because maxr is too large relative to the length of the full sequence.
ifail=10
The number of runs requested were not found, only value out of the requested value where found.
All statistics are returned and may still be of use.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7
Accuracy

The computations are believed to be stable. The computation of prob given the values of chi and df will obtain a relative accuracy of five significant figures for most cases.

8
Parallelism and Performance

g08eaf is not thread safe and should not be called from a multithreaded user program. Please see Section 3.12.1 in How to Use the NAG Library and its Documentation for more information on thread safety.
g08eaf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9
Further Comments

The time taken by g08eaf increases with the number of observations n, and also depends to some extent on whether the call to g08eaf is an only, first, intermediate or last call.

10
Example

The following program performs a runs up test on 500 pseudorandom numbers. g08eaf is called 5 times with 100 observations each time. No limit is placed on the number of runs to be counted. All runs of length 6 or more are counted together.

10.1
Program Text

Program Text (g08eafe.f90)

10.2
Program Data

Program Data (g08eafe.d)

10.3
Program Results

Program Results (g08eafe.r)