Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_nonpar_randtest_pairs (g08eb)

## Purpose

nag_nonpar_randtest_pairs (g08eb) performs a pairs test on a sequence of observations in the interval [0,1]$\left[0,1\right]$.

## Syntax

[ncount, ex, chi, df, prob, wrk, ifail] = g08eb(cl, x, lag, ncount, wrk, 'n', n, 'msize', msize)
[ncount, ex, chi, df, prob, wrk, ifail] = nag_nonpar_randtest_pairs(cl, x, lag, ncount, wrk, 'n', n, 'msize', msize)

## Description

nag_nonpar_randtest_pairs (g08eb) computes the statistics for performing a pairs test which may be used to investigate deviations from randomness in a sequence, x = {xi : i = 1,2,,n}$x=\left\{{x}_{i}:i=1,2,\dots ,n\right\}$, of [0,1]$\left[0,1\right]$ observations.
For a given lag, l1$l\ge 1$, an m$m$ by m$m$ matrix, C$C$, of counts is formed as follows. The element cjk${c}_{jk}$ of C$C$ is the number of pairs (xi,xi + l)$\left({x}_{i},{x}_{i+l}\right)$ such that
 (j − 1)/m ≤ xi < j/m $j-1m≤xi
 (k − 1)/m ≤ xi + l < k/m $k- 1m≤xi+l
where i = 1,3,5,,n1$i=1,3,5,\dots ,n-1$ if l = 1$l=1$, and i = 1,2,,l,2l + 1,2l + 2,3l,4l + 1,,nl$i=1,2,\dots ,l,2l+1,2l+2,\dots 3l,4l+1,\dots ,n-l$, if l > 1$l>1$.
Note that all pairs formed are non-overlapping pairs and are thus independent under the assumption of randomness.
Under the assumption that the sequence is random, the expected number of pairs for each class (i.e., each element of the matrix of counts) is the same; that is, the pairs should be uniformly distributed over the unit square [0,1]2${\left[0,1\right]}^{2}$. Thus the expected number of pairs for each class is just the total number of pairs, j,k = 1mcjk$\sum _{j,k=1}^{m}{c}_{jk}$, divided by the number of classes, m2${m}^{2}$.
The χ2${\chi }^{2}$ test statistic used to test the hypothesis of randomness is defined as
 m X2 = ∑ ((cjk − e)2)/e, j,k = 1
$X2=∑j,k=1m (cjk-e) 2e,$
where e = j,k = 1mcjk / m2 = $e=\sum _{j,k=1}^{m}{c}_{jk}/{m}^{2}=\text{}$ expected number of pairs in each class.
The use of the χ2${\chi }^{2}$-distribution as an approximation to the exact distribution of the test statistic, X2${X}^{2}$, improves as the length of the sequence relative to m$m$ increases and hence the expected value, e$e$, increases.
nag_nonpar_randtest_pairs (g08eb) may be used in two different modes:
 (i) a single call to nag_nonpar_randtest_pairs (g08eb) which computes all test statistics after counting the pairs; (ii) multiple calls to nag_nonpar_randtest_pairs (g08eb) 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_pairs (g08eb).
