NAG FL Interface
g01anf (quantiles_​stream_​fixed)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

g01anf finds approximate quantiles from a data stream of known size using an out-of-core algorithm.

2 Specification

Fortran Interface
Subroutine g01anf ( ind, n, rv, nb, eps, np, q, qv, nq, rcomm, lrcomm, icomm, licomm, ifail)
Integer, Intent (In) :: n, nb, nq, lrcomm, licomm
Integer, Intent (Inout) :: ind, icomm(licomm), ifail
Integer, Intent (Out) :: np
Real (Kind=nag_wp), Intent (In) :: rv(*), eps, q(*)
Real (Kind=nag_wp), Intent (Inout) :: qv(*), rcomm(lrcomm)
C Header Interface
#include <nag.h>
void  g01anf_ (Integer *ind, const Integer *n, const double rv[], const Integer *nb, const double *eps, Integer *np, const double q[], double qv[], const Integer *nq, double rcomm[], const Integer *lrcomm, Integer icomm[], const Integer *licomm, Integer *ifail)
The routine may be called by the names g01anf or nagf_stat_quantiles_stream_fixed.

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.
g01anf uses a slightly modified version of an algorithm described in a paper by Zhang and Wang (2007) to determine ε-approximate quantiles of a data stream of n real values, where n is known. Given any quantile q[0.0,1.0], an ε-approximate quantile is defined as an element in the data stream whose rank falls within [(q-ε)n,(q+ε)n] . In case of more than one ε-approximate quantile being available, the one closest to qn is returned.

4 References

Zhang Q and Wang W (2007) A fast algorithm for approximate quantiles in high speed data streams Proceedings of the 19th International Conference on Scientific and Statistical Database Management IEEE Computer Society 29

5 Arguments

1: ind Integer Input/Output
On entry: indicates the action required in the current call to g01anf.
ind=0
Return the required length of rcomm and icomm in icomm(1) and icomm(2) respectively. n and eps must be set and licomm must be at least 2.
ind=1
Initialise the communication arrays and process the first nb values from the data stream as supplied in rv.
ind=2
Process the next block of nb values from the data stream. The calling program must update rv and (if required) nb, and re-enter g01anf with all other parameters unchanged.
ind=3
Calculate the nq ε-approximate quantiles specified in q. The calling program must set q and nq and re-enter g01anf with all other parameters unchanged. This option can be chosen only when npexp(1.0)/eps.
On exit: indicates output from a successful call.
ind=1
Lengths of rcomm and icomm have been returned in icomm(1) and icomm(2) respectively.
ind=2
g01anf has processed np data points and expects to be called again with additional data (i.e., np<n).
ind=3
g01anf has returned the requested ε-approximate quantiles in qv. These quantiles are based on np data points.
ind=4
Routine has processed all n data points (i.e., np=n).
Constraint: on entry ind=0, 1, 2 or 3.
2: n Integer Input
On entry: n, the total number of values in the data stream.
Constraint: n>0.
3: rv(*) Real (Kind=nag_wp) array Input
Note: the dimension of the array rv must be at least nb if ind=1 or 2.
On entry: if ind=1 or 2, the vector containing the current block of data, otherwise rv is not referenced.
4: nb Integer Input
On entry: if ind=1 or 2, the size of the current block of data. The size of blocks of data in array rv can vary;, therefore, nb can change between calls to g01anf.
Constraint: if ind=1 or 2, nb>0.
5: eps Real (Kind=nag_wp) Input
On entry: approximation factor ε.
Constraint: epsexp(1.0)/n ​ and ​eps1.0.
6: np Integer Output
On exit: the number of elements processed so far.
7: q(*) Real (Kind=nag_wp) array Input
Note: the dimension of the array q must be at least nq if ind=3.
On entry: if ind=3, the quantiles to be calculated, otherwise q is not referenced. Note that q(i)=0.0, corresponds to the minimum value and q(i)=1.0 to the maximum value.
Constraint: if ind=3, 0.0q(i)1.0, for i=1,2,,nq.
8: qv(*) Real (Kind=nag_wp) array Output
Note: the dimension of the array qv must be at least nq if ind=3.
On exit: if ind=3, qv(i) contains the ε-approximate quantiles specified by the value provided in q(i).
9: nq Integer Input
On entry: if ind=3, the number of quantiles requested, otherwise nq is not referenced.
Constraint: if ind=3, nq>0.
10: rcomm(lrcomm) Real (Kind=nag_wp) array Communication Array
11: lrcomm Integer Input
On entry: the dimension of the array rcomm as declared in the (sub)program from which g01anf is called.
Constraint: if ind0, lrcomm must be at least equal to the value returned in icomm(1) by a call to g01anf with ind=0. This will not be more than x+2× min(x, x/2.0 + 1 ) × log2(n/x+1.0) + 1 , where x= max(1,log(eps×n)/eps) .
12: icomm(licomm) Integer array Communication Array
13: licomm Integer Input
On entry: the dimension of the array icomm as declared in the (sub)program from which g01anf is called.
Constraints:
  • if ind=0, licomm2;
  • otherwise licomm must be at least equal to the value returned in icomm(2) by a call to g01anf with ind=0. This will not be more than 2 × (x+2×min(x, x/2.0 +1 )×y) + y + 6 , where x = max(1,log(eps×n)/eps) and y = log2(n/x+1.0) + 1 .
14: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1. If you are unfamiliar with this argument you should refer to Section 7 in the Introduction to the NAG Library CL Interface for details.
On exit: ifail=0 unless the routine detects an error (see Section 6).
As an out-of-core routine g01anf will only perform certain argument checks when a data checkpoint (including completion of data input) is signaled. As such it will usually be inappropriate to halt program execution when an error is detected since any errors may be subsequently resolved without losing any processing already carried out. Therefore, setting ifail to a value of −1 or 1 is recommended. If the output of error messages is undesirable, the value 1 is recommended. When the value -1 or 1 is used it is essential to test the value of ifail on exit.

6 Error Indicators and Warnings

If on entry 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:
ifail=1
On entry, ind=value.
Constraint: ind=0, 1, 2 or 3.
ifail=2
On entry, n=value.
Constraint: n>0.
ifail=3
On entry, eps=value.
Constraint: exp(1.0)/neps1.0.
ifail=4
On entry, ind=1 or 2 and nb=value.
Constraint: if ind=1 or 2 then nb>0.
ifail=5
On entry, licomm is too small: licomm=value.
ifail=6
On entry, lrcomm is too small: lrcomm=value.
ifail=7
Number of data elements streamed, value is not sufficient for a quantile query when eps=value.
Supply more data or reprocess the data with a higher eps value.
ifail=8
On entry, ind=3 and nq=value.
Constraint: if ind=3 then nq>0.
ifail=9
On entry, ind=3 and q(value)=value.
Constraint: if ind=3 then 0.0q(i)1.0 for all i.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
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.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

Not applicable.

8 Parallelism and Performance

g01anf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

The average time taken by g01anf is n log(1/εlog(εn)) .

10 Example

This example calculates ε-approximate quantile for q=0.25, 0.5 and 1.0 for a data stream of 60 values. The stream is read in four blocks of varying size.

10.1 Program Text

Program Text (g01anfe.f90)

10.2 Program Data

Program Data (g01anfe.d)

10.3 Program Results

Program Results (g01anfe.r)