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:
Mark 23: rv 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$0.5$ quantile because half the values are less than or equal to it; and the 0.25$0.25$ quantile is the 25$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:     rv(n) – double array
n, the dimension of the array, must satisfy the constraint n > 0${\mathbf{n}}>0$.
The vector whose quantiles are to be determined.
2:     q(nq) – double array
nq, the dimension of the array, must satisfy the constraint nq > 0${\mathbf{nq}}>0$.
The quantiles to be calculated, in ascending order. Note that these must be between 0.0$0.0$ and 1.0$1.0$, with 0.0$0.0$ returning the smallest element and 1.0$1.0$ the largest.
Constraints:
• 0.0q(i)1.0$0.0\le {\mathbf{q}}\left(\mathit{i}\right)\le 1.0$, for i = 1,2,,nq$\mathit{i}=1,2,\dots ,{\mathbf{nq}}$;
• q(i)q(i + 1)${\mathbf{q}}\left(\mathit{i}\right)\le {\mathbf{q}}\left(\mathit{i}+1\right)$, for i = 1,2,,nq1$\mathit{i}=1,2,\dots ,{\mathbf{nq}}-1$.

### Optional Input Parameters

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

None.

### Output Parameters

1:     qv(nq) – double array
qv(i)${\mathbf{qv}}\left(i\right)$ contains the quantile specified by the value provided in q(i)${\mathbf{q}}\left(i\right)$, or an interpolated value if the quantile falls between two data values.
2:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 1${\mathbf{n}}<1$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, nq < 1${\mathbf{nq}}<1$.
ifail = 3${\mathbf{ifail}}=3$
On entry, some q < 0.0${\mathbf{q}}<0.0$ or q > 1.0${\mathbf{q}}>1.0$.
ifail = 4${\mathbf{ifail}}=4$
On entry, q is not in ascending order.
ifail = 5${\mathbf{ifail}}=5$

## Accuracy

Not applicable.

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

## Example

```function nag_stat_quantiles_example
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] = nag_stat_quantiles(rv, q)
```
```

qv =

2.2000
4.9000
9.7000

ifail =

0

```
```function g01am_example
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)
```
```

qv =

2.2000
4.9000
9.7000

ifail =

0

```