g02 Chapter Contents
g02 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_sum_sqs_update (g02btc)

## 1  Purpose

nag_sum_sqs_update (g02btc) 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

 #include #include
 void nag_sum_sqs_update (Nag_SumSquare mean, Integer m, double wt, const double x[], Integer incx, double *sw, double xbar[], double c[], NagError *fail)

## 3  Description

nag_sum_sqs_update (g02btc) 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 $m$ variables ${X}_{j}$, for $j=1,2,\dots ,m$. For the first $i-1$ observations let the mean of the $j$th variable be ${\stackrel{-}{x}}_{j}\left(i-1\right)$, the cross-product about the mean for the $j$th and $k$th variables be ${c}_{jk}\left(i-1\right)$ and the sum of weights be ${W}_{i-1}$. These are updated by the $i$th observation, ${x}_{ij}$, for $\mathit{j}=1,2,\dots ,m$, with weight ${w}_{i}$ 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 ${\stackrel{-}{x}}_{j}\left(1\right)={x}_{1j}$, the first observation and ${c}_{ij}\left(1\right)=0.0$.
For the unweighted case ${w}_{i}=1$ and ${W}_{i}=i$ for all $i$.
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:    $\mathbf{mean}$Nag_SumSquareInput
On entry: indicates whether nag_sum_sqs_update (g02btc) is to calculate sums of squares and cross-products, or sums of squares and cross-products of deviations about the mean.
${\mathbf{mean}}=\mathrm{Nag_AboutMean}$
The sums of squares and cross-products of deviations about the mean are calculated.
${\mathbf{mean}}=\mathrm{Nag_AboutZero}$
The sums of squares and cross-products are calculated.
Constraint: ${\mathbf{mean}}=\mathrm{Nag_AboutMean}$ or $\mathrm{Nag_AboutZero}$.
2:    $\mathbf{m}$IntegerInput
On entry: $m$, the number of variables.
Constraint: ${\mathbf{m}}\ge 1$.
3:    $\mathbf{wt}$doubleInput
On entry: the weight to use for the current observation, ${w}_{i}$.
For unweighted means and cross-products set ${\mathbf{wt}}=1.0$. The use of a suitable negative value of wt, e.g., $-{w}_{i}$ will have the effect of deleting the observation.
4:    $\mathbf{x}\left[{\mathbf{m}}×{\mathbf{incx}}\right]$const doubleInput
On entry: ${\mathbf{x}}\left[\left(j-1\right)×{\mathbf{incx}}\right]$ must contain the value of the $j$th variable for the current observation, $j=1,2,\dots ,m$.
5:    $\mathbf{incx}$IntegerInput
On entry: the increment of x.
Constraint: ${\mathbf{incx}}>0$.
6:    $\mathbf{sw}$double *Input/Output
On entry: the sum of weights for the previous observations, ${W}_{i-1}$.
${\mathbf{sw}}=0.0$
The update procedure is initialized.
${\mathbf{sw}}+{\mathbf{wt}}=0.0$
All elements of xbar and c are set to zero.
Constraint: ${\mathbf{sw}}\ge 0.0$ and ${\mathbf{sw}}+{\mathbf{wt}}\ge 0.0$.
On exit: contains the updated sum of weights, ${W}_{i}$.
7:    $\mathbf{xbar}\left[{\mathbf{m}}\right]$doubleInput/Output
On entry: if ${\mathbf{sw}}=0.0$, xbar is initialized, otherwise ${\mathbf{xbar}}\left[\mathit{j}-1\right]$ must contain the weighted mean of the $\mathit{j}$th variable for the previous $\left(\mathit{i}-1\right)$ observations, ${\stackrel{-}{x}}_{\mathit{j}}\left(\mathit{i}-1\right)$, for $\mathit{j}=1,2,\dots ,m$.
On exit: ${\mathbf{xbar}}\left[\mathit{j}-1\right]$ contains the weighted mean of the $\mathit{j}$th variable, ${\stackrel{-}{x}}_{\mathit{j}}\left(\mathit{i}\right)$, for $\mathit{j}=1,2,\dots ,m$.
8:    $\mathbf{c}\left[\left({\mathbf{m}}×{\mathbf{m}}+{\mathbf{m}}\right)/2\right]$doubleInput/Output
On entry: if ${\mathbf{sw}}\ne 0.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 $j$th and $k$th variable, $k\ge j$, is stored in ${\mathbf{c}}\left[k×\left(k-1\right)/2+j-1\right]$.
On exit: the update sums of squares and cross-products stored as on input.
9:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 2.7 in How to Use the NAG Library and its Documentation).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{incx}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{incx}}\ge 1$.
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
NE_REAL
On entry, ${\mathbf{sw}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{sw}}\ge 0.0$.
NE_SUM_WEIGHT
On entry, $\left({\mathbf{sw}}+{\mathbf{wt}}\right)=〈\mathit{\text{value}}〉$.
Constraint: $\left({\mathbf{sw}}+{\mathbf{wt}}\right)\ge 0.0$.

## 7  Accuracy

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

## 8  Parallelism and Performance

nag_sum_sqs_update (g02btc) is not threaded in any implementation.

nag_sum_sqs_update (g02btc) may be used to update the results returned by nag_sum_sqs (g02buc).
nag_cov_to_corr (g02bwc) may be used to calculate the correlation matrix from the matrix of sums of squares and cross-products of deviations about the mean .

## 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 (g02btce.c)

### 10.2  Program Data

Program Data (g02btce.d)

### 10.3  Program Results

Program Results (g02btce.r)