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)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


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


[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)


nag_rand_sample_wgt (g05ne) selects m elements from either the set of values 1,2,,n or a supplied population vector of length n. The probability of selecting the ith element is proportional to a user-supplied weight, wi. 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).




Compulsory Input Parameters

1:     order – string (length ≥ 1)
A flag indicating the sorted status of the wt vector.
wt is sorted in ascending order,
wt is sorted in descending order,
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', 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', 'D' or 'U'.
2:     wtn – double array
wi, the relative probability weights. These weights need not sum to 1.0.
  • wti0.0, for i=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.
the population is assumed to be the integers 1,2,,n and ipop is not referenced,
the population must be supplied in ipop.
Constraint: pop='D' or 'S'.
4:     ipop: int64int32nag_int array
The dimension of the array ipop must be at least n if pop='S'
The population to be sampled. If pop='D' then the population is assumed to be the set of values 1,2,,n and the array ipop is not referenced. Elements of ipop with the same value are not combined, therefore if wti0,wtj0 and ij then there is a nonzero probability that the sample will contain both ipopi and ipopj. If ipopi=ipopj then that value can appear in isampl more than once.
5:     m int64int32nag_int scalar
m, the size of the sample required.
Constraint: 0mn.
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.
n, the size of the population.
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:
On entry, order=_ was an illegal value.
On entry, order had an illegal value.
On entry, at least one weight was less than zero.
On entry, pop had an illegal value.
Constraint: n1.
Constraint: 0mn.
On entry, state vector has been corrupted or not initialized.
Constraint: must be at least m nonzero weights.
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.


Not applicable.

Further Comments

nag_rand_sample_wgt (g05ne) internally allocates 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.


This example samples from a population of 25.
function g05ne_example

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

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

% Data to sample with weights
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];

% Generate the sample sice m without replacement, unequal weights
m     = int64(10);
pop   = 's';
order = 'u';
[isampl, state, ifail] = g05ne( ...
                                order, wt, pop, ipop, m, state);

% Display the results

g05ne example results


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