G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG05NEF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G05NEF selects a pseudorandom sample, without replacement and allowing for unequal probabilities.

## 2  Specification

 SUBROUTINE G05NEF ( ORDER, WT, POP, IPOP, N, ISAMPL, M, STATE, IFAIL)
 INTEGER IPOP(*), N, ISAMPL(M), M, STATE(*), IFAIL REAL (KIND=nag_wp) WT(N) CHARACTER(1) ORDER, POP

## 3  Description

G05NEF 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 routines G05KFF (for a repeatable sequence if computed sequentially) or G05KGF (for a non-repeatable sequence) must be called prior to the first call to G05NEF.

None.

## 5  Parameters

1:     ORDER – CHARACTER(1)Input
On entry: 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 G05NEF 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 G05NEF 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:     WT(N) – REAL (KIND=nag_wp) arrayInput
On entry: ${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:     POP – CHARACTER(1)Input
On entry: 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:     IPOP($*$) – INTEGER arrayInput
Note: the dimension of the array IPOP must be at least ${\mathbf{N}}$ if ${\mathbf{POP}}=\text{'S'}$.
On entry: 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:     N – INTEGERInput
On entry: $n$, the size of the population.
Constraint: ${\mathbf{N}}\ge 1$.
6:     ISAMPL(M) – INTEGER arrayOutput
On exit: the selected sample.
7:     M – INTEGERInput
On entry: $m$, the size of the sample required.
Constraint: $0\le {\mathbf{M}}\le {\mathbf{N}}$.
8:     STATE($*$) – INTEGER arrayCommunication Array
Note: the actual argument supplied must be the array STATE supplied to the initialization routines G05KFF or G05KGF.
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
9:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
On entry, ${\mathbf{ORDER}}\ne \text{'A'}$, $\text{'D'}$ or $\text{'U'}$
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{WT}}\left(i\right)<0.0$.
${\mathbf{IFAIL}}=3$
On entry, ${\mathbf{POP}}\ne \text{'D'}$ or $\text{'S'}$
${\mathbf{IFAIL}}=5$
 On entry, ${\mathbf{N}}<1$.
${\mathbf{IFAIL}}=7$
 On entry, ${\mathbf{M}}<0$ or ${\mathbf{M}}>{\mathbf{N}}$.
${\mathbf{IFAIL}}=8$
 On entry, STATE vector was not initialized or has been corrupted.
${\mathbf{IFAIL}}=21$
On entry, there are less than M nonzero weights.

## 7  Accuracy

Not applicable.

G05NEF internally allocates $\left({\mathbf{N}}+1\right)$ reals and N integers.
Although it is possible to use G05NEF 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 G05NDF. To sample with replacement, G05TDF can be used when the probabilities are unequal and G05TLF when the probabilities are equal.

## 9  Example

This example samples from a population of $25$.

### 9.1  Program Text

Program Text (g05nefe.f90)

### 9.2  Program Data

Program Data (g05nefe.d)

### 9.3  Program Results

Program Results (g05nefe.r)