NAG FL Interface
g05nff (resample)

Settings help

FL Name Style:

FL Specification Language:

1 Purpose

g05nff selects a pseudorandom sample (or resamples) from a vector of integers based on the supplied weights.

2 Specification

Fortran Interface
Subroutine g05nff ( rtype, n, wt, ipop, lipop, m, nrs, otype, isampl, ldisampl, state, ifail)
Integer, Intent (In) :: rtype, n, ipop(lipop), lipop, m, nrs, otype, ldisampl
Integer, Intent (Inout) :: isampl(ldisampl,*), state(*), ifail
Real (Kind=nag_wp), Intent (In) :: wt(n)
C Header Interface
#include <nag.h>
void  g05nff_ (const Integer *rtype, const Integer *n, const double wt[], const Integer ipop[], const Integer *lipop, const Integer *m, const Integer *nrs, const Integer *otype, Integer isampl[], const Integer *ldisampl, Integer state[], Integer *ifail)
The routine may be called by the names g05nff or nagf_rand_resample.

3 Description

Given a vector V, of n integer values, g05nff selects m elements with the probability of selecting Vj proportional to a user-supplied weight, wj. The sampling is done with replacement, so that each value, Vj, may appear more than once in the sample.
The most common usage case for g05nff is where V was obtained using some other sampling method, for example, importance sampling. In such a case, this routine is used to perform resampling.
Several methods of calculating Nj, the number of times Vj appears in the sample, are available: See g05tgf for more information on the multinomial distribution and Douc et al. (2005) for more details on the resampling methods.
If multiple samples are requested (nrs>1) then the chosen resampling method is performed independently for each sample.
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 g05nff.

4 References

Douc R, Cappe O, and Moulines E (2005) Comparison of resampling schemes for particle filtering Proceedings of the 4th International Symposium on Image and Signal Processing and Analysis 64–69
Li T, Bolic M, and Djuric P M (2015) Resampling Methods for Particle Filtering: Classification, implementation, and strategies IEEE Signal Processing Magazine vol. 32, no. 3 70–86

5 Arguments

1: rtype Integer Input
On entry: a flag indicating the resampling method to use.
Multinomial resampling will be used.
Systematic resampling will be used.
Residual resampling will be used.
Constraint: rtype=1, 2 or 3.
2: n Integer Input
On entry: n, the size of the population.
Constraint: n0.
3: wt(n) Real (Kind=nag_wp) array Input
On entry: wi, the weights. These weights need not sum to 1.0.
  • wt(i)0.0, for i=1,2,,n;
  • at least one (and preferably more than one) weight must be nonzero.
4: ipop(lipop) Integer array Input
On entry: V, the vector to be sampled from. If lipop=0 then the V is assumed to be the set of values (1,2,,n) and 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), irrespective of their values.
If the values to be returned in isampl are counts, i.e., otype=2, then ipop is not referenced.
5: lipop Integer Input
On entry: the dimension of the array ipop as declared in the (sub)program from which g05nff is called.
Constraint: lipop=0 or lipop=n.
6: m Integer Input
On entry: m, the size of the sample required.
Constraint: m0.
7: nrs Integer Input
On entry: the number of times to resample.
Constraint: nrs0.
8: otype Integer Input
On entry: a flag indicating what is returned in isampl.
The values returned in isampl are taken from the population.
The values returned in isampl are counts.
See isampl for more information.
Constraint: otype=1 or 2.
9: isampl(ldisampl,*) Integer array Output
Note: the second dimension of the array isampl must be at least nrs.
On exit: the selected samples.
If otype=1 then each column of isampl contains the m values from V that make up the sample. If otype=2 then isampl(j,k) contains the number of times that Vj appears in the kth sample.
10: ldisampl Integer Input
On entry: the first dimension of the array isampl as declared in the (sub)program from which g05nff is called.
  • if otype=1, ldisamplm;
  • otherwise ldisampln.
11: 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.
12: 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:
On entry, rtype=value.
Constraint: rtype=1, 2 or 3.
On entry, n=value.
Constraint: n0.
On entry, i=value and wt(i)=value.
Constraint: wt(i)0.0.
On entry, all the weights are zero.
Constraint: at least one weight must be nonzero.
On entry, n=value and lipop=value.
Constraint: lipop=0 or lipop=n.
On entry, m=value.
Constraint: m0.
On entry, nrs=value.
Constraint: nrs0.
On entry, otype=value.
Constraint: otype=1 or 2.
There was no random component to the sample. Check the sample size and weights are as expected.
Specifically, check that more than one weight is nonzero. If rtype=3, also check the combination of m and weights.
On entry, m=value, otype=1 and ldisampl=value.
Constraint: ldisamplm.
On entry, n=value, otype=2 and ldisampl=value.
Constraint: ldisampln.
On entry, state vector has been corrupted or not initialized.
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.
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.
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

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

It should be noted that whilst a given sample is a random selection from V, the ordering of the sample within isampl may not be. For example, when otype=1 the values returned are likely to be in the same order that the values appear in V. If it is important that the returned values represent a random sample from V rather than a ordered random sample then each sample should be randomly permuted via a subsequent call to g05ncf. The same applies to the order in which multiple samples are returned. One consequence of this is that if you call g05nff once with nrs=1, say, and then again (using the same initial values for state), with nrs=2 the first column of isampl may not be the same in both cases since, on the second call, the sample from the first call may be returned in the second column rather than the first.
See also g05ndf, g05nef and g05tlf for other types of sampling.

10 Example

This example draws five samples of twenty five from a population of the same length and prints out the values.

10.1 Program Text

Program Text (g05nffe.f90)

10.2 Program Data

Program Data (g05nffe.d)

10.3 Program Results

Program Results (g05nffe.r)