NAG Library Routine Document
G02BUF
1 Purpose
G02BUF calculates the sample means and sums of squares and crossproducts, or sums of squares and crossproducts of deviations from the mean, in a single pass for a set of data. The data may be weighted.
2 Specification
SUBROUTINE G02BUF ( 
MEAN, WEIGHT, N, M, X, LDX, WT, SW, WMEAN, C, IFAIL) 
INTEGER 
N, M, LDX, IFAIL 
REAL (KIND=nag_wp) 
X(LDX,M), WT(*), SW, WMEAN(M), C((M*M+M)/2) 
CHARACTER(1) 
MEAN, WEIGHT 

3 Description
G02BUF is an adaptation of West's WV2 algorithm; see
West (1979). This routine calculates the (optionally weighted) sample means and (optionally weighted) sums of squares and crossproducts or sums of squares and crossproducts of deviations from the (weighted) mean for a sample of
$n$ observations on
$m$ variables
${X}_{j}$, for
$\mathit{j}=1,2,\dots ,m$. The algorithm makes a single pass through the data.
For the first
$i1$ observations let the mean of the
$j$th variable be
${\stackrel{}{x}}_{j}\left(i1\right)$, the crossproduct about the mean for the
$j$th and
$k$th variables be
${c}_{jk}\left(i1\right)$ and the sum of weights be
${W}_{i1}$. These are updated by the
$i$th observation,
${x}_{ij}$, for
$\mathit{j}=1,2,\dots ,m$, with weight
${w}_{i}$ as follows:
and
The algorithm is initialized by taking ${\stackrel{}{x}}_{j}\left(1\right)={x}_{1j}$, the first observation, and ${c}_{ij}\left(1\right)=0.0$.
For the unweighted case ${w}_{i}=1$ and ${W}_{i}=i$ for all $i$.
Note that only the upper triangle of the matrix is calculated and returned packed by column.
4 References
Chan T F, Golub G H and Leveque R J (1982) Updating Formulae and a Pairwise Algorithm for Computing Sample Variances Compstat, PhysicaVerlag
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555
5 Parameters
 1: $\mathrm{MEAN}$ – CHARACTER(1)Input

On entry: indicates whether G02BUF is to calculate sums of squares and crossproducts, or sums of squares and crossproducts of deviations about the mean.
 ${\mathbf{MEAN}}=\text{'M'}$
 The sums of squares and crossproducts of deviations about the mean are calculated.
 ${\mathbf{MEAN}}=\text{'Z'}$
 The sums of squares and crossproducts are calculated.
Constraint:
${\mathbf{MEAN}}=\text{'M'}$ or $\text{'Z'}$.
 2: $\mathrm{WEIGHT}$ – CHARACTER(1)Input

On entry: indicates whether the data is weighted or not.
 ${\mathbf{WEIGHT}}=\text{'U'}$
 The calculations are performed on unweighted data.
 ${\mathbf{WEIGHT}}=\text{'W'}$
 The calculations are performed on weighted data.
Constraint:
${\mathbf{WEIGHT}}=\text{'W'}$ or $\text{'U'}$.
 3: $\mathrm{N}$ – INTEGERInput

On entry: $n$, the number of observations in the dataset.
Constraint:
${\mathbf{N}}\ge 1$.
 4: $\mathrm{M}$ – INTEGERInput

On entry: $m$, the number of variables.
Constraint:
${\mathbf{M}}\ge 1$.
 5: $\mathrm{X}\left({\mathbf{LDX}},{\mathbf{M}}\right)$ – REAL (KIND=nag_wp) arrayInput

On entry: ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}$th observation on the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,m$.
 6: $\mathrm{LDX}$ – INTEGERInput

On entry: the first dimension of the array
X as declared in the (sub)program from which G02BUF is called.
Constraint:
${\mathbf{LDX}}\ge {\mathbf{N}}$.
 7: $\mathrm{WT}\left(*\right)$ – REAL (KIND=nag_wp) arrayInput

