NAG Library Routine Document
G08EAF
1 Purpose
G08EAF performs a runs up (or a runs down) test on a sequence of observations.
2 Specification
SUBROUTINE G08EAF ( 
CL, N, X, M, MAXR, NRUNS, NCOUNT, EX, COV, LDCOV, CHI, DF, PROB, WRK, LWRK, IFAIL) 
INTEGER 
N, M, MAXR, NRUNS, NCOUNT(MAXR), LDCOV, LWRK, IFAIL 
REAL (KIND=nag_wp) 
X(N), EX(MAXR), COV(LDCOV,MAXR), CHI, DF, PROB, WRK(LWRK) 
CHARACTER(1) 
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 parameter
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 ${x}_{k}$ when ${x}_{k}>{x}_{k+1}$ and the new run then begins at ${x}_{k+1}$. G08EAF counts the number of runs up of different lengths. Let ${c}_{\mathit{i}}$ denote the number of runs of length $\mathit{i}$, for $\mathit{i}=1,2,\dots ,r1$. The number of runs of length $r$ or greater is then denoted by ${c}_{r}$.
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:
 ($0.20$ $0.40$ $0.45$ $0.40$ $0.15$ $0.75$ $0.95$ $0.23$) and
 ($0.27$ $0.40$ $0.25$ $0.10$ $0.34$ $0.39$ $0.61$ $0.12$).
Then after the second call G08EAF would have counted the runs up of the following lengths:
 3, $1$, $3$, $3$, $1$, and $4$.
