# NAG CL Interfaceg05nec (sample_​wgt)

## 1Purpose

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

## 2Specification

 #include
 void g05nec (Nag_SortOrder sortorder, const double wt[], const Integer ipop[], Integer n, Integer isampl[], Integer m, Integer state[], NagError *fail)
The function may be called by the names: g05nec, nag_rand_sample_wgt or nag_rand_sample_unequal.

## 3Description

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

None.

## 5Arguments

1: $\mathbf{sortorder}$Nag_SortOrder Input
On entry: a flag indicating the sorted status of the wt vector.
${\mathbf{sortorder}}=\mathrm{Nag_Ascending}$
wt is sorted in ascending order,
${\mathbf{sortorder}}=\mathrm{Nag_Descending}$
wt is sorted in descending order,
${\mathbf{sortorder}}=\mathrm{Nag_Unsorted}$
wt is unsorted and g05nec will sort the weights prior to using them.
Irrespective of the value of sortorder, no checks are made on the sorted status of wt, e.g., it is possible to supply ${\mathbf{sortorder}}=\mathrm{Nag_Ascending}$, even when wt is not sorted. In such cases the wt array will not be sorted internally, but g05nec will still work correctly except, possibly, in cases of extreme weight values.
It is usually more efficient to specify a value of sortorder that is consistent with the status of wt.
Constraint: ${\mathbf{sortorder}}=\mathrm{Nag_Ascending}$, $\mathrm{Nag_Descending}$ or $\mathrm{Nag_Unsorted}$.
2: $\mathbf{wt}\left[{\mathbf{n}}\right]$const double Input
On entry: ${w}_{i}$, the relative probability weights. These weights need not sum to $1.0$.
Constraints:
• ${\mathbf{wt}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$;
• at least m values must be nonzero.
3: $\mathbf{ipop}\left[\mathit{dim}\right]$const Integer Input
Note: the dimension, dim, of the array ipop must be at least
• ${\mathbf{n}}$, when ${\mathbf{ipop}}\phantom{\rule{0.25em}{0ex}}\text{is not}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$;
• otherwise ${\mathbf{ipop}}$ is not referenced and may be NULL.
On entry: the population to be sampled. If ${\mathbf{ipop}}\phantom{\rule{0.25em}{0ex}}\text{is}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$ 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-1\right]\ne 0,{\mathbf{wt}}\left[j-1\right]\ne 0$ and $i\ne j$ then there is a nonzero probability that the sample will contain both ${\mathbf{ipop}}\left[i-1\right]$ and ${\mathbf{ipop}}\left[j-1\right]$. If ${\mathbf{ipop}}\left[i-1\right]={\mathbf{ipop}}\left[j-1\right]$ then that value can appear in isampl more than once.
4: $\mathbf{n}$Integer Input
On entry: $n$, the size of the population.
Constraint: ${\mathbf{n}}\ge 1$.
5: $\mathbf{isampl}\left[{\mathbf{m}}\right]$Integer Output
On exit: the selected sample.
6: $\mathbf{m}$Integer Input
On entry: $m$, the size of the sample required.
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
7: $\mathbf{state}\left[\mathit{dim}\right]$Integer Communication Array
Note: the dimension, $\mathit{dim}$, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
8: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
NE_INT_2
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_INVALID_STATE
On entry, state vector has been corrupted or not initialized.
NE_NEG_WEIGHT
On entry, at least one weight was less than zero.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_NON_ZERO_WEIGHTS
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$, number of nonzero weights $=〈\mathit{\text{value}}〉$.
Constraint: must be at least m nonzero weights.

Not applicable.

## 8Parallelism and Performance

g05nec 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 function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

g05nec internally allocates $\left({\mathbf{n}}+1\right)$ doubles and n Integers.
Although it is possible to use g05nec 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 g05ndc. To sample with replacement, g05tdc can be used when the probabilities are unequal and g05tlc when the probabilities are equal.

## 10Example

This example samples from a population of $25$.

### 10.1Program Text

Program Text (g05nece.c)

### 10.2Program Data

Program Data (g05nece.d)

### 10.3Program Results

Program Results (g05nece.r)