G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG01AMF

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.

## 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 $25$th 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: ${\mathbf{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: ${\mathbf{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.0\le {\mathbf{Q}}\left(\mathit{i}\right)\le 1.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{NQ}}$;
• ${\mathbf{Q}}\left(\mathit{i}\right)\le {\mathbf{Q}}\left(\mathit{i}+1\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{NQ}}-1$.
5:     QV(NQ) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{QV}}\left(i\right)$ contains the quantile specified by the value provided in ${\mathbf{Q}}\left(i\right)$, 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\text{​ 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\text{​ 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 $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}<1$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{NQ}}<1$.
${\mathbf{IFAIL}}=3$
On entry, some ${\mathbf{Q}}<0.0$ or ${\mathbf{Q}}>1.0$.
${\mathbf{IFAIL}}=4$
On entry, Q is not in ascending order.
${\mathbf{IFAIL}}=5$

## 7  Accuracy

Not applicable.

The average time taken by G01AMF is approximately proportional to ${\mathbf{N}}×\left(1+\mathrm{log}{\mathbf{NQ}}\right)$. The worst case time is proportional to ${{\mathbf{N}}}^{2}$ 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)