NAG FL Interface
g01amf (quantiles)

1 Purpose

g01amf finds specified quantiles from a vector of unsorted data.

2 Specification

Fortran Interface
Subroutine g01amf ( n, rv, nq, q, qv, ifail)
Integer, Intent (In) :: n, nq
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: q(nq)
Real (Kind=nag_wp), Intent (Inout) :: rv(n)
Real (Kind=nag_wp), Intent (Out) :: qv(nq)
C Header Interface
#include <nag.h>
void  g01amf_ (const Integer *n, double rv[], const Integer *nq, const double q[], double qv[], Integer *ifail)
The routine may be called by the names g01amf or nagf_stat_quantiles.

3 Description

A quantile is a value which divides a frequency distribution such that there is a given proportion of data values below the quantile. For example, the median of a dataset is the 0.5 quantile because half the values are less than or equal to it; and the 0.25 quantile is the 25th percentile.
g01amf uses a modified version of Singleton's ‘median-of-three’ Quicksort algorithm (Singleton (1969)) to determine specified quantiles of a vector of real values. The input vector is partially sorted, as far as is required to compute desired quantiles; for a single quantile, this is much faster than sorting the entire vector. Where necessary, linear interpolation is also carried out to return the values of quantiles which lie between two data points.

4 References

Singleton R C (1969) An efficient algorithm for sorting with minimal storage: Algorithm 347 Comm. ACM 12 185–187

5 Arguments

1: n Integer Input
On entry: the number of elements in the input vector rv.
Constraint: n>0.
2: rvn Real (Kind=nag_wp) array Input/Output
On entry: the vector whose quantiles are to be determined.
On exit: the order of the elements in rv is not, in general, preserved.
3: nq Integer Input
On entry: the number of quantiles requested.
Constraint: nq>0.
4: qnq Real (Kind=nag_wp) array Input
On entry: the quantiles to be calculated, in ascending order. Note that these must be between 0.0 and 1.0, with 0.0 returning the smallest element and 1.0 the largest.
Constraints:
  • 0.0qi1.0, for i=1,2,,nq;
  • qiqi+1, for i=1,2,,nq-1.
5: qvnq Real (Kind=nag_wp) array Output
On exit: qvi contains the quantile specified by the value provided in qi, or an interpolated value if the quantile falls between two data values.
6: 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, n=value.
Constraint: n>0.
ifail=2
On entry, nq=value.
Constraint: nq>0.
ifail=3
On entry, an element of q was less than 0.0 or greater than 1.0.
ifail=4
On entry, q was not in ascending order.
ifail=5
Internal error. Please contact NAG.
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

g01amf is not threaded in any implementation.

9 Further Comments

The average time taken by g01amf is approximately proportional to n×1+lognq. The worst case time is proportional to n2 but this is extremely unlikely to occur.

10 Example

This example computes a list of quantiles from an array of reals and an array of point values.

10.1 Program Text

Program Text (g01amfe.f90)

10.2 Program Data

Program Data (g01amfe.d)

10.3 Program Results

Program Results (g01amfe.r)