Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# 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, cov, chi, df, prob, ifail] = g08ea(cl, x, m, nruns, ncount, 'n', n, 'maxr', maxr)
[nruns, ncount, ex, cov, 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$-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 parameter cl in Section [Parameters] 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${x}_{k}$ when xk > xk + 1${x}_{k}>{x}_{k+1}$ and the new run then begins at xk + 1${x}_{k+1}$. nag_nonpar_randtest_runs (g08ea) counts the number of runs up of different lengths. Let ci${c}_{\mathit{i}}$ denote the number of runs of length i$\mathit{i}$, for i = 1,2,,r1$\mathit{i}=1,2,\dots ,r-1$. The number of runs of length r$r$ or greater is then denoted by cr${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.20$ 0.40$0.40$ 0.45$0.45$ 0.40$0.40$ 0.15$0.15$ 0.75$0.75$ 0.95$0.95$ 0.23$0.23$) and
• (0.27$0.27$ 0.40$0.40$ 0.25$0.25$ 0.10$0.10$ 0.34$0.34$ 0.39$0.39$ 0.61$0.61$ 0.12$0.12$).
Then after the second call nag_nonpar_randtest_runs (g08ea) would have counted the runs up of the following lengths:
• 3, 1$1$, 3$3$, 3$3$, 1$1$, and 4$4$.
When the counting of runs is complete nag_nonpar_randtest_runs (g08ea) computes the expected values and covariances of the counts, ci${c}_{i}$. For the details of the method used see Knuth (1981). An approximate χ2${\chi }^{2}$ statistic with r$r$ degrees of freedom is computed, where
 X2 = (c − μc)T Σc − 1 (c − μc) , $X2= (c-μc)T Σ c -1 (c-μc) ,$
where
• c$c$ is the vector of counts, ci${c}_{\mathit{i}}$, for i = 1,2,,r$\mathit{i}=1,2,\dots ,r$,
• μc${\mu }_{c}$ is the vector of expected values,
• ei${e}_{\mathit{i}}$, for i = 1,2,,r$\mathit{i}=1,2,\dots ,r$, where ei${e}_{i}$ is the expected value for ci${c}_{i}$ under the null hypothesis of randomness, and
• Σc${\Sigma }_{c}$ is the covariance matrix of c$c$ under the null hypothesis.
The use of the χ2${\chi }^{2}$-distribution as an approximation to the exact distribution of the test statistic, X2${X}^{2}$, improves as the length of the sequence relative to m$m$ increases and hence the expected value, e$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:     cl – string (length ≥ 1)
Must specify the type of call to nag_nonpar_randtest_runs (g08ea).
cl = 'S'${\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.
cl = 'F'${\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).
cl = 'I'${\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).
cl = 'L'${\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: cl = 'S'${\mathbf{cl}}=\text{'S'}$, 'F'$\text{'F'}$, 'I'$\text{'I'}$ or 'L'$\text{'L'}$.
2:     x(n) – double array
n, the dimension of the array, must satisfy the constraint
• if cl = 'S'${\mathbf{cl}}=\text{'S'}$, n3${\mathbf{n}}\ge 3$;
• otherwise n1${\mathbf{n}}\ge 1$.
The sequence of observations.
3:     m – int64int32nag_int scalar
The maximum number of runs to be sought. If m0${\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 mn${\mathbf{m}}\le {\mathbf{n}}$, cl = 'S'${\mathbf{cl}}=\text{'S'}$.
4:     nruns – int64int32nag_int scalar
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'F'$\text{'F'}$, nruns need not be set.
If cl = 'I'${\mathbf{cl}}=\text{'I'}$ or 'L'$\text{'L'}$, nruns must contain the value returned by the previous call to nag_nonpar_randtest_runs (g08ea).
5:     ncount(maxr) – int64int32nag_int array
maxr, the dimension of the array, must satisfy the constraint maxr1${\mathbf{maxr}}\ge 1$ and if cl = 'S'${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxr}}<{\mathbf{n}}$.
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'F'$\text{'F'}$, ncount need not be set.
If cl = 'I'${\mathbf{cl}}=\text{'I'}$ or 'L'$\text{'L'}$, ncount must contain the values returned by the previous call to nag_nonpar_randtest_runs (g08ea).

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array x.
n$n$, the length of the current sequence of observations.
Constraints:
• if cl = 'S'${\mathbf{cl}}=\text{'S'}$, n3${\mathbf{n}}\ge 3$;
• otherwise n1${\mathbf{n}}\ge 1$.
2:     maxr – int64int32nag_int scalar
Default: The dimension of the array ncount.
r$r$, the length of the longest run for which tabulation is desired. That is, all runs with length greater than or equal to r$r$ are counted together.
maxr must not be changed between calls to nag_nonpar_randtest_runs (g08ea).
Constraint: maxr1${\mathbf{maxr}}\ge 1$ and if cl = 'S'${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxr}}<{\mathbf{n}}$.

ldcov wrk lwrk

### Output Parameters

1:     nruns – int64int32nag_int scalar
The number of runs actually found.
2:     ncount(maxr) – int64int32nag_int array
The counts of runs of the different lengths, ci${c}_{\mathit{i}}$, for i = 1,2,,r$\mathit{i}=1,2,\dots ,r$.
3:     ex(maxr) – double array
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$, (i.e., if it is the final exit) then ex contains the expected values of the counts, ei${e}_{\mathit{i}}$, for i = 1,2,,r$\mathit{i}=1,2,\dots ,r$.
Otherwise the elements of ex are not set.
4:     cov(ldcov,maxr) – double array
ldcovmaxr$\mathit{ldcov}\ge {\mathbf{maxr}}$.
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$ (i.e., if it is the final exit) then cov contains the covariance matrix of the counts, Σc${\Sigma }_{c}$.
Otherwise the elements of cov are not set.
5:     chi – double scalar
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$ (i.e., if it is the final exit) then chi contains the approximate χ2${\chi }^{2}$ test statistic, X2${X}^{2}$.
Otherwise chi is not set.
6:     df – double scalar
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$ (i.e., if it is the final exit) then df contains the degrees of freedom of the χ2${\chi }^{2}$ statistic.
Otherwise df is not set.
7:     prob – double scalar
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$, (i.e., if it is the final exit) then prob contains the upper tail probability corresponding to the χ2${\chi }^{2}$ test statistic, i.e., the significance level.
Otherwise prob is not set.
8:     ifail – int64int32nag_int scalar
${\mathrm{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.

ifail = 1${\mathbf{ifail}}=1$
On entry, cl = _${\mathbf{cl}}=_$.
Constraint: cl = 'S'${\mathbf{cl}}=\text{'S'}$, 'F'$\text{'F'}$, 'I'$\text{'I'}$ or 'L'$\text{'L'}$.
ifail = 2${\mathbf{ifail}}=2$
Constraint: if cl = 'S'${\mathbf{cl}}=\text{'S'}$, n3${\mathbf{n}}\ge 3$, otherwise n1${\mathbf{n}}\ge 1$.
ifail = 3${\mathbf{ifail}}=3$
Constraint: if cl = 'S'${\mathbf{cl}}=\text{'S'}$, mn${\mathbf{m}}\le {\mathbf{n}}$.
ifail = 4${\mathbf{ifail}}=4$
Constraint: if cl = 'S'${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxr}}<{\mathbf{n}}$.
Constraint: maxr1${\mathbf{maxr}}\ge 1$.
ifail = 5${\mathbf{ifail}}=5$
Constraint: ldcovmaxr$\mathit{ldcov}\ge {\mathbf{maxr}}$.
ifail = 6${\mathbf{ifail}}=6$
lwrkmaxr is too small.
ifail = 7${\mathbf{ifail}}=7$
There is a tie in the sequence of observations.
ifail = 8${\mathbf{ifail}}=8$
The total length of the runs found is less than maxr.
ifail = 9${\mathbf{ifail}}=9$
The covariance matrix stored in cov is not positive definite, thus the approximate χ2${\chi }^{2}$ test statistic cannot be computed.
This may be because maxr is too large relative to the length of the full sequence.
W ifail = 10${\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.

## 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 nag_nonpar_randtest_runs (g08ea) increases with the number of observations n$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

```function nag_nonpar_randtest_runs_example
% Initialize the seed
seed = [int64(324213)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

m = int64(0);
nruns = int64(0);
ncount = [int64(0);0;0;0;0;0];
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
for i=1:10
% Generate some U(0,1) values
[state, x, ifail] = nag_rand_dist_uniform(int64(1000), 0, 1, state);
if i==1
cl = 'F';
elseif i==10
cl = 'L';
else
cl = 'I';
end
[nruns, ncount, ex, covar, chi, df, prob, ifail] = ...
nag_nonpar_randtest_runs(cl, x, m, nruns, ncount);
end
if (ifail == 0 || ifail == 10)
fprintf('\nTotal number of runs found = %d\n', nruns);
if ifail == 10
fprintf(' ** Note : the number of runs requested were not found.\n');
end
fprintf('\n                             Count\n');
fprintf('          1        2        3        4        5       >5\n  ');
for i=1:numel(ncount)
fprintf('%9d', ncount(i));
end
fprintf('\n\n                             Expect\n');
fprintf('          1        2        3        4        5       >5\n  ');
for i=1:numel(ex)
fprintf('%9.2f', ex(i));
end
fprintf('\n\n                       Covariance matrix\n');
fprintf('          1        2        3        4        5       >5\n');
[mc, nc] = size(covar);
for i = 1:mc
fprintf('%2d', i);
for j = 1:nc
fprintf('%9.2f', covar(i,j));
end
fprintf('\n');
end
fprintf('\nChisq = %10.4f\n', chi);
fprintf('DF    = %7.1f\n', df);
fprintf('Prob  = %10.4f\n', prob);
end
```
```

Total number of runs found = 5024

Count
1        2        3        4        5       >5
1703     2072      919      268       51       11

Expect
1        2        3        4        5       >5
1667.17  2083.17   916.46   263.80    57.51    11.90

Covariance matrix
1        2        3        4        5       >5
1  1278.11  -194.59  -148.85   -71.59   -22.92    -6.67
2  -194.59  1410.00  -490.51  -197.23   -55.20   -14.36
3  -148.85  -490.51   601.32  -117.41   -31.23    -7.79
4   -71.59  -197.23  -117.41   222.05   -10.75    -2.61
5   -22.92   -55.20   -31.23   -10.75    54.81    -0.65
6    -6.67   -14.36    -7.79    -2.61    -0.65    11.75

Chisq =     1.8717
DF    =     6.0
Prob  =     0.9311

```
```function g08ea_example
% Initialize the seed
seed = [int64(324213)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

m = int64(0);
nruns = int64(0);
ncount = [int64(0);0;0;0;0;0];
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
for i=1:10
% Generate some U(0,1) values
[state, x, ifail] = g05sq(int64(1000), 0, 1, state);
if i==1
cl = 'F';
elseif i==10
cl = 'L';
else
cl = 'I';
end
[nruns, ncount, ex, covar, chi, df, prob, ifail] = g08ea(cl, x, m, nruns, ncount);
end
if (ifail == 0 || ifail == 10)
fprintf('\nTotal number of runs found = %d\n', nruns);
if ifail == 10
fprintf(' ** Note : the number of runs requested were not found.\n');
end
fprintf('\n                             Count\n');
fprintf('          1        2        3        4        5       >5\n  ');
for i=1:numel(ncount)
fprintf('%9d', ncount(i));
end
fprintf('\n\n                             Expect\n');
fprintf('          1        2        3        4        5       >5\n  ');
for i=1:numel(ex)
fprintf('%9.2f', ex(i));
end
fprintf('\n\n                       Covariance matrix\n');
fprintf('          1        2        3        4        5       >5\n');
[mc, nc] = size(covar);
for i = 1:mc
fprintf('%2d', i);
for j = 1:nc
fprintf('%9.2f', covar(i,j));
end
fprintf('\n');
end
fprintf('\nChisq = %10.4f\n', chi);
fprintf('DF    = %7.1f\n', df);
fprintf('Prob  = %10.4f\n', prob);
end
```
```

Total number of runs found = 5024

Count
1        2        3        4        5       >5
1703     2072      919      268       51       11

Expect
1        2        3        4        5       >5
1667.17  2083.17   916.46   263.80    57.51    11.90

Covariance matrix
1        2        3        4        5       >5
1  1278.11  -194.59  -148.85   -71.59   -22.92    -6.67
2  -194.59  1410.00  -490.51  -197.23   -55.20   -14.36
3  -148.85  -490.51   601.32  -117.41   -31.23    -7.79
4   -71.59  -197.23  -117.41   222.05   -10.75    -2.61
5   -22.92   -55.20   -31.23   -10.75    54.81    -0.65
6    -6.67   -14.36    -7.79    -2.61    -0.65    11.75

Chisq =     1.8717
DF    =     6.0
Prob  =     0.9311

```