# NAG FL Interfaceg08ecf (randtest_​triplets)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

g08ecf performs the triplets test on a sequence of observations from the interval $\left[0,1\right]$.

## 2Specification

Fortran Interface
 Subroutine g08ecf ( cl, n, x, ldc, ex, chi, df, prob,
 Integer, Intent (In) :: n, msize, ldc Integer, Intent (Inout) :: ncount(ldc,ldc,msize), ifail Real (Kind=nag_wp), Intent (In) :: x(n) Real (Kind=nag_wp), Intent (Out) :: ex, chi, df, prob Character (1), Intent (In) :: cl
#include <nag.h>
 void g08ecf_ (const char *cl, const Integer *n, const double x[], const Integer *msize, Integer ncount[], const Integer *ldc, double *ex, double *chi, double *df, double *prob, Integer *ifail, const Charlen length_cl)
The routine may be called by the names g08ecf or nagf_nonpar_randtest_triplets.

## 3Description

g08ecf computes the statistics for performing a triplets test which may be used to investigate deviations from randomness in a sequence, $x=\left\{{x}_{i}:i=1,2,\dots ,n\right\}$, of $\left[0,1\right]$ observations.
An $m×m$ matrix, $C$, of counts is formed as follows. The element ${c}_{\mathrm{jkl}}$ of $C$ is the number of triplets $\left({x}_{i},{x}_{i+1},{x}_{i+2}\right)$ for $i=1,4,7,\dots ,n-2$, such that
 $j-1m≤xi
 $k- 1m≤xi+ 1< km$
 $l-1m≤xi+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 ${\left[0,1\right]}^{3}$. Thus the expected number of triplets for each class is just the total number of triplets, $\sum _{j,k,l=1}^{m}{c}_{\mathrm{jkl}}$, divided by the number of classes, ${m}^{3}$.
The ${\chi }^{2}$ test statistic used to test the hypothesis of randomness is defined as
 $X2=∑j,k,l=1m (cjkl-e) 2e,$
where $e=\sum _{j,k,l=1}^{m}{c}_{\mathrm{jkl}}/{m}^{3}=\text{}$ expected number of triplets in each class.
The use of the ${\chi }^{2}$-distribution as an approximation to the exact distribution of the test statistic, ${X}^{2}$, 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:
1. (i)a single call to g08ecf which computes all test statistics after counting the triplets;
2. (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 argument cl in Section 5 for details on how to invoke each mode.
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

## 5Arguments

1: $\mathbf{cl}$Character(1) Input
On entry: indicates the type of call to g08ecf.
${\mathbf{cl}}=\text{'S'}$
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.
${\mathbf{cl}}=\text{'F'}$
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.
${\mathbf{cl}}=\text{'I'}$
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.
${\mathbf{cl}}=\text{'L'}$
This is the last call to g08ecf. The test statistics are computed after the final counting of the triplets is complete.
Constraint: ${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
2: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
Constraints:
• if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{n}}\ge 3$;
• otherwise ${\mathbf{n}}\ge 1$.
3: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the sequence of observations.
Constraint: $0.0\le {\mathbf{x}}\left(\mathit{i}\right)\le 1.0$, for $\mathit{i}=1,2,\dots ,n$.
4: $\mathbf{msize}$Integer Input
On entry: $m$, the size of the count matrix to be formed.
msize must not be changed between calls to g08ecf.
Constraint: ${\mathbf{msize}}\ge 2$.
5: $\mathbf{ncount}\left({\mathbf{ldc}},{\mathbf{ldc}},{\mathbf{msize}}\right)$Integer array Input/Output
On entry: 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 g08ecf.
On exit: is an msize by msize by msize matrix containing the counts of the number of triplets, ${c}_{jkl}$, for $\mathit{j}=1,2,\dots ,m$, $\mathit{k}=1,2,\dots ,m$ and $\mathit{l}=1,2,\dots ,m$.
6: $\mathbf{ldc}$Integer Input
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: ${\mathbf{ldc}}\ge {\mathbf{msize}}$.
7: $\mathbf{ex}$Real (Kind=nag_wp) Output
On exit: if ${\mathbf{cl}}=\text{'S'}$ or $\text{'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: $\mathbf{chi}$Real (Kind=nag_wp) Output
On exit: 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.
9: $\mathbf{df}$Real (Kind=nag_wp) Output
On exit: 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.
10: $\mathbf{prob}$Real (Kind=nag_wp) Output
On exit: 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.
11: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $-1$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
Note: in some cases g08ecf may return useful information.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{cl}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{n}}\ge 3$, otherwise ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{msize}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{msize}}\ge 2$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{ldc}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{msize}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{msize}}$.
${\mathbf{ifail}}=5$
On entry, at least one element of x is out of range.
Constraint: $0\le {\mathbf{x}}\left(i\right)\le 1$, for $i=1,2,\dots ,{\mathbf{n}}$.
${\mathbf{ifail}}=6$
No triplets were found because less than $3$ observations were provided in total.
${\mathbf{ifail}}=7$
msize 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 ${\chi }^{2}$ distribution may not be a very good approximation to the distribution of the test statistic.
${\mathbf{msize}}=⟨\mathit{\text{value}}⟩$, number of triplets $\text{}=⟨\mathit{\text{value}}⟩$ and $\text{expected value}=⟨\mathit{\text{value}}⟩$.
All statistics are returned and may still be of use.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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.

## 8Parallelism and Performance

g08ecf is not threaded in any implementation.

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.

## 10Example

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×2×2$ matrix.

### 10.1Program Text

Program Text (g08ecfe.f90)

### 10.2Program Data

Program Data (g08ecfe.d)

### 10.3Program Results

Program Results (g08ecfe.r)