G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02BZF

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

G02BZF combines two sets of sample means and sums of squares and cross-products matrices. It is designed to be used in conjunction with G02BUF to allow large datasets to be summarised.

## 2  Specification

 SUBROUTINE G02BZF ( MEAN, M, XSW, XMEAN, XC, YSW, YMEAN, YC, IFAIL)
 INTEGER M, IFAIL REAL (KIND=nag_wp) XSW, XMEAN(M), XC((M*M+M)/2), YSW, YMEAN(M), YC((M*M+M)/2) CHARACTER(1) MEAN

## 3  Description

Let $X$ and $Y$ denote two sets of data, each with $m$ variables and ${n}_{x}$ and ${n}_{y}$ observations respectively. Let ${\mu }_{x}$ denote the (optionally weighted) vector of $m$ means for the first dataset and ${C}_{x}$ denote either the sums of squares and cross-products of deviations from ${\mu }_{x}$
 $Cx= X-e⁢ μxT T ⁢ Dx ⁢ X-e⁢ μxT$
or the sums of squares and cross-products, in which case
 $Cx = XT ⁢ Dx ⁢X$
where $e$ is a vector of $n$ ones and ${D}_{x}$ is a diagonal matrix of (optional) weights, with the sum of the diagonal elements of ${D}_{x}$ equal to ${W}_{x}$. Similarly, let ${\mu }_{y}$, ${C}_{y}$ and ${W}_{y}$ denote the same quantities for the second dataset.
Given ${\mu }_{x},{\mu }_{y},{C}_{x},{C}_{y},{W}_{x}$ and ${W}_{y}$ G02BZF calculates ${\mu }_{z}$, ${C}_{z}$ and ${W}_{z}$ as if a dataset $Z$, with $m$ variables and ${n}_{x}+{n}_{y}$ observations were supplied to G02BUF, with $Z$ constructed as
 $Z = X Y .$
G02BZF has been designed to combine the results from two calls to G02BUF allowing large datasets, or cases where all the data is not available at the same time, to be summarised.

## 4  References

Bennett J, Pebay P, Roe D and Thompson D (2009) Numerically stable, single-pass, parallel statistics algorithms Proceedings of IEEE International Conference on Cluster Computing

## 5  Parameters

1:     MEAN – CHARACTER(1)Input
On entry: indicates whether the matrices supplied in XC and YC are sums of squares and cross-products, or sums of squares and cross-products of deviations about the mean.
${\mathbf{MEAN}}=\text{'M'}$
Sums of squares and cross-products of deviations about the mean have been supplied.
${\mathbf{MEAN}}=\text{'Z'}$
Sums of squares and cross-products have been supplied.
Constraint: ${\mathbf{MEAN}}=\text{'M'}$ or $\text{'Z'}$.
2:     M – INTEGERInput
On entry: $m$, the number of variables.
Constraint: ${\mathbf{M}}\ge 1$.
3:     XSW – REAL (KIND=nag_wp)Input/Output
On entry: ${W}_{x}$, the sum of weights, from the first set of data, $X$. If the data is unweighted then this will be the number of observations in the first dataset.
On exit: ${W}_{z}$, the sum of weights, from the combined dataset, $Z$. If both datasets are unweighted then this will be the number of observations in the combined dataset.
Constraint: ${\mathbf{XSW}}\ge 0$.
4:     XMEAN(M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${\mu }_{x}$, the sample means for the first set of data, $X$.
On exit: ${\mu }_{z}$, the sample means for the combined data, $Z$.
5:     XC($\left({\mathbf{M}}×{\mathbf{M}}+{\mathbf{M}}\right)/2$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: ${C}_{x}$, the sums of squares and cross-products matrix for the first set of data, $X$, as returned by G02BUF.
G02BUF, returns this matrix packed by columns, i.e., the cross-product between the $j$th and $k$th variable, $k\ge j$, is stored in ${\mathbf{XC}}\left(k×\left(k-1\right)/2+j\right)$.
No check is made that ${C}_{x}$ is a valid cross-products matrix.
On exit: ${C}_{z}$, the sums of squares and cross-products matrix for the combined dataset, $Z$.
This matrix is again stored packed by columns.
6:     YSW – REAL (KIND=nag_wp)Input
On entry: ${W}_{y}$, the sum of weights, from the second set of data, $Y$. If the data is unweighted then this will be the number of observations in the second dataset.
Constraint: ${\mathbf{YSW}}\ge 0$.
7:     YMEAN(M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mu }_{y}$, the sample means for the second set of data, $Y$.
8:     YC($\left({\mathbf{M}}×{\mathbf{M}}+{\mathbf{M}}\right)/2$) – REAL (KIND=nag_wp) arrayInput
On entry: ${C}_{y}$, the sums of squares and cross-products matrix for the second set of data, $Y$, as returned by G02BUF.
G02BUF, returns this matrix packed by columns, i.e., the cross-product between the $j$th and $k$th variable, $k\ge j$, is stored in ${\mathbf{YC}}\left(k×\left(k-1\right)/2+j\right)$.
No check is made that ${C}_{y}$ is a valid cross-products matrix.
9:     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{MEAN}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{MEAN}}=\text{'M'}$ or $\text{'Z'}$.
${\mathbf{IFAIL}}=21$
On entry, ${\mathbf{M}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{M}}\ge 1$.
${\mathbf{IFAIL}}=31$
On entry, ${\mathbf{XSW}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{XSW}}\ge 0.0$.
${\mathbf{IFAIL}}=61$
On entry, ${\mathbf{YSW}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{YSW}}\ge 0.0$.

Not applicable.

None.

## 9  Example

This example illustrates the use of G02BZF by dividing a dataset into three blocks of $4$, $5$ and $3$ observations respectively. Each block of data is summarised using G02BUF and then the three summaries combined using G02BZF.
The resulting sums of squares and cross-products matrix is then scaled to obtain the covariance matrix for the whole dataset.

### 9.1  Program Text

Program Text (g02bzfe.f90)

### 9.2  Program Data

Program Data (g02bzfe.d)

### 9.3  Program Results

Program Results (g02bzfe.r)