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

NAG Library Routine Document

G05NEF

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.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

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

2  Specification

SUBROUTINE G05NEF ( ORDER, WT, POP, IPOP, N, ISAMPL, M, STATE, IFAIL)
INTEGER  IPOP(*), N, ISAMPL(M), M, STATE(*), IFAIL
REAL (KIND=nag_wp)  WT(N)
CHARACTER(1)  ORDER, POP

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

None.

5  Parameters

1:     ORDER – CHARACTER(1)Input
On entry: a flag indicating the sorted status of the WT vector.
ORDER='A'
WT is sorted in ascending order,
ORDER='D'
WT is sorted in descending order,
ORDER='U'
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:     WT(N) – REAL (KIND=nag_wp) arrayInput
On entry: wi, the relative probability weights. These weights need not sum to 1.0.
Constraints:
  • 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.
POP='D'
the population is assumed to be the integers 1,2,,N and IPOP is not referenced,
POP='S'
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:     ISAMPL(M) – 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:
IFAIL=1
On entry, ORDER'A', 'D' or 'U'
IFAIL=2
On entry,WTi<0.0.
IFAIL=3
On entry, POP'D' or 'S'
IFAIL=5
On entry,N<1.
IFAIL=7
On entry,M<0 or M>N.
IFAIL=8
On entry,STATE vector was not initialized or has been corrupted.
IFAIL=21
On entry, there are less than M nonzero weights.

7  Accuracy

Not applicable.

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

9  Example

This example samples from a population of 25.

9.1  Program Text

Program Text (g05nefe.f90)

9.2  Program Data

Program Data (g05nefe.d)

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