NAG C Library Function Document

nag_gaps_test (g08edc)

1
Purpose

nag_gaps_test (g08edc) performs a gaps test on a sequence of observations.

2
Specification

#include <nag.h>
#include <nagg08.h>
void  nag_gaps_test (Integer n, const double x[], Integer num_gaps, Integer max_gap, double lower, double upper, double length, double *chi, double *df, double *prob, NagError *fail)

3
Description

Gaps tests are used to test for cyclical trend in a sequence of observations. nag_gaps_test (g08edc) computes certain statistics for the gaps test.
The term gap is used to describe the distance between two numbers in the sequence that lie in the interval rl,ru. That is, a gap ends at xj if rlxjru. The next gap then begins at xj+1. The interval rl,ru should lie within the region of all possible numbers. For example if the test is carried out on a sequence of 0,1 random numbers then the interval rl,ru must be contained in the whole interval 0,1. Let tlen be the length of the interval which specifies all possible numbers.
nag_gaps_test (g08edc) counts the number of gaps of different lengths. Let ci denote the number of gaps of length i, for i=1,2,,k-1. The number of gaps of length k or greater is then denoted by ck. An unfinished gap at the end of a sequence is not counted. The following is a trivial example.
Suppose we called nag_gaps_test (g08edc) with the following sequence and with rl=0.30 and ru=0.60:
nag_gaps_test (g08edc) would have counted the gaps of the following lengths:
When the counting of gaps is complete nag_gaps_test (g08edc) computes the expected values of the counts. An approximate χ2 statistic with k degrees of freedom is computed where
X2=i=1k ci-ei 2ei,  
where
The use of the χ2-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 gaps to be found. If the specified number of gaps is found before the end of a sequence nag_gaps_test (g08edc) will exit before counting any further gaps.

4
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

5
Arguments

1:     n IntegerInput
On entry: n, the length of the current sequence of observations.
Constraint: n1.
2:     x[n] const doubleInput
On entry: the sequence of observations.
3:     num_gaps IntegerInput
On entry: the maximum number of gaps to be sought. If num_gaps0 then there is no limit placed on the number of gaps that are found.
Constraint: num_gapsn.
4:     max_gap IntegerInput
On entry: k, the length of the longest gap for which tabulation is desired.
Constraint: 1<max_gapn.
5:     lower doubleInput
On entry: the lower limit of the interval to be used to define the gaps, rl.
6:     upper doubleInput
On entry: the upper limit of the interval to be used to define the gaps, ru.
Constraint: upper>lower.
7:     length doubleInput
On entry: the total length of the interval which contains all possible numbers that may arise in the sequence.
Constraint: length>0.0 and upper-lower<length.
8:     chi double *Output
On exit: contains the χ2 test statistic, X2, for testing the null hypothesis of randomness.
9:     df double *Output
On exit: contains the degrees of freedom for the χ2 statistic.
10:   prob double *Output
On exit: contains the upper tail probability associated with the χ2 test statistic, i.e., the significance level.
11:   fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

6
Error Indicators and Warnings

NE_2_INT_ARG_GT
On entry, num_gaps=value and n=value.
Constraint: num_gapsn.
NE_2_REAL_ARG_GE
On entry, lower=value and upper=value.
Constraint: upper>lower.
NE_3_REAL_ARG_CONS
On entry, lower=value, upper=value and length=value.
Constraint: upper-lower<length.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_G08ED_FREQ_LT_ONE
The expected frequency of at least one class is less than 1.
This implies that the χ2 may not be a very good approximation to the distribution of the test statistics.
All statistics are returned and may still be of use.
NE_G08ED_FREQ_ZERO
The expected frequency in class i=value is zero. The value of upper-lower/length may be too close to 0.0 or 1.0. or max_gap is too large relative to the number of gaps found.
NE_G08ED_GAPS
The number of gaps requested were not found, only value out of the requested value where found.
All statistics are returned and may still be of use.
NE_G08ED_GAPS_ZERO
No gaps were found. Try using a longer sequence, or increase the size of the interval upper-lower.
NE_INT_2
On entry, max_gap=value and n=value.
Constraint: 1<max_gapn.
NE_INT_ARG_LT
On entry, n=value.
Constraint: n1.
NE_INTERNAL_ERROR
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 for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
NE_REAL_ARG_LE
On entry, length=value.
Constraint: length>0.0.

7
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 places for most cases.

8
Parallelism and Performance

nag_gaps_test (g08edc) is not threaded in any implementation.

9
Further Comments

The time taken by nag_gaps_test (g08edc) increases with the number of observations n.

10
Example

The following program performs the gaps test on 5000 pseudorandom numbers taken from a uniform distribution U0,1, generated by nag_rand_uniform (g05sqc). All gaps of length 10 or more are counted together.

10.1
Program Text

Program Text (g08edce.c)

10.2
Program Data

None.

10.3
Program Results

Program Results (g08edce.r)