When the counting of runs is complete G08EAF computes the expected values and covariances of the counts,
${c}_{i}$. For the details of the method used see
Knuth (1981). An approximate
${\chi}^{2}$ statistic with
$r$ degrees of freedom is computed, where
where
 $c$ is the vector of counts, ${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,r$,
 ${\mu}_{c}$ is the vector of expected values,
 ${e}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,r$, where ${e}_{i}$ is the expected value for ${c}_{i}$ under the null hypothesis of randomness, and
 ${\Sigma}_{c}$ is the covariance matrix of $c$ under the null hypothesis.
The use of the ${\chi}^{2}$distribution as an approximation to the exact distribution of the test statistic, ${X}^{2}$, 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 Parameters
 1: CL – CHARACTER(1)Input
On entry: must specify the type of call to G08EAF.
 ${\mathbf{CL}}=\text{'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.
 ${\mathbf{CL}}=\text{'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.
 ${\mathbf{CL}}=\text{'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.
 ${\mathbf{CL}}=\text{'L'}$
 This is the last call to G08EAF. The test statistics are computed after the final counting of runs is completed.
Constraint:
${\mathbf{CL}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
 2: N – INTEGERInput
On entry: $n$, the length of the current sequence of observations.
Constraints:
 if ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{N}}\ge 3$;
 otherwise ${\mathbf{N}}\ge 1$.
 3: X(N) – 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
${\mathbf{M}}\le 0$ then no limit is placed on the number of runs that are found.
M must not be changed between calls to G08EAF.
Constraint:
if ${\mathbf{M}}\le {\mathbf{N}}$, ${\mathbf{CL}}=\text{'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:
${\mathbf{MAXR}}\ge 1$ and if ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{MAXR}}<{\mathbf{N}}$.
 6: NRUNS – INTEGERInput/Output

On entry: if
${\mathbf{CL}}=\text{'S'}$ or
$\text{'F'}$,
NRUNS need not be set.
If
${\mathbf{CL}}=\text{'I'}$ or
$\text{'L'}$,
NRUNS must contain the value returned by the previous call to G08EAF.
On exit: the number of runs actually found.
 7: NCOUNT(MAXR) – INTEGER arrayInput/Output
On entry: if
${\mathbf{CL}}=\text{'S'}$ or
$\text{'F'}$,
NCOUNT need not be set.
If
${\mathbf{CL}}=\text{'I'}$ or
$\text{'L'}$,
NCOUNT must contain the values returned by the previous call to G08EAF.
On exit: the counts of runs of the different lengths,
${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,r$.
 8: EX(MAXR) – REAL (KIND=nag_wp) arrayOutput

On exit: if
${\mathbf{CL}}=\text{'S'}$ or
$\text{'L'}$, (i.e., if it is the final exit) then
EX contains the expected values of the counts,
${e}_{\mathit{i}}$, for
$\mathit{i}=1,2,\dots ,r$.
Otherwise the elements of
EX are not set.
 9: COV(LDCOV,MAXR) – REAL (KIND=nag_wp) arrayOutput
On exit: if
${\mathbf{CL}}=\text{'S'}$ or
$\text{'L'}$ (i.e., if it is the final exit) then
COV contains the covariance matrix of the counts,
${\Sigma}_{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:
${\mathbf{LDCOV}}\ge {\mathbf{MAXR}}$.
 11: CHI – REAL (KIND=nag_wp)Output

On exit: if
${\mathbf{CL}}=\text{'S'}$ or
$\text{'L'}$ (i.e., if it is the final exit) then
CHI contains the approximate
${\chi}^{2}$ test statistic,
${X}^{2}$.
Otherwise
CHI is not set.
 12: DF – REAL (KIND=nag_wp)Output

On exit: if
${\mathbf{CL}}=\text{'S'}$ or
$\text{'L'}$ (i.e., if it is the final exit) then
DF contains the degrees of freedom of the
${\chi}^{2}$ statistic.
 13: PROB – REAL (KIND=nag_wp)Output

On exit: if
${\mathbf{CL}}=\text{'S'}$ or
$\text{'L'}$, (i.e., if it is the final exit) then
PROB contains the upper tail probability corresponding to the
${\chi}^{2}$ test statistic, i.e., the significance level.
Otherwise
PROB is not set.
 14: WRK(LWRK) – 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:
${\mathbf{LWRK}}\ge \frac{{\mathbf{MAXR}}\times \left({\mathbf{MAXR}}+5\right)}{2}+1$.
 16: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ 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 parameters may be useful even if
${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is
$1$.
When the value $\mathbf{1}\text{ or}1$ is used it is essential to test the value of IFAIL on exit.
On exit:
${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see
Section 6).
6 Error Indicators and Warnings
If on entry
${\mathbf{IFAIL}}={\mathbf{0}}$ or
${{\mathbf{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:
 ${\mathbf{IFAIL}}=1$

On entry,  ${\mathbf{CL}}\ne \text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$. 
 ${\mathbf{IFAIL}}=2$
On entry,  ${\mathbf{N}}<3$ if ${\mathbf{CL}}=\text{'S'}$, 
or  ${\mathbf{N}}<1$ otherwise. 
 ${\mathbf{IFAIL}}=3$

On entry,  with ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{M}}>{\mathbf{N}}$. 
 ${\mathbf{IFAIL}}=4$
On entry,  ${\mathbf{MAXR}}<1$, 
or  ${\mathbf{MAXR}}\ge {\mathbf{N}}$ and ${\mathbf{CL}}=\text{'S'}$. 
 ${\mathbf{IFAIL}}=5$
On entry,  ${\mathbf{LDCOV}}<{\mathbf{MAXR}}$. 
 ${\mathbf{IFAIL}}=6$
On entry,  ${\mathbf{LWRK}}<{\mathbf{MAXR}}\times \left({\mathbf{MAXR}}+5\right)/2+1$. 
 ${\mathbf{IFAIL}}=7$
There is a tie in the sequence of observations.
 ${\mathbf{IFAIL}}=8$
The total length of the runs found is less than
MAXR.
 ${\mathbf{IFAIL}}=9$
The covariance matrix stored in
COV is not positive definite. This may be because the value of
MAXR is too large relative to the full length of the series. Thus the approximate
${\chi}^{2}$ test statistic cannot be computed.
 ${\mathbf{IFAIL}}=10$
The number of runs requested were not found. All statistics are still computed and the information returned may still be of use.
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.
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.
9 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.
9.1 Program Text
Program Text (g08eafe.f90)
9.2 Program Data
Program Data (g08eafe.d)
9.3 Program Results
Program Results (g08eafe.r)