g05 Chapter Contents
g05 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_rand_sample_unequal (g05nec)

## 1  Purpose

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

## 2  Specification

 #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)

## 3  Description

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.

## 5  Arguments

1:     sortorderNag_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:     wt[n]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:     ipop[$\mathit{dim}$]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:     nIntegerInput
On entry: $n$, the size of the population.
Constraint: ${\mathbf{n}}\ge 1$.
5:     isampl[m]IntegerOutput
On exit: the selected sample.
6:     mIntegerInput
On entry: $m$, the size of the sample required.
Constraint: $0\le {\mathbf{m}}\le {\mathbf{n}}$.
7:     state[$\mathit{dim}$]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:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
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.
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_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.

## 8  Parallelism and Performance

Not applicable.

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.

## 10  Example

This example samples from a population of $25$.

### 10.1  Program Text

Program Text (g05nece.c)

### 10.2  Program Data

Program Data (g05nece.d)

### 10.3  Program Results

Program Results (g05nece.r)