NAG Library Routine Document
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.
|SUBROUTINE G02BUF (
||MEAN, WEIGHT, N, M, X, LDX, WT, SW, WMEAN, C, IFAIL)
||N, M, LDX, IFAIL
||X(LDX,M), WT(*), SW, WMEAN(M), C((M*M+M)/2)
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
. The algorithm makes a single pass through the data.
For the first
observations let the mean of the
th variable be
, the cross-product about the mean for the
th variables be
and the sum of weights be
. These are updated by the
, with weight
The algorithm is initialized by taking , the first observation, and .
For the unweighted case and for all .
Note that only the upper triangle of the matrix is calculated and returned packed by column.
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
- 1: MEAN – CHARACTER(1)Input
: indicates whether G02BUF is to calculate sums of squares and cross-products, or sums of squares and cross-products of deviations about the mean.
- The sums of squares and cross-products of deviations about the mean are calculated.
- The sums of squares and cross-products are calculated.
- 2: WEIGHT – CHARACTER(1)Input
: indicates whether the data is weighted or not.
- The calculations are performed on unweighted data.
- The calculations are performed on weighted data.
- 3: N – INTEGERInput
On entry: , the number of observations in the dataset.
- 4: M – INTEGERInput
On entry: , the number of variables.
- 5: X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: must contain the th observation on the th variable, for and .
- 6: LDX – INTEGERInput
: the first dimension of the array X
as declared in the (sub)program from which G02BUF is called.
- 7: WT() – REAL (KIND=nag_wp) arrayInput
the dimension of the array WT
must be at least
, and at least
: the optional weights of each observation.
is not referenced.
If , must contain the weight for the th observation.
if , , for .
- 8: SW – REAL (KIND=nag_wp)Output
: the sum of weights.
contains the number of observations,
- 9: WMEAN(M) – REAL (KIND=nag_wp) arrayOutput
On exit: the sample means. contains the mean for the th variable.
- 10: C() – REAL (KIND=nag_wp) arrayOutput
: the cross-products.
contains the upper triangular part of the matrix of (weighted) sums of squares and cross-products of deviations about the mean.
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 th and th variable, , is stored in .
- 11: IFAIL – INTEGERInput/Output
must be set to
. 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
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
. When the value is used it is essential to test the value of IFAIL on exit.
unless the routine detects an error or a warning has been flagged (see Section 6
6 Error Indicators and Warnings
If on entry
, explanatory error messages are output on the current error message unit (as defined by X04AAF
Errors or warnings detected by the routine:
|On entry,|| or .|
|On entry,|| or .|
|On entry,|| , and a value of .|
For a detailed discussion of the accuracy of this algorithm see Chan et al. (1982)
or West (1979)
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 (DSCAL)
to give a variance-covariance matrix.
The means and cross-products produced by G02BUF may be updated by adding or removing observations using G02BTF
A program to calculate the means, the required sums of squares and cross-products matrix, and the variance matrix for a set of observations of variables.
9.1 Program Text
Program Text (g02bufe.f90)
9.2 Program Data
Program Data (g02bufe.d)
9.3 Program Results
Program Results (g02bufe.r)