G01AMF (PDF version)
G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G01AMF

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

G01AMF finds specified quantiles from a vector of unsorted data.

2  Specification

SUBROUTINE G01AMF ( N, RV, NQ, Q, QV, IFAIL)
INTEGER  N, NQ, IFAIL
REAL (KIND=nag_wp)  RV(N), Q(NQ), QV(NQ)

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  Parameters

1:     N – INTEGERInput
On entry: the number of elements in the input vector RV.
Constraint: N>0.
2:     RVN – REAL (KIND=nag_wp) arrayInput/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 – INTEGERInput
On entry: the number of quantiles requested.
Constraint: NQ>0.
4:     QNQ – REAL (KIND=nag_wp) arrayInput
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) arrayOutput
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 – 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, 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 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

Not applicable.

8  Parallelism and Performance

Not applicable.

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)


G01AMF (PDF version)
G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

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