PDF version (NAG web site
, 64-bit version, 64-bit 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
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 when and the new run then begins at . nag_nonpar_randtest_runs (g08ea) counts the number of runs up of different lengths. Let denote the number of runs of length , for . The number of runs of length or greater is then denoted by .
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:
- ( ) and
- ( ).
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,
. For the details of the method used see
Knuth (1981). An approximate
statistic with
degrees of freedom is computed, where
where
- is the vector of counts, , for ,
- is the vector of expected values,
- , for , where is the expected value for under the null hypothesis of randomness, and
- is the covariance matrix of under the null hypothesis.
The use of the -distribution as an approximation to the exact distribution of the test statistic, , improves as the length of the sequence relative to increases and hence the expected value, , 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:
– string (length ≥ 1)
-
Must specify the type of call to
nag_nonpar_randtest_runs (g08ea).
- 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.
- 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).
- 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).
- 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:
, , or .
- 2:
– double array
-
The sequence of observations.
- 3:
– int64int32nag_int scalar
-
The maximum number of runs to be sought. If
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 , .
- 4:
– int64int32nag_int scalar
-
If
or
,
nruns need not be set.
If
or
,
nruns must contain the value returned by the previous call to
nag_nonpar_randtest_runs (g08ea).
- 5:
– int64int32nag_int array
-
If
or
,
ncount need not be set.
If
or
,
ncount must contain the values returned by the previous call to
nag_nonpar_randtest_runs (g08ea).
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the array
x.
, the length of the current sequence of observations.
Constraints:
- if , ;
- otherwise .
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the array
ncount.
, the length of the longest run for which tabulation is desired. That is, all runs with length greater than or equal to
are counted together.
maxr must not be changed between calls to
nag_nonpar_randtest_runs (g08ea).
Constraint:
and if , .
Output Parameters
- 1:
– int64int32nag_int scalar
-
The number of runs actually found.
- 2:
– int64int32nag_int array
-
The counts of runs of the different lengths,
, for .
- 3:
– double array
-
If
or
, (i.e., if it is the final exit) then
ex contains the expected values of the counts,
, for
.
Otherwise the elements of
ex are not set.
- 4:
– double array
-
If
or
(i.e., if it is the final exit) then
covar contains the covariance matrix of the counts,
.
Otherwise the elements of
covar are not set.
- 5:
– double scalar
-
If
or
(i.e., if it is the final exit) then
chi contains the approximate
test statistic,
.
Otherwise
chi is not set.
- 6:
– double scalar
-
If
or
(i.e., if it is the final exit) then
df contains the degrees of freedom of the
statistic.
- 7:
– double scalar
-
If
or
, (i.e., if it is the final exit) then
prob contains the upper tail probability corresponding to the
test statistic, i.e., the significance level.
Otherwise
prob is not set.
- 8:
– int64int32nag_int scalar
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.
-
-
On entry, .
Constraint: , , or .
-
-
Constraint: if , , otherwise .
-
-
Constraint: if , .
-
-
Constraint: if , .
Constraint: .
-
-
Constraint: .
-
-
-
-
There is a tie in the sequence of observations.
-
-
The total length of the runs found is less than
maxr.
-
-
The covariance matrix stored in
covar is not positive definite, thus the approximate
test statistic cannot be computed.
This may be because
maxr is too large relative to the length of the full sequence.
- W
-
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.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
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 , 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 pseudorandom numbers. nag_nonpar_randtest_runs (g08ea) is called times with observations each time. No limit is placed on the number of runs to be counted. All runs of length 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==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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015