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_wgt (g05ne)

Purpose

nag_rand_sample_wgt (g05ne) selects a pseudorandom sample, without replacement and allowing for unequal probabilities.

Syntax

[isampl, state, ifail] = g05ne(order, wt, pop, ipop, m, state, 'n', n)
[isampl, state, ifail] = nag_rand_sample_wgt(order, wt, pop, ipop, m, state, 'n', n)

Description

nag_rand_sample_wgt (g05ne) selects mm elements from either the set of values (1,2,,n)(1,2,,n) or a supplied population vector of length nn. The probability of selecting the iith element is proportional to a user-supplied weight, wiwi. Each element will appear at most once in the sample, i.e., the sampling is done without replacement.
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_wgt (g05ne).

References

None.

Parameters

Compulsory Input Parameters

1:     order – string (length ≥ 1)
A flag indicating the sorted status of the wt vector.
order = 'A'order='A'
wt is sorted in ascending order,
order = 'D'order='D'
wt is sorted in descending order,
order = 'U'order='U'
wt is unsorted and nag_rand_sample_wgt (g05ne) will sort the weights prior to using them.
Irrespective of the value of order, no checks are made on the sorted status of wt, e.g., it is possible to supply order = 'A'order='A', even when wt is not sorted. In such cases the wt array will not be sorted internally, but nag_rand_sample_wgt (g05ne) will still work correctly except, possibly, in cases of extreme weight values.
It is usually more efficient to specify a value of order that is consistent with the status of wt.
Constraint: order = 'A'order='A', 'D''D' or 'U''U'.
2:     wt(n) – double array
n, the dimension of the array, must satisfy the constraint n1n1.
wiwi, the relative probability weights. These weights need not sum to 1.01.0.
Constraints:
  • wt(i)0.0wti0.0, for i = 1,2,,ni=1,2,,n;
  • at least m values must be nonzero.
3:     pop – string (length ≥ 1)
A flag indicating whether a population to be sampled has been supplied.
pop = 'D'pop='D'
the population is assumed to be the integers (1,2,,n)(1,2,,n) and ipop is not referenced,
pop = 'S'pop='S'
the population must be supplied in ipop.
Constraint: pop = 'D'pop='D' or 'S''S'.
4:     ipop( : :) – int64int32nag_int array
Note: the dimension of the array ipop must be at least nn if pop = 'S'pop='S'.
The population to be sampled. If pop = 'D'pop='D' then the population is assumed to be the set of values (1,2,,n)(1,2,,n) and the array ipop is not referenced. Elements of ipop with the same value are not combined, therefore if wt(i)0,wt(j)0wti0,wtj0 and ijij then there is a nonzero probability that the sample will contain both ipop(i)ipopi and ipop(j)ipopj. If ipop(i) = ipop(j)ipopi=ipopj then that value can appear in isampl more than once.
5:     m – int64int32nag_int scalar
mm, the size of the sample required.
Constraint: 0mn0mn.
6:     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 wt.
nn, the size of the population.
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 = 1ifail=1
On entry, order had an illegal value.
  ifail = 2ifail=2
On entry, at least one weight was less than zero.
  ifail = 3ifail=3
On entry, pop had an illegal value.
  ifail = 5ifail=5
Constraint: n1n1.
  ifail = 7ifail=7
Constraint: 0mn0mn.
  ifail = 8ifail=8
On entry, state vector has been corrupted or not initialized. On entry, state vector has been corrupted or not initialized.
  ifail = 21ifail=21
Constraint: must be at least m nonzero weights.
  ifail = 999ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

nag_rand_sample_wgt (g05ne) internally allocates (n + 1)(n+1) doubles and n integers.
Although it is possible to use nag_rand_sample_wgt (g05ne) to sample using equal probabilities, by setting all elements of the input array wt to the same positive value, it is more efficient to use nag_rand_sample (g05nd). To sample with replacement, nag_rand_int_general (g05td) can be used when the probabilities are unequal and nag_rand_int_uniform (g05tl) when the probabilities are equal.

Example

function nag_rand_sample_wgt_example
genid = int64(3);
subid = int64(0);
seed  = [int64(1762543)];
n = int64(25);
m = int64(10);
pop = 's';
order = 'u';
ipop = [int64(171); 52; 172; 139; 196; 125; 36; 70; 25; 86; 76; 37; 185; 40; 90;
                27; 79; 118; 142; 127; 101; 22; 41; 199; 59];
wt = [ 85.54;
       71.78;
      118.13;
       13.68;
      153.60;
      165.35;
      122.35;
       35.87;
      151.78;
      128.33;
      178.27;
      183.37;
      165.81;
      101.41;
      145.16;
       42.01;
       59.08;
       17.53;
       87.14;
       69.20;
       31.13;
       60.26;
       21.00;
       85.06;
      119.73];


% Initialise the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);

% Generate the sample without replacement, unequal weights
[isampl, state, ifail] = nag_rand_sample_wgt(order, wt, pop, ipop, m, state);

% Display the results
if ifail == 0
  disp(isampl);
end
 
                  125
                   41
                  185
                   40
                   37
                  196
                   22
                   25
                   76
                  172


function g05ne_example
genid = int64(3);
subid = int64(0);
seed  = [int64(1762543)];
n = int64(25);
m = int64(10);
pop = 's';
order = 'u';
ipop = [int64(171); 52; 172; 139; 196; 125; 36; 70; 25; 86; 76; 37; 185; 40; 90;
                27; 79; 118; 142; 127; 101; 22; 41; 199; 59];
wt = [ 85.54;
       71.78;
      118.13;
       13.68;
      153.60;
      165.35;
      122.35;
       35.87;
      151.78;
      128.33;
      178.27;
      183.37;
      165.81;
      101.41;
      145.16;
       42.01;
       59.08;
       17.53;
       87.14;
       69.20;
       31.13;
       60.26;
       21.00;
       85.06;
      119.73];


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

% Generate the sample without replacement, unequal weights
[isampl, state, ifail] = g05ne(order, wt, pop, ipop, m, state);

% Display the results
if ifail == 0
  disp(isampl);
end
 
                  125
                   41
                  185
                   40
                   37
                  196
                   22
                   25
                   76
                  172



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