G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG08EDF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G08EDF performs a gaps test on a sequence of observations.

## 2  Specification

 SUBROUTINE G08EDF ( CL, N, X, M, MAXG, RLO, RUP, TOTLEN, NGAPS, NCOUNT, EX, CHI, DF, PROB, IFAIL)
 INTEGER N, M, MAXG, NGAPS, NCOUNT(MAXG), IFAIL REAL (KIND=nag_wp) X(N), RLO, RUP, TOTLEN, EX(MAXG), CHI, DF, PROB CHARACTER(1) CL

## 3  Description

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 parameter 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 $\left({r}_{l},{r}_{u}\right)$. 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 $\left({r}_{l},{r}_{u}\right)$ should lie within the region of all possible numbers. For example if the test is carried out on a sequence of $\left(0,1\right)$ random numbers then the interval $\left({r}_{l},{r}_{u}\right)$ must be contained in the whole interval $\left(0,1\right)$. 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
• ($0.27$ $0.40$ $0.25$ $0.10$ $0.34$ $0.39$ $0.61$ $0.12$).
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
 $X2=∑i=1k ci-ei 2ei,$
where
• ${e}_{i}=\mathit{ngaps}×p×{\left(1-p\right)}^{i-1}$, if $i;
• ${e}_{i}=\mathit{ngaps}×{\left(1-p\right)}^{i-1}$, if $i=k$;
• $\mathit{ngaps}=\text{}$ the number of gaps found and
• $p=\left({r}_{u}-{r}_{l}\right)/{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.

## 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  Parameters

1:     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:     N – INTEGERInput
On entry: $n$, the length of the current sequence of observations.
Constraint: ${\mathbf{N}}\ge 1$.
3:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the sequence of observations.
4:     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.
M should not be changed between calls to G08EDF.
Constraint: if ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{M}}\le {\mathbf{N}}$.
5:     MAXG – INTEGERInput
On entry: $k$, the length of the longest gap for which tabulation is desired.
MAXG must not be changed between calls to G08EDF.
Constraints:
• ${\mathbf{MAXG}}>1$;
• if ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{MAXG}}\le {\mathbf{N}}$.
6:     RLO – REAL (KIND=nag_wp)Input
On entry: the lower limit of the interval to be used to define the gaps, ${r}_{l}$.
RLO must not be changed between calls to G08EDF.
7:     RUP – REAL (KIND=nag_wp)Input
On entry: the upper limit of the interval to be used to define the gaps, ${r}_{u}$.
RUP must not be changed between calls to G08EDF.
Constraint: ${\mathbf{RUP}}>{\mathbf{RLO}}$.
8:     TOTLEN – REAL (KIND=nag_wp)Input
On entry: the total length of the interval which contains all possible numbers that may arise in the sequence.
Constraint: ${\mathbf{TOTLEN}}>0.0$ and ${\mathbf{RUP}}-{\mathbf{RLO}}<{\mathbf{TOTLEN}}$.
9:     NGAPS – INTEGERInput/Output
On entry: if ${\mathbf{CL}}=\text{'S'}$ or $\text{'F'}$, NGAPS need not be set.
If ${\mathbf{CL}}=\text{'I'}$ or $\text{'L'}$, NGAPS must contain the value returned by the previous call to G08EDF.
On exit: the number of gaps actually found, $\mathit{ngaps}$.
10:   NCOUNT(MAXG) – INTEGER arrayInput/Output
On entry: if ${\mathbf{CL}}=\text{'S'}$ or $\text{'F'}$, NCOUNT need not be set.
If ${\mathbf{CL}}=\text{'I'}$ or $\text{'L'}$, NCOUNT must contain the values returned by the previous call to G08EDF.
On exit: the counts of gaps of the different lengths, ${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
11:   EX(MAXG) – REAL (KIND=nag_wp) arrayOutput
On exit: if ${\mathbf{CL}}=\text{'S'}$ or $\text{'L'}$ (i.e., if it is a final exit) then EX contains the expected values of the counts.
Otherwise the elements of EX are not set.
12:   CHI – REAL (KIND=nag_wp)Output
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.
Otherwise CHI is not set.
13:   DF – REAL (KIND=nag_wp)Output
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.
Otherwise DF is not set.
14:   PROB – REAL (KIND=nag_wp)Output
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.
Otherwise PROB is not set.
15:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: G08EDF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{CL}}\ne \text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{N}}<1$.
${\mathbf{IFAIL}}=3$
 On entry, with ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{M}}>{\mathbf{N}}$.
${\mathbf{IFAIL}}=4$
 On entry, ${\mathbf{MAXG}}\le 1$, or with ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{MAXG}}>{\mathbf{N}}$.
${\mathbf{IFAIL}}=5$
 On entry, ${\mathbf{RLO}}\ge {\mathbf{RUP}}$, or ${\mathbf{TOTLEN}}\le 0.0$, or ${\mathbf{RUP}}-{\mathbf{RLO}}\ge {\mathbf{TOTLEN}}$.
${\mathbf{IFAIL}}=6$
No gaps were found. You may need to use a longer sequence or increase the size of the interval $\left({r}_{l},{r}_{u}\right)$.
${\mathbf{IFAIL}}=7$
The expected frequency of a certain class is zero, that is ${e}_{i}=0$, for some $i=1,2,\dots ,k$.
${\mathbf{IFAIL}}=8$
${\mathbf{IFAIL}}=9$
Some classes have expected frequencies less than $1.0$. This implies that the ${\chi }^{2}$-distribution may not be a very good approximation to the distribution of the test statistic.

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

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.

## 9  Example

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.

### 9.1  Program Text

Program Text (g08edfe.f90)

### 9.2  Program Data

Program Data (g08edfe.d)

### 9.3  Program Results

Program Results (g08edfe.r)