NAG C Library Function Document

nag_double_quantiles (g01amc)

1
Purpose

nag_double_quantiles (g01amc) finds specified quantiles from a vector of unsorted data.

2
Specification

#include <nag.h>
#include <nagg01.h>
void  nag_double_quantiles (Integer n, double rv[], Integer nq, const double q[], double qv[], NagError *fail)

3
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 25th percentile.
nag_double_quantiles (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.

4
References

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

5
Arguments

1:     n IntegerInput
On entry: the number of elements in the input vector rv.
Constraint: n>0.
2:     rv[n] doubleInput/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:     nq IntegerInput
On entry: the number of quantiles requested.
Constraint: nq>0.
4:     q[nq] const doubleInput
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.0q[i-1]1.0, for i=1,2,,nq;
  • q[i-1]q[i], for i=1,2,,nq-1.
5:     qv[nq] doubleOutput
On exit: qv[i-1] contains the quantile specified by the value provided in q[i-1], or an interpolated value if the quantile falls between two data values.
6:     fail NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

6
Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_INT
On entry, n=value.
Constraint: n>0.
On entry, nq=value.
Constraint: 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 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation 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
Internal error. Please contact NAG.

7
Accuracy

Not applicable.

8
Parallelism and Performance

nag_double_quantiles (g01amc) is not threaded in any implementation.

9
Further Comments

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

10
Example

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

10.1
Program Text

Program Text (g01amce.c)

10.2
Program Data

Program Data (g01amce.d)

10.3
Program Results

Program Results (g01amce.r)