hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_stat_quantiles (g01am)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


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


[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


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 25th 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.


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


Compulsory Input Parameters

1:     rvn – double array
The vector whose quantiles are to be determined.
2:     qnq – 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.
  • 0.0qi1.0, for i=1,2,,nq;
  • qiqi+1, for i=1,2,,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.
2:     nq int64int32nag_int scalar
Default: the dimension of the array q.
The number of quantiles requested.
Constraint: nq>0.

Output Parameters

1:     qvnq – double array
qvi contains the quantile specified by the value provided in qi, or an interpolated value if the quantile falls between two data values.
2:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
Constraint: n>0.
Constraint: nq>0.
On entry, an element of q was less than 0.0 or greater than 1.0.
On entry, q was not in ascending order.
Internal error. Please contact NAG.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


Not applicable.

Further Comments

The average time taken by nag_stat_quantiles (g01am) is approximately proportional to n×1+lognq. The worst case time is proportional to n2 but this is extremely unlikely to occur.


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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015