G08 Chapter Contents
G08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG08EBF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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

## 2  Specification

 SUBROUTINE G08EBF ( CL, N, X, MSIZE, LAG, NCOUNT, LDC, EX, CHI, DF, PROB, WRK, IFAIL)
 INTEGER N, MSIZE, LAG, NCOUNT(LDC,MSIZE), LDC, IFAIL REAL (KIND=nag_wp) X(N), EX, CHI, DF, PROB, WRK(2*LAG) CHARACTER(1) CL

## 3  Description

G08EBF 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.
G08EBF may be used in two different modes:
 (i) a single call to G08EBF which computes all test statistics after counting the pairs; (ii) multiple calls to G08EBF 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 5 for details on how to invoke each mode.
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  Parameters

1:     CL – CHARACTER(1)Input
On entry: indicates the type of call to G08EBF.
${\mathbf{CL}}=\text{'S'}$
This is the one and only call to G08EBF (single call mode). All data are to be input at once. All test statistics are computed after the counting of pairs is complete.
${\mathbf{CL}}=\text{'F'}$
This is the first call to the routine. 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 G08EBF.
${\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 G08EBF.
${\mathbf{CL}}=\text{'L'}$
This is the last call to G08EBF. The test statistics are computed after the final counting of runs is complete.
Constraint: ${\mathbf{CL}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
2:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraints:
• if ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{N}}\ge 2$;
• otherwise ${\mathbf{N}}\ge 1$.
3:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the sequence of observations.
Constraint: $0.0\le {\mathbf{X}}\left(\mathit{i}\right)\le 1.0$, for $\mathit{i}=1,2,\dots ,n$.
4:     MSIZE – INTEGERInput
On entry: $m$, the size of the matrix of counts.
MSIZE must not be changed between calls to G08EBF.
Constraint: ${\mathbf{MSIZE}}\ge 2$.
5:     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}\right),{\mathbf{X}}\left(\mathit{i}+1\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\right),{\mathbf{X}}\left(i+l\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.
LAG must not be changed between calls to G08EBF.
Constraints:
• ${\mathbf{LAG}}\ge 1$;
• if ${\mathbf{CL}}=\text{'S'}$, ${\mathbf{LAG}}<{\mathbf{N}}$.
6:     NCOUNT(LDC,MSIZE) – INTEGER arrayInput/Output
On entry: 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 G08EBF.
On exit: is an MSIZE by MSIZE matrix containing the counts of the number of pairs in each cell, ${c}_{ij}$, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,m$.
7:     LDC – INTEGERInput
On entry: the first dimension of the array NCOUNT as declared in the (sub)program from which G08EBF is called.
Constraint: ${\mathbf{LDC}}\ge {\mathbf{MSIZE}}$.
8:     EX – REAL (KIND=nag_wp)Output
On exit: if ${\mathbf{CL}}=\text{'S'}$ or $\text{'L'}$ (i.e., if it is a final exit) then EX contains the expected number of counts in each cell, $e$.
Otherwise EX is not set.
9:     CHI – REAL (KIND=nag_wp)Output
On exit: 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.
10:   DF – REAL (KIND=nag_wp)Output
On exit: 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.
Otherwise DF is not set.
11:   PROB – REAL (KIND=nag_wp)Output
On exit: 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.
12:   WRK($2×{\mathbf{LAG}}$) – REAL (KIND=nag_wp) arrayCommunication Array
WRK is used to store information between successive calls to G08EBF and therefore must not be changed.
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}1$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: G08EBF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{CL}}\ne \text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{N}}<1$. or ${\mathbf{CL}}=\text{'S'}$ and ${\mathbf{N}}<2$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{MSIZE}}\le 1$.
${\mathbf{IFAIL}}=4$
 On entry, ${\mathbf{LAG}}<1$, or ${\mathbf{CL}}=\text{'S'}$ and ${\mathbf{LAG}}\ge {\mathbf{N}}$.
${\mathbf{IFAIL}}=5$
 On entry, ${\mathbf{LDC}}<{\mathbf{MSIZE}}$.
${\mathbf{IFAIL}}=6$
 On entry, ${\mathbf{X}}\left(i\right)<0.0$, or ${\mathbf{X}}\left(i\right)>1.0$ for some $i=1,2,\dots ,n$.
${\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.
${\mathbf{IFAIL}}=8$
The expected value for each 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 the test statistic.

## 7  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 G08EBF 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 G08EBF. Clearly an observation left over from an only or final call to G08EBF is ignored.
The time taken by the routine increases with the number of observations $n$, and also depends to some extent on whether the call to G08EBF is an only, first, intermediate or last call.

## 9  Example

The following program performs the pairs test on $500$ pseudorandom numbers. G08EBF is called $5$ times with $100$ observations on each call. ${\mathbf{LAG}}=1$ is used and the pairs are tallied into a $5$ by $5$ matrix.

### 9.1  Program Text

Program Text (g08ebfe.f90)

### 9.2  Program Data

Program Data (g08ebfe.d)

### 9.3  Program Results

Program Results (g08ebfe.r)