Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_stat_quantiles (g01am)

## Purpose

nag_stat_quantiles (g01am) finds specified quantiles from a vector of unsorted data.

## Syntax

[qv, ifail] = g01am(rv, q, 'n', n, 'nq', nq)
[qv, ifail] = nag_stat_quantiles(rv, q, 'n', n, 'nq', nq)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 23: rv is no longer an output parameter

## 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.
nag_stat_quantiles (g01am) 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.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{rv}\left({\mathbf{n}}\right)$ – double array
The vector whose quantiles are to be determined.
2:     $\mathrm{q}\left({\mathbf{nq}}\right)$ – double array
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$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array rv.
The number of elements in the input vector rv.
Constraint: ${\mathbf{n}}>0$.
2:     $\mathrm{nq}$int64int32nag_int scalar
Default: the dimension of the array q.
The number of quantiles requested.
Constraint: ${\mathbf{nq}}>0$.

### Output Parameters

1:     $\mathrm{qv}\left({\mathbf{nq}}\right)$ – double array
${\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.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{n}}>0$.
${\mathbf{ifail}}=2$
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$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Not applicable.

The average time taken by nag_stat_quantiles (g01am) 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.

## Example

This example computes a list of quantiles from an array of doubles and an array of point values.
```function g01am_example

fprintf('g01am example results\n\n');

rv = [4.9   7   3.9   9.5   1.3   3.1   9.7   0.3   8.5   0.6   6.2];
q  = [0.25  0.5 1];

[qv, ifail] = g01am(rv, q);

fprintf('Quantile    Result\n');
fprintf('%7.2f    %7.4f\n',[q; qv']);

```
```g01am example results

Quantile    Result
0.25     2.2000
0.50     4.9000
1.00     9.7000
```