G08EDF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G08EDF

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.

 Contents

    1  Purpose
    7  Accuracy

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

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 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​ 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 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 one.
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.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction 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

Not applicable.

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)


G08EDF (PDF version)
G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015