NAG CPP Interface
nagcpp::stat::quantiles (g01am)

Settings help

CPP Name Style:



1 Purpose

quantiles finds specified quantiles from a vector of unsorted data.

2 Specification

#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)

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

4 References

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

5 Arguments

1: rv(n) 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: q(nq) 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.0q(i-1)1.0, for i=1,2,,nq;
  • q(i-1)q(i), for i=1,2,,nq-1.
3: qv(nq) double array Output
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.
4: opt OptionalG01AM Input/Output
Optional parameter container, derived from Optional.

5.1Additional Quantities

1: n
The number of elements in the input vector rv
2: nq
The number of quantiles requested.

6 Exceptions 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
errorid=1
On entry, n = value.
Constraint: n>0.
errorid=2
On entry, nq = value.
Constraint: nq>0.
errorid=3
On entry, an element of q was less than 0.0 or greater than 1.0.
errorid=4
On entry, q was not in ascending order.
errorid=10601
On entry, argument value must be a vector of size value array.
Supplied argument has value dimensions.
errorid=10601
On entry, argument value must be a vector of size value array.
Supplied argument was a vector of size value.
errorid=10601
On entry, argument value must be a vector of size value array.
The size for the supplied array could not be ascertained.
errorid=10602
On entry, the raw data component of value is null.
errorid=10603
On entry, unable to ascertain a value for value.
errorid=−99
An unexpected error has been triggered by this routine.
errorid=−399
Your licence key may have expired or may not have been installed correctly.
errorid=−999
Dynamic memory allocation failed.

7 Accuracy

Not applicable.

8 Parallelism and Performance

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

9 Further Comments

The average time taken by quantiles is approximately proportional to n×(1+log(nq)). 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 reals and an array of point values.

10.1 Example Program

Source File Data Results
ex_g01am.cpp None ex_g01am.r