The function may be called by the names: g08edc, nag_nonpar_randtest_gaps or nag_gaps_test.
3Description
Gaps tests are used to test for cyclical trend in a sequence of observations. 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 $({r}_{l},{r}_{u})$. That is, a gap ends at ${x}_{j}$ if ${r}_{l}\le {x}_{j}\le {r}_{u}$. The next gap then begins at ${x}_{j+1}$. The interval $({r}_{l},{r}_{u})$ 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 $({r}_{l},{r}_{u})$ must be contained in the whole interval $(0,1)$. Let ${t}_{\text{len}}$ be the length of the interval which specifies all possible numbers.
g08edc counts the number of gaps of different lengths. Let ${c}_{\mathit{i}}$ denote the number of gaps of length $\mathit{i}$, for $\mathit{i}=1,2,\dots ,k-1$. The number of gaps of length $k$ or greater is then denoted by ${c}_{k}$. An unfinished gap at the end of a sequence is not counted. The following is a trivial example.
Suppose we called g08edc with the following sequence and with ${r}_{l}=0.30$ and ${r}_{u}=0.60$:
g08edc would have counted the gaps of the following lengths:
$2$, $1$, $1$, $6$, $3$ and $1$.
When the counting of gaps is complete g08edc computes the expected values of the counts. An approximate ${\chi}^{2}$ statistic with $k$ degrees of freedom is computed where
${e}_{i}=\mathit{ngaps}\times p\times {(1-p)}^{i-1}$, if $i<k$;
${e}_{i}=\mathit{ngaps}\times {(1-p)}^{i-1}$, if $i=k$;
$\mathit{ngaps}=\text{}$ the number of gaps found and
$p=({r}_{u}-{r}_{l})/{t}_{\text{len}}$.
The use of the ${\chi}^{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 g08edc will exit before counting any further gaps.
4References
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
5Arguments
1: $\mathbf{n}$ – IntegerInput
On entry: $n$, the length of the current sequence of observations.
On entry: the lower limit of the interval to be used to define the gaps, ${r}_{l}$.
6: $\mathbf{upper}$ – doubleInput
On entry: the upper limit of the interval to be used to define the gaps, ${r}_{u}$.
Constraint:
${\mathbf{upper}}>{\mathbf{lower}}$.
7: $\mathbf{length}$ – doubleInput
On entry: the total length of the interval which contains all possible numbers that may arise in the sequence.
Constraint:
${\mathbf{length}}>0.0$ and ${\mathbf{upper}}-{\mathbf{lower}}<{\mathbf{length}}$.
8: $\mathbf{chi}$ – double *Output
On exit: contains the ${\chi}^{2}$ test statistic, ${X}^{2}$, for testing the null hypothesis of randomness.
9: $\mathbf{df}$ – double *Output
On exit: contains the degrees of freedom for the ${\chi}^{2}$ statistic.
10: $\mathbf{prob}$ – double *Output
On exit: contains the upper tail probability associated with the ${\chi}^{2}$ test statistic, i.e., the significance level.
11: $\mathbf{fail}$ – NagError *Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).
6Error Indicators and Warnings
NE_2_INT_ARG_GT
On entry, ${\mathbf{num\_gaps}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{num\_gaps}}\le {\mathbf{n}}$.
NE_2_REAL_ARG_GE
On entry, ${\mathbf{lower}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{upper}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{upper}}>{\mathbf{lower}}$.
NE_3_REAL_ARG_CONS
On entry, ${\mathbf{lower}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{upper}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{length}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{upper}}-{\mathbf{lower}}<{\mathbf{length}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_BAD_PARAM
On entry, argument $\u27e8\mathit{\text{value}}\u27e9$ 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 ${\chi}^{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=\u27e8\mathit{\text{value}}\u27e9$ is zero. The value of $({\mathbf{upper}}-{\mathbf{lower}})/{\mathbf{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 $\u27e8\mathit{\text{value}}\u27e9$ out of the requested $\u27e8\mathit{\text{value}}\u27e9$ 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 ${\mathbf{upper}}-{\mathbf{lower}}$.
NE_INT_2
On entry, ${\mathbf{max\_gap}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: $1<{\mathbf{max\_gap}}\le {\mathbf{n}}$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{n}}\ge 1$.
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 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL_ARG_LE
On entry, ${\mathbf{length}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{length}}>0.0$.
7Accuracy
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.
8Parallelism and Performance
g08edc is not threaded in any implementation.
9Further Comments
The time taken by g08edc increases with the number of observations $n$.
10Example
The following program performs the gaps test on $5000$ pseudorandom numbers taken from a uniform distribution $U(0,1)$, generated by g05sqc. All gaps of length $10$ or more are counted together.