# NAG Library Routine Document

## 1Purpose

g01amf finds specified quantiles from a vector of unsorted data.

## 2Specification

Fortran Interface
 Subroutine g01amf ( n, rv, nq, q, qv,
 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)
#include nagmk26.h
 void g01amf_ (const Integer *n, double rv[], const Integer *nq, const double q[], double qv[], Integer *ifail)

## 3Description

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.

## 4References

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

## 5Arguments

1:     $\mathbf{n}$ – IntegerInput
On entry: the number of elements in the input vector rv.
Constraint: ${\mathbf{n}}>0$.
2:     $\mathbf{rv}\left({\mathbf{n}}\right)$ – 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:     $\mathbf{nq}$ – IntegerInput
On entry: the number of quantiles requested.
Constraint: ${\mathbf{nq}}>0$.
4:     $\mathbf{q}\left({\mathbf{nq}}\right)$ – 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:     $\mathbf{qv}\left({\mathbf{nq}}\right)$ – 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:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation 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 argument, 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{nq}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nq}}>0$.
${\mathbf{ifail}}=3$
On entry, an element of q was less than $0.0$ or greater than $1.0$.
${\mathbf{ifail}}=4$
On entry, q was not in ascending order.
${\mathbf{ifail}}=5$
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

Not applicable.

## 8Parallelism and Performance

g01amf is not threaded in any implementation.

The average time taken by g01amf is approximately proportional to ${\mathbf{n}}×\left(1+\mathrm{log}\left({\mathbf{nq}}\right)\right)$. The worst case time is proportional to ${{\mathbf{n}}}^{2}$ but this is extremely unlikely to occur.

## 10Example

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

### 10.1Program Text

Program Text (g01amfe.f90)

### 10.2Program Data

Program Data (g01amfe.d)

### 10.3Program Results

Program Results (g01amfe.r)

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