Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

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 $m$ elements from either the set of values $\left(1,2,\dots ,n\right)$ or a supplied population vector of length $n$. The probability of selecting the $i$th element is proportional to a user-supplied weight, ${w}_{i}$. 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).

None.

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{order}$ – string (length ≥ 1)
A flag indicating the sorted status of the wt vector.
${\mathbf{order}}=\text{'A'}$
wt is sorted in ascending order,
${\mathbf{order}}=\text{'D'}$
wt is sorted in descending order,
${\mathbf{order}}=\text{'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 ${\mathbf{order}}=\text{'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: ${\mathbf{order}}=\text{'A'}$, $\text{'D'}$ or $\text{'U'}$.
2:     $\mathrm{wt}\left({\mathbf{n}}\right)$ – double array
${w}_{i}$, the relative probability weights. These weights need not sum to $1.0$.
Constraints:
• ${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$;
• at least m values must be nonzero.
3:     $\mathrm{pop}$ – string (length ≥ 1)
A flag indicating whether a population to be sampled has been supplied.
${\mathbf{pop}}=\text{'D'}$
the population is assumed to be the integers $\left(1,2,\dots ,{\mathbf{n}}\right)$ and ipop is not referenced,
${\mathbf{pop}}=\text{'S'}$
the population must be supplied in ipop.
Constraint: ${\mathbf{pop}}=\text{'D'}$ or $\text{'S'}$.
4:     $\mathrm{ipop}\left(:\right)$int64int32nag_int array
The dimension of the array ipop must be at least ${\mathbf{n}}$ if ${\mathbf{pop}}=\text{'S'}$
The population to be sampled. If ${\mathbf{pop}}=\text{'D'}$ then the population is assumed to be the set of values $\left(1,2,\dots ,{\mathbf{n}}\right)$ and the array ipop is not referenced. Elements of ipop with the same value are not combined, therefore if ${\mathbf{wt}}\left(i\right)\ne 0,{\mathbf{wt}}\left(j\right)\ne 0$ and $i\ne j$ then there is a nonzero probability that the sample will contain both ${\mathbf{ipop}}\left(i\right)$ and ${\mathbf{ipop}}\left(j\right)$. If ${\mathbf{ipop}}\left(i\right)={\mathbf{ipop}}\left(j\right)$ then that value can appear in isampl more than once.
5:     $\mathrm{m}$int64int32nag_int scalar
$m$, the size of the sample required.
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
6:     $\mathrm{state}\left(:\right)$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:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array wt.
$n$, the size of the population.
Constraint: ${\mathbf{n}}\ge 1$.

### Output Parameters

1:     $\mathrm{isampl}\left({\mathbf{m}}\right)$int64int32nag_int array
The selected sample.
2:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{order}}=_$ was an illegal value.
On entry, order had an illegal value.
${\mathbf{ifail}}=2$
On entry, at least one weight was less than zero.
${\mathbf{ifail}}=3$
On entry, pop had an illegal value.
${\mathbf{ifail}}=5$
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=7$
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=8$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=21$
Constraint: must be at least m nonzero weights.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Not applicable.

nag_rand_sample_wgt (g05ne) internally allocates $\left({\mathbf{n}}+1\right)$ 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

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
disp(isampl);

```
```g05ne example results

125
41
185
40
37
196
22
25
76
172

```