cl = 'S'${\mathbf{cl}}=\text{'S'}$
This is the one and only call to nag_nonpar_randtest_pairs (g08eb) (single call mode). All data are to be input at once. All test statistics are computed after the counting of pairs is complete.
cl = 'F'${\mathbf{cl}}=\text{'F'}$
This is the first call to the function. All initializations are carried out and the counting of pairs begins. The final test statistics are not computed since further calls will be made to nag_nonpar_randtest_pairs (g08eb).
cl = 'I'${\mathbf{cl}}=\text{'I'}$
This is an intermediate call during which the counts of pairs are updated. The final test statistics are not computed since further calls will be made to nag_nonpar_randtest_pairs (g08eb).
cl = 'L'${\mathbf{cl}}=\text{'L'}$
This is the last call to nag_nonpar_randtest_pairs (g08eb). The test statistics are computed after the final counting of runs is complete.
Constraint: cl = 'S'${\mathbf{cl}}=\text{'S'}$, 'F'$\text{'F'}$, 'I'$\text{'I'}$ or 'L'$\text{'L'}$.
2:     x(n) – double array
n, the dimension of the array, must satisfy the constraint
• if cl = 'S'${\mathbf{cl}}=\text{'S'}$, n2${\mathbf{n}}\ge 2$;
• otherwise n1${\mathbf{n}}\ge 1$.
The sequence of observations.
Constraint: 0.0x(i)1.0$0.0\le {\mathbf{x}}\left(\mathit{i}\right)\le 1.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
3:     lag – int64int32nag_int scalar
l$l$, the lag to be used in choosing pairs.
If lag = 1${\mathbf{lag}}=1$, then we consider the pairs (x(i),x(i + 1))$\left({\mathbf{x}}\left(\mathit{i}\right),{\mathbf{x}}\left(\mathit{i}+1\right)\right)$, for i = 1,3,,n1$\mathit{i}=1,3,\dots ,n-1$, where n$n$ is the number of observations.
If lag > 1${\mathbf{lag}}>1$, then we consider the pairs (x(i),x(i + l))$\left({\mathbf{x}}\left(i\right),{\mathbf{x}}\left(i+l\right)\right)$, for i = 1,2,,l,2l + 1,2l + 2,,3l,4l + 1,,nl$i=1,2,\dots ,l,2l+1,2l+2,\dots ,3l,4l+1,\dots ,n-l$, where n$n$ is the number of observations.
lag must not be changed between calls to nag_nonpar_randtest_pairs (g08eb).
Constraints:
• lag1${\mathbf{lag}}\ge 1$;
• if cl = 'S'${\mathbf{cl}}=\text{'S'}$, lag < n${\mathbf{lag}}<{\mathbf{n}}$.
4:     ncount(ldc,msize) – int64int32nag_int array
ldc, the first dimension of the array, must satisfy the constraint ldcmsize$\mathit{ldc}\ge {\mathbf{msize}}$.
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'F'$\text{'F'}$, ncount need not be set.
If cl = 'I'${\mathbf{cl}}=\text{'I'}$ or 'L'$\text{'L'}$, ncount must contain the values returned by the previous call to nag_nonpar_randtest_pairs (g08eb).
5:     wrk(2 × lag$2×{\mathbf{lag}}$) – double array
wrk is used to store information between successive calls to nag_nonpar_randtest_pairs (g08eb) and therefore must not be changed.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array x.
n$n$, the number of observations.
Constraints:
• if cl = 'S'${\mathbf{cl}}=\text{'S'}$, n2${\mathbf{n}}\ge 2$;
• otherwise n1${\mathbf{n}}\ge 1$.
2:     msize – int64int32nag_int scalar
Default: The first dimension of the array ncount and the second dimension of the array ncount. (An error is raised if these dimensions are not equal.)
m$m$, the size of the matrix of counts.
msize must not be changed between calls to nag_nonpar_randtest_pairs (g08eb).
Constraint: msize2${\mathbf{msize}}\ge 2$.

ldc

### Output Parameters

1:     ncount(ldc,msize) – int64int32nag_int array
ldcmsize$\mathit{ldc}\ge {\mathbf{msize}}$.
Is an msize by msize matrix containing the counts of the number of pairs in each cell, cij${c}_{ij}$, for i = 1,2,,m$\mathit{i}=1,2,\dots ,m$ and j = 1,2,,m$\mathit{j}=1,2,\dots ,m$.
2:     ex – double scalar
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$ (i.e., if it is a final exit) then ex contains the expected number of counts in each cell, e$e$.
Otherwise ex is not set.
3:     chi – double scalar
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$ (i.e., if it is a final exit) then chi contains the χ2${\chi }^{2}$ test statistic, X2${X}^{2}$, for testing the null hypothesis of randomness.
Otherwise chi is not set.
4:     df – double scalar
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$ (i.e., if it is a final exit) then df contains the degrees of freedom for the χ2${\chi }^{2}$ statistic.
Otherwise df is not set.
5:     prob – double scalar
If cl = 'S'${\mathbf{cl}}=\text{'S'}$ or 'L'$\text{'L'}$ (i.e., if it is a final exit) then prob contains the upper tail probability associated with the χ2${\chi }^{2}$ test statistic, i.e., the significance level.
Otherwise prob is not set.
6:     wrk(2 × lag$2×{\mathbf{lag}}$) – double array
7:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Note: nag_nonpar_randtest_pairs (g08eb) 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 = 1${\mathbf{ifail}}=1$
On entry, cl = _${\mathbf{cl}}=_$.
Constraint: cl = 'S'${\mathbf{cl}}=\text{'S'}$, 'F'$\text{'F'}$, 'I'$\text{'I'}$ or 'L'$\text{'L'}$.
ifail = 2${\mathbf{ifail}}=2$
Constraint: if cl = 'S'${\mathbf{cl}}=\text{'S'}$, n2${\mathbf{n}}\ge 2$, otherwise n1${\mathbf{n}}\ge 1$.
ifail = 3${\mathbf{ifail}}=3$
Constraint: msize2${\mathbf{msize}}\ge 2$
ifail = 4${\mathbf{ifail}}=4$
Constraint: lag > 0${\mathbf{lag}}>0$ and if cl = 'S'${\mathbf{cl}}=\text{'S'}$, lag < n${\mathbf{lag}}<{\mathbf{n}}$.
ifail = 5${\mathbf{ifail}}=5$
Constraint: ldcmsize$\mathit{ldc}\ge {\mathbf{msize}}$.
ifail = 6${\mathbf{ifail}}=6$
On entry, at least one element of x is out of range.
Constraint: 0x(i)1$0\le {\mathbf{x}}\left(i\right)\le 1$, for i = 1,2,,n$i=1,2,\dots ,{\mathbf{n}}$. On entry, at least one element of x is out of range.
Constraint: 0x(i)1$0\le {\mathbf{x}}\left(i\right)\le 1$, for i = 1,2,,n$i=1,2,\dots ,{\mathbf{n}}$.
ifail = 7${\mathbf{ifail}}=7$
No pairs were found. This will occur if the value of lag is greater than or equal to the total number of observations.
W ifail = 8${\mathbf{ifail}}=8$
msize is too large relative to the number of pairs, therefore the expected value for at least one cell is less than or equal to 5.0$5.0$.

