naginterfaces.library.nonpar.randtest_​runs

naginterfaces.library.nonpar.randtest_runs(cl, x, m, nruns, ncount, comm)[source]

randtest_runs performs a runs up (or a runs down) test on a sequence of observations.

For full information please refer to the NAG Library document for g08ea

https://support.nag.com/numeric/nl/nagdoc_30.2/flhtml/g08/g08eaf.html

Parameters
clstr, length 1

Must specify the type of call to randtest_runs.

This is the one and only call to randtest_runs (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 randtest_runs.

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 randtest_runs.

This is the last call to randtest_runs. The test statistics are computed after the final counting of runs is completed.

xfloat, array-like, shape

The sequence of observations.

mint

The maximum number of runs to be sought. If then no limit is placed on the number of runs that are found.

must not be changed between calls to randtest_runs.

nrunsint

If or , need not be set.

If or , must contain the value returned by the previous call to randtest_runs.

ncountint, array-like, shape

If or , need not be set.

If or , must contain the values returned by the previous call to randtest_runs.

commdict, communication object, modified in place

Communication structure.

On initial entry: need not be set.

Returns
nrunsint

The number of runs actually found.

ncountint, ndarray, shape

The counts of runs of the different lengths, , for .

exfloat, ndarray, shape

If or , (i.e., if it is the final exit) then contains the expected values of the counts, , for .

Otherwise the elements of are not set.

covfloat, ndarray, shape

If or (i.e., if it is the final exit) then contains the covariance matrix of the counts, .

Otherwise the elements of are not set.

chifloat

If or (i.e., if it is the final exit), contains the approximate test statistic, .

Otherwise is not set.

dffloat

If or (i.e., if it is the final exit) then contains the degrees of freedom of the statistic.

Otherwise is not set.

probfloat

If or , (i.e., if it is the final exit) then contains the upper tail probability corresponding to the test statistic, i.e., the significance level.

Otherwise is not set.

Raises
NagValueError
(errno )

On entry, .

Constraint: , , or .

(errno )

On entry, .

Constraint: if , , otherwise .

(errno )

On entry, and .

Constraint: if , .

(errno )

On entry, and .

Constraint: if , .

(errno )

On entry, .

Constraint: .

(errno )

There is a tie in the sequence of observations.

(errno )

The total length of the runs found is less than .

whereas the total length of all runs is .

(errno )

The covariance matrix stored in is not positive definite, thus the approximate test statistic cannot be computed.

This may be because is too large relative to the length of the full sequence.

Warns
NagAlgorithmicWarning
(errno )

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.

Notes

Runs tests may be used to investigate for trends in a sequence of observations. randtest_runs computes statistics for the runs up test. If the runs down test is desired then each observation must be multiplied by before randtest_runs is called with the modified vector of observations. randtest_runs may be used in two different modes:

  1. a single call to randtest_runs which computes all test statistics after counting the runs;

  2. multiple calls to randtest_runs 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 in 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 when and the new run then begins at . randtest_runs 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 randtest_runs (i.e., unless there is another call to randtest_runs to follow) in which case the unfinished run is used together with the beginning of the next sequence of numbers input to randtest_runs in the next call. The following is a trivial example.

Suppose we called randtest_runs twice with the following two sequences:

( ) and

( ).

Then after the second call randtest_runs would have counted the runs up of the following lengths:

, , , , , and .

When the counting of runs is complete randtest_runs 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 randtest_runs will exit before counting any further runs. The number of runs actually counted and used to compute the test statistic is returned via .

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