G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG03DBF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G03DBF computes Mahalanobis squared distances for group or pooled variance-covariance matrices. It is intended for use after G03DAF.

## 2  Specification

 SUBROUTINE G03DBF ( EQUAL, MODE, NVAR, NG, GMN, LDGMN, GC, NOBS, M, ISX, X, LDX, D, LDD, WK, IFAIL)
 INTEGER NVAR, NG, LDGMN, NOBS, M, ISX(*), LDX, LDD, IFAIL REAL (KIND=nag_wp) GMN(LDGMN,NVAR), GC((NG+1)*NVAR*(NVAR+1)/2), X(LDX,*), D(LDD,NG), WK(2*NVAR) CHARACTER(1) EQUAL, MODE

## 3  Description

Consider $p$ variables observed on ${n}_{g}$ populations or groups. Let ${\stackrel{-}{x}}_{j}$ be the sample mean and ${S}_{j}$ the within-group variance-covariance matrix for the $j$th group and let ${x}_{k}$ be the $k$th sample point in a dataset. A measure of the distance of the point from the $j$th population or group is given by the Mahalanobis distance, ${D}_{kj}$:
 $Dkj2=xk-x-jTSj-1xk-x-j.$
If the pooled estimated of the variance-covariance matrix $S$ is used rather than the within-group variance-covariance matrices, then the distance is:
 $Dkj2=xk-x-jTS-1xk-x-j.$
Instead of using the variance-covariance matrices $S$ and ${S}_{j}$, G03DBF uses the upper triangular matrices $R$ and ${R}_{j}$ supplied by G03DAF such that $S={R}^{\mathrm{T}}R$ and ${S}_{j}={R}_{j}^{\mathrm{T}}{R}_{j}$. ${D}_{kj}^{2}$ can then be calculated as ${z}^{\mathrm{T}}z$ where ${R}_{j}z=\left({x}_{k}-{\stackrel{-}{x}}_{j}\right)$ or $Rz=\left({x}_{k}-{\stackrel{-}{x}}_{j}\right)$ as appropriate.
A particular case is when the distance between the group or population means is to be estimated. The Mahalanobis squared distance between the $i$th and $j$th groups is:
 $Dij2=x-i-x-jTSj-1x-i-x-j$
or
 $Dij2=x-i-x-jTS-1x-i-x-j.$
Note:  ${D}_{jj}^{2}=0$ and that in the case when the pooled variance-covariance matrix is used ${D}_{ij}^{2}={D}_{ji}^{2}$ so in this case only the lower triangular values of ${D}_{ij}^{2}$, $i>j$, are computed.

## 4  References

Aitchison J and Dunsmore I R (1975) Statistical Prediction Analysis Cambridge
Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press

## 5  Parameters

1:     EQUAL – CHARACTER(1)Input
On entry: indicates whether or not the within-group variance-covariance matrices are assumed to be equal and the pooled variance-covariance matrix used.
${\mathbf{EQUAL}}=\text{'E'}$
The within-group variance-covariance matrices are assumed equal and the matrix $R$ stored in the first $p\left(p+1\right)/2$ elements of GC is used.
${\mathbf{EQUAL}}=\text{'U'}$
The within-group variance-covariance matrices are assumed to be unequal and the matrices ${R}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{n}_{g}$, stored in the remainder of GC are used.
Constraint: ${\mathbf{EQUAL}}=\text{'E'}$ or $\text{'U'}$.
2:     MODE – CHARACTER(1)Input
On entry: indicates whether distances from sample points are to be calculated or distances between the group means.
${\mathbf{MODE}}=\text{'S'}$
The distances between the sample points given in X and the group means are calculated.
${\mathbf{MODE}}=\text{'M'}$
The distances between the group means will be calculated.
Constraint: ${\mathbf{MODE}}=\text{'M'}$ or $\text{'S'}$.
3:     NVAR – INTEGERInput
On entry: $p$, the number of variables in the variance-covariance matrices as specified to G03DAF.
Constraint: ${\mathbf{NVAR}}\ge 1$.
4:     NG – INTEGERInput
On entry: the number of groups, ${n}_{g}$.
Constraint: ${\mathbf{NG}}\ge 2$.
5:     GMN(LDGMN,NVAR) – REAL (KIND=nag_wp) arrayInput
On entry: the $\mathit{j}$th row of GMN contains the means of the $p$ selected variables for the $\mathit{j}$th group, for $\mathit{j}=1,2,\dots ,{n}_{g}$. These are returned by G03DAF.
6:     LDGMN – INTEGERInput
On entry: the first dimension of the array GMN as declared in the (sub)program from which G03DBF is called.
Constraint: ${\mathbf{LDGMN}}\ge {\mathbf{NG}}$.
7:     GC($\left({\mathbf{NG}}+1\right)×{\mathbf{NVAR}}×\left({\mathbf{NVAR}}+1\right)/2$) – REAL (KIND=nag_wp) arrayInput
On entry: the first $p\left(p+1\right)/2$ elements of GC should contain the upper triangular matrix $R$ and the next ${n}_{g}$ blocks of $p\left(p+1\right)/2$ elements should contain the upper triangular matrices ${R}_{j}$. All matrices must be stored packed by column. These matrices are returned by G03DAF. If ${\mathbf{EQUAL}}=\text{'E'}$ only the first $p\left(p+1\right)/2$ elements are referenced, if ${\mathbf{EQUAL}}=\text{'U'}$ only the elements $p\left(p+1\right)/2+1$ to $\left({n}_{g}+1\right)p\left(p+1\right)/2$ are referenced.
Constraints:
• if ${\mathbf{EQUAL}}=\text{'E'}$, $R\ne 0.0$;
• if ${\mathbf{EQUAL}}=\text{'U'}$, the diagonal elements of the ${R}_{\mathit{j}}\ne 0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{NG}}$.
8:     NOBS – INTEGERInput
On entry: if ${\mathbf{MODE}}=\text{'S'}$, the number of sample points in X for which distances are to be calculated.
If ${\mathbf{MODE}}=\text{'M'}$, NOBS is not referenced.
Constraint: if ${\mathbf{NOBS}}\ge 1$, ${\mathbf{MODE}}=\text{'S'}$.
9:     M – INTEGERInput
On entry: if ${\mathbf{MODE}}=\text{'S'}$, the number of variables in the data array X.
If ${\mathbf{MODE}}=\text{'M'}$, M is not referenced.
Constraint: if ${\mathbf{M}}\ge {\mathbf{NVAR}}$, ${\mathbf{MODE}}=\text{'S'}$.
10:   ISX($*$) – INTEGER arrayInput
Note: the dimension of the array ISX must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{M}}\right)$.
On entry: if ${\mathbf{MODE}}=\text{'S'}$, ${\mathbf{ISX}}\left(\mathit{l}\right)$ indicates if the $\mathit{l}$th variable in X is to be included in the distance calculations. If ${\mathbf{ISX}}\left(\mathit{l}\right)>0$ the $\mathit{l}$th variable is included, for $\mathit{l}=1,2,\dots ,{\mathbf{M}}$; otherwise the $\mathit{l}$th variable is not referenced.
If ${\mathbf{MODE}}=\text{'M'}$, ISX is not referenced.
Constraint: if ${\mathbf{MODE}}=\text{'S'}$, ${\mathbf{ISX}}\left(l\right)>0$ for NVAR values of $l$.
11:   X(LDX,$*$) – REAL (KIND=nag_wp) arrayInput
Note: the second dimension of the array X must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{M}}\right)$.
On entry: if ${\mathbf{MODE}}=\text{'S'}$ the $\mathit{k}$th row of X must contain ${x}_{\mathit{k}}$. That is ${\mathbf{X}}\left(\mathit{k},\mathit{l}\right)$ must contain the $\mathit{k}$th sample value for the $\mathit{l}$th variable, for $\mathit{k}=1,2,\dots ,{\mathbf{NOBS}}$ and $\mathit{l}=1,2,\dots ,{\mathbf{M}}$. Otherwise X is not referenced.
12:   LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03DBF is called.
Constraints:
• if ${\mathbf{MODE}}=\text{'S'}$, ${\mathbf{LDX}}\ge {\mathbf{NOBS}}$;
• otherwise $1$.
13:   D(LDD,NG) – REAL (KIND=nag_wp) arrayOutput
On exit: the squared distances.
If ${\mathbf{MODE}}=\text{'S'}$, ${\mathbf{D}}\left(\mathit{k},\mathit{j}\right)$ contains the squared distance of the $\mathit{k}$th sample point from the $\mathit{j}$th group mean, ${D}_{\mathit{k}\mathit{j}}^{2}$, for $\mathit{k}=1,2,\dots ,{\mathbf{NOBS}}$ and $\mathit{j}=1,2,\dots ,{n}_{g}$.
If ${\mathbf{MODE}}=\text{'M'}$ and ${\mathbf{EQUAL}}=\text{'U'}$, ${\mathbf{D}}\left(\mathit{i},\mathit{j}\right)$ contains the squared distance between the $\mathit{i}$th mean and the $\mathit{j}$th mean, ${D}_{\mathit{i}\mathit{j}}^{2}$, for $\mathit{i}=1,2,\dots ,{n}_{g}$ and $\mathit{j}=1,2,\dots ,\mathit{i}-1,\mathit{i}+1,\dots ,{n}_{g}$. The elements ${\mathbf{D}}\left(\mathit{i},\mathit{i}\right)$ are not referenced, for $\mathit{i}=1,2,\dots ,{n}_{g}$.
If ${\mathbf{MODE}}=\text{'M'}$ and ${\mathbf{EQUAL}}=\text{'E'}$, ${\mathbf{D}}\left(\mathit{i},\mathit{j}\right)$ contains the squared distance between the $\mathit{i}$th mean and the $\mathit{j}$th mean, ${D}_{\mathit{i}\mathit{j}}^{2}$, for $\mathit{i}=1,2,\dots ,{n}_{g}$ and $\mathit{j}=1,2,\dots ,\mathit{i}-1$. Since ${D}_{\mathit{i}\mathit{j}}={D}_{\mathit{j}\mathit{i}}$ the elements ${\mathbf{D}}\left(\mathit{i},\mathit{j}\right)$ are not referenced, for $\mathit{i}=1,2,\dots ,{n}_{g}$ and $\mathit{j}=\mathit{i}+1,\dots ,{n}_{g}$.
14:   LDD – INTEGERInput
On entry: the first dimension of the array D as declared in the (sub)program from which G03DBF is called.
Constraints:
• if ${\mathbf{MODE}}=\text{'S'}$, ${\mathbf{LDD}}\ge {\mathbf{NOBS}}$;
• if ${\mathbf{MODE}}=\text{'M'}$, ${\mathbf{LDD}}\ge {\mathbf{NG}}$.
15:   WK($2×{\mathbf{NVAR}}$) – REAL (KIND=nag_wp) arrayWorkspace
16:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. 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 $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{NVAR}}<1$, or ${\mathbf{NG}}<2$, or ${\mathbf{LDGMN}}<{\mathbf{NG}}$, or ${\mathbf{MODE}}=\text{'S'}$ and ${\mathbf{NOBS}}<1$, or ${\mathbf{MODE}}=\text{'S'}$ and ${\mathbf{M}}<{\mathbf{NVAR}}$, or ${\mathbf{MODE}}=\text{'S'}$ and ${\mathbf{LDX}}<{\mathbf{NOBS}}$, or ${\mathbf{MODE}}=\text{'S'}$ and ${\mathbf{LDD}}<{\mathbf{NOBS}}$, or ${\mathbf{MODE}}=\text{'M'}$ and ${\mathbf{LDD}}<{\mathbf{NG}}$, or ${\mathbf{EQUAL}}\ne \text{'E'}$ or ‘U’, or ${\mathbf{MODE}}\ne \text{'M'}$ or ‘S’.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{MODE}}=\text{'S'}$ and the number of variables indicated by ISX is not equal to NVAR, or ${\mathbf{EQUAL}}=\text{'E'}$ and a diagonal element of $R$ is zero, or ${\mathbf{EQUAL}}=\text{'U'}$ and a diagonal element of ${R}_{j}$ for some $j$ is zero.

## 7  Accuracy

The accuracy will depend upon the accuracy of the input $R$ or ${R}_{j}$ matrices.

If the distances are to be used for discrimination, see also G03DCF.

## 9  Example

The data, taken from Aitchison and Dunsmore (1975), is concerned with the diagnosis of three ‘types’ of Cushing's syndrome. The variables are the logarithms of the urinary excretion rates (mg/24hr) of two steroid metabolites. Observations for a total of $21$ patients are input and the group means and $R$ matrices are computed by G03DAF. A further six observations of unknown type are input, and the distances from the group means of the $21$ patients of known type are computed under the assumption that the within-group variance-covariance matrices are not equal. These results are printed and indicate that the first four are close to one of the groups while observations $5$ and $6$ are some distance from any group.

### 9.1  Program Text

Program Text (g03dbfe.f90)

### 9.2  Program Data

Program Data (g03dbfe.d)

### 9.3  Program Results

Program Results (g03dbfe.r)