PDF version (NAG web site
, 64-bit version, 64-bit version)
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 .
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, , of observations.
For a given lag,
, an
by
matrix,
, of counts is formed as follows. The element
of
is the number of pairs
such that
where
if
, and
, if
.
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 . Thus the expected number of pairs for each class is just the total number of pairs, , divided by the number of classes, .
The
test statistic used to test the hypothesis of randomness is defined as
where
expected number of pairs in each class.
The use of the -distribution as an approximation to the exact distribution of the test statistic, , improves as the length of the sequence relative to increases and hence the expected value, , 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 argument
cl in
Arguments 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:
– string (length ≥ 1)
-
Indicates the type of call to
nag_nonpar_randtest_pairs (g08eb).
- 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.
- 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).
- 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).
- 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:
, , or .
- 2:
– double array
-
The sequence of observations.
Constraint:
, for .
- 3:
– int64int32nag_int scalar
-
, the lag to be used in choosing pairs.
If , then we consider the pairs , for , where is the number of observations.
If , then we consider the pairs , for , where is the number of observations.
lag must not be changed between calls to
nag_nonpar_randtest_pairs (g08eb).
- 4:
– int64int32nag_int array
-
ldc, the first dimension of the array, must satisfy the constraint
.
If
or
,
ncount need not be set.
If
or
,
ncount must contain the values returned by the previous call to
nag_nonpar_randtest_pairs (g08eb).
- 5:
– 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:
– int64int32nag_int scalar
-
Default:
the dimension of the array
x.
, the number of observations.
Constraints:
- if , ;
- otherwise .
- 2:
– 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.)
, the size of the matrix of counts.
msize must not be changed between calls to
nag_nonpar_randtest_pairs (g08eb).
Constraint:
.
Output Parameters
- 1:
– int64int32nag_int array
-
Is an
msize by
msize matrix containing the counts of the number of pairs in each cell,
, for
and
.
- 2:
– double scalar
-
If
or
(i.e., if it is a final exit) then
ex contains the expected number of counts in each cell,
.
- 3:
– double scalar
-
If
or
(i.e., if it is a final exit) then
chi contains the
test statistic,
, for testing the null hypothesis of randomness.
Otherwise
chi is not set.
- 4:
– double scalar
-
If
or
(i.e., if it is a final exit) then
df contains the degrees of freedom for the
statistic.
- 5:
– double scalar
-
If
or
(i.e., if it is a final exit) then
prob contains the upper tail probability associated with the
test statistic, i.e., the significance level.
Otherwise
prob is not set.
- 6:
– double array
-
- 7:
– int64int32nag_int scalar
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.
-
-
On entry, .
Constraint: , , or .
-
-
Constraint: if , , otherwise .
-
-
Constraint:
-
-
Constraint: and if , .
-
-
Constraint: .
-
-
On entry, at least one element of
x is out of range.
Constraint:
, for
.
-
-
No pairs were found. This will occur if the value of
lag is greater than or equal to the total number of observations.
- W
-
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
.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
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.
Further Comments
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 , 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
The following program performs the pairs test on pseudorandom numbers. nag_nonpar_randtest_pairs (g08eb) is called times with observations on each call. is used and the pairs are tallied into a by matrix.
Open in the MATLAB editor:
g08eb_example
function g08eb_example
fprintf('g08eb example results\n\n');
seed = [int64(324213)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);
lag = int64(1);
wrk = zeros(2*lag, 1);
m = 5;
ncount = zeros(m, m, 'int64');
n = int64(100);
nsampl = 5;
cl = 'F';
for i=1:nsampl
[state, x, ifail] = g05sq( ...
n, 0, 1, state);
[ncount, ex, chi, df, prob, wrk, ifail] = ...
g08eb( ...
cl, x, lag, ncount, wrk);
cl = 'I';
if i==nsampl-1
cl = 'L';
end
end
[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);
g08eb example results
Count matrix
1 2 3 4 5
1 7 10 5 16 8
2 9 10 7 6 8
3 13 15 10 10 12
4 10 21 7 5 13
5 13 5 10 12 8
Expected value = 10.00
Chisq = 34.8000
DF = 24.0
Probability = 0.0714
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015