## Accuracy

The computations are believed to be stable. The computation of prob given the values of chi and df will obtain a relative accuracy of five significant figures for most cases.

If after forming the pairs in an initial or intermediate call to nag_nonpar_randtest_pairs (g08eb) there is an observation left over at the end of the sequence, this observation is used at the beginning of the new sequence provided by the following call to nag_nonpar_randtest_pairs (g08eb). Clearly an observation left over from an only or final call to nag_nonpar_randtest_pairs (g08eb) is ignored.
The time taken by the function increases with the number of observations n$n$, and also depends to some extent on whether the call to nag_nonpar_randtest_pairs (g08eb) is an only, first, intermediate or last call.

## Example

```function nag_nonpar_randtest_pairs_example
% Initialize the seed
seed = [int64(438532)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

lag = 1;
wrk = zeros(2*lag, 1);
ncount = zeros(10, 10, '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, wrk, ifail] = ...
nag_nonpar_randtest_pairs(cl, x, int64(lag), ncount, wrk);
end
if (ifail == 0 || ifail == 8)
[ifail] = nag_file_print_matrix_integer('General', ' ', ncount, 'Count matrix');
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
```
```
Count matrix
1  2  3  4  5  6  7  8  9 10
1  44 52 55 51 50 52 52 46 48 61
2  51 40 46 53 44 53 31 31 48 44
3  57 40 43 49 51 45 49 54 63 48
4  51 50 58 65 51 51 43 51 52 47
5  55 45 42 41 57 66 53 60 46 59
6  57 47 42 45 57 47 41 51 41 48
7  53 68 50 49 49 42 48 53 51 49
8  51 63 49 45 59 52 63 43 47 52
9  48 48 41 65 46 52 52 45 51 55
10  35 43 45 42 56 52 56 52 59 51

Expected value =    50.00
Chisq          =    96.7200
DF             =    99.0
Probability    =     0.5461

```
```function g08eb_example
% Initialize the seed
seed = [int64(438532)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

lag = 1;
wrk = zeros(2*lag, 1);
ncount = zeros(10, 10, '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, wrk, ifail] = g08eb(cl, x, int64(lag), ncount, wrk);
end
if (ifail == 0 || ifail == 8)
[ifail] = x04ea('General', ' ', ncount, 'Count matrix');
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
```
```
Count matrix
1  2  3  4  5  6  7  8  9 10
1  44 52 55 51 50 52 52 46 48 61
2  51 40 46 53 44 53 31 31 48 44
3  57 40 43 49 51 45 49 54 63 48
4  51 50 58 65 51 51 43 51 52 47
5  55 45 42 41 57 66 53 60 46 59
6  57 47 42 45 57 47 41 51 41 48
7  53 68 50 49 49 42 48 53 51 49
8  51 63 49 45 59 52 63 43 47 52
9  48 48 41 65 46 52 52 45 51 55
10  35 43 45 42 56 52 56 52 59 51

Expected value =    50.00
Chisq          =    96.7200
DF             =    99.0
Probability    =     0.5461

```