# NAG Library Function Document

## 1Purpose

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

## 2Specification

 #include #include
 void nag_rand_sample_unequal (Nag_SortOrder sortorder, const double wt[], const Integer ipop[], Integer n, Integer isampl[], Integer m, Integer state[], NagError *fail)

## 3Description

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

None.

## 5Arguments

1:    $\mathbf{sortorder}$Nag_SortOrderInput
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 nag_rand_sample_unequal (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 nag_rand_sample_unequal (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 doubleInput
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 IntegerInput
Note: the dimension, dim, of the array ipop must be at least
• ${\mathbf{n}}$ when ipop is not 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}$IntegerInput
On entry: $n$, the size of the population.
Constraint: ${\mathbf{n}}\ge 1$.
5:    $\mathbf{isampl}\left[{\mathbf{m}}\right]$IntegerOutput
On exit: the selected sample.
6:    $\mathbf{m}$IntegerInput
On entry: $m$, the size of the sample required.
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
7:    $\mathbf{state}\left[\mathit{dim}\right]$IntegerCommunication 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 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation 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 2.7.6 in How to Use the NAG Library and its Documentation 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 2.7.5 in How to Use the NAG Library and its Documentation 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

nag_rand_sample_unequal (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.

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

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017