hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_nonpar_randtest_runs (g08ea)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 xk when xk>xk+1 and the new run then begins at xk+1. nag_nonpar_randtest_runs (g08ea) counts the number of runs up of different lengths. Let ci denote the number of runs of length i, for i=1,2,,r-1. The number of runs of length r or greater is then denoted by cr.
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:
Then after the second call nag_nonpar_randtest_runs (g08ea) would have counted the runs up of the following lengths:
When the counting of runs is complete nag_nonpar_randtest_runs (g08ea) computes the expected values and covariances of the counts, ci. For the details of the method used see Knuth (1981). An approximate χ2 statistic with r degrees of freedom is computed, where
X2= c-μcT Σ c -1 c-μc ,  
where
The use of the χ2-distribution as an approximation to the exact distribution of the test statistic, X2, 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:     cl – string (length ≥ 1)
Must specify the type of call to nag_nonpar_randtest_runs (g08ea).
cl='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'
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'
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'
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', 'F', 'I' or 'L'.
2:     xn – double array
The sequence of observations.
3:     m int64int32nag_int scalar
The maximum number of runs to be sought. If m0 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, cl='S'.
4:     nruns int64int32nag_int scalar
If cl='S' or 'F', nruns need not be set.
If cl='I' or 'L', nruns must contain the value returned by the previous call to nag_nonpar_randtest_runs (g08ea).
5:     ncountmaxr int64int32nag_int array
If cl='S' or 'F', ncount need not be set.
If cl='I' or '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, the length of the current sequence of observations.
Constraints:
  • if cl='S', n3;
  • otherwise n1.
2:     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: maxr1 and if cl='S', maxr<n.

Output Parameters

1:     nruns int64int32nag_int scalar
The number of runs actually found.
2:     ncountmaxr int64int32nag_int array
The counts of runs of the different lengths, ci, for i=1,2,,r.
3:     exmaxr – double array
If cl='S' or 'L', (i.e., if it is the final exit) then ex contains the expected values of the counts, ei, for i=1,2,,r.
Otherwise the elements of ex are not set.
4:     covarldcovmaxr – double array
If cl='S' or 'L' (i.e., if it is the final exit) then covar contains the covariance matrix of the counts, Σc.
Otherwise the elements of covar are not set.
5:     chi – double scalar
If cl='S' or 'L' (i.e., if it is the final exit) then chi contains the approximate χ2 test statistic, X2.
Otherwise chi is not set.
6:     df – double scalar
If cl='S' or 'L' (i.e., if it is the final exit) then df contains the degrees of freedom of the χ2 statistic.
Otherwise df is not set.
7:     prob – double scalar
If cl='S' or 'L', (i.e., if it is the final exit) then prob contains the upper tail probability corresponding to the χ2 test statistic, i.e., the significance level.
Otherwise prob is not set.
8:     ifail int64int32nag_int scalar
ifail=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
On entry, cl=_.
Constraint: cl='S', 'F', 'I' or 'L'.
   ifail=2
Constraint: if cl='S', n3, otherwise n1.
   ifail=3
Constraint: if cl='S', mn.
   ifail=4
Constraint: if cl='S', maxr<n.
Constraint: maxr1.
   ifail=5
Constraint: ldcovmaxr.
   ifail=6
lwrkmaxr is too small.
   ifail=7
There is a tie in the sequence of observations.
   ifail=8
The total length of the runs found is less than maxr.
   ifail=9
The covariance matrix stored in covar is not positive definite, thus the approximate χ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
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.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   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.
function g08ea_example


fprintf('g08ea example results\n\n');

% Initialize the base generator to a repeatable sequence
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
  % Generate a sample from U(0,1)
  [state, x, ifail] = g05sq( ...
                             n, 0, 1, state);
  % Process the sample
  [nruns, ncount, ex, covar, chi, df, prob, ifail] = ...
  g08ea( ...
         cl, x, m, nruns, ncount);
  % Adjust CL
  cl = 'I';
  if i==nsampl-1
     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, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015