PDF version (NAG web site
, 64bit version, 64bit version)
NAG Toolbox: nag_nonpar_randtest_runs (g08ea)
Purpose
nag_nonpar_randtest_runs (g08ea) performs a runs up (or a runs down) test on a sequence of observations.
Syntax
[
nruns,
ncount,
ex,
covar,
chi,
df,
prob,
ifail] = g08ea(
cl,
x,
m,
nruns,
ncount, 'n',
n, 'maxr',
maxr)
[
nruns,
ncount,
ex,
covar,
chi,
df,
prob,
ifail] = nag_nonpar_randtest_runs(
cl,
x,
m,
nruns,
ncount, 'n',
n, 'maxr',
maxr)
Description
Runs tests may be used to investigate for trends in a sequence of observations.
nag_nonpar_randtest_runs (g08ea) computes statistics for the runs up test. If the runs down test is desired then each observation must be multiplied by
$1$ before
nag_nonpar_randtest_runs (g08ea) is called with the modified vector of observations.
nag_nonpar_randtest_runs (g08ea) may be used in two different modes:
(i) 
a single call to nag_nonpar_randtest_runs (g08ea) which computes all test statistics after counting the runs; 
(ii) 
multiple calls to nag_nonpar_randtest_runs (g08ea) 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
Arguments 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}$. nag_nonpar_randtest_runs (g08ea) 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 nag_nonpar_randtest_runs (g08ea) (i.e., unless there is another call to nag_nonpar_randtest_runs (g08ea) to follow) in which case the unfinished run is used together with the beginning of the next sequence of numbers input to nag_nonpar_randtest_runs (g08ea) in the next call. The following is a trivial example.
Suppose we called
nag_nonpar_randtest_runs (g08ea) 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
nag_nonpar_randtest_runs (g08ea) 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_nonpar_randtest_runs (g08ea) 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_nonpar_randtest_runs (g08ea) will exit before counting any further runs. The number of runs actually counted and used to compute the test statistic is returned via
nruns.
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
Parameters
Compulsory Input Parameters
 1:
$\mathrm{cl}$ – string (length ≥ 1)

Must specify the type of call to
nag_nonpar_randtest_runs (g08ea).
 ${\mathbf{cl}}=\text{'S'}$
 This is the one and only call to nag_nonpar_randtest_runs (g08ea) (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 function. 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 nag_nonpar_randtest_runs (g08ea).
 ${\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 nag_nonpar_randtest_runs (g08ea).
 ${\mathbf{cl}}=\text{'L'}$
 This is the last call to nag_nonpar_randtest_runs (g08ea). 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:
$\mathrm{x}\left({\mathbf{n}}\right)$ – double array

The sequence of observations.
 3:
$\mathrm{m}$ – int64int32nag_int scalar

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
nag_nonpar_randtest_runs (g08ea).
Constraint:
if ${\mathbf{m}}\le {\mathbf{n}}$, ${\mathbf{cl}}=\text{'S'}$.
 4:
$\mathrm{nruns}$ – int64int32nag_int scalar

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
nag_nonpar_randtest_runs (g08ea).
 5:
$\mathrm{ncount}\left({\mathbf{maxr}}\right)$ – int64int32nag_int array

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
nag_nonpar_randtest_runs (g08ea).
Optional Input Parameters
 1:
$\mathrm{n}$ – int64int32nag_int scalar

Default:
the dimension of the array
x.
$n$, the length of the current sequence of observations.
Constraints:
 if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{n}}\ge 3$;
 otherwise ${\mathbf{n}}\ge 1$.
 2:
$\mathrm{maxr}$ – int64int32nag_int scalar

Default:
the dimension of the array
ncount.
$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
nag_nonpar_randtest_runs (g08ea).
Constraint:
${\mathbf{maxr}}\ge 1$ and if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxr}}<{\mathbf{n}}$.
Output Parameters
 1:
$\mathrm{nruns}$ – int64int32nag_int scalar

The number of runs actually found.
 2:
$\mathrm{ncount}\left({\mathbf{maxr}}\right)$ – int64int32nag_int array

