Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_nonpar_randtest_gaps (g08ed)

## Purpose

nag_nonpar_randtest_gaps (g08ed) performs a gaps test on a sequence of observations.

## Syntax

[ngaps, ncount, ex, chi, df, prob, ifail] = g08ed(cl, x, m, rlo, rup, totlen, ngaps, ncount, 'n', n, 'maxg', maxg)
[ngaps, ncount, ex, chi, df, prob, ifail] = nag_nonpar_randtest_gaps(cl, x, m, rlo, rup, totlen, ngaps, ncount, 'n', n, 'maxg', maxg)

## Description

Gaps tests are used to test for cyclical trend in a sequence of observations. nag_nonpar_randtest_gaps (g08ed) computes certain statistics for the gaps test.
nag_nonpar_randtest_gaps (g08ed) may be used in two different modes:
 (i) a single call to nag_nonpar_randtest_gaps (g08ed) which computes all test statistics after counting the gaps; (ii) multiple calls to nag_nonpar_randtest_gaps (g08ed) 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 Arguments 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.
nag_nonpar_randtest_gaps (g08ed) 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 nag_nonpar_randtest_gaps (g08ed) (i.e., unless there is another call to nag_nonpar_randtest_gaps (g08ed) to follow) in which case the unfinished gap is used. The following is a trivial example.
Suppose we called nag_nonpar_randtest_gaps (g08ed) 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 nag_nonpar_randtest_gaps (g08ed) would have counted the gaps of the following lengths:
• 2, $1$, $1$, $6$, $3$ and $1$.
When the counting of gaps is complete nag_nonpar_randtest_gaps (g08ed) 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 nag_nonpar_randtest_gaps (g08ed) will exit before counting any further gaps.

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{cl}$ – string (length ≥ 1)
Indicates the type of call to nag_nonpar_randtest_gaps (g08ed).
${\mathbf{cl}}=\text{'S'}$
This is the one and only call to nag_nonpar_randtest_gaps (g08ed) (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 function. 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 nag_nonpar_randtest_gaps (g08ed).
${\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 nag_nonpar_randtest_gaps (g08ed).
${\mathbf{cl}}=\text{'L'}$
This is the last call to nag_nonpar_randtest_gaps (g08ed). 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:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The sequence of observations.
3:     $\mathrm{m}$int64int32nag_int scalar
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 nag_nonpar_randtest_gaps (g08ed).
Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{m}}\le {\mathbf{n}}$.
4:     $\mathrm{rlo}$ – double scalar
The lower limit of the interval to be used to define the gaps, ${r}_{l}$.
rlo must not be changed between calls to nag_nonpar_randtest_gaps (g08ed).
5:     $\mathrm{rup}$ – double scalar
The upper limit of the interval to be used to define the gaps, ${r}_{u}$.
rup must not be changed between calls to nag_nonpar_randtest_gaps (g08ed).
Constraint: ${\mathbf{rup}}>{\mathbf{rlo}}$.
6:     $\mathrm{totlen}$ – double scalar
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}}$.
7:     $\mathrm{ngaps}$int64int32nag_int scalar
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 nag_nonpar_randtest_gaps (g08ed).
8:     $\mathrm{ncount}\left({\mathbf{maxg}}\right)$int64int32nag_int array
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 nag_nonpar_randtest_gaps (g08ed).

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array x.
$n$, the length of the current sequence of observations.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathrm{maxg}$int64int32nag_int scalar
Default: the dimension of the array ncount.
$k$, the length of the longest gap for which tabulation is desired.
maxg must not be changed between calls to nag_nonpar_randtest_gaps (g08ed).
Constraints:
• ${\mathbf{maxg}}>1$;
• if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxg}}\le {\mathbf{n}}$.

### Output Parameters

1:     $\mathrm{ngaps}$int64int32nag_int scalar
The number of gaps actually found, $\mathit{ngaps}$.
2:     $\mathrm{ncount}\left({\mathbf{maxg}}\right)$int64int32nag_int array
The counts of gaps of the different lengths, ${c}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,k$.
3:     $\mathrm{ex}\left({\mathbf{maxg}}\right)$ – double array
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.
4:     $\mathrm{chi}$ – double scalar
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.
5:     $\mathrm{df}$ – double scalar
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.
6:     $\mathrm{prob}$ – double scalar
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.
7:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Note: nag_nonpar_randtest_gaps (g08ed) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\mathbf{ifail}}=1$
On entry, ${\mathbf{cl}}=_$.
Constraint: ${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=3$
Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{m}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=4$
Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{maxg}}\le {\mathbf{n}}$.
Constraint: ${\mathbf{maxg}}>1$.
${\mathbf{ifail}}=5$
Constraint: ${\mathbf{rup}}>{\mathbf{rlo}}$.
Constraint: ${\mathbf{rup}}-{\mathbf{rlo}}<{\mathbf{totlen}}$.
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 is zero. The value of $\left({\mathbf{rup}}-{\mathbf{rlo}}\right)/{\mathbf{totlen}}$ may be too close to $0.0\text{​ or ​}1.0$. or maxg is too large relative to the number of gaps found.
W  ${\mathbf{ifail}}=8$
The number of gaps requested were not found, only $_$ out of the requested $_$ where found.
All statistics are returned and may still be of use.
W  ${\mathbf{ifail}}=9$
The expected frequency of at least one class is less than one.
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$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## 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 nag_nonpar_randtest_gaps (g08ed) increases with the number of observations $n$, and depends to some extent whether the call is an only, first, intermediate or last call.

## Example

The following program performs the gaps test on $500$ pseudorandom numbers. nag_nonpar_randtest_gaps (g08ed) is called $5$ times with $100$ observations on each call. All gaps of length $10$ or more are counted together.
function g08ed_example

fprintf('g08ed example results\n\n');

% Initialize the base generator to a repeatable sequence
seed  = [int64(324213)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);

m      = int64(0);
rlo    = 0.4;
rup    = 0.6;
totlen = 1;
ncount = zeros(10, 1, 'int64');
ngaps  = int64(0);
nsampl = 5;
n      = int64(100);
cl    = 'F';

for i=1:nsampl
% Generate a sample from U(0,1)
[state, x, ifail] = g05sq( ...
n, 0, 1, state);
% Process the sample
[ngaps, ncount, ex, chi, df, prob, ifail] = ...
g08ed( ...
cl, x, m, rlo, rup, totlen, ngaps, ncount);
cl = 'I';
if i==nsampl-1
cl = 'L';
end
end

fprintf('Total number of gaps found = %d\n', ngaps);
fprintf('\n%33s\n', 'Count');
fprintf('%7d', ncount);

fprintf('\n\n%34s\n', 'Expect');
fprintf('%7.1f', ex);
fprintf('\n\n');

fprintf('Chisq = %10.4f\n', chi);
fprintf('DF    = %7.1f\n', df);
fprintf('Prob  = %10.4f\n', prob);

g08ed example results

Total number of gaps found = 99

Count
0      1      2      3      4      5      6      7      8     >8
22     11     10     13      6     12      4      6      2     13

Expect
0      1      2      3      4      5      6      7      8     >8
19.8   15.8   12.7   10.1    8.1    6.5    5.2    4.2    3.3   13.3

Chisq =     9.9540
DF    =     9.0
Prob  =     0.3542