g08 Chapter Contents
g08 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_gaps_test (g08edc)

## 1  Purpose

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

## 2  Specification

 #include #include
 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 $\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 (0,1) random numbers then the interval $\left({r}_{l},{r}_{u}\right)$ must be contained in the whole interval (0,1). Let ${t}_{\mathit{len}}$ be the length of the interval which specifies all possible numbers.
nag_gaps_test (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 nag_gaps_test (g08edc) with the following sequence and with ${r}_{l}=0.30$ and ${r}_{u}=0.60$:
• 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.
nag_gaps_test (g08edc) will count gaps of the following lengths:
• 2, 1, 1, 6, 3 and 1.
When the counting of gaps is complete nag_gaps_test (g08edc) computes the expected values of the counts. An approximate ${\chi }^{2}$ statistic with max_gap degrees of freedom is computed where
 $X 2 = ∑ i=1 k c i - e i 2 e i$
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}_{\mathit{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 nag_gaps_test (g08edc) will exit before counting any further gaps.
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:     nIntegerInput
On entry: the length of the current sequence of observations, $n$.
Constraint: ${\mathbf{n}}\ge 1$.
2:     x[n]const doubleInput
On entry: the sequence of observations.
3:     num_gapsIntegerInput
On entry: the maximum number of gaps to be sought. If ${\mathbf{num_gaps}}\le 0$ then there is no limit placed on the number of gaps that are found.
Constraint: ${\mathbf{num_gaps}}\le {\mathbf{n}}$.
4:     max_gapIntegerInput
On entry: the length of the longest gap for which tabulation is desired, $k$.
Constraint: ${\mathbf{max_gap}}>1$ and ${\mathbf{max_gap}}\le {\mathbf{n}}$.
5:     lowerdoubleInput
On entry: the lower limit of the interval to be used to define the gaps, ${r}_{l}$.
Constraint: ${\mathbf{lower}}<{\mathbf{upper}}$ and ${\mathbf{upper}}-{\mathbf{lower}}<{\mathbf{length}}$.
6:     upperdoubleInput
On entry: the upper limit of the interval to be used to define the gaps, ${r}_{u}$.
Constraint: ${\mathbf{upper}}>{\mathbf{lower}}$ and ${\mathbf{upper}}-{\mathbf{lower}}<{\mathbf{length}}$.
7:     lengthdoubleInput
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:     chidouble *Output
On exit: contains the ${\chi }^{2}$ test statistic, ${X}^{2}$, for testing the null hypothesis of randomness.
9:     dfdouble *Output
On exit: contains the degrees of freedom for the ${\chi }^{2}$ statistic.
10:   probdouble *Output
On exit: contains the upper tail probability associated with the ${\chi }^{2}$ test statistic, i.e., the significance level.
11:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_GT
On entry, ${\mathbf{num_gaps}}=⟨\mathit{\text{value}}⟩$ while ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{num_gaps}}\le {\mathbf{n}}$.
NE_2_REAL_ARG_GE
On entry, ${\mathbf{lower}}=⟨\mathit{\text{value}}⟩$, while ${\mathbf{upper}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{upper}}<{\mathbf{lower}}$.
NE_3_REAL_ARG_CONS
On entry, ${\mathbf{lower}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{upper}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{length}}=⟨\mathit{\text{value}}⟩$. These arguments must satisfy ${\mathbf{upper}}-{\mathbf{lower}}<{\mathbf{length}}$.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_G08ED_FREQ_LT_ONE
Some classes have expected frequencies less than 1.0. This implies that the ${\chi }^{2}$ may not be a good approximation to the distribution of the test statistic.
NE_G08ED_FREQ_ZERO
The expected frequency of a certain class is zero, that is ${e}_{i}=0$, for some $i=1,2,\dots ,k$. For further details please refer to Section 3.
NE_G08ED_GAPS
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}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: $1\le {\mathbf{max_gap}}\le {\mathbf{n}}$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
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.
NE_REAL_ARG_LE
On entry, length must not be less than or equal to 0.0: ${\mathbf{length}}=⟨\mathit{\text{value}}⟩$.

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

Not applicable.

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

## 10  Example

The following program performs the pairs test on 10000 pseudorandom numbers from a uniform distribution between 0 and 1 generated by nag_rand_uniform (g05sqc). nag_gaps_test (g08edc) is called 10 times with 1000 observations on each call. All gaps of length 10 or more are counted together.

### 10.1  Program Text

Program Text (g08edce.c)

None.

### 10.3  Program Results

Program Results (g08edce.r)