The counts of runs of the different lengths,
${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,r$.
 3:
$\mathrm{ex}\left({\mathbf{maxr}}\right)$ – double array

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.
 4:
$\mathrm{covar}\left(\mathit{ldcov},{\mathbf{maxr}}\right)$ – double array

If
${\mathbf{cl}}=\text{'S'}$ or
$\text{'L'}$ (i.e., if it is the final exit) then
covar contains the covariance matrix of the counts,
${\Sigma}_{c}$.
Otherwise the elements of
covar are not set.
 5:
$\mathrm{chi}$ – double scalar

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.
 6:
$\mathrm{df}$ – double scalar

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.
 7:
$\mathrm{prob}$ – double scalar

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.
 8:
$\mathrm{ifail}$ – int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Note: nag_nonpar_randtest_runs (g08ea) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:
Cases prefixed with W are classified as warnings and
do not generate an error of type NAG:error_n. See nag_issue_warnings.
 ${\mathbf{ifail}}=1$

On entry, ${\mathbf{cl}}=\_$.
Constraint: ${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
 ${\mathbf{ifail}}=2$

Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{n}}\ge 3$, otherwise ${\mathbf{n}}\ge 1$.
 ${\mathbf{ifail}}=3$

Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{m}}\le {\mathbf{n}}$.
 ${\mathbf{ifail}}=4$

Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxr}}<{\mathbf{n}}$.
Constraint: ${\mathbf{maxr}}\ge 1$.
 ${\mathbf{ifail}}=5$

Constraint: $\mathit{ldcov}\ge {\mathbf{maxr}}$.
 ${\mathbf{ifail}}=6$

 ${\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
covar 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.
 W ${\mathbf{ifail}}=10$

The number of runs requested were not found, only $\_$ out of the requested $\_$ 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.
 ${\mathbf{ifail}}=399$
Your licence key may have expired or may not have been installed correctly.
 ${\mathbf{ifail}}=999$
Dynamic memory allocation failed.
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.
Further Comments
The time taken by nag_nonpar_randtest_runs (g08ea) increases with the number of observations $n$, and also depends to some extent on whether the call to nag_nonpar_randtest_runs (g08ea) is an only, first, intermediate or last call.
Example
The following program performs a runs up test on $500$ pseudorandom numbers. nag_nonpar_randtest_runs (g08ea) 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.
Open in the MATLAB editor:
g08ea_example
function g08ea_example
fprintf('g08ea example results\n\n');
seed = [int64(324213)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);
m = int64(0);
nruns = int64(0);
ncount = [int64(0);0;0;0;0;0];
n = int64(100);
nsampl = 5;
cl = 'F';
for i=1:nsampl
[state, x, ifail] = g05sq( ...
n, 0, 1, state);
[nruns, ncount, ex, covar, chi, df, prob, ifail] = ...
g08ea( ...
cl, x, m, nruns, ncount);
cl = 'I';
if i==nsampl1
cl = 'L';
end
end
fprintf('Total number of runs found = %d\n', nruns);
fprintf('\n%33s\n', 'Count');
head = ' 1 2 3 4 5 >5';
fprintf('%s\n', head);
fprintf('%9d', ncount);
fprintf('\n\n%34s\n', 'Expect');
fprintf('%s\n', head);
fprintf('%9.1f', ex);
fprintf('\n\n');
[ifail] = x04ca( ...
'General', ' ', covar, 'Covariance matrix');
fprintf('\n\nChisq = %10.4f\n', chi);
fprintf('DF = %7.1f\n', df);
fprintf('Prob = %10.4f\n', prob);
g08ea example results
Total number of runs found = 251
Count
1 2 3 4 5 >5
77 120 39 12 1 2
Expect
1 2 3 4 5 >5
83.8 104.0 45.6 13.1 2.9 0.6
Covariance matrix
1 2 3 4 5 6
1 64.2222 9.8639 7.4780 3.5759 1.1406 0.3305
2 9.8639 70.2942 24.4639 9.8092 2.7386 0.7103
3 7.4780 24.4639 29.9473 5.8284 1.5474 0.3852
4 3.5759 9.8092 5.8284 11.0343 0.5319 0.1289
5 1.1406 2.7386 1.5474 0.5319 2.7169 0.0318
6 0.3305 0.7103 0.3852 0.1289 0.0318 0.5809
Chisq = 9.7559
DF = 6.0
Prob = 0.1353
PDF version (NAG web site
, 64bit version, 64bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015