NAG Library Function Document
nag_runs_test (g08eac) performs a runs up (or a runs down) test on a sequence of observations.
||nag_runs_test (Integer n,
const double x,
Runs tests may be used to investigate for trends in a sequence of observations. nag_runs_test (g08eac) computes statistics for the runs up test. If the runs down test is desired then each observation must be multiplied by before nag_runs_test (g08eac) is called with the modified vector of observations.
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_runs_test (g08eac) 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. The following is a trivial example.
Suppose we called nag_runs_test (g08eac) with the following sequence:
- 0.20 0.40 0.45 0.40 0.15 0.75 0.95 0.23 0.27 0.40 0.25 0.10 0.34 0.39 0.61 0.12.
Then nag_runs_test (g08eac) would have counted the runs up of the following lengths:
When the counting of runs is complete nag_runs_test (g08eac) computes the expected values and covariances of the counts,
. For the details of the method used see Knuth (1981)
. An approximate
degrees of freedom is computed, 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 expected values increase.
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_runs_test (g08eac) will exit before counting any further runs. The number of runs actually counted and used to compute the test statistic is returned via nruns
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
n – IntegerInput
On entry: the length of the current sequence of observations, .
x[n] – const doubleInput
On entry: the sequence of observations.
max_run – IntegerInput
On entry: 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.
nruns – Integer *Output
On exit: the number of runs actually found.
chi – double *Output
On exit: contains the approximate test statistic, .
df – double *Output
On exit: contains the degrees of freedom of the statistic.
prob – double *Output
On exit: contains the upper tail probability corresponding to the test statistic, i.e., the significance level.
fail – NagError *Input/Output
The NAG error argument (see Section 3.6
in the Essential Introduction).
6 Error Indicators and Warnings
On entry, while . These arguments must satisfy .
Dynamic memory allocation failed.
Internally computed covariance matrix is not positive definite. This may be because the value of max_run
is too large relative to the full length of the series. Thus the approximate
test statistic cannot be computed.
The number of runs requested were not found. All statistics are still computed and the information returned may still be of use.
The total length of the runs found is less than max_run
There is a tie in the sequence of observations.
On entry, max_run
must not be less than 1:
On entry, .
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG
The computations are believed to be stable. The computation of prob
given the values of chi
will obtain a relative accuracy of five significant figures for most cases.
The time taken by nag_runs_test (g08eac) increases with the number of observations .
The following program performs a runs up test on 10000 pseudorandom numbers taken from a uniform distribution
, generated by nag_rand_uniform (g05sqc)
. All runs of length 6 or more are counted together.
9.1 Program Text
Program Text (g08eace.c)
9.2 Program Data
9.3 Program Results
Program Results (g08eace.r)