# NAG Library Function Document

## 1Purpose

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

## 2Specification

 #include #include
 void nag_pairs_test (Integer n, const double x[], Integer max_count, Integer lag, double *chi, double *df, double *prob, NagError *fail)

## 3Description

nag_pairs_test (g08ebc) computes the statistics for performing a pairs 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.
For a given lag, $l\ge 1$, an $m$ by $m$ matrix, $C$, of counts is formed as follows. The element ${c}_{jk}$ of $C$ is the number of pairs $\left({x}_{i},{x}_{i+l}\right)$ such that
 $j-1m≤xi
 $k- 1m≤xi+l
where $i=1,3,5,\dots ,n-1$ if $l=1$, and $i=1,2,\dots ,l,2l+1,2l+2,\dots 3l,4l+1,\dots ,n-l$, if $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 ${\left[0,1\right]}^{2}$. Thus the expected number of pairs for each class is just the total number of pairs, $\sum _{j,k=1}^{m}{c}_{jk}$, divided by the number of classes, ${m}^{2}$.
The ${\chi }^{2}$ test statistic used to test the hypothesis of randomness is defined as
 $X2=∑j,k=1m cjk-e 2e,$
where $e=\sum _{j,k=1}^{m}{c}_{jk}/{m}^{2}=\text{}$ expected number of pairs 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.

## 4References

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{n}$IntegerInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
2:    $\mathbf{x}\left[{\mathbf{n}}\right]$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:    $\mathbf{max_count}$IntegerInput
On entry: $m$, the size of the matrix of counts.
Constraint: ${\mathbf{max_count}}\ge 2$.
4:    $\mathbf{lag}$IntegerInput
On entry: $l$, the lag to be used in choosing pairs.
If ${\mathbf{lag}}=1$, then we consider the pairs $\left({\mathbf{x}}\left[\mathit{i}-1\right],{\mathbf{x}}\left[\mathit{i}\right]\right)$, for $\mathit{i}=1,3,\dots ,n-1$, where $n$ is the number of observations.
If ${\mathbf{lag}}>1$, then we consider the pairs $\left({\mathbf{x}}\left[i-1\right],{\mathbf{x}}\left[i+l-1\right]\right)$, for $i=1,2,\dots ,l,2l+1,2l+2,\dots ,3l,4l+1,\dots ,n-l$, where $n$ is the number of observations.
Constraint: $1\le {\mathbf{lag}}<{\mathbf{n}}$.
5:    $\mathbf{chi}$double *Output
On exit: contains the ${\chi }^{2}$ test statistic, ${X}^{2}$, for testing the null hypothesis of randomness.
6:    $\mathbf{df}$double *Output
On exit: contains the degrees of freedom for the ${\chi }^{2}$ statistic.
7:    $\mathbf{prob}$double *Output
On exit: contains the upper tail probability associated with the ${\chi }^{2}$ test statistic, i.e., the significance level.
8:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_G08EB_CELL
max_count 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$.
This implies that the ${\chi }^{2}$ distribution may not be a very good approximation to the distribution of test statistic.
${\mathbf{max_count}}=〈\mathit{\text{value}}〉$, number of pairs $\text{}=〈\mathit{\text{value}}〉$ and expected value $=〈\mathit{\text{value}}〉$.
All statistics are returned and may still be of use.
NE_G08EB_PAIRS
No pairs were found. This will occur if the value of lag is greater than or equal to the total number of observations.
NE_INT_2
On entry, ${\mathbf{lag}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: $1\le {\mathbf{lag}}<{\mathbf{n}}$.
NE_INT_ARG_LE
On entry, ${\mathbf{max_count}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{max_count}}\ge 2$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 2$
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.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
NE_REAL_ARRAY_CONS
On entry, at least one element of x is out of range.
Constraint: $0\le {\mathbf{x}}\left[i-1\right]\le 1$, for $i=1,2,\dots ,{\mathbf{n}}$.

## 7Accuracy

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.

## 8Parallelism and Performance

nag_pairs_test (g08ebc) is not threaded in any implementation.

The time taken by the function increases with the number of observations $n$.

## 10Example

The following program performs the pairs test on $10000$ pseudorandom numbers taken from a uniform distribution $U\left(0,1\right)$, generated by nag_rand_basic (g05sac). nag_pairs_test (g08ebc) is called with ${\mathbf{lag}}=1$ and ${\mathbf{max_count}}=10$..

### 10.1Program Text

Program Text (g08ebce.c)

None.

### 10.3Program Results

Program Results (g08ebce.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017