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_rand_sample (g05nd)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_rand_sample (g05nd) selects a pseudorandom sample without replacement from an integer vector.

Syntax

[isampl, state, ifail] = g05nd(ipop, m, state, 'n', n)
[isampl, state, ifail] = nag_rand_sample(ipop, m, state, 'n', n)

Description

nag_rand_sample (g05nd) selects m elements from a population vector ipop of length n and places them in a sample vector isampl. Their order in ipop will be preserved in isampl. Each of the n m  possible combinations of elements of isampl may be regarded as being equally probable.
For moderate or large values of n it is theoretically impossible that all combinations of size m may occur, unless m is near 1 or near n. This is because n m  exceeds the cycle length of any of the base generators. For practical purposes this is irrelevant, as the time taken to generate all possible combinations is many millenia.
One of the initialization functions nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_sample (g05nd).

References

Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin
Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley

Parameters

Compulsory Input Parameters

1:     ipopn int64int32nag_int array
The population to be sampled.
2:     m int64int32nag_int scalar
The sample size.
Constraint: 1mn.
3:     state: int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array ipop.
The number of elements in the population vector to be sampled.
Constraint: n1.

Output Parameters

1:     isamplm int64int32nag_int array
The selected sample.
2:     state: int64int32nag_int array
Contains updated information on the state of the generator.
3:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
   ifail=2
Constraint: n1.
   ifail=4
Constraint: 1mn.
   ifail=5
On entry, state vector has been corrupted or not initialized.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

The time taken by nag_rand_sample (g05nd) is of order n.
In order to sample other kinds of vectors, or matrices of higher dimension, the following technique may be used:
(a) set ipopi=i, for i=1,2,,n;
(b) use nag_rand_sample (g05nd) to take a sample from ipop and put it into isampl;
(c) use the contents of isampl as a set of indices to access the relevant vector or matrix.
In order to divide a population into several groups, nag_rand_permute (g05nc) is more efficient.

Example

In the example program random samples of size 1,2,,8 are selected from a vector containing the first eight positive integers in ascending order. The samples are generated and printed for each sample size by a call to nag_rand_sample (g05nd) after initialization by nag_rand_init_repeat (g05kf).
function g05nd_example


fprintf('g05nd example results\n\n');

% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf( ...
                        genid, subid, seed);

in     = int64(8);
ipop   = int64([1:in]);
isampl = zeros(in, in, 'int64');

fprintf('\n Samples from the first 8 integers\n\n');
fprintf(' Sample size     Values\n');
for m=1:in
  [isampl(m, 1:m), state, ifail] = ...
     g05nd( ...
            ipop, m, state);

  fprintf('     %d           ', m);
  fprintf('%d ', isampl(m, 1:m));
  fprintf('\n');
end


g05nd example results


 Samples from the first 8 integers

 Sample size     Values
     1           2 
     2           3 6 
     3           1 5 7 
     4           2 6 7 8 
     5           1 2 3 4 8 
     6           1 3 4 5 6 7 
     7           1 3 4 5 6 7 8 
     8           1 2 3 4 5 6 7 8 

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–2015