NAG Library Routine Document
g08ebf (randtest_pairs)
1
Purpose
g08ebf performs a pairs test on a sequence of observations in the interval $\left[0,1\right]$.
2
Specification
Fortran Interface
Subroutine g08ebf ( 
cl, n, x, msize, lag, ncount, ldc, ex, chi, df, prob, wrk, ifail) 
Integer, Intent (In)  ::  n, msize, lag, ldc  Integer, Intent (Inout)  ::  ncount(ldc,msize), ifail  Real (Kind=nag_wp), Intent (In)  ::  x(n)  Real (Kind=nag_wp), Intent (Inout)  ::  wrk(2*lag)  Real (Kind=nag_wp), Intent (Out)  ::  ex, chi, df, prob  Character (1), Intent (In)  ::  cl 

C Header Interface
#include <nagmk26.h>
void 
g08ebf_ (const char *cl, const Integer *n, const double x[], const Integer *msize, const Integer *lag, Integer ncount[], const Integer *ldc, double *ex, double *chi, double *df, double *prob, double wrk[], Integer *ifail, const Charlen length_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
where
$i=1,3,5,\dots ,n1$ if
$l=1$, and
$i=1,2,\dots ,l,2l+1,2l+2,\dots 3l,4l+1,\dots ,nl$, if
$l>1$.
Note that all pairs formed are nonoverlapping 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
where
$e={\displaystyle \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 argument
cl in
Section 5 for details on how to invoke each mode.
4
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
5
Arguments
 1: $\mathbf{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: $\mathbf{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: $\mathbf{x}\left({\mathbf{n}}\right)$ – 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: $\mathbf{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: $\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}\right),{\mathbf{x}}\left(\mathit{i}+1\right)\right)$, for $\mathit{i}=1,3,\dots ,n1$, 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 ,nl$, 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: $\mathbf{ncount}\left({\mathbf{ldc}},{\mathbf{msize}}\right)$ – 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: $\mathbf{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: $\mathbf{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$.
 9: $\mathbf{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: $\mathbf{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.
 11: $\mathbf{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: $\mathbf{wrk}\left(2\times {\mathbf{lag}}\right)$ – Real (Kind=nag_wp) arrayCommunication Array

wrk is used to store information between successive calls to
g08ebf and therefore must not be changed.
 13: $\mathbf{ifail}$ – IntegerInput/Output

On entry:
ifail must be set to
$0$,
$1\text{or}1$. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation 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 arguments 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}}=0$ or
$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}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{cl}}=\text{'S'}$, $\text{'F'}$, $\text{'I'}$ or $\text{'L'}$.
 ${\mathbf{ifail}}=2$

On entry, ${\mathbf{n}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{n}}\ge 2$, otherwise ${\mathbf{n}}\ge 1$.
 ${\mathbf{ifail}}=3$

On entry, ${\mathbf{msize}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{msize}}\ge 2$
 ${\mathbf{ifail}}=4$

On entry, ${\mathbf{lag}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{n}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{lag}}>0$ and if ${\mathbf{cl}}=\text{'S'}$, ${\mathbf{lag}}<{\mathbf{n}}$.
 ${\mathbf{ifail}}=5$

On entry, ${\mathbf{ldc}}=\u2329\mathit{\text{value}}\u232a$ and ${\mathbf{msize}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{ldc}}\ge {\mathbf{msize}}$.
 ${\mathbf{ifail}}=6$

On entry, at least one element of
x is out of range.
Constraint:
$0\le {\mathbf{x}}\left(i\right)\le 1$, for
$i=1,2,\dots ,{\mathbf{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$

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$.
This implies that the
${\chi}^{2}$ distribution may not be a very good approximation to the distribution of test statistic.
${\mathbf{msize}}=\u2329\mathit{\text{value}}\u232a$, number of pairs
$\text{}=\u2329\mathit{\text{value}}\u232a$ and expected value
$=\u2329\mathit{\text{value}}\u232a$.
All statistics are returned and may still be of use.
 ${\mathbf{ifail}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{ifail}}=399$
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{ifail}}=999$
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
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.
8
Parallelism and Performance
g08ebf is not thread safe and should not be called from a multithreaded user program. Please see
Section 3.12.1 in How to Use the NAG Library and its Documentation for more information on thread safety.
g08ebf is not threaded in any implementation.
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.
10
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.
10.1
Program Text
Program Text (g08ebfe.f90)
10.2
Program Data
Program Data (g08ebfe.d)
10.3
Program Results
Program Results (g08ebfe.r)