NAG FL Interfaceg01amf (quantiles)

▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

1Purpose

g01amf finds specified quantiles from a vector of unsorted data.

2Specification

Fortran Interface
 Subroutine g01amf ( n, rv, nq, q, qv,
 Integer, Intent (In) :: n, nq Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: q(nq) Real (Kind=nag_wp), Intent (Inout) :: rv(n) Real (Kind=nag_wp), Intent (Out) :: qv(nq)
#include <nag.h>
 void g01amf_ (const Integer *n, double rv[], const Integer *nq, const double q[], double qv[], Integer *ifail)
The routine may be called by the names g01amf or nagf_stat_quantiles.

3Description

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

4References

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

5Arguments

1: $\mathbf{n}$Integer Input
On entry: the number of elements in the input vector rv.
Constraint: ${\mathbf{n}}>0$.
2: $\mathbf{rv}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) 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.
3: $\mathbf{nq}$Integer Input
On entry: the number of quantiles requested.
Constraint: ${\mathbf{nq}}>0$.
4: $\mathbf{q}\left({\mathbf{nq}}\right)$Real (Kind=nag_wp) 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.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$.
5: $\mathbf{qv}\left({\mathbf{nq}}\right)$Real (Kind=nag_wp) array Output
On exit: ${\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.
6: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}>0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{nq}}=⟨\mathit{\text{value}}⟩$.
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$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

8Parallelism and Performance

g01amf is not threaded in any implementation.

The average time taken by g01amf 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.

10Example

This example computes a list of quantiles from an array of reals and an array of point values.
Example 2 in m01daf demonstrates how to calculate weighted quantiles using ranking, binary searches and interpolation.

10.1Program Text

Program Text (g01amfe.f90)

10.2Program Data

Program Data (g01amfe.d)

10.3Program Results

Program Results (g01amfe.r)