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:     RV(N) – 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:     Q(NQ) – 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:     QV(NQ) – 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<1.
IFAIL=2
On entry,NQ<1.
IFAIL=3
On entry, some Q<0.0 or Q>1.0.
IFAIL=4
On entry, Q is not in ascending order.
IFAIL=5
Internal error. Check all array sizes and calls to G01AMF. Please contact NAG.

7  Accuracy

Not applicable.

8  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.

9  Example

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

9.1  Program Text

Program Text (g01amfe.f90)

9.2  Program Data

Program Data (g01amfe.d)

9.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. 2012