hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_nonpar_randtest_gaps (g08ed)

Purpose

nag_nonpar_randtest_gaps (g08ed) performs a gaps test on a sequence of observations.

Syntax

[ngaps, ncount, ex, chi, df, prob, ifail] = g08ed(cl, x, m, rlo, rup, totlen, ngaps, ncount, 'n', n, 'maxg', maxg)
[ngaps, ncount, ex, chi, df, prob, ifail] = nag_nonpar_randtest_gaps(cl, x, m, rlo, rup, totlen, ngaps, ncount, 'n', n, 'maxg', maxg)

Description

Gaps tests are used to test for cyclical trend in a sequence of observations. nag_nonpar_randtest_gaps (g08ed) computes certain statistics for the gaps test.
nag_nonpar_randtest_gaps (g08ed) may be used in two different modes:
(i) a single call to nag_nonpar_randtest_gaps (g08ed) which computes all test statistics after counting the gaps;
(ii) multiple calls to nag_nonpar_randtest_gaps (g08ed) with the final test statistics only being computed in the last call.
The second mode is necessary if all the data does not fit into the memory. See parameter cl in Section [Parameters] for details on how to invoke each mode.
The term gap is used to describe the distance between two numbers in the sequence that lie in the interval (rl,ru)(rl,ru). That is, a gap ends at xjxj if rlxjrurlxjru. The next gap then begins at xj + 1xj+1. The interval (rl,ru)(rl,ru) should lie within the region of all possible numbers. For example if the test is carried out on a sequence of (0,1)(0,1) random numbers then the interval (rl,ru)(rl,ru) must be contained in the whole interval (0,1)(0,1). Let tlentlen be the length of the interval which specifies all possible numbers.
nag_nonpar_randtest_gaps (g08ed) counts the number of gaps of different lengths. Let cici denote the number of gaps of length ii, for i = 1,2,,k1i=1,2,,k-1. The number of gaps of length kk or greater is then denoted by ckck. An unfinished gap at the end of a sequence is not counted unless the sequence is part of an initial or intermediate call to nag_nonpar_randtest_gaps (g08ed) (i.e., unless there is another call to nag_nonpar_randtest_gaps (g08ed) to follow) in which case the unfinished gap is used. The following is a trivial example.
Suppose we called nag_nonpar_randtest_gaps (g08ed) twice (i.e., with cl = 'F'cl='F' and then with cl = 'L'cl='L') with the following two sequences and with rlo = 0.30rlo=0.30 and rup = 0.60rup=0.60:
Then after the second call nag_nonpar_randtest_gaps (g08ed) would have counted the gaps of the following lengths:
When the counting of gaps is complete nag_nonpar_randtest_gaps (g08ed) computes the expected values of the counts. An approximate χ2χ2 statistic with kk degrees of freedom is computed where
X2 = (i = 1k(ciei)2)/(ei),
X2=i=1k (ci-ei) 2ei,
where
The use of the χ2χ2-distribution as an approximation to the exact distribution of the test statistic improves as the expected values increase.
You may specify the total number of gaps to be found. If the specified number of gaps is found before the end of a sequence nag_nonpar_randtest_gaps (g08ed) will exit before counting any further gaps.

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:     cl – string (length ≥ 1)
Indicates the type of call to nag_nonpar_randtest_gaps (g08ed).
cl = 'S'cl='S'
This is the one and only call to nag_nonpar_randtest_gaps (g08ed) (single call mode). All data are to be input at once. All test statistics are computed after the counting of gaps is complete.
cl = 'F'cl='F'
This is the first call to the function. All initializations are carried out before the counting of gaps begins. The final test statistics are not computed since further calls will be made to nag_nonpar_randtest_gaps (g08ed).
cl = 'I'cl='I'
This is an intermediate call during which the counts of gaps are updated. The final test statistics are not computed since further calls will be made to nag_nonpar_randtest_gaps (g08ed).
cl = 'L'cl='L'
This is the last call to nag_nonpar_randtest_gaps (g08ed). The test statistics are computed after the final counting of gaps is complete.
Constraint: cl = 'S'cl='S', 'F''F', 'I''I' or 'L''L'.
2:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n1n1.
The sequence of observations.
3:     m – int64int32nag_int scalar
The maximum number of gaps to be sought. If m0m0 then there is no limit placed on the number of gaps that are found.
m should not be changed between calls to nag_nonpar_randtest_gaps (g08ed).
Constraint: if cl = 'S'cl='S', mnmn.
4:     rlo – double scalar
The lower limit of the interval to be used to define the gaps, rlrl.
rlo must not be changed between calls to nag_nonpar_randtest_gaps (g08ed).
5:     rup – double scalar
The upper limit of the interval to be used to define the gaps, ruru.
rup must not be changed between calls to nag_nonpar_randtest_gaps (g08ed).
Constraint: rup > rlorup>rlo.
6:     totlen – double scalar
The total length of the interval which contains all possible numbers that may arise in the sequence.
Constraint: totlen > 0.0totlen>0.0 and ruprlo < totlenrup-rlo<totlen.
7:     ngaps – int64int32nag_int scalar
If cl = 'S'cl='S' or 'F''F', ngaps need not be set.
If cl = 'I'cl='I' or 'L''L', ngaps must contain the value returned by the previous call to nag_nonpar_randtest_gaps (g08ed).
8:     ncount(maxg) – int64int32nag_int array
maxg, the dimension of the array, must satisfy the constraint
  • maxg > 1maxg>1
  • if cl = 'S'cl='S', maxgnmaxgn
  • .
    If cl = 'S'cl='S' or 'F''F', ncount need not be set.
    If cl = 'I'cl='I' or 'L''L', ncount must contain the values returned by the previous call to nag_nonpar_randtest_gaps (g08ed).

    Optional Input Parameters

    1:     n – int64int32nag_int scalar
    Default: The dimension of the array x.
    nn, the length of the current sequence of observations.
    Constraint: n1n1.
    2:     maxg – int64int32nag_int scalar
    Default: The dimension of the array ncount.
    kk, the length of the longest gap for which tabulation is desired.
    maxg must not be changed between calls to nag_nonpar_randtest_gaps (g08ed).
    Constraints:
    • maxg > 1maxg>1;
    • if cl = 'S'cl='S', maxgnmaxgn.

    Input Parameters Omitted from the MATLAB Interface

    None.

    Output Parameters

    1:     ngaps – int64int32nag_int scalar
    The number of gaps actually found, ngapsngaps.
    2:     ncount(maxg) – int64int32nag_int array
    The counts of gaps of the different lengths, cici, for i = 1,2,,ki=1,2,,k.
    3:     ex(maxg) – double array
    If cl = 'S'cl='S' or 'L''L' (i.e., if it is a final exit) then ex contains the expected values of the counts.
    Otherwise the elements of ex are not set.
    4:     chi – double scalar
    If cl = 'S'cl='S' or 'L''L' (i.e., if it is a final exit) then chi contains the χ2χ2 test statistic, X2X2, for testing the null hypothesis of randomness.
    Otherwise chi is not set.
    5:     df – double scalar
    If cl = 'S'cl='S' or 'L''L' (i.e., if it is a final exit) then df contains the degrees of freedom for the χ2χ2 statistic.
    Otherwise df is not set.
    6:     prob – double scalar
    If cl = 'S'cl='S' or 'L''L' (i.e., if it is a final exit) then prob contains the upper tail probability associated with the χ2χ2 test statistic, i.e., the significance level.
    Otherwise prob is not set.
    7:     ifail – int64int32nag_int scalar
    ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

    Error Indicators and Warnings

    Note: nag_nonpar_randtest_gaps (g08ed) 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.

      ifail = 1ifail=1
    On entry, cl = _cl=_.
    Constraint: cl = 'S'cl='S', 'F''F', 'I''I' or 'L''L'.
      ifail = 2ifail=2
    Constraint: n1n1.
      ifail = 3ifail=3
    Constraint: if cl = 'S'cl='S', mnmn.
      ifail = 4ifail=4
    Constraint: if cl = 'S'cl='S', maxgnmaxgn.
    Constraint: maxg > 1maxg>1.
      ifail = 5ifail=5
    Constraint: rup > rlorup>rlo.
    Constraint: ruprlo < totlenrup-rlo<totlen.
    Constraint: totlen > 0.0totlen>0.0.
      ifail = 6ifail=6
    No gaps were found. Try using a longer sequence, or increase the size of the interval ruprlorup-rlo.
      ifail = 7ifail=7
    The expected frequency in class is zero. The value of (ruprlo) / totlen(rup-rlo)/totlen may be too close to 0.0​ or ​1.00.0​ or ​1.0. or maxg is too large relative to the number of gaps found.
    W ifail = 8ifail=8
    The number of gaps requested were not found, only __ out of the requested __ where found.
    All statistics are returned and may still be of use.
    W ifail = 9ifail=9
    The expected frequency of at least one class is less than one.
    This implies that the χ2χ2 may not be a very good approximation to the distribution of the test statistics.
    All statistics are returned and may still be of use.

    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 places for most cases.

    Further Comments

    The time taken by nag_nonpar_randtest_gaps (g08ed) increases with the number of observations nn, and depends to some extent whether the call is an only, first, intermediate or last call.

    Example

    function nag_nonpar_randtest_gaps_example
    % Initialize the seed
    seed = [int64(424232)];
    % genid and subid identify the base generator
    genid = int64(1);
    subid =  int64(1);
    
    m = int64(0);
    rlo = 0.4;
    rup = 0.6;
    totlen = 1;
    ncount = zeros(10, 1, 'int64');
    ngaps = int64(0);
    
    % Initialize the generator to a repeatable sequence
    [state, ifail] = nag_rand_init_repeat(genid, subid, seed);
    for i=1:5
      % Generate some U(0,1) values
      [state, x, ifail] = nag_rand_dist_uniform(int64(1000), 0, 1, state);
      if i==1
        cl = 'F';
      elseif i==5
        cl = 'L';
      else
        cl = 'I';
      end
      [ngaps, ncount, ex, chi, df, prob, ifail] = ...
              nag_nonpar_randtest_gaps(cl, x, m, rlo, rup, totlen, ngaps, ncount);
    end
    if (ifail == 0 || ifail >= 8)
      fprintf('\nTotal number of gaps found = %d\n', ngaps);
      if ifail == 8
        fprintf(' ** Note : the number of gaps requested were not found.\n');
      end
      fprintf('\nCount\n      ');
      fprintf('0      1      2      3      4      5      6      7      8     >9\n');
      for i=1:numel(ncount)
        fprintf('%7d', ncount(i));
      end
      fprintf('\n\nExpect\n      ');
      fprintf('0      1      2      3      4      5      6      7      8     >9\n');
      for i=1:numel(ex)
        fprintf('%7.1f', ex(i));
      end
      fprintf('\n\nChisq = %10.4f\n', chi);
      fprintf('DF    = %7.1f\n', df);
      fprintf('Prob  = %10.4f\n', prob);
      if ifail == 9
        fprintf('\n** Note : the chi square approximation may not be very good.\n');
      end
    end
    
     
    
    Total number of gaps found = 1007
    
    Count
          0      1      2      3      4      5      6      7      8     >9
        220    158    127     96     79     79     44     43     30    131
    
    Expect
          0      1      2      3      4      5      6      7      8     >9
      201.4  161.1  128.9  103.1   82.5   66.0   52.8   42.2   33.8  135.2
    
    Chisq =     7.0401
    DF    =     9.0
    Prob  =     0.6329
    
    
    function g08ed_example
    % Initialize the seed
    seed = [int64(424232)];
    % genid and subid identify the base generator
    genid = int64(1);
    subid =  int64(1);
    
    m = int64(0);
    rlo = 0.4;
    rup = 0.6;
    totlen = 1;
    ncount = zeros(10, 1, 'int64');
    ngaps = int64(0);
    
    % Initialize the generator to a repeatable sequence
    [state, ifail] = g05kf(genid, subid, seed);
    for i=1:5
      % Generate some U(0,1) values
      [state, x, ifail] = g05sq(int64(1000), 0, 1, state);
      if i==1
        cl = 'F';
      elseif i==5
        cl = 'L';
      else
        cl = 'I';
      end
      [ngaps, ncount, ex, chi, df, prob, ifail] = ...
              g08ed(cl, x, m, rlo, rup, totlen, ngaps, ncount);
    end
    if (ifail == 0 || ifail >= 8)
      fprintf('\nTotal number of gaps found = %d\n', ngaps);
      if ifail == 8
        fprintf(' ** Note : the number of gaps requested were not found.\n');
      end
      fprintf('\nCount\n      ');
      fprintf('0      1      2      3      4      5      6      7      8     >9\n');
      for i=1:numel(ncount)
        fprintf('%7d', ncount(i));
      end
      fprintf('\n\nExpect\n      ');
      fprintf('0      1      2      3      4      5      6      7      8     >9\n');
      for i=1:numel(ex)
        fprintf('%7.1f', ex(i));
      end
      fprintf('\n\nChisq = %10.4f\n', chi);
      fprintf('DF    = %7.1f\n', df);
      fprintf('Prob  = %10.4f\n', prob);
      if ifail == 9
        fprintf('\n** Note : the chi square approximation may not be very good.\n');
      end
    end
    
     
    
    Total number of gaps found = 1007
    
    Count
          0      1      2      3      4      5      6      7      8     >9
        220    158    127     96     79     79     44     43     30    131
    
    Expect
          0      1      2      3      4      5      6      7      8     >9
      201.4  161.1  128.9  103.1   82.5   66.0   52.8   42.2   33.8  135.2
    
    Chisq =     7.0401
    DF    =     9.0
    Prob  =     0.6329
    
    

    PDF version (NAG web site, 64-bit version, 64-bit version)
    Chapter Contents
    Chapter Introduction
    NAG Toolbox

    © The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013