NAG C Library Function Document
nag_runs_test (g08eac)
1
Purpose
nag_runs_test (g08eac) performs a runs up (or a runs down) test on a sequence of observations.
2
Specification
#include <nag.h> 
#include <nagg08.h> 
void 
nag_runs_test (Integer n,
const double x[],
Integer max_run,
Integer *nruns,
double *chi,
double *df,
double *prob,
NagError *fail) 

3
Description
Runs tests may be used to investigate for trends in a sequence of observations. nag_runs_test (g08eac) computes statistics for the runs up test. If the runs down test is desired then each observation must be multiplied by $1$ before nag_runs_test (g08eac) is called with the modified vector of observations.
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}$. nag_runs_test (g08eac) 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. The following is a trivial example.
Suppose we called
nag_runs_test (g08eac) with the following sequence:
 $0.20$ $0.40$ $0.45$ $0.40$ $0.15$ $0.75$ $0.95$ $0.23$ $0.27$ $0.40$ $0.25$ $0.10$ $0.34$ $0.39$ $0.61$ $0.12$.
Then
nag_runs_test (g08eac) would have counted the runs up of the following lengths:
 $3$, $1$, $3$, $3$, $1$, and $4$.
When the counting of runs is complete
nag_runs_test (g08eac) 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
nag_runs_test (g08eac) 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:
$\mathbf{n}$ – IntegerInput

On entry: $n$, the length of the current sequence of observations.
Constraint:
${\mathbf{n}}\ge 3$.
 2:
$\mathbf{x}\left[{\mathbf{n}}\right]$ – const doubleInput

On entry: the sequence of observations.
 3:
$\mathbf{max\_run}$ – 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.
Constraint:
$1\le {\mathbf{max\_run}}<{\mathbf{n}}$.
 4:
$\mathbf{nruns}$ – Integer *Output

On exit: the number of runs actually found.
 5:
$\mathbf{chi}$ – double *Output

On exit: contains the approximate ${\chi}^{2}$ test statistic, ${X}^{2}$.
 6:
$\mathbf{df}$ – double *Output

On exit: contains the degrees of freedom of the ${\chi}^{2}$ statistic.
 7:
$\mathbf{prob}$ – double *Output

On exit: contains the upper tail probability corresponding to the ${\chi}^{2}$ test statistic, i.e., the significance level.
 8:
$\mathbf{fail}$ – NagError *Input/Output

The NAG error argument (see
Section 3.7 in How to Use the NAG Library and its Documentation).
6
Error Indicators and Warnings
 NE_2_INT_ARG_GE

On entry, ${\mathbf{max\_run}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{n}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{max\_run}}<{\mathbf{n}}$.
 NE_ALLOC_FAIL

Dynamic memory allocation failed.
See
Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
 NE_BAD_PARAM

On entry, argument $\u2329\mathit{\text{value}}\u232a$ had an illegal value.
 NE_G08EA_COVAR

Internally computed covariance matrix is not positive definite.
This may be because the value of
max_run is too large relative to the full length of the series.
Thus the approximate
${\chi}^{2}$ test statistic cannout be computed.
 NE_G08EA_RUNS

The number of runs requested were not found, only $\u2329\mathit{\text{value}}\u232a$ out of the requested $\u2329\mathit{\text{value}}\u232a$ where found.
All statistics are returned and may still be of use.
 NE_G08EA_RUNS_LENGTH

The total length of the runs found is less than
max_run.
${\mathbf{max\_run}}=\u2329\mathit{\text{value}}\u232a$ whereas the total length of all runs is
$\u2329\mathit{\text{value}}\u232a$.
 NE_G08EA_TIE

There is a tie in the sequence of observations.
 NE_INT_ARG_LT

On entry, ${\mathbf{max\_run}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{max\_run}}\ge 1$.
On entry, ${\mathbf{n}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{n}}\ge 3$.
 NE_INTERNAL_ERROR

An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact
NAG for assistance.
See
Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
 NE_NO_LICENCE

Your licence key may have expired or may not have been installed correctly.
See
Section 2.7.5 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
nag_runs_test (g08eac) 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 function. Please also consult the
Users' Note for your implementation for any additional implementationspecific information.
The time taken by nag_runs_test (g08eac) increases with the number of observations $n$.
10
Example
The following program performs a runs up test on
$10000$ pseudorandom numbers taken from a uniform distribution
$U\left(0,1\right)$, generated by
nag_rand_uniform (g05sqc). All runs of length
$6$ or more are counted together.
10.1
Program Text
Program Text (g08eace.c)
10.2
Program Data
None.
10.3
Program Results
Program Results (g08eace.r)