G02BTF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G02BTF

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

G02BTF updates the sample means and sums of squares and cross-products, or sums of squares and cross-products of deviations about the mean, for a new observation. The data may be weighted.

2  Specification

SUBROUTINE G02BTF ( MEAN, M, WT, X, INCX, SW, XBAR, C, IFAIL)
INTEGER  M, INCX, IFAIL
REAL (KIND=nag_wp)  WT, X(M*INCX), SW, XBAR(M), C((M*M+M)/2)
CHARACTER(1)  MEAN

3  Description

G02BTF is an adaptation of West's WV2 algorithm; see West (1979). This routine updates the weighted means of variables and weighted sums of squares and cross-products or weighted sums of squares and cross-products of deviations about the mean for observations on m variables Xj, for j=1,2,,m. For the first i-1 observations let the mean of the jth variable be x-ji-1, the cross-product about the mean for the jth and kth variables be cjki-1 and the sum of weights be Wi-1. These are updated by the ith observation, xij, for j=1,2,,m, with weight wi as follows:
Wi=Wi-1+wi,  x-ji=x-ji-1+wiWixj-x-ji-1,  j=1,2,,m
and
cjki=cjki- 1+wiWixj-x-ji- 1xk-x-ki- 1Wi- 1,   j= 1,2,,m;k=j,j+ 1,2,,m.
The algorithm is initialized by taking x-j1=x1j, the first observation and cij1=0.0.
For the unweighted case wi=1 and Wi=i for all i.

4  References

Chan T F, Golub G H and Leveque R J (1982) Updating Formulae and a Pairwise Algorithm for Computing Sample Variances Compstat, Physica-Verlag
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555

5  Parameters

1:     MEAN – CHARACTER(1)Input
On entry: indicates whether G02BTF is to calculate sums of squares and cross-products, or sums of squares and cross-products of deviations about the mean.
MEAN='M'
The sums of squares and cross-products of deviations about the mean are calculated.
MEAN='Z'
The sums of squares and cross-products are calculated.
Constraint: MEAN='M' or 'Z'.
2:     M – INTEGERInput
On entry: m, the number of variables.
Constraint: M1.
3:     WT – REAL (KIND=nag_wp)Input
On entry: the weight to use for the current observation, wi.
For unweighted means and cross-products set WT=1.0. The use of a suitable negative value of WT, e.g., -wi will have the effect of deleting the observation.
4:     X(M×INCX) – REAL (KIND=nag_wp) arrayInput
On entry: Xj-1×INCX+1 must contain the value of the jth variable for the current observation, j=1,2,,m.
5:     INCX – INTEGERInput
On entry: the increment of X. Two situations are common.
If INCX=1, the data values are to be found in consecutive locations in X, i.e., in a column.
If INCX=ldx, for some positive integer ldx, the data values are to be found as a row of an array with first dimension ldx.
Constraint: INCX>0.
6:     SW – REAL (KIND=nag_wp)Input/Output
On entry: the sum of weights for the previous observations, Wi-1.
SW=0.0
The update procedure is initialized.
SW+WT=0.0
All elements of XBAR and C are set to zero.
Constraint: SW0.0 and SW+WT0.0.
On exit: contains the updated sum of weights, Wi.
7:     XBAR(M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if SW=0.0, XBAR is initialized, otherwise XBARj must contain the weighted mean of the jth variable for the previous i-1 observations, x-ji-1, for j=1,2,,m.
On exit: XBARj contains the weighted mean of the jth variable, x-ji, for j=1,2,,m.
8:     C(M×M+M/2) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if SW0.0, C must contain the upper triangular part of the matrix of weighted sums of squares and cross-products or weighted sums of squares and cross-products of deviations about the mean. It is stored packed form by column, i.e., the cross-product between the jth and kth variable, kj, is stored in Ck×k-1/2+j.
On exit: the update sums of squares and cross-products stored as on input.
9:     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.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ 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 -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

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,M<1,
orINCX<1.
IFAIL=2
On entry,SW<0.0.
IFAIL=3
On entry,SW+WT<0.0, the current weight causes the sum of weights to be less than 0.0.
IFAIL=4
On entry,MEAN'M' or 'Z'.

7  Accuracy

For a detailed discussion of the accuracy of this method see Chan et al. (1982) and West (1979).

8  Further Comments

G02BTF may be used to update the results returned by G02BUF.
G02BWF may be used to calculate the correlation matrix from the matrix of sums of squares and cross-products of deviations about the mean and the matrix may be scaled using F06EDF (DSCAL) or F06FDF to produce a variance-covariance matrix.

9  Example

A program to calculate the means, the required sums of squares and cross-products matrix, and the variance matrix for a set of 3 observations of 3 variables.

9.1  Program Text

Program Text (g02btfe.f90)

9.2  Program Data

Program Data (g02btfe.d)

9.3  Program Results

Program Results (g02btfe.r)


G02BTF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

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