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)

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 xkxk when xk > xk + 1xk>xk+1 and the new run then begins at xk + 1xk+1. nag_nonpar_randtest_runs (g08ea) counts the number of runs up of different lengths. Let cici denote the number of runs of length ii, for i = 1,2,,r1i=1,2,,r-1. The number of runs of length rr or greater is then denoted by crcr.
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, cici. For the details of the method used see Knuth (1981). An approximate χ2χ2 statistic with rr degrees of freedom is computed, where
X2 = (cμc)T Σc1 (cμc) ,
X2= (c-μc)T Σ c -1 (c-μc) ,
where
The use of the χ2χ2-distribution as an approximation to the exact distribution of the test statistic, X2X2, improves as the length of the sequence relative to mm increases and hence the expected value, ee, 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'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'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'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'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'cl='S', 'F''F', 'I''I' or 'L''L'.
2:     x(n) – double array
n, the dimension of the array, must satisfy the constraint
  • if cl = 'S'cl='S', n3n3;
  • otherwise n1n1.
The sequence of observations.
3:     m – int64int32nag_int scalar
The maximum number of runs to be sought. If m0m0 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 mnmn, cl = 'S'cl='S'.
4:     nruns – int64int32nag_int scalar
If cl = 'S'cl='S' or 'F''F', nruns need not be set.
If cl = 'I'cl='I' or 'L''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 maxr1maxr1 and if cl = 'S'cl='S', maxr < nmaxr<n.
If cl = 'S'cl='S' or 'F''F', ncount need not be set.
If cl = 'I'cl='I' or 'L''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.
nn, the length of the current sequence of observations.
Constraints:
  • if cl = 'S'cl='S', n3n3;
  • otherwise n1n1.
2:     maxr – int64int32nag_int scalar
Default: The dimension of the array ncount.
rr, the length of the longest run for which tabulation is desired. That is, all runs with length greater than or equal to rr are counted together.
maxr must not be changed between calls to nag_nonpar_randtest_runs (g08ea).
Constraint: maxr1maxr1 and if cl = 'S'cl='S', maxr < nmaxr<n.

Input Parameters Omitted from the MATLAB Interface

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, cici, for i = 1,2,,ri=1,2,,r.
3:     ex(maxr) – double array
If cl = 'S'cl='S' or 'L''L', (i.e., if it is the final exit) then ex contains the expected values of the counts, eiei, for i = 1,2,,ri=1,2,,r.
Otherwise the elements of ex are not set.
4:     cov(ldcov,maxr) – double array
ldcovmaxrldcovmaxr.
If cl = 'S'cl='S' or 'L''L' (i.e., if it is the final exit) then cov contains the covariance matrix of the counts, ΣcΣc.
Otherwise the elements of cov are not set.
5:     chi – double scalar
If cl = 'S'cl='S' or 'L''L' (i.e., if it is the final exit) then chi contains the approximate χ2χ2 test statistic, X2X2.
Otherwise chi is not set.
6:     df – double scalar
If cl = 'S'cl='S' or 'L''L' (i.e., if it is the final exit) then df contains the degrees of freedom of the χ2χ2 statistic.
Otherwise df is not set.
7:     prob – double scalar
If cl = 'S'cl='S' or 'L''L', (i.e., if it is the final exit) then prob contains the upper tail probability corresponding to the χ2χ2 test statistic, i.e., the significance level.
Otherwise prob is not set.
8:     ifail – int64int32nag_int scalar
ifail = 0ifail=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 = 1ifail=1
On entry, cl = _cl=_.
Constraint: cl = 'S'cl='S', 'F''F', 'I''I' or 'L''L'.
  ifail = 2ifail=2
Constraint: if cl = 'S'cl='S', n3n3, otherwise n1n1.
  ifail = 3ifail=3
Constraint: if cl = 'S'cl='S', mnmn.
  ifail = 4ifail=4
Constraint: if cl = 'S'cl='S', maxr < nmaxr<n.
Constraint: maxr1maxr1.
  ifail = 5ifail=5
Constraint: ldcovmaxrldcovmaxr.
  ifail = 6ifail=6
lwrkmaxr is too small.
  ifail = 7ifail=7
There is a tie in the sequence of observations.
  ifail = 8ifail=8
The total length of the runs found is less than maxr.
  ifail = 9ifail=9
The covariance matrix stored in cov is not positive definite, thus the approximate χ2χ2 test statistic cannot be computed.
This may be because maxr is too large relative to the length of the full sequence.
W ifail = 10ifail=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.

Further Comments

The time taken by nag_nonpar_randtest_runs (g08ea) increases with the number of observations nn, 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


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–2013