NAG Library Function Document

nag_rand_sample_unequal (g05nec)

 Contents

    1  Purpose
    7  Accuracy

1
Purpose

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

2
Specification

#include <nag.h>
#include <nagg05.h>
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 1,2,,n or a supplied population vector of length n. The probability of selecting the ith element is proportional to a user-supplied weight, wi. 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).

4
References

None.

5
Arguments

1:     sortorder Nag_SortOrderInput
On entry: a flag indicating the sorted status of the wt vector.
sortorder=Nag_Ascending
wt is sorted in ascending order,
sortorder=Nag_Descending
wt is sorted in descending order,
sortorder=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 sortorder=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: sortorder=Nag_Ascending, Nag_Descending or Nag_Unsorted.
2:     wt[n] const doubleInput
On entry: wi, the relative probability weights. These weights need not sum to 1.0.
Constraints:
  • wt[i-1]0.0, for i=1,2,,n;
  • at least m values must be nonzero.
3:     ipop[dim] const IntegerInput
Note: the dimension, dim, of the array ipop must be at least
  • n when ipop is not NULL.
On entry: the population to be sampled. If ipopisNULL then the population is assumed to be the set of values 1,2,,n and the array ipop is not referenced. Elements of ipop with the same value are not combined, therefore if wt[i-1]0,wt[j-1]0 and ij then there is a nonzero probability that the sample will contain both ipop[i-1] and ipop[j-1]. If ipop[i-1]=ipop[j-1] then that value can appear in isampl more than once.
4:     n IntegerInput
On entry: n, the size of the population.
Constraint: n1.
5:     isampl[m] IntegerOutput
On exit: the selected sample.
6:     m IntegerInput
On entry: m, the size of the sample required.
Constraint: 0mn.
7:     state[dim] IntegerCommunication Array
Note: the dimension, 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:     fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

6
Error 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.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_INT
On entry, n=value.
Constraint: n1.
NE_INT_2
On entry, m=value and n=value.
Constraint: 0mn.
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, m=value, number of nonzero weights =value.
Constraint: must be at least m nonzero weights.

7
Accuracy

Not applicable.

8
Parallelism 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.

9
Further Comments

nag_rand_sample_unequal (g05nec) internally allocates n+1 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)

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