G01APF (PDF version)
G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G01APF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

G01APF finds approximate quantiles from a large arbitrary-sized data stream using an out-of-core algorithm.

2  Specification

SUBROUTINE G01APF ( IND, RV, NB, EPS, NP, Q, QV, NQ, RCOMM, LRCOMM, ICOMM, LICOMM, IFAIL)
INTEGER  IND, NB, NP, NQ, LRCOMM, ICOMM(LICOMM), LICOMM, IFAIL
REAL (KIND=nag_wp)  RV(*), EPS, Q(*), QV(*), RCOMM(LRCOMM)

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.
G01APF uses a slightly modified version of an algorithm described in a paper by Zhang and Wang (2007) to determine ε-approximate quantiles of a large arbitrary-sized data stream of real values, where ε is a user-defined approximation factor. Let m denote the number of data elements processed so far then, given any quantile q0.0,1.0, an ε-approximate quantile is defined as an element in the data stream whose rank falls within q-εm,q+εm . In case of more than one ε-approximate quantile being available, the one closest to qm is used.

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  Parameters

1:     IND – INTEGERInput/Output
On initial entry: must be set to 0.
On entry: indicates the action required in the current call to G01APF.
IND=0
Initialize the communication arrays and attempt to process the first NB values from the data stream. EPS, RV and NB must be set and LICOMM must be at least 10.
IND=1
Attempt to process the next block of NB values from the data stream. The calling program must update RV and (if required) NB, and re-enter G01APF with all other parameters unchanged.
IND=2
Continue calculation following the reallocation of either or both of the communication arrays RCOMM and ICOMM.
IND=3
Calculate the NQ ε-approximate quantiles specified in Q. The calling program must set Q and NQ and re-enter G01APF with all other parameters unchanged. This option can be chosen only when NPexp1.0/EPS.
On exit: indicates output from the call.
IND=1
G01APF has processed NP data points and expects to be called again with additional data.
IND=2
Either one or more of the communication arrays RCOMM and ICOMM is too small. The new minimum lengths of RCOMM and ICOMM have been returned in ICOMM1 and ICOMM2 respectively. If the new minimum length is greater than the current length then the corresponding communication array needs to be reallocated, its contents preserved and G01APF called again with all other parameters unchanged.
If there is more data to be processed, it is recommended that LRCOMM and LICOMM are made significantly bigger than the minimum to limit the number of reallocations.
IND=3
G01APF has returned the requested ε-approximate quantiles in QV. These quantiles are based on NP data points.
Constraint: IND=0, 1, 2 or 3.
2:     RV(*) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array RV must be at least NB if IND=0, 1 or 2.
On entry: if IND=0, 1 or 2, the vector containing the current block of data, otherwise RV is not referenced.
3:     NB – INTEGERInput
On entry: if IND=0, 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 G01APF.
Constraint: if IND=0, 1 or 2, NB>0.
4:     EPS – REAL (KIND=nag_wp)Input
On entry: approximation factor ε.
Constraint: EPS>0.0 ​ and ​EPS1.0.
5:     NP – INTEGEROutput
On exit: m, the number of elements processed so far.
6:     Q(*) – REAL (KIND=nag_wp) arrayInput
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 Qi=0.0, corresponds to the minimum value and Qi=1.0 to the maximum value.
Constraint: if IND=3, 0.0Qi1.0, for i=1,2,,NQ.
7:     QV(*) – REAL (KIND=nag_wp) arrayOutput
Note: the dimension of the array QV must be at least NQ if IND=3.
On exit: if IND=3, QVi contains the ε-approximate quantiles specified by the value provided in Qi.
8:     NQ – INTEGERInput
On entry: if IND=3, the number of quantiles requested, otherwise NQ is not referenced.
Constraint: if IND=3, NQ>0.
9:     RCOMM(LRCOMM) – REAL (KIND=nag_wp) arrayCommunication Array
On entry: if IND=1 or 2 then the first l elements of RCOMM as supplied to G01APF must be identical to the first l elements of RCOMM returned from the last call to G01APF, where l is the value of LRCOMM used in the last call. In other words, the contents of RCOMM must not be altered between calls to this routine. If RCOMM needs to be reallocated then its contents must be preserved. If IND=0 then RCOMM need not be set.
On exit: RCOMM holds information required by subsequent calls to G01APF
10:   LRCOMM – INTEGERInput
On entry: the dimension of the array RCOMM as declared in the (sub)program from which G01APF is called.
Constraints:
  • if IND=0, LRCOMM1;
  • otherwise LRCOMMICOMM1.
11:   ICOMM(LICOMM) – INTEGER arrayCommunication Array
On entry: if IND=1 or 2 then the first l elements of ICOMM as supplied to G01APF must be identical to the first l elements of ICOMM returned from the last call to G01APF, where l is the value of LICOMM used in the last call. In other words, the contents of ICOMM must not be altered between calls to this routine. If ICOMM needs to be reallocated then its contents must be preserved. If IND=0 then ICOMM need not be set.
On exit: ICOMM1 holds the minimum required length for RCOMM and ICOMM2 holds the minimum required length for ICOMM. The remaining elements of ICOMM are used for communication between subsequent calls to G01APF.
12:   LICOMM – INTEGERInput
On entry: the dimension of the array ICOMM as declared in the (sub)program from which G01APF is called.
Constraints:
  • if IND=0, LICOMM10;
  • otherwise LICOMMICOMM2.
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
On exit: IFAIL=0 unless the routine detects an error (see Section 6).
As an out-of-core routine G01APF will only perform certain parameter 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, then 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,IND0, 1, 2 or 3
IFAIL=2
On entry,EPS0.0 or EPS>1.0.
IFAIL=3
On entry,IND3 and NB<1.
IFAIL=4
On entry,LICOMM<10.
IFAIL=5
On entry,LRCOMM<1.
IFAIL=6
On entry, ICOMM contents returned by a previous call have not been preserved.
IFAIL=7
On entry, RCOMM contents returned by a previous call have not been preserved.
IFAIL=8
On entry, number of data elements streamed is not sufficient for the ε-approximate quantile query. Supply more data or reprocess the data with a higher EPS value.
IFAIL=9
On entry,IND=3 and NQ<1.
IFAIL=10
On entry,IND=3 and Qi<0.0 or Qi>1.0.

7  Accuracy

Not applicable.

8  Further Comments

The average time taken by G01APF scales as NP log 1/ε logεNP .
It is not possible to determine in advance the final size of the communication arrays RCOMM and ICOMM without knowing the size of the dataset. However, if a rough size (n) is known, the speed of the computation can be increased if the sizes of the communication arrays are not smaller than
LRCOMM = log2 n×EPS+1.0 - 2 × 1.0/EPS +1+x+ 2× minx, x/2.0 +1 × y +1 LICOMM = log2 n×EPS+1.0 - 2 × 2 × 1.0/EPS +1 + 1 + 2 × x+2× minx, x/2.0 +1 × y + y + 11
where
x= max1, log EPS×n / EPS y = log2 n/x+1.0 +1 .

9  Example

This example computes a list of ε-approximate quantiles. The data is processed in blocks of 20 observations at a time to simulate a situation in which the data is made available in a piecemeal fashion.

9.1  Program Text

Program Text (g01apfe.f90)

9.2  Program Data

Program Data (g01apfe.d)

9.3  Program Results

Program Results (g01apfe.r)


G01APF (PDF version)
G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012