PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_correg_ssqmat_update (g02bt)
Purpose
nag_correg_ssqmat_update (g02bt) 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.
Syntax
[
sw,
xbar,
c,
ifail] = g02bt(
wt,
x,
sw,
xbar,
c, 'mean_p',
mean_p, 'm',
m, 'incx',
incx)
[
sw,
xbar,
c,
ifail] = nag_correg_ssqmat_update(
wt,
x,
sw,
xbar,
c, 'mean_p',
mean_p, 'm',
m, 'incx',
incx)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 24: |
mean_p was made optional |
At Mark 23: |
incx was made optional (default 1) |
Description
nag_correg_ssqmat_update (g02bt) is an adaptation of West's WV2 algorithm; see
West (1979). This function 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
variables
, for
. For the first
observations let the mean of the
th variable be
, the cross-product about the mean for the
th and
th variables be
and the sum of weights be
. These are updated by the
th observation,
, for
, with weight
as follows:
and
The algorithm is initialized by taking
, the first observation and
.
For the unweighted case and for all .
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
Parameters
Compulsory Input Parameters
- 1:
– double scalar
-
The weight to use for the current observation,
.
For unweighted means and cross-products set
. The use of a suitable negative value of
wt, e.g.,
will have the effect of deleting the observation.
- 2:
– double array
-
must contain the value of the th variable for the current observation, .
- 3:
– double scalar
-
The sum of weights for the previous observations,
.
- The update procedure is initialized.
- All elements of xbar and c are set to zero.
Constraint:
and .
- 4:
– double array
-
If
,
xbar is initialized, otherwise
must contain the weighted mean of the
th variable for the previous
observations,
, for
.
- 5:
– double array
-
If
,
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
th and
th variable,
, is stored in
.
Optional Input Parameters
- 1:
– string (length ≥ 1)
Default:
Indicates whether
nag_correg_ssqmat_update (g02bt) 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.
Constraint:
or .
- 2:
– int64int32nag_int scalar
-
Default:
the dimension of the array
xbar.
, the number of variables.
Constraint:
.
- 3:
– int64int32nag_int scalar
Default:
The increment of
x. Two situations are common.
If
, the data values are to be found in consecutive locations in
x, i.e., in a column.
If , for some positive integer , the data values are to be found as a row of an array with first dimension .
Constraint:
.
Output Parameters
- 1:
– double scalar
-
Contains the updated sum of weights, .
- 2:
– double array
-
contains the weighted mean of the th variable, , for .
- 3:
– double array
-
The update sums of squares and cross-products stored as on input.
- 4:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
-
-
-
-
On entry, | , the current weight causes the sum of weights to be less than . |
-
-
On entry, | or . |
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
For a detailed discussion of the accuracy of this method see
Chan et al. (1982) and
West (1979).
Further Comments
nag_correg_ssqmat_update (g02bt) may be used to update the results returned by
nag_correg_ssqmat (g02bu).
nag_correg_ssqmat_to_corrmat (g02bw) 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
to produce a variance-covariance matrix.
Example
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.
Open in the MATLAB editor:
g02bt_example
function g02bt_example
fprintf('g02bt example results\n\n');
wt = [0.1300 1.3070 0.3700];
x = [9.1231 0.9310 0.0009;
3.7011 0.0900 0.0099;
4.5230 0.8870 0.0999];
[m,n] = size(x);
cn = (m*(m+1))/2;
m = int64(m);
sw = 0;
xbar = zeros(n,1);
c = zeros(cn,1);
for j = 1:n
[sw, xbar, c, ifail] = g02bt( ...
wt(j), x(:,j), sw, xbar, c);
end
disp('Means');
disp(xbar');
mtitle = 'Sums of squares and cross-products:';
uplo = 'Upper';
diag = 'Non-unit';
[ifail] = x04cc( ...
uplo, diag, m, c, mtitle);
v = c/(sw-1);
fprintf('\n');
mtitle = 'Variance matrix:';
[ifail] = x04cc( ...
uplo, diag, m, v, mtitle);
g02bt example results
Means
1.3299 0.3334 0.9874
Sums of squares and cross-products:
1 2 3
1 8.7569 3.6978 4.0707
2 1.5905 1.6861
3 1.9297
Variance matrix:
1 2 3
1 10.8512 4.5822 5.0443
2 1.9709 2.0893
3 2.3912
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015