# NAG CPP Interfacenagcpp::stat::quantiles (g01am)

## 1Purpose

quantiles finds specified quantiles from a vector of unsorted data.

## 2Specification

```#include "g01/nagcpp_g01am.hpp"
```
```template <typename RV, typename Q, typename QV>

void function quantiles(RV &&rv, const Q &q, QV &&qv, OptionalG01AM opt)```
```template <typename RV, typename Q, typename QV>

void function quantiles(RV &&rv, const Q &q, QV &&qv)```

## 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.
quantiles 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{rv}\left({\mathbf{n}}\right)$double array 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.
2: $\mathbf{q}\left({\mathbf{nq}}\right)$double array 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$.
3: $\mathbf{qv}\left({\mathbf{nq}}\right)$double array 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.
4: $\mathbf{opt}$OptionalG01AM Input/Output
Optional parameter container, derived from Optional.

1: $\mathbf{n}$
The number of elements in the input vector rv
2: $\mathbf{nq}$
The number of quantiles requested.

## 6Exceptions and Warnings

Errors or warnings detected by the function:
All errors and warnings have an associated numeric error code field, errorid, stored either as a member of the thrown exception object (see errorid), or as a member of opt.ifail, depending on how errors and warnings are being handled (see Error Handling for more details).
Raises: ErrorException
$\mathbf{errorid}=1$
On entry, ${\mathbf{n}}=⟨\mathit{value}⟩$.
Constraint: ${\mathbf{n}}>0$.
$\mathbf{errorid}=2$
On entry, ${\mathbf{nq}}=⟨\mathit{value}⟩$.
Constraint: ${\mathbf{nq}}>0$.
$\mathbf{errorid}=3$
On entry, an element of q was less than $0.0$ or greater than $1.0$.
$\mathbf{errorid}=4$
On entry, q was not in ascending order.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a vector of size $⟨\mathit{\text{value}}⟩$ array.
Supplied argument has $⟨\mathit{\text{value}}⟩$ dimensions.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a vector of size $⟨\mathit{\text{value}}⟩$ array.
Supplied argument was a vector of size $⟨\mathit{\text{value}}⟩$.
$\mathbf{errorid}=10601$
On entry, argument $⟨\mathit{\text{value}}⟩$ must be a vector of size $⟨\mathit{\text{value}}⟩$ array.
The size for the supplied array could not be ascertained.
$\mathbf{errorid}=10602$
On entry, the raw data component of $⟨\mathit{\text{value}}⟩$ is null.
$\mathbf{errorid}=10603$
On entry, unable to ascertain a value for $⟨\mathit{\text{value}}⟩$.
$\mathbf{errorid}=-99$
An unexpected error has been triggered by this routine.
$\mathbf{errorid}=-399$
Your licence key may have expired or may not have been installed correctly.
$\mathbf{errorid}=-999$
Dynamic memory allocation failed.

Not applicable.

## 8Parallelism and Performance

Please see the description for the underlying computational routine in this section of the FL Interface documentation.

The average time taken by quantiles 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.