NAG FL Interface
g02btf (ssqmat_​update)

Settings help

FL Name Style:


FL Specification Language:


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

Fortran Interface
Subroutine g02btf ( mean, m, wt, x, incx, sw, xbar, c, ifail)
Integer, Intent (In) :: m, incx
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: wt, x(m*incx)
Real (Kind=nag_wp), Intent (Inout) :: sw, xbar(m), c((m*m+m)/2)
Character (1), Intent (In) :: mean
C Header Interface
#include <nag.h>
void  g02btf_ (const char *mean, const Integer *m, const double *wt, const double x[], const Integer *incx, double *sw, double xbar[], double c[], Integer *ifail, const Charlen length_mean)
The routine may be called by the names g02btf or nagf_correg_ssqmat_update.

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¯j(i-1), the cross-product about the mean for the jth and kth variables be cjk(i-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¯j(i)=x¯j(i-1)+wiWi(xj-x¯j(i-1)),  j=1,2,,m  
and
cjk(i)=cjk(i-1)+wiWi(xj-x¯j(i-1))(xk-x¯k(i-1))Wi- 1,   j= 1,2,,m;k=j,j+ 1,2,,m.  
The algorithm is initialized by taking x¯j(1)=x1j, the first observation and cij(1)=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 Arguments

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 Integer Input
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) array Input
On entry: x((j-1)×incx+1) must contain the value of the jth variable for the current observation, j=1,2,,m.
5: incx Integer Input
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) array Input/Output
On entry: if sw=0.0, xbar is initialized, otherwise xbar(j) must contain the weighted mean of the jth variable for the previous (i-1) observations, x¯j(i-1), for j=1,2,,m.
On exit: xbar(j) contains the weighted mean of the jth variable, x¯j(i), for j=1,2,,m.
8: c((m×m+m)/2) Real (Kind=nag_wp) array Input/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 c(k×(k-1)/2+j).
On exit: the update sums of squares and cross-products stored as on input.
9: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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, incx=value.
Constraint: incx1.
On entry, m=value.
Constraint: m1.
ifail=2
On entry, sw=value.
Constraint: sw0.0.
ifail=3
On entry, (sw+wt)=value.
Constraint: (sw+wt)0.0.
ifail=4
On entry, mean=value.
Constraint: mean='M' or 'Z'.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

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

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g02btf is not threaded in any implementation.

9 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 or f06fdf to produce a variance-covariance matrix.

10 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.

10.1 Program Text

Program Text (g02btfe.f90)

10.2 Program Data

Program Data (g02btfe.d)

10.3 Program Results

Program Results (g02btfe.r)