Note: the dimension of the array
WT
must be at least
${\mathbf{N}}$ if
${\mathbf{WEIGHT}}=\text{'W'}$, and at least
$1$ otherwise.
On entry: the optional weights of each observation.
If
${\mathbf{WEIGHT}}=\text{'U'}$,
WT is not referenced.
If ${\mathbf{WEIGHT}}=\text{'W'}$, ${\mathbf{WT}}\left(i\right)$ must contain the weight for the $i$th observation.
Constraint:
if ${\mathbf{WEIGHT}}=\text{'W'}$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$.
 8: $\mathrm{SW}$ – REAL (KIND=nag_wp)Output

On exit: the sum of weights.
If
${\mathbf{WEIGHT}}=\text{'U'}$,
SW contains the number of observations,
$n$.
 9: $\mathrm{WMEAN}\left({\mathbf{M}}\right)$ – REAL (KIND=nag_wp) arrayOutput

On exit: the sample means. ${\mathbf{WMEAN}}\left(j\right)$ contains the mean for the $j$th variable.
 10: $\mathrm{C}\left(\left({\mathbf{M}}\times {\mathbf{M}}+{\mathbf{M}}\right)/2\right)$ – REAL (KIND=nag_wp) arrayOutput

On exit: the crossproducts.
If
${\mathbf{MEAN}}=\text{'M'}$,
C contains the upper triangular part of the matrix of (weighted) sums of squares and crossproducts of deviations about the mean.
If
${\mathbf{MEAN}}=\text{'Z'}$,
C contains the upper triangular part of the matrix of (weighted) sums of squares and crossproducts.
These are stored packed by columns, i.e., the crossproduct between the $j$th and $k$th variable, $k\ge j$, is stored in ${\mathbf{C}}\left(k\times \left(k1\right)/2+j\right)$.
 11: $\mathrm{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}}=1$

On entry,  ${\mathbf{M}}<1$, 
or  ${\mathbf{N}}<1$, 
or  ${\mathbf{LDX}}<{\mathbf{N}}$. 
 ${\mathbf{IFAIL}}=2$

On entry,  ${\mathbf{MEAN}}\ne \text{'M'}$ or $\text{'Z'}$. 
 ${\mathbf{IFAIL}}=3$

On entry,  ${\mathbf{WEIGHT}}\ne \text{'W'}$ or $\text{'U'}$. 
 ${\mathbf{IFAIL}}=4$

On entry,  ${\mathbf{WEIGHT}}=\text{'W'}$, and a value of ${\mathbf{WT}}<0.0$. 
 ${\mathbf{IFAIL}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.8 in the Essential Introduction for further information.
 ${\mathbf{IFAIL}}=399$
Your licence key may have expired or may not have been installed correctly.
See
Section 3.7 in the Essential Introduction for further information.
 ${\mathbf{IFAIL}}=999$
Dynamic memory allocation failed.
See
Section 3.6 in the Essential Introduction for further information.
7 Accuracy
For a detailed discussion of the accuracy of this algorithm see
Chan et al. (1982) or
West (1979).
8 Parallelism and Performance
Not applicable.
G02BWF may be used to calculate the correlation coefficients from the crossproducts of deviations about the mean. The crossproducts of deviations about the mean may be scaled
using
F06EDF (DSCAL) or
F06FDF
to give a variancecovariance matrix.
The means and crossproducts produced by G02BUF may be updated by adding or removing observations using
G02BTF.
Two sets of means and crossproducts, as produced by G02BUF, can be combined using
G02BZF.
10 Example
A program to calculate the means, the required sums of squares and crossproducts matrix, and the variance matrix for a set of $3$ observations of $3$ variables.
10.1 Program Text
Program Text (g02bufe.f90)
10.2 Program Data
Program Data (g02bufe.d)
10.3 Program Results
Program Results (g02bufe.r)