NAG Library Routine Document
g02buf
(ssqmat)
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 nagmk26.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) |
|
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
observations on
variables
, for
. 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 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 .
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: – 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.
- 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: – Character(1)Input
-
On entry: indicates whether the data is weighted or not.
- The calculations are performed on unweighted data.
- The calculations are performed on weighted data.
Constraint:
or .
- 3: – IntegerInput
-
On entry: , the number of observations in the dataset.
Constraint:
.
- 4: – IntegerInput
-
On entry: , the number of variables.
Constraint:
.
- 5: – Real (Kind=nag_wp) arrayInput
-
On entry: must contain the th observation on the th variable, for and .
- 6: – IntegerInput
-
On entry: the first dimension of the array
x as declared in the (sub)program from which
g02buf is called.
Constraint:
.
- 7: – Real (Kind=nag_wp) arrayInput
-
Note: the dimension of the array
wt
must be at least
if
, and at least
otherwise.
On entry: the optional weights of each observation.
If
,
wt is not referenced.
If , must contain the weight for the th observation.
Constraint:
if , , for .
- 8: – Real (Kind=nag_wp)Output
-
On exit: the sum of weights.
If
,
sw contains the number of observations,
.
- 9: – Real (Kind=nag_wp) arrayOutput
-
On exit: the sample means. contains the mean for the th variable.
- 10: – Real (Kind=nag_wp) arrayOutput
-
On exit: the cross-products.
If
,
c contains the upper triangular part of the matrix of (weighted) sums of squares and cross-products of deviations about the mean.
If
,
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 th and th variable, , is stored in .
- 11: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation 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 argument, the recommended value is
.
When the value is used it is essential to test the value of ifail on exit.
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
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
-
On entry, and .
Constraint: .
On entry, .
Constraint: .
On entry, .
Constraint: .
-
On entry, .
Constraint: or .
-
On entry, .
Constraint: or .
-
On entry, .
Constraint: , for .
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation 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 is not threaded in any implementation.
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 (dscal) 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 observations of 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)