PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_rand_sample (g05nd)
Purpose
nag_rand_sample (g05nd) selects a pseudorandom sample without replacement from an integer vector.
Syntax
Description
nag_rand_sample (g05nd) selects
elements from a population vector
ipop of length
and places them in a sample vector
isampl. Their order in
ipop will be preserved in
isampl. Each of the
possible combinations of elements of
isampl may be regarded as being equally probable.
For moderate or large values of it is theoretically impossible that all combinations of size may occur, unless is near 1 or near . This is because 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:
– int64int32nag_int array
-
The population to be sampled.
- 2:
– int64int32nag_int scalar
-
The sample size.
Constraint:
.
- 3:
– 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:
– int64int32nag_int scalar
-
Default:
the dimension of the array
ipop.
The number of elements in the population vector to be sampled.
Constraint:
.
Output Parameters
- 1:
– int64int32nag_int array
-
The selected sample.
- 2:
– int64int32nag_int array
-
Contains updated information on the state of the generator.
- 3:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
Constraint: .
-
-
Constraint: .
-
-
On entry,
state vector has been corrupted or not initialized.
-
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
Not applicable.
Further Comments
The time taken by nag_rand_sample (g05nd) is of order .
In order to sample other kinds of vectors, or matrices of higher dimension, the following technique may be used:
(a) |
set , for ; |
(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
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).
Open in the MATLAB editor:
g05nd_example
function g05nd_example
fprintf('g05nd example results\n\n');
seed = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015