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

NAG Library Routine Document


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

G08ECF performs the triplets test on a sequence of observations from the interval 0,1.

2  Specification

REAL (KIND=nag_wp)  X(N), EX, CHI, DF, PROB

3  Description

G08ECF computes the statistics for performing a triplets test which may be used to investigate deviations from randomness in a sequence, x=xi:i=1,2,,n, of 0,1 observations.
An m by m matrix, C, of counts is formed as follows. The element cjkl of C is the number of triplets xi,xi+1,xi+2 for i=1,4,7,,n-2, such that
k- 1mxi+ 1< km
l-1mxi+2< lm.
Note that all triplets formed are non-overlapping and are thus independent under the assumption of randomness.
Under the assumption that the sequence is random, the expected number of triplets for each class (i.e., each element of the count matrix) is the same; that is, the triplets should be uniformly distributed over the unit cube 0,13. Thus the expected number of triplets for each class is just the total number of triplets, j,k,l=1mcjkl, divided by the number of classes, m3.
The χ2 test statistic used to test the hypothesis of randomness is defined as
X2=j,k,l=1m cjkl-e 2e,
where e=j,k,l=1mcjkl/m3= expected number of triplets in each class.
The use of the χ2-distribution as an approximation to the exact distribution of the test statistic, X2, improves as the length of the sequence relative to m increases and hence the expected value, e, increases.
G08ECF may be used in two different modes:
(i) a single call to G08ECF which computes all test statistics after counting the triplets;
(ii) multiple calls to G08ECF with the final test statistics only being computed in the last call.
The second mode is necessary if all the data do not fit into the memory. See parameter CL in Section 5 for details on how to invoke each mode.

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 G08ECF.
This is the one and only call to G08ECF (single call mode). All data are to be input at once. All test statistics are computed after counting of the triplets is complete.
This is the first call to the routine. All initializations are carried out and the counting of triplets begins. The final test statistics are not computed since further calls will be made to G08ECF.
This is an intermediate call during which counts of the triplets are updated. The final test statistics are not computed since further calls will be made to G08ECF.
This is the last call to G08ECF. The test statistics are computed after the final counting of the triplets is complete.
Constraint: CL='S', 'F', 'I' or 'L'.
2:     N – INTEGERInput
On entry: n, the number of observations.
  • if CL='S', N3;
  • otherwise N1.
3:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the sequence of observations.
Constraint: 0.0Xi1.0, for i=1,2,,n.
4:     MSIZE – INTEGERInput
On entry: m, the size of the count matrix to be formed.
MSIZE must not be changed between calls to G08ECF.
Constraint: MSIZE2.
5:     NCOUNT(LDC,LDC,MSIZE) – 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 G08ECF.
On exit: is an MSIZE by MSIZE by MSIZE matrix containing the counts of the number of triplets, cjkl, for j=1,2,,m, k=1,2,,m and l=1,2,,m.
6:     LDC – INTEGERInput
On entry: the first dimension of the array NCOUNT and the second dimension of the array NCOUNT as declared in the (sub)program from which G08ECF is called.
Constraint: LDCMSIZE.
7:     EX – REAL (KIND=nag_wp)Output
On exit: if CL='S' or 'L' (i.e., if it is a final exit) then EX contains the expected number of counts for each element of the count matrix.
Otherwise EX is not set.
8:     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.
9:     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.
10:   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.
11:   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: G08ECF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
On entry,CL'S', 'F', 'I' or 'L'.
On entry,N<1,
orCL='S' and N<3.
On entry,MSIZE1.
On entry,LDC<MSIZE.
On entry,Xi<0.0,
orXi>1.0, for some i=1,2,,n.
No triplets were found because less than 3 observations were provided in total.
The expected value for the counts in each element of the count matrix is less than or equal to 5.0. This implies that the χ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 computations of PROB given the values of CHI and DF will obtain a relative accuracy of five significant figures for most cases.

8  Further Comments

If the call to G08ECF is an initial call or intermediate call with further calls to follow then any unused observations are saved for use at the beginning of the new sequence provided in the following call. Clearly any observations left over from an only or final call to G08ECF are ignored.
The time taken by the routine increases with the number of observations n, and also depends to some extent whether the call to G08ECF is an only, first, intermediate or last call.

9  Example

The following program performs the triplets test on 500 pseudorandom numbers. G08ECF is called 5 times with 100 observations on each call. The triplets are tallied into a 2 by 2 by 2 matrix.

9.1  Program Text

Program Text (g08ecfe.f90)

9.2  Program Data

Program Data (g08ecfe.d)

9.3  Program Results

Program Results (g08ecfe.r)

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

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