# NAG FL Interfaceg05nef (sample_​wgt)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g05nef ( wt, pop, ipop, n, m,
 Integer, Intent (In) :: ipop(*), n, m Integer, Intent (Inout) :: state(*), ifail Integer, Intent (Out) :: isampl(m) Real (Kind=nag_wp), Intent (In) :: wt(n) Character (1), Intent (In) :: order, pop
#include <nag.h>
 void g05nef_ (const char *order, const double wt[], const char *pop, const Integer ipop[], const Integer *n, Integer isampl[], const Integer *m, Integer state[], Integer *ifail, const Charlen length_order, const Charlen length_pop)
The routine may be called by the names g05nef or nagf_rand_sample_wgt.

## 3Description

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.

## 5Arguments

1: $\mathbf{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: $\mathbf{wt}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{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: $\mathbf{ipop}\left(*\right)$Integer array Input
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: $\mathbf{n}$Integer Input
On entry: $n$, the size of the population.
Constraint: ${\mathbf{n}}\ge 1$.
6: $\mathbf{isampl}\left({\mathbf{m}}\right)$Integer array Output
On exit: the selected sample.
7: $\mathbf{m}$Integer Input
On entry: $m$, the size of the sample required.
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
8: $\mathbf{state}\left(*\right)$Integer array Communication 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: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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}}=⟨\mathit{\text{value}}⟩$ was 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$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=7$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=8$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=21$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$, number of nonzero weights $=⟨\mathit{\text{value}}⟩$.
Constraint: must be at least m nonzero weights.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism and Performance

g05nef is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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.

## 10Example

This example samples from a population of $25$.

### 10.1Program Text

Program Text (g05nefe.f90)

### 10.2Program Data

Program Data (g05nefe.d)

### 10.3Program Results

Program Results (g05nefe.r)