nag_pairs_test (g08ebc) (PDF version)
g08 Chapter Contents
g08 Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_pairs_test (g08ebc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_pairs_test (g08ebc) performs a pairs test on a sequence of observations in the interval 0,1 .

2  Specification

#include <nag.h>
#include <nagg08.h>
void  nag_pairs_test (Integer n, const double x[], Integer max_count, Integer lag, double *chi, double *df, double *prob, NagError *fail)

3  Description

nag_pairs_test (g08ebc) computes the statistics for performing a pairs test which may be used to investigate deviations from randomness in a sequence of 0,1  observations.
For a given lag, l1 , an m  by m  matrix, C , of counts is formed as follows. The element c jk  of C  is the number of pairs (x i ,x i+1 ) such that
where i=1,3,5,,n-1  if l=1 , and i=1,2,,l,2l+1,2l+2,,3l,4l+1,,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 0,1 2 . Thus the expected number of pairs for each class is just the total number of pairs, j , k = 1 m c jk , divided by the number of classes, m 2 .
The χ 2  test statistic used to test the hypothesis of randomness is defined as:
X 2 = j , k = 1 m c jk - e 2 e
where e = j , k = 1 m c jk / m 2 =  expected number of pairs in each class.
The use of the χ 2  distribution as an approximation to the exact distribution of the test statistic, x 2 , improves as the expected value, e , increases.

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:     nIntegerInput
On entry: the number of observations, n .
Constraint: n2 .
2:     x[n]const doubleInput
On entry: the sequence of observations.
Constraint: 0.0 x[i-1] 1.0 , for i=1,2,,n.
3:     max_countIntegerInput
On entry: the size of the matrix of counts, m .
Constraint: max_count2 .
4:     lagIntegerInput
On entry: the lag, l , to be used in choosing pairs.
We consider the pairs x[i-1],x[i] , for i=1,2,,n - 1, where n  is the number of observations.
We consider the pairs x[i-1],x[ x + l - 1 ] , for i=1,2,,l , 2 l + 1 , 2 l + 2 , , 3 l , 4 l + 1 , , n-l, where n  is the number of observations.
Constraint: lag>0 , ​ lag<n .
5:     chidouble *Output
On exit: contains the χ 2  test statistic, X 2 , for testing the null hypothesis of randomness.
6:     dfdouble *Output
On exit: contains the degrees of freedom for the χ 2  statistic.
7:     probdouble *Output
On exit: contains the upper tail probability associated with the χ 2  test statistic, i.e., the significance level.
8:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

Dynamic memory allocation failed.
The expected value for each cell is less than or equal to 5.0. This implies that the χ 2  distribution may not be a very good approximation to the test statistic.
No pairs were found. This will occur if the value of lag is greater than or equal to the total number of observations.
On entry, lag=value , n=value .
Constraint: 1 lag < n .
On entry, max_count must not be less than or equal to 1: max_count=value .
On entry, n=value.
Constraint: n2.
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.
On entry, x[0] = value.
Constraint: 0.0 x[i-1] 1.0 , for i=1,2,,n - 1.

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  Further Comments

The time taken by nag_pairs_test (g08ebc) increases with the number of observations, n .

9  Example

The following program performs the pairs test on 10000 pseudorandom numbers from a uniform distribution U 0,1  generated by nag_rand_basic (g05sac). nag_pairs_test (g08ebc) is called with lag=1  and m=10 .

9.1  Program Text

Program Text (g08ebce.c)

9.2  Program Data


9.3  Program Results

Program Results (g08ebce.r)

nag_pairs_test (g08ebc) (PDF version)
g08 Chapter Contents
g08 Chapter Introduction
NAG C Library Manual

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