PDF version (NAG web site
, 64bit version, 64bit version)
NAG Toolbox: nag_nonpar_randtest_triplets (g08ec)
Purpose
nag_nonpar_randtest_triplets (g08ec) performs the triplets test on a sequence of observations from the interval $\left[0,1\right]$.
Syntax
[
ncount,
ex,
chi,
df,
prob,
ifail] = g08ec(
cl,
x,
ncount, 'n',
n, 'msize',
msize)
[
ncount,
ex,
chi,
df,
prob,
ifail] = nag_nonpar_randtest_triplets(
cl,
x,
ncount, 'n',
n, 'msize',
msize)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 22: 
msize was made optional 
Description
nag_nonpar_randtest_triplets (g08ec) 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$ by
$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 ,n2$, such that
Note that all triplets formed are nonoverlapping 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
where
$e={\displaystyle \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.
nag_nonpar_randtest_triplets (g08ec) may be used in two different modes:
(i) 
a single call to nag_nonpar_randtest_triplets (g08ec) which computes all test statistics after counting the triplets; 
(ii) 
multiple calls to nag_nonpar_randtest_triplets (g08ec) 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
Arguments for details on how to invoke each mode.
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
Parameters
Compulsory Input Parameters
 1:
$\mathrm{cl}$ – string (length ≥ 1)

Indicates the type of call to
nag_nonpar_randtest_triplets (g08ec).
 ${\mathbf{cl}}=\text{'S'}$
 This is the one and only call to nag_nonpar_randtest_triplets (g08ec) (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 function. 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 nag_nonpar_randtest_triplets (g08ec).
 ${\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 nag_nonpar_randtest_triplets (g08ec).
 ${\mathbf{cl}}=\text{'L'}$
 This is the last call to nag_nonpar_randtest_triplets (g08ec). 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:
$\mathrm{x}\left({\mathbf{n}}\right)$ – double array

The sequence of observations.
Constraint:
$0.0\le {\mathbf{x}}\left(\mathit{i}\right)\le 1.0$, for $\mathit{i}=1,2,\dots ,n$.
 3:
$\mathrm{ncount}\left(\mathit{ldc},\mathit{ldc},{\mathbf{msize}}\right)$ – int64int32nag_int array

ldc, the first dimension of the array, must satisfy the constraint
$\mathit{ldc}\ge {\mathbf{msize}}$.
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
nag_nonpar_randtest_triplets (g08ec).
Optional Input Parameters
 1:
$\mathrm{n}$ – int64int32nag_int scalar

Default:
the dimension of the array
x.
$n$, the number of observations.
Constraints:
 if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{n}}\ge 3$;
 otherwise ${\mathbf{n}}\ge 1$.
 2:
$\mathrm{msize}$ – int64int32nag_int scalar

Default:
the first dimension of the array
ncount and the third dimension of the array
ncount. (An error is raised if these dimensions are not equal.)
$m$, the size of the count matrix to be formed.
msize must not be changed between calls to
nag_nonpar_randtest_triplets (g08ec).
Constraint:
${\mathbf{msize}}\ge 2$.
Output Parameters
 1:
$\mathrm{ncount}\left(\mathit{ldc},\mathit{ldc},{\mathbf{msize}}\right)$ – int64int32nag_int array

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$.
 2:
$\mathrm{ex}$ – double scalar

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.
 3:
$\mathrm{chi}$ – double scalar

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.
 4:
$\mathrm{df}$ – double scalar

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.
 5:
$\mathrm{prob}$ – double scalar

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.
 6:
$\mathrm{ifail}$ – int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Note: nag_nonpar_randtest_triplets (g08ec) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:
Cases prefixed with W are classified as warnings and
do not generate an error of type NAG:error_n. See nag_issue_warnings.
 ${\mathbf{ifail}}=1$

On entry, ${\mathbf{cl}}=\_$.
Constraint: ${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
 ${\mathbf{ifail}}=2$

Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{n}}\ge 3$, otherwise ${\mathbf{n}}\ge 1$.
 ${\mathbf{ifail}}=3$

Constraint: ${\mathbf{msize}}\ge 2$.
 ${\mathbf{ifail}}=4$

Constraint: $\mathit{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(\mathrm{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.
 W ${\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$.
 ${\mathbf{ifail}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
 ${\mathbf{ifail}}=399$
Your licence key may have expired or may not have been installed correctly.
 ${\mathbf{ifail}}=999$
Dynamic memory allocation failed.
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.
Further Comments
If the call to nag_nonpar_randtest_triplets (g08ec) 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 nag_nonpar_randtest_triplets (g08ec) are ignored.
The time taken by the function increases with the number of observations $n$, and also depends to some extent whether the call to nag_nonpar_randtest_triplets (g08ec) is an only, first, intermediate or last call.
Example
The following program performs the triplets test on $500$ pseudorandom numbers. nag_nonpar_randtest_triplets (g08ec) is called $5$ times with $100$ observations on each call. The triplets are tallied into a $2$ by $2$ by $2$ matrix.
Open in the MATLAB editor:
g08ec_example
function g08ec_example
fprintf('g08ec example results\n\n');
seed = [int64(324213)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);
m = 2;
ncount = zeros(m, m, m, 'int64');
n = int64(100);
nsampl = 5;
cl = 'F';
for i=1:nsampl
[state, x, ifail] = g05sq( ...
n, 0, 1, state);
[ncount, ex, chi, df, prob, ifail] = ...
g08ec( ...
cl, x, ncount);
cl = 'I';
if i==nsampl1
cl = 'L';
end
end
for k = 1:m
mtitle = sprintf('k = %2d',k);
[ifail] = x04ea('General', ' ', reshape(ncount(:,:,k),m,m), mtitle);
fprintf('\n');
end
fprintf('\nExpected value = %8.2f\n', ex);
fprintf('Chisq = %10.4f\n', chi);
fprintf('DF = %7.1f\n', df);
fprintf('Probability = %10.4f\n', prob);
g08ec example results
k = 1
1 2
1 22 25
2 18 17
k = 2
1 2
1 23 24
2 24 13
Expected value = 20.75
Chisq = 6.1446
DF = 7.0
Probability = 0.5230
PDF version (NAG web site
, 64bit version, 64bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015