The routine may be called by the names g08edf or nagf_nonpar_randtest_gaps.
3Description
Gaps tests are used to test for cyclical trend in a sequence of observations. g08edf computes certain statistics for the gaps test.
g08edf may be used in two different modes:
(i)a single call to g08edf which computes all test statistics after counting the gaps;
(ii)multiple calls to g08edf with the final test statistics only being computed in the last call.
The second mode is necessary if all the data does not fit into the memory. See argument cl in Section 5 for details on how to invoke each mode.
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.
g08edf 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 unless the sequence is part of an initial or intermediate call to g08edf (i.e., unless there is another call to g08edf to follow) in which case the unfinished gap is used. The following is a trivial example.
Suppose we called g08edf twice (i.e., with ${\mathbf{cl}}=\text{'F'}$ and then with ${\mathbf{cl}}=\text{'L'}$) with the following two sequences and with ${\mathbf{rlo}}=0.30$ and ${\mathbf{rup}}=0.60$:
($0.20$$0.40$$0.45$$0.40$$0.15$$0.75$$0.95$$0.23$) and
Then after the second call g08edf would have counted the gaps of the following lengths:
$2$, $1$, $1$, $6$, $3$ and $1$.
When the counting of gaps is complete g08edf 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 g08edf 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{cl}$ – Character(1)Input
On entry: indicates the type of call to g08edf.
${\mathbf{cl}}=\text{'S'}$
This is the one and only call to g08edf (single call mode). All data are to be input at once. All test statistics are computed after the counting of gaps is complete.
${\mathbf{cl}}=\text{'F'}$
This is the first call to the routine. All initializations are carried out before the counting of gaps begins. The final test statistics are not computed since further calls will be made to g08edf.
${\mathbf{cl}}=\text{'I'}$
This is an intermediate call during which the counts of gaps are updated. The final test statistics are not computed since further calls will be made to g08edf.
${\mathbf{cl}}=\text{'L'}$
This is the last call to g08edf. The test statistics are computed after the final counting of gaps is complete.
Constraint:
${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
2: $\mathbf{n}$ – IntegerInput
On entry: $n$, the length of the current sequence of observations.
Constraint:
${\mathbf{n}}\ge 1$.
3: $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the sequence of observations.
4: $\mathbf{m}$ – IntegerInput
On entry: the maximum number of gaps to be sought. If ${\mathbf{m}}\le 0$ then there is no limit placed on the number of gaps that are found.
On exit: if ${\mathbf{cl}}=\text{'S'}$ or $\text{'L'}$ (i.e., if it is a final exit) then chi contains the ${\chi}^{2}$ test statistic, ${X}^{2}$, for testing the null hypothesis of randomness.
On exit: if ${\mathbf{cl}}=\text{'S'}$ or $\text{'L'}$ (i.e., if it is a final exit) then df contains the degrees of freedom for the ${\chi}^{2}$ statistic.
On exit: if ${\mathbf{cl}}=\text{'S'}$ or $\text{'L'}$ (i.e., if it is a final exit) then prob contains the upper tail probability associated with the ${\chi}^{2}$ test statistic, i.e., the significance level.
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $-1$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
Note: in some cases g08edf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{cl}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{m}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{m}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{maxg}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{maxg}}>1$.
On entry, ${\mathbf{maxg}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxg}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{rlo}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{rup}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{totlen}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{rup}}-{\mathbf{rlo}}<{\mathbf{totlen}}$.
On entry, ${\mathbf{rlo}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{rup}}=\u27e8\mathit{\text{value}}\u27e9$. Constraint: ${\mathbf{rup}}>{\mathbf{rlo}}$.
On entry, ${\mathbf{totlen}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{totlen}}>0.0$.
${\mathbf{ifail}}=6$
No gaps were found. Try using a longer sequence, or increase the size of the interval ${\mathbf{rup}}-{\mathbf{rlo}}$.
${\mathbf{ifail}}=7$
The expected frequency in class $i=\u27e8\mathit{\text{value}}\u27e9$ is zero. The value of $({\mathbf{rup}}-{\mathbf{rlo}})/{\mathbf{totlen}}$ may be too close to $0.0$ or $1.0$. or maxg is too large relative to the number of gaps found.
${\mathbf{ifail}}=8$
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.
${\mathbf{ifail}}=9$
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.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
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
g08edf is not thread safe and should not be called from a multithreaded user program. Please see Section 1 in FL Interface Multithreading for more information on thread safety.
g08edf is not threaded in any implementation.
9Further Comments
The time taken by g08edf increases with the number of observations $n$, and depends to some extent whether the call is an only, first, intermediate or last call.
10Example
The following program performs the gaps test on $500$ pseudorandom numbers. g08edf is called $5$ times with $100$ observations on each call. All gaps of length $10$ or more are counted together.