NAG CL Interface
g05nec (sample_​wgt)

Settings help

CL Name Style:


1 Purpose

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

2 Specification

#include <nag.h>
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.

3 Description

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

4 References

None.

5 Arguments

1: sortorder Nag_SortOrder Input
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 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 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 double Input
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 Integer Input
Note: the dimension, dim, of the array ipop must be at least
  • n, when ipopis notNULL;
  • otherwise ipop is not referenced and may be 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 Integer Input
On entry: n, the size of the population.
Constraint: n1.
5: isampl[m] Integer Output
On exit: the selected sample.
6: m Integer Input
On entry: m, the size of the sample required.
Constraint: 0mn.
7: state[dim] Integer Communication 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 7 in the Introduction to the NAG Library CL Interface).

6 Error 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.
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 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, m=value, number of nonzero weights =value.
Constraint: must be at least m nonzero weights.

7 Accuracy

Not applicable.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
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

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

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)