NAG FL Interface
g02buf (ssqmat)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

g02buf calculates the sample means and sums of squares and cross-products, or sums of squares and cross-products of deviations from the mean, in a single pass for a set of data. The data may be weighted.

2 Specification

Fortran Interface
Subroutine g02buf ( mean, weight, n, m, x, ldx, wt, sw, wmean, c, ifail)
Integer, Intent (In) :: n, m, ldx
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: x(ldx,m), wt(*)
Real (Kind=nag_wp), Intent (Out) :: sw, wmean(m), c((m*m+m)/2)
Character (1), Intent (In) :: mean, weight
C Header Interface
#include <nag.h>
void  g02buf_ (const char *mean, const char *weight, const Integer *n, const Integer *m, const double x[], const Integer *ldx, const double wt[], double *sw, double wmean[], double c[], Integer *ifail, const Charlen length_mean, const Charlen length_weight)
The routine may be called by the names g02buf or nagf_correg_ssqmat.

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 cross-products or sums of squares and cross-products of deviations from the (weighted) mean for a sample of n observations on m variables Xj, for j=1,2,,m. The algorithm makes a single pass through the data.
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) + wi Wi (xj-x¯j(i-1)) (xk-x¯k(i-1)) Wi-1 ,   j=1,2,,m ​ and ​ k=j,j+ 1,,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.
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, 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 g02buf 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: weight Character(1) Input
On entry: indicates whether the data is weighted or not.
weight='U'
The calculations are performed on unweighted data.
weight='W'
The calculations are performed on weighted data.
Constraint: weight='W' or 'U'.
3: n Integer Input
On entry: n, the number of observations in the dataset.
Constraint: n1.
4: m Integer Input
On entry: m, the number of variables.
Constraint: m1.
5: x(ldx,m) Real (Kind=nag_wp) array Input
On entry: x(i,j) must contain the ith observation on the jth variable, for i=1,2,,n and j=1,2,,m.
6: ldx Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g02buf is called.
Constraint: ldxn.
7: wt(*) Real (Kind=nag_wp) array Input
Note: the dimension of the array wt must be at least n if weight='W'.
On entry: the optional weights of each observation.
If weight='U', wt is not referenced.
If weight='W', wt(i) must contain the weight for the ith observation.
Constraint: if weight='W', wt(i)0.0, for i=1,2,,n.
8: sw Real (Kind=nag_wp) Output
On exit: the sum of weights.
If weight='U', sw contains the number of observations, n.
9: wmean(m) Real (Kind=nag_wp) array Output
On exit: the sample means. wmean(j) contains the mean for the jth variable.
10: c((m×m+m)/2) Real (Kind=nag_wp) array Output
On exit: the cross-products.
If mean='M', c contains the upper triangular part of the matrix of (weighted) sums of squares and cross-products of deviations about the mean.
If mean='Z', c contains the upper triangular part of the matrix of (weighted) sums of squares and cross-products.
These are stored packed by columns, i.e., the cross-product between the jth and kth variable, kj, is stored in c(k×(k-1)/2+j).
11: 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, ldx=value and n=value.
Constraint: ldxn.
On entry, m=value.
Constraint: m1.
On entry, n=value.
Constraint: n1.
ifail=2
On entry, mean=value.
Constraint: mean='M' or 'Z'.
ifail=3
On entry, weight=value.
Constraint: weight='W' or 'U'.
ifail=4
On entry, wt(value)<0.0.
Constraint: wt(i)0.0, for i=1,2,,n.
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 algorithm see Chan et al. (1982) or West (1979).

8 Parallelism and Performance

g02buf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

g02bwf may be used to calculate the correlation coefficients from the cross-products of deviations about the mean. The cross-products of deviations about the mean may be scaled using f06edf or f06fdf to give a variance-covariance matrix.
The means and cross-products produced by g02buf may be updated by adding or removing observations using g02btf.
Two sets of means and cross-products, as produced by g02buf, can be combined using g02bzf.

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 (g02bufe.f90)

10.2 Program Data

Program Data (g02bufe.d)

10.3 Program Results

Program Results (g02bufe.r)