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)

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 mm elements from a population vector ipop of length nn and places them in a sample vector isampl. Their order in ipop will be preserved in isampl. Each of the
(n)
m
n m  possible combinations of elements of isampl may be regarded as being equally probable.
For moderate or large values of nn it is theoretically impossible that all combinations of size mm may occur, unless mm is near 1 or near nn. This is because
(n)
m
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:     ipop(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint n1n1.
The population to be sampled.
2:     m – int64int32nag_int scalar
The sample size.
Constraint: 1mn1mn.
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: n1n1.

Input Parameters Omitted from the MATLAB Interface

None.

Output Parameters

1:     isampl(m) – int64int32nag_int array
The selected sample.
2:     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 updated information on the state of the generator.
3:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 2ifail=2
On entry, n < 1n<1.
  ifail = 4ifail=4
On entry,m < 1m<1,
orm > nm>n.
  ifail = 5ifail=5
On entry,state vector was not initialized or has been corrupted.

Accuracy

Not applicable.

Further Comments

The time taken by nag_rand_sample (g05nd) is of order nn.
In order to sample other kinds of vectors, or matrices of higher dimension, the following technique may be used:
(a) set ipop(i) = iipopi=i, for i = 1,2,,ni=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

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

ipop = [int64(1);2;3;4;5;6;7;8];
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
fprintf('\n Samples from the first 8 integers\n\n');
fprintf(' Sample size     Values\n');
isampl = zeros(8, 8, 'int64');
ifail  = zeros(8, 1, 'int64');
for m=1:8
  [isampl(m, 1:m), state, ifail(m)] = nag_rand_sample(ipop, int64(m), state);
  if ifail == 0
    fprintf('     %d           ', m);
    fprintf('%d ', isampl(m, 1:m));
    fprintf('\n');
  end
end
 

 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 

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

ipop = [int64(1);2;3;4;5;6;7;8];
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
fprintf('\n Samples from the first 8 integers\n\n');
fprintf(' Sample size     Values\n');
isampl = zeros(8, 8, 'int64');
ifail  = zeros(8, 1, 'int64');
for m=1:8
  [isampl(m, 1:m), state, ifail(m)] = g05nd(ipop, int64(m), state);
  if ifail == 0
    fprintf('     %d           ', m);
    fprintf('%d ', isampl(m, 1:m));
    fprintf('\n');
  end
end
 

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