G01 Chapter Contents
G01 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG01ATF

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.

## 1  Purpose

G01ATF calculates the mean, standard deviation, coefficients of skewness and kurtosis, and the maximum and minimum values for a set of (optionally weighted) data. The input data can be split into arbitrary sized blocks, allowing large datasets to be summarised.

## 2  Specification

 SUBROUTINE G01ATF ( NB, X, IWT, WT, PN, XMEAN, XSD, XSKEW, XKURT, XMIN, XMAX, RCOMM, IFAIL)
 INTEGER NB, IWT, PN, IFAIL REAL (KIND=nag_wp) X(NB), WT(*), XMEAN, XSD, XSKEW, XKURT, XMIN, XMAX, RCOMM(20)

## 3  Description

Given a sample of $n$ observations, denoted by $x=\left\{{x}_{i}:i=1,2,\dots ,n\right\}$ and a set of non-negative weights, $w=\left\{{w}_{i}:i=1,2,\dots ,n\right\}$, G01ATF calculates a number of quantities:
(a) Mean
 $x- = ∑ i=1 n wi xi W , where W = ∑ i=1 n wi .$
(b) Standard deviation
 $s2 = ∑ i=1 n wi xi - x- 2 d , where d = W - ∑ i=1 n wi2 W .$
(c) Coefficient of skewness
 $s3 = ∑ i=1 n wi xi - x- 3 d ⁢ s23 .$
(d) Coefficient of kurtosis
 $s4 = ∑ i=1 n wi xi - x- 4 d ⁢ s24 -3 .$
(e) Maximum and minimum elements, with ${w}_{i}\ne 0$.
These quantities are calculated using the one pass algorithm of West (1979).
For large datasets, or where all the data is not available at the same time, $x$ and $w$ can be split into arbitrary sized blocks and G01ATF called multiple times.

## 4  References

West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

## 5  Parameters

1:     NB – INTEGERInput
On entry: $b$, the number of observations in the current block of data. The size of the block of data supplied in X and WT can vary; therefore NB can change between calls to G01ATF.
Constraint: ${\mathbf{NB}}\ge 0$.
2:     X(NB) – REAL (KIND=nag_wp) arrayInput
On entry: the current block of observations, corresponding to ${x}_{\mathit{i}}$, for $\mathit{i}=k+1,\dots ,k+b$, where $k$ is the number of observations processed so far and $b$ is the size of the current block of data.
3:     IWT – INTEGERInput
On entry: indicates whether user-supplied weights are provided:
${\mathbf{IWT}}=1$
User-supplied weights are given in the array WT.
${\mathbf{IWT}}=0$
${w}_{i}=1$, for all $i$, so no user-supplied weights are given and WT is not referenced.
Constraint: ${\mathbf{IWT}}=0$ or $1$.
4:     WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least ${\mathbf{NB}}$ if ${\mathbf{IWT}}=1$.
On entry: if ${\mathbf{IWT}}=1$, WT must contain the user-supplied weights corresponding to the block of data supplied in X, that is ${w}_{\mathit{i}}$, for $\mathit{i}=k+1,\dots ,k+b$.
Constraint: if ${\mathbf{IWT}}=1$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{NB}}$.
5:     PN – INTEGERInput/Output
On entry: the number of valid observations processed so far, that is the number of observations with ${w}_{i}>0$, for $\mathit{i}=1,2,\dots ,k$. On the first call to G01ATF, or when starting to summarise a new dataset, PN must be set to $0$.
If ${\mathbf{PN}}\ne 0$, it must be the same value as returned by the last call to G01ATF.
On exit: the updated number of valid observations processed, that is the number of observations with ${w}_{i}>0$, for $\mathit{i}=1,2,\dots ,k+b$.
Constraint: ${\mathbf{PN}}\ge 0$.
6:     XMEAN – REAL (KIND=nag_wp)Output
On exit: $\stackrel{-}{x}$, the mean of the first $k+b$ observations.
7:     XSD – REAL (KIND=nag_wp)Output
On exit: ${s}_{2}$, the standard deviation of the first $k+b$ observations.
8:     XSKEW – REAL (KIND=nag_wp)Output
On exit: ${s}_{3}$, the coefficient of skewness for the first $k+b$ observations.
9:     XKURT – REAL (KIND=nag_wp)Output
On exit: ${s}_{4}$, the coefficient of kurtosis for the first $k+b$ observations.
10:   XMIN – REAL (KIND=nag_wp)Output
On exit: the smallest value in the first $k+b$ observations.
11:   XMAX – REAL (KIND=nag_wp)Output
On exit: the largest value in the first $k+b$ observations.
12:   RCOMM($20$) – REAL (KIND=nag_wp) arrayCommunication Array
On entry: communication array, used to store information between calls to G01ATF. If ${\mathbf{PN}}=0$, RCOMM need not be initialized, otherwise it must be unchanged since the last call to this routine.
On exit: the updated communication array. The first five elements of RCOMM hold information that may be of interest with
 $RCOMM1 = ∑ i=1 k+b wi RCOMM2 = ∑ i=1 k+b wi 2 - ∑ i=1 k+b wi2 RCOMM3 = ∑ i=1 k+b wi xi - x- 2 RCOMM4 = ∑ i=1 k+b wi xi - x- 3 RCOMM5 = ∑ i=1 k+b wi xi - x- 4$
the remaining elements of RCOMM are used for workspace and so are undefined.
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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}}=11$
On entry, ${\mathbf{NB}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{NB}}\ge 0$.
${\mathbf{IFAIL}}=31$
On entry, ${\mathbf{IWT}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{IWT}}=0$ or $1$.
${\mathbf{IFAIL}}=41$
On entry, ${\mathbf{WT}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{IWT}}=1$ then ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{NB}}$.
${\mathbf{IFAIL}}=51$
On entry, ${\mathbf{PN}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{PN}}\ge 0$.
${\mathbf{IFAIL}}=52$
On entry, ${\mathbf{PN}}=⟨\mathit{\text{value}}⟩$.
On exit from previous call, ${\mathbf{PN}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{PN}}>0$, PN must be unchanged since previous call.
${\mathbf{IFAIL}}=53$
On entry, the number of valid observations is zero.
${\mathbf{IFAIL}}=71$
On exit we were unable to calculate XSKEW or XKURT. A value of $0$ has been returned.
${\mathbf{IFAIL}}=72$
On exit we were unable to calculate XSD, XSKEW or XKURT. A value of $0$ has been returned.
${\mathbf{IFAIL}}=121$
RCOMM has been corrupted between calls.

## 7  Accuracy

Not applicable.

Both G01ATF and G01AUF consolidate results from multiple summaries. Whereas the former can only be used to combine summaries calculated sequentially, the latter combines summaries calculated in an arbitrary order allowing, for example, summaries calculated on different processing units to be combined.

## 9  Example

This example summarises some simulated data. The data is supplied in three blocks, the first consisting of $21$ observations, the second $51$ observations and the last $28$ observations.

### 9.1  Program Text

Program Text (g01atfe.f90)

### 9.2  Program Data

Program Data (g01atfe.d)

### 9.3  Program Results

Program Results (g01atfe.r)