NAG Library Routine Document

g08edf (randtest_gaps)

1
Purpose

g08edf performs a gaps test on a sequence of observations.

2
Specification

Fortran Interface
Subroutine g08edf ( cl, n, x, m, maxg, rlo, rup, totlen, ngaps, ncount, ex, chi, df, prob, ifail)
Integer, Intent (In):: n, m, maxg
Integer, Intent (Inout):: ngaps, ncount(maxg), ifail
Real (Kind=nag_wp), Intent (In):: x(n), rlo, rup, totlen
Real (Kind=nag_wp), Intent (Out):: ex(maxg), chi, df, prob
Character (1), Intent (In):: cl
C Header Interface
#include <nagmk26.h>
void  g08edf_ (const char *cl, const Integer *n, const double x[], const Integer *m, const Integer *maxg, const double *rlo, const double *rup, const double *totlen, Integer *ngaps, Integer ncount[], double ex[], double *chi, double *df, double *prob, Integer *ifail, const Charlen length_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 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 rl,ru. That is, a gap ends at xj if rlxjru. The next gap then begins at xj+1. The interval rl,ru 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 rl,ru must be contained in the whole interval 0,1. Let tlen be the length of the interval which specifies all possible numbers.
g08edf counts the number of gaps of different lengths. Let ci denote the number of gaps of length i, for i=1,2,,k-1. The number of gaps of length k or greater is then denoted by ck. 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 cl='F' and then with cl='L') with the following two sequences and with rlo=0.30 and rup=0.60:
Then after the second call g08edf would have counted the gaps of the following lengths:
When the counting of gaps is complete g08edf computes the expected values of the counts. An approximate χ2 statistic with k degrees of freedom is computed where
X2=i=1k ci-ei 2ei,  
where
The use of the χ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
Arguments

1:     cl – Character(1)Input
On entry: indicates the type of call to g08edf.
cl='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.
cl='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.
cl='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.
cl='L'
This is the last call to g08edf. The test statistics are computed after the final counting of gaps is complete.
Constraint: cl='S', 'F', 'I' or 'L'.
2:     n – IntegerInput
On entry: n, the length of the current sequence of observations.
Constraint: n1.
3:     xn – 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 m0 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 cl='S', mn.
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:
  • maxg>1;
  • if cl='S', maxgn.
6:     rlo – Real (Kind=nag_wp)Input
On entry: the lower limit of the interval to be used to define the gaps, rl.
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, ru.
rup must not be changed between calls to g08edf.
Constraint: rup>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: totlen>0.0 and rup-rlo<totlen.
9:     ngaps – IntegerInput/Output
On entry: if cl='S' or 'F', ngaps need not be set.
If cl='I' or 'L', ngaps must contain the value returned by the previous call to g08edf.
On exit: the number of gaps actually found, ngaps.
10:   ncountmaxg – Integer arrayInput/Output
On entry: if cl='S' or 'F', ncount need not be set.
If cl='I' or 'L', ncount must contain the values returned by the previous call to g08edf.
On exit: the counts of gaps of the different lengths, ci, for i=1,2,,k.
11:   exmaxg – Real (Kind=nag_wp) arrayOutput
On exit: if cl='S' or '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 cl='S' or 'L' (i.e., if it is a final exit) then chi contains the χ2 test statistic, X2, for testing the null hypothesis of randomness.
Otherwise chi is not set.
13:   df – Real (Kind=nag_wp)Output
On exit: if cl='S' or 'L' (i.e., if it is a final exit) then df contains the degrees of freedom for the χ2 statistic.
Otherwise df is not set.
14:   prob – Real (Kind=nag_wp)Output
On exit: if cl='S' or 'L' (i.e., if it is a final exit) then prob contains the upper tail probability associated with the χ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 or 1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1 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 arguments may be useful even if ifail0 on exit, the recommended value is -1. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6
Error Indicators and Warnings

If on entry ifail=0 or -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:
ifail=1
On entry, cl=value.
Constraint: cl='S', 'F', 'I' or 'L'.
ifail=2
On entry, n=value.
Constraint: n1.
ifail=3
On entry, m=value and n=value.
Constraint: if cl='S', mn.
ifail=4
On entry, maxg=value.
Constraint: maxg>1.
On entry, maxg=value and n=value.
Constraint: if cl='S', maxgn.
ifail=5
On entry, rlo=value, rup=value and totlen=value.
Constraint: rup-rlo<totlen.
On entry, rlo=value and rup=value.
Constraint: rup>rlo.
On entry, totlen=value.
Constraint: totlen>0.0.
ifail=6
No gaps were found. Try using a longer sequence, or increase the size of the interval rup-rlo.
ifail=7
The expected frequency in class i=value is zero. The value of rup-rlo/totlen may be too close to 0.0 or 1.0. or maxg is too large relative to the number of gaps found.
ifail=8
The number of gaps requested were not found, only value out of the requested value where found.
All statistics are returned and may still be of use.
ifail=9
The expected frequency of at least one class is less than 1.
This implies that the χ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.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

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

g08edf is not thread safe and should not be called from a multithreaded user program. Please see Section 3.12.1 in How to Use the NAG Library and its Documentation for more information on thread safety.
g08edf is not threaded in any implementation.

9
Further 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.

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

10.1
Program Text

Program Text (g08edfe.f90)

10.2
Program Data

Program Data (g08edfe.d)

10.3
Program Results

Program Results (g08edfe.r)