# NAG Library Function Documentnag_triplets_test (g08ecc)

## 1  Purpose

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

## 2  Specification

#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)

## 3  Description

nag_triplets_test (g08ecc) computes the statistics for performing a triplets test which may be used to investigate deviations from randomness in a sequence of $\left[0,1\right]$ observations.
An $m$ by $m$ matrix, $C$, of counts is formed as follows. The element ${c}_{jkl}$ of $C$ is the number of triplets (x$\left(\mathit{i}\right)$, x$\left(\mathit{i}+1\right)$, x$\left(\mathit{i}+2\right)$), for $\mathit{i}=1,4,\dots ,n-2$, such that

$j-1m≤Xi<jm$

$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}_{jkl}$, divided by the number of classes, ${m}^{3}$.
The ${\chi }^{2}$ test statistic used to test the hypothesis of randomness is defined as:

$X 2 = ∑ j , k , l = 1 m c jkl - e 2 e$
where $e={\sum }_{j,k,l=1}^{m}{c}_{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, hence the expected value, $e$, increases.

## 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:     n – IntegerInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 3$.
2:     x[n] – const doubleInput
On entry: the sequence of observations.
Constraint: $0.0\le {\mathbf{x}}\left[\mathit{i}-1\right]\le 1.0$, for $\mathit{i}=1,2,\dots ,n$.
3:     max_count – IntegerInput
On entry: the size of the count matrix to be formed, $m$.
Constraint: ${\mathbf{max_count}}\ge 2$.
4:     chi – double *Output
On exit: contains the ${\chi }^{2}$ test statistic, ${X}^{2}$, for testing the null hypothesis of randomness.
5:     df – double *Output
On exit: contains the degrees of freedom for the ${\chi }^{2}$ statistic.
6:     prob – double *Output
On exit: contains the upper tail probability associated with the ${\chi }^{2}$ test statistic, i.e., the significance level.
7:     fail – NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_G08EC_CELL
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 ${\chi }^{2}$ distribution may not be a very good approximation to the test statistic.
NE_G08EC_TRIPLETS
No triplets were found because less than 3 observations were provided in total.
NE_INT_ARG_LE
On entry, max_count must not be less than or equal to 1: ${\mathbf{max_count}}=〈\mathit{\text{value}}〉$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 3$.
NE_INTERNAL_ERROR
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.
NE_REAL_ARRAY_CONS
On entry, ${\mathbf{x}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: $0<{\mathbf{x}}\left[\mathit{i}\right]<1.0$, for $\mathit{i}=0,1,\dots ,n-1$.

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

The time taken by nag_triplets_test (g08ecc) increases with the number of observations, $n$.

## 9  Example

The following program performs the pairs test on 10000 pseudorandom numbers from a uniform distribution $U\left(0,1\right)$ generated by nag_rand_basic (g05sac). nag_triplets_test (g08ecc) is called with max_count set to 5.

### 9.1  Program Text

Program Text (g08ecce.c)

None.

### 9.3  Program Results

Program Results (g08ecce.r)
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 43, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.7492587566375732, "perplexity_flag": "middle"}