hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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 [0,1][0,1].

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:
Mark 22: msize has been 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 = {xi : i = 1,2,,n}x={xi:i=1,2,,n}, of [0,1][0,1] observations.
An mm by mm matrix, CC, of counts is formed as follows. The element cjklcjkl of CC is the number of triplets (xi,xi + 1,xi + 2)(xi,xi+1,xi+2) for i = 1,4,7,,n2i=1,4,7,,n-2, such that
(j1)/mxi < j/m
j-1mxi<jm
(k 1)/mxi + 1 < k/m
k- 1mxi+ 1< km
(l1)/mxi + 2 < l/m.
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,1]3[0,1]3. Thus the expected number of triplets for each class is just the total number of triplets, j,k,l = 1mcjklj,k,l=1mcjkl, divided by the number of classes, m3m3.
The χ2χ2 test statistic used to test the hypothesis of randomness is defined as
m
X2 = ((cjkle)2)/e,
j,k,l = 1
X2=j,k,l=1m (cjkl-e) 2e,
where e = j,k,l = 1mcjkl / m3 = e=j,k,l=1mcjkl/m3= expected number of triplets in each class.
The use of the χ2χ2-distribution as an approximation to the exact distribution of the test statistic, X2X2, improves as the length of the sequence relative to mm increases and hence the expected value, ee, 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 parameter cl in Section [Parameters] 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:     cl – string (length ≥ 1)
Indicates the type of call to nag_nonpar_randtest_triplets (g08ec).
cl = 'S'cl='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.
cl = 'F'cl='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).
cl = 'I'cl='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).
cl = 'L'cl='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: cl = 'S'cl='S', 'F''F', 'I''I' or 'L''L'.
2:     x(n) – double array
n, the dimension of the array, must satisfy the constraint
  • if cl = 'S'cl='S', n3n3;
  • otherwise n1n1.
The sequence of observations.
Constraint: 0.0x(i)1.00.0xi1.0, for i = 1,2,,ni=1,2,,n.
3:     ncount(ldc,ldc,msize) – int64int32nag_int array
ldc, the first dimension of the array, must satisfy the constraint ldcmsizeldcmsize.
If cl = 'S'cl='S' or 'F''F', ncount need not be set.
If cl = 'I'cl='I' or 'L''L', ncount must contain the values returned by the previous call to nag_nonpar_randtest_triplets (g08ec).

Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array x.
nn, the number of observations.
Constraints:
  • if cl = 'S'cl='S', n3n3;
  • otherwise n1n1.
2:     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.)
mm, the size of the count matrix to be formed.
msize must not be changed between calls to nag_nonpar_randtest_triplets (g08ec).
Constraint: msize2msize2.

Input Parameters Omitted from the MATLAB Interface

ldc

Output Parameters

1:     ncount(ldc,ldc,msize) – int64int32nag_int array
ldcmsizeldcmsize.
Is an msize by msize by msize matrix containing the counts of the number of triplets, cjklcjkl, for j = 1,2,,mj=1,2,,m, k = 1,2,,mk=1,2,,m and l = 1,2,,ml=1,2,,m.
2:     ex – double scalar
If cl = 'S'cl='S' or 'L''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.
3:     chi – double scalar
If cl = 'S'cl='S' or 'L''L' (i.e., if it is a final exit) then chi contains the χ2χ2 test statistic, X2X2, for testing the null hypothesis of randomness.
Otherwise chi is not set.
4:     df – double scalar
If cl = 'S'cl='S' or 'L''L' (i.e., if it is a final exit) then df contains the degrees of freedom for the χ2χ2 statistic.
Otherwise df is not set.
5:     prob – double scalar
If cl = 'S'cl='S' or 'L''L' (i.e., if it is a final exit) then prob contains the upper tail probability associated with the χ2χ2 test statistic, i.e., the significance level.
Otherwise prob is not set.
6:     ifail – int64int32nag_int scalar
ifail = 0ifail=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.

  ifail = 1ifail=1
On entry, cl = _cl=_.
Constraint: cl = 'S'cl='S', 'F''F', 'I''I' or 'L''L'.
  ifail = 2ifail=2
Constraint: if cl = 'S'cl='S', n3n3, otherwise n1n1.
  ifail = 3ifail=3
Constraint: msize2msize2.
  ifail = 4ifail=4
Constraint: ldcmsizeldcmsize.
  ifail = 5ifail=5
On entry, at least one element of x is out of range.
Constraint: 0x(i)10xi1, for i = 1,2,,ni=1,2,,n. On entry, at least one element of x is out of range.
Constraint: 0x(i)10xi1, for i = 1,2,,ni=1,2,,n.
  ifail = 6ifail=6
No triplets were found because less than 33 observations were provided in total.
W ifail = 7ifail=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.05.0.

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 nn, and also depends to some extent whether the call to nag_nonpar_randtest_triplets (g08ec) is an only, first, intermediate or last call.

Example

function nag_nonpar_randtest_triplets_example
% Initialize the seed
seed = [int64(32423)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
ncount = zeros(5, 5, 5, 'int64');
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
for i=1:10
  % Generate some U(0,1) values
  [state, x, ifail] = nag_rand_dist_uniform(int64(1000), 0, 1, state);
  if i==1
    cl = 'F';
  elseif i==10
    cl = 'L';
  else
    cl = 'I';
  end
  [ncount, ex, chi, df, prob, ifail] = nag_nonpar_randtest_triplets(cl, x, ncount);
end
if (ifail == 0 || ifail == 8)
  for i = 1:5
    [ifail] = ...
    nag_file_print_matrix_integer('General', ' ', reshape(ncount(i,:,:),5,5), ['i = ', int2str(i)]);
    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);
end
 
 i = 1
     1  2  3  4  5
 1  22 27 23 34 31
 2  28 36 16 26 27
 3  23 28 28 24 29
 4  26 35 27 30 24
 5  22 29 20 30 31

 i = 2
     1  2  3  4  5
 1  18 26 32 25 16
 2  24 32 20 23 27
 3  30 32 28 30 23
 4  16 36 19 27 30
 5  28 21 23 30 29

 i = 3
     1  2  3  4  5
 1  31 34 21 23 25
 2  22 27 19 26 26
 3  29 26 30 26 19
 4  28 34 18 23 40
 5  21 33 19 24 28

 i = 4
     1  2  3  4  5
 1  29 22 30 22 34
 2  22 25 30 30 29
 3  27 22 22 24 23
 4  31 37 23 24 29
 5  30 27 31 20 15

 i = 5
     1  2  3  4  5
 1  25 28 25 27 26
 2  31 33 33 21 29
 3  35 29 36 28 21
 4  33 29 28 19 30
 5  20 23 34 34 25


Expected value =    26.66
Chisq          =   120.1578
DF             =   124.0
Probability    =     0.5809

function g08ec_example
% Initialize the seed
seed = [int64(32423)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
ncount = zeros(5, 5, 5, 'int64');
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
for i=1:10
  % Generate some U(0,1) values
  [state, x, ifail] = g05sq(int64(1000), 0, 1, state);
  if i==1
    cl = 'F';
  elseif i==10
    cl = 'L';
  else
    cl = 'I';
  end
  [ncount, ex, chi, df, prob, ifail] = g08ec(cl, x, ncount);
end
if (ifail == 0 || ifail == 8)
  for i = 1:5
    [ifail] = x04ea('General', ' ', reshape(ncount(i,:,:),5,5), ['i = ', int2str(i)]);
    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);
end
 
 i = 1
     1  2  3  4  5
 1  22 27 23 34 31
 2  28 36 16 26 27
 3  23 28 28 24 29
 4  26 35 27 30 24
 5  22 29 20 30 31

 i = 2
     1  2  3  4  5
 1  18 26 32 25 16
 2  24 32 20 23 27
 3  30 32 28 30 23
 4  16 36 19 27 30
 5  28 21 23 30 29

 i = 3
     1  2  3  4  5
 1  31 34 21 23 25
 2  22 27 19 26 26
 3  29 26 30 26 19
 4  28 34 18 23 40
 5  21 33 19 24 28

 i = 4
     1  2  3  4  5
 1  29 22 30 22 34
 2  22 25 30 30 29
 3  27 22 22 24 23
 4  31 37 23 24 29
 5  30 27 31 20 15

 i = 5
     1  2  3  4  5
 1  25 28 25 27 26
 2  31 33 33 21 29
 3  35 29 36 28 21
 4  33 29 28 19 30
 5  20 23 34 34 25


Expected value =    26.66
Chisq          =   120.1578
DF             =   124.0
Probability    =     0.5809


PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013