NAG FL Interface
g05nef (sample_​wgt)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

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

2 Specification

Fortran Interface
Subroutine g05nef ( order, wt, pop, ipop, n, isampl, m, state, ifail)
Integer, Intent (In) :: ipop(*), n, m
Integer, Intent (Inout) :: state(*), ifail
Integer, Intent (Out) :: isampl(m)
Real (Kind=nag_wp), Intent (In) :: wt(n)
Character (1), Intent (In) :: order, pop
C Header Interface
#include <nag.h>
void  g05nef_ (const char *order, const double wt[], const char *pop, const Integer ipop[], const Integer *n, Integer isampl[], const Integer *m, Integer state[], Integer *ifail, const Charlen length_order, const Charlen length_pop)
The routine may be called by the names g05nef or nagf_rand_sample_wgt.

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 Arguments

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) array Input
On entry: wi, the relative probability weights. These weights need not sum to 1.0.
Constraints:
  • wt(i)0.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 array Input
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 wt(i)0,wt(j)0 and ij then there is a nonzero probability that the sample will contain both ipop(i) and ipop(j). If ipop(i)=ipop(j) then that value can appear in isampl more than once.
5: n Integer Input
On entry: n, the size of the population.
Constraint: n1.
6: isampl(m) Integer array Output
On exit: the selected sample.
7: m Integer Input
On entry: m, the size of the sample required.
Constraint: 0mn.
8: state(*) Integer array Communication 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 Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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=value was an illegal value.
ifail=2
On entry, at least one weight was less than zero.
ifail=3
On entry, pop had an illegal value.
ifail=5
On entry, n=value.
Constraint: n1.
ifail=7
On entry, m=value and n=value.
Constraint: 0mn.
ifail=8
On entry, state vector has been corrupted or not initialized.
ifail=21
On entry, m=value, number of nonzero weights =value.
Constraint: must be at least m nonzero weights.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface 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)