NAG C Library Function Document

nag_triplets_test (g08ecc)


nag_triplets_test (g08ecc) performs the triplets test on a sequence of observations from the interval 0,1.


#include <nag.h>
#include <nagg08.h>
void  nag_triplets_test (Integer n, const double x[], Integer max_count, double *chi, double *df, double *prob, NagError *fail)


nag_triplets_test (g08ecc) 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.


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


1:     n IntegerInput
On entry: n, the number of observations.
Constraint: n3.
2:     x[n] const doubleInput
On entry: the sequence of observations.
Constraint: 0.0x[i-1]1.0, for i=1,2,,n.
3:     max_count IntegerInput
On entry: m, the size of the count matrix to be formed.
Constraint: max_count2.
4:     chi double *Output
On exit: contains the χ2 test statistic, X2, for testing the null hypothesis of randomness.
5:     df double *Output
On exit: contains the degrees of freedom for the χ2 statistic.
6:     prob double *Output
On exit: contains the upper tail probability associated with the χ2 test statistic, i.e., the significance level.
7:     fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

Error Indicators and Warnings

Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, argument value had an illegal value.
max_count is too large relative to the number of triplets, therefore the expected value for at least one cell 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.
max_count=value , number of triplets =value and expected value=value.
All statistics are returned and may still be of use.
No triplets were found because less than 3 observations were provided in total.
On entry, max_count=value.
Constraint: max_count2.
On entry, n=value.
Constraint: n3.
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.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
On entry, at least one element of x is out of range.
Constraint: 0x[i-1]1, for i=1,2,,n.


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.

Parallelism and Performance

nag_triplets_test (g08ecc) is not threaded in any implementation.

Further Comments

The time taken by the function increases with the number of observations n.


The following program performs the triplets test on 10000 pseudorandom numbers taken from a uniform distribution U0,1, generated by nag_rand_basic (g05sac). nag_triplets_test (g08ecc) is called with max_count=5.

Program Text

Program Text (g08ecce.c)

Program Data


Program Results

Program Results (g08ecce.r)