# NAG CL Interfaceg01amc (quantiles)

## 1Purpose

g01amc finds specified quantiles from a vector of unsorted data.

## 2Specification

 #include
 void g01amc (Integer n, double rv[], Integer nq, const double q[], double qv[], NagError *fail)
The function may be called by the names: g01amc, nag_stat_quantiles or nag_double_quantiles.

## 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.
g01amc 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}$Integer Input
On entry: the number of elements in the input vector rv.
Constraint: ${\mathbf{n}}>0$.
2: $\mathbf{rv}\left[{\mathbf{n}}\right]$double 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: $\mathbf{nq}$Integer Input
On entry: the number of quantiles requested.
Constraint: ${\mathbf{nq}}>0$.
4: $\mathbf{q}\left[{\mathbf{nq}}\right]$const double 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.0\le {\mathbf{q}}\left[\mathit{i}-1\right]\le 1.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{nq}}$;
• ${\mathbf{q}}\left[\mathit{i}-1\right]\le {\mathbf{q}}\left[\mathit{i}\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{nq}}-1$.
5: $\mathbf{qv}\left[{\mathbf{nq}}\right]$double Output
On exit: ${\mathbf{qv}}\left[i-1\right]$ contains the quantile specified by the value provided in ${\mathbf{q}}\left[i-1\right]$, or an interpolated value if the quantile falls between two data values.
6: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>0$.
On entry, ${\mathbf{nq}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nq}}>0$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_Q_NOT_ASCENDING
On entry, q was not in ascending order.
NE_Q_OUT_OF_RANGE
On entry, an element of q was less than $0.0$ or greater than $1.0$.
NE_STACK_OVERFLOW

Not applicable.

## 8Parallelism and Performance

g01amc is not threaded in any implementation.

The average time taken by g01amc 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 doubles and an array of point values.

### 10.1Program Text

Program Text (g01amce.c)

### 10.2Program Data

Program Data (g01amce.d)

### 10.3Program Results

Program Results (g01amce.r)