NAG Library Routine Document

g05nef  (sample_wgt)

 Contents

    1  Purpose
    7  Accuracy

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

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:     wtn – 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:     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 argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation 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 argument, 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=value was an illegal value.
On entry, order had 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 3.9 in How to Use the NAG Library and its Documentation for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation 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)

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