The routine may be called by the names g08eaf or nagf_nonpar_randtest_runs.
3Description
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 $\mathrm{-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 ${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 ,r-1$. 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
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
$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.
4References
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
5Arguments
1: $\mathbf{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: $\mathbf{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: $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the sequence of observations.
4: $\mathbf{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.
Constraint:
if ${\mathbf{m}}\le {\mathbf{n}}$, ${\mathbf{cl}}=\text{'S'}$.
5: $\mathbf{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.
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: $\mathbf{ex}\left({\mathbf{maxr}}\right)$ – 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$.
9: $\mathbf{cov}({\mathbf{ldcov}},{\mathbf{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}$.
On exit: if ${\mathbf{cl}}=\text{'S'}$ or $\text{'L'}$ (i.e., if it is the final exit), chi contains the approximate ${\chi}^{2}$ test statistic, ${X}^{2}$.
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.
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.
On entry: ifail must be set to $0$, $\mathrm{-1}$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $\mathrm{-1}$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $\mathrm{-1}$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $\mathrm{-1}$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ or $\mathbf{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).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $\mathrm{-1}$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
Note: in some cases g08eaf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{cl}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{n}}\ge 3$, otherwise ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{m}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{m}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{maxr}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{maxr}}\ge 1$.
On entry, ${\mathbf{maxr}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxr}}<{\mathbf{n}}$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{ldcov}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{maxr}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{ldcov}}\ge {\mathbf{maxr}}$.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{lwrk}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{lwrk}}\ge {\mathbf{maxr}}\times ({\mathbf{maxr}}+5)/2+1=\u27e8\mathit{\text{value}}\u27e9$.
${\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{maxr}}=\u27e8\mathit{\text{value}}\u27e9$ whereas the total length of all runs is $\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=9$
The covariance matrix stored in cov is not positive definite, thus the approximate ${\chi}^{2}$ test statistic cannot be computed. This may be because maxr is too large relative to the length of the full sequence.
${\mathbf{ifail}}=10$
The number of runs requested were not found, only $\u27e8\mathit{\text{value}}\u27e9$ out of the requested $\u27e8\mathit{\text{value}}\u27e9$ where found. All statistics are returned and may still be of use.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
7Accuracy
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.
8Parallelism and Performance
g08eaf is not thread safe and should not be called from a multithreaded user program. Please see Section 1 in FL Interface Multithreading 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.
9Further 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.
10Example
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.