G05NEF (PDF version)
G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual

NAG Library Routine Document


Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.


    1  Purpose
    7  Accuracy

1  Purpose

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

2  Specification

REAL (KIND=nag_wp)  WT(N)

3  Description

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

4  References


5  Parameters

1:     ORDER – CHARACTER(1)Input
On entry: a flag indicating the sorted status of the WT vector.
WT is sorted in ascending order,
WT is sorted in descending order,
WT is unsorted and G05NEF will sort the weights prior to using them.
Irrespective of the value of ORDER, no checks are made on the sorted status of WT, e.g., it is possible to supply ORDER='A', even when WT is not sorted. In such cases the WT array will not be sorted internally, but G05NEF will still work correctly except, possibly, in cases of extreme weight values.
It is usually more efficient to specify a value of ORDER that is consistent with the status of WT.
Constraint: ORDER='A', 'D' or 'U'.
2:     WTN – REAL (KIND=nag_wp) arrayInput
On entry: wi, the relative probability weights. These weights need not sum to 1.0.
  • WTi0.0, for i=1,2,,N;
  • at least M values must be nonzero.
3:     POP – CHARACTER(1)Input
On entry: a flag indicating whether a population to be sampled has been supplied.
the population is assumed to be the integers 1,2,,N and IPOP is not referenced,
the population must be supplied in IPOP.
Constraint: POP='D' or 'S'.
4:     IPOP* – INTEGER arrayInput
Note: the dimension of the array IPOP must be at least N if POP='S'.
On entry: the population to be sampled. If POP='D' 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 WTi0,WTj0 and ij then there is a nonzero probability that the sample will contain both IPOPi and IPOPj. If IPOPi=IPOPj then that value can appear in ISAMPL more than once.
5:     N – INTEGERInput
On entry: n, the size of the population.
Constraint: N1.
6:     ISAMPLM – INTEGER arrayOutput
On exit: the selected sample.
7:     M – INTEGERInput
On entry: m, the size of the sample required.
Constraint: 0MN.
8:     STATE* – INTEGER arrayCommunication Array
Note: the actual argument supplied must be the array STATE supplied to the initialization routines G05KFF or G05KGF.
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
9:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
On entry, ORDER=value was an illegal value.
On entry, ORDER had an illegal value.
On entry, at least one weight was less than zero.
On entry, POP had an illegal value.
On entry, N=value.
Constraint: N1.
On entry, M=value and N=value.
Constraint: 0MN.
On entry, STATE vector has been corrupted or not initialized.
On entry, M=value, number of nonzero weights =value.
Constraint: must be at least M nonzero weights.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

Not applicable.

8  Parallelism and Performance

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

9  Further Comments

G05NEF internally allocates N+1 reals and N integers.
Although it is possible to use G05NEF 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 G05NDF. To sample with replacement, G05TDF can be used when the probabilities are unequal and G05TLF when the probabilities are equal.

10  Example

This example samples from a population of 25.

10.1  Program Text

Program Text (g05nefe.f90)

10.2  Program Data

Program Data (g05nefe.d)

10.3  Program Results

Program Results (g05nefe.r)

G05NEF (PDF version)
G05 Chapter Contents
G05 Chapter Introduction
NAG Library Manual

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