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$m$ elements from either the set of values (1,2,,n)$\left(1,2,\dots ,n\right)$ or a supplied population vector of length n$n$. The probability of selecting the i$i$th element is proportional to a user-supplied weight, wi${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:     order – string (length ≥ 1)
A flag indicating the sorted status of the wt vector.
order = 'A'${\mathbf{order}}=\text{'A'}$
wt is sorted in ascending order,
order = 'D'${\mathbf{order}}=\text{'D'}$
wt is sorted in descending order,
order = 'U'${\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 order = 'A'${\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: order = 'A'${\mathbf{order}}=\text{'A'}$, 'D'$\text{'D'}$ or 'U'$\text{'U'}$.
2:     wt(n) – double array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
wi${w}_{i}$, the relative probability weights. These weights need not sum to 1.0$1.0$.
Constraints:
• wt(i)0.0${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{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'${\mathbf{pop}}=\text{'D'}$
the population is assumed to be the integers (1,2,,n)$\left(1,2,\dots ,{\mathbf{n}}\right)$ and ipop is not referenced,
pop = 'S'${\mathbf{pop}}=\text{'S'}$
the population must be supplied in ipop.
Constraint: pop = 'D'${\mathbf{pop}}=\text{'D'}$ or 'S'$\text{'S'}$.
4:     ipop( : $:$) – int64int32nag_int array
Note: the dimension of the array ipop must be at least n${\mathbf{n}}$ if pop = 'S'${\mathbf{pop}}=\text{'S'}$.
The population to be sampled. If pop = 'D'${\mathbf{pop}}=\text{'D'}$ then the population is assumed to be the set of values (1,2,,n)$\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 wt(i)0,wt(j)0${\mathbf{wt}}\left(i\right)\ne 0,{\mathbf{wt}}\left(j\right)\ne 0$ and ij$i\ne j$ then there is a nonzero probability that the sample will contain both ipop(i)${\mathbf{ipop}}\left(i\right)$ and ipop(j)${\mathbf{ipop}}\left(j\right)$. If ipop(i) = ipop(j)${\mathbf{ipop}}\left(i\right)={\mathbf{ipop}}\left(j\right)$ then that value can appear in isampl more than once.
5:     m – int64int32nag_int scalar
m$m$, the size of the sample required.
Constraint: 0mn$0\le {\mathbf{m}}\le {\mathbf{n}}$.
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$n$, the size of the population.
Constraint: n1${\mathbf{n}}\ge 1$.

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
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
On entry, order had an illegal value.
ifail = 2${\mathbf{ifail}}=2$
On entry, at least one weight was less than zero.
ifail = 3${\mathbf{ifail}}=3$
On entry, pop had an illegal value.
ifail = 5${\mathbf{ifail}}=5$
Constraint: n1${\mathbf{n}}\ge 1$.
ifail = 7${\mathbf{ifail}}=7$
Constraint: 0mn$0\le {\mathbf{m}}\le {\mathbf{n}}$.
ifail = 8${\mathbf{ifail}}=8$
On entry, state vector has been corrupted or not initialized. On entry, state vector has been corrupted or not initialized.
ifail = 21${\mathbf{ifail}}=21$
Constraint: must be at least m nonzero weights.
ifail = 999${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Not applicable.

nag_rand_sample_wgt (g05ne) internally allocates (n + 1)$\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

```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

```