﻿ g02bx Method
g02bx calculates the sample means, the standard deviations, the variance-covariance matrix, and the matrix of Pearson product-moment correlation coefficients for a set of data. Weights may be used.

# Syntax

C#
```public static void g02bx(
string weight,
int n,
int m,
double[,] x,
double[] wt,
double[] xbar,
double[] std,
double[,] v,
double[,] r,
out int ifail
)```
Visual Basic
```Public Shared Sub g02bx ( _
weight As String, _
n As Integer, _
m As Integer, _
x As Double(,), _
wt As Double(), _
xbar As Double(), _
std As Double(), _
v As Double(,), _
r As Double(,), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g02bx(
String^ weight,
int n,
int m,
array<double,2>^ x,
array<double>^ wt,
array<double>^ xbar,
array<double>^ std,
array<double,2>^ v,
array<double,2>^ r,
[OutAttribute] int% ifail
)```
F#
```static member g02bx :
weight : string *
n : int *
m : int *
x : float[,] *
wt : float[] *
xbar : float[] *
std : float[] *
v : float[,] *
r : float[,] *
ifail : int byref -> unit
```

#### Parameters

weight
Type: System..::..String
On entry: indicates whether weights are to be used.
${\mathbf{weight}}=\text{"U"}$
Weights are not used and unit weights are assumed.
${\mathbf{weight}}=\text{"W"}$ or $\text{"V"}$
Weights are used and must be supplied in wt. The only difference between ${\mathbf{weight}}=\text{"W"}$ or ${\mathbf{weight}}=\text{"V"}$ is in computing the variance. If ${\mathbf{weight}}=\text{"W"}$ the divisor for the variance is the sum of the weights minus one and if ${\mathbf{weight}}=\text{"V"}$ the divisor is the number of observations with nonzero weights minus one. The former is useful if the weights represent the frequency of the observed values.
Constraint: ${\mathbf{weight}}=\text{"U"}$, $\text{"V"}$ or $\text{"W"}$.
n
Type: System..::..Int32
On entry: the number of data observations in the sample.
Constraint: ${\mathbf{n}}>1$.
m
Type: System..::..Int32
On entry: the number of variables.
Constraint: ${\mathbf{m}}\ge 1$.
x
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: ${\mathbf{x}}\left[\mathit{i}-1,\mathit{j}-1\right]$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th variable, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
wt
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
Note: the dimension of the array wt must be at least ${\mathbf{n}}$ if ${\mathbf{weight}}=\text{"W"}$ or $\text{"V"}$, and at least $1$ otherwise.
On entry: $w$, the optional frequency weighting for each observation, with ${\mathbf{wt}}\left[i-1\right]={w}_{i}$. Usually ${w}_{i}$ will be an integral value corresponding to the number of observations associated with the $i$th data value, or zero if the $i$th data value is to be ignored. If ${\mathbf{weight}}=\text{"U"}$, ${w}_{i}$ is set to $1$ for all $i$ and wt is not referenced.
Constraint: if ${\mathbf{weight}}=\text{"W"}$ or $\text{"V"}$, $\sum _{\mathit{i}=0}^{{\mathbf{n}}-1}{\mathbf{wt}}\left[\mathit{i}\right]>1.0$, ${\mathbf{wt}}\left[\mathit{i}\right]\ge 0.0$, for $\mathit{i}=0,1,\dots ,{\mathbf{n}}-1$.
xbar
Type: array<System..::..Double>[]()[][]
An array of size [m]
On exit: the sample means. ${\mathbf{xbar}}\left[j-1\right]$ contains the mean of the $j$th variable.
std
Type: array<System..::..Double>[]()[][]
An array of size [m]
On exit: the standard deviations. ${\mathbf{std}}\left[j-1\right]$ contains the standard deviation for the $j$th variable.
v
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{m}}$
On exit: the variance-covariance matrix. ${\mathbf{v}}\left[\mathit{j}-1,\mathit{k}-1\right]$ contains the covariance between variables $\mathit{j}$ and $\mathit{k}$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$ and $\mathit{k}=1,2,\dots ,{\mathbf{m}}$.
r
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{m}}$
On exit: the matrix of Pearson product-moment correlation coefficients. ${\mathbf{r}}\left[j-1,k-1\right]$ contains the correlation coefficient between variables $j$ and $k$.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

For $n$ observations on $m$ variables the one-pass algorithm of West (1979) as implemented in g02bu is used to compute the means, the standard deviations, the variance-covariance matrix, and the Pearson product-moment correlation matrix for $p$ selected variables. Suitables weights may be used to indicate multiple observations and to remove missing values. The quantities are defined by:
(a) The means
 $x-j=∑i=1nwixij∑i=1nwi j=1,…,p$
(b) The variance-covariance matrix
 $Cjk=∑i=1nwixij-x-jxik-x-k∑i=1nwi-1 j,k=1,…,p$
(c) The standard deviations
 $sj=Cjj j=1,…,p$
(d) The Pearson product-moment correlation coefficients
 $Rjk=CjkCjjCkk j,k=1,…,p$
where ${x}_{ij}$ is the value of the $i$th observation on the $j$th variable and ${w}_{i}$ is the weight for the $i$th observation which will be 1 in the unweighted case.
Note that the denominator for the variance-covariance is ${\sum }_{i=1}^{n}{w}_{i}-1$, so the weights should be scaled so that the sum of weights reflects the true sample size.

# 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

# Error Indicators and Warnings

Note: g02bx may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDX, LDV) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{m}}<1$, or ${\mathbf{n}}\le 1$,
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{weight}}\ne \text{"U"}$, $\text{"V"}$ or $\text{"W"}$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{weight}}=\text{"W"}$ or $\text{"V"}$ and a value of ${\mathbf{wt}}<0.0$.
${\mathbf{ifail}}=4$
${\mathbf{weight}}=\text{"W"}$ and the sum of weights is not greater than $1.0$, or ${\mathbf{weight}}=\text{"V"}$ and fewer than $2$ observations have nonzero weights.
${\mathbf{ifail}}=5$
A variable has a zero variance. In this case v and std are returned as calculated but r will contain zero for any correlation involving a variable with zero variance.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

For a discussion of the accuracy of the one pass algorithm see Chan et al. (1982) and West (1979).

None.

None.

# Example

The data are some of the results from 1988 Olympic Decathlon. They are the times (in seconds) for the 100m and 400m races and the distances (in metres) for the long jump, high jump and shot. Twenty observations are input and the correlation matrix is computed and printed.

Example program (C#): g02bxe.cs

Example program data: g02bxe.d

Example program results: g02bxe.r