G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG03EAF

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

G03EAF computes a distance (dissimilarity) matrix.

## 2  Specification

 SUBROUTINE G03EAF ( UPDATE, DIST, SCAL, N, M, X, LDX, ISX, S, D, IFAIL)
 INTEGER N, M, LDX, ISX(M), IFAIL REAL (KIND=nag_wp) X(LDX,M), S(M), D(N*(N-1)/2) CHARACTER(1) UPDATE, DIST, SCAL

## 3  Description

Given $n$ objects, a distance or dissimilarity matrix is a symmetric matrix with zero diagonal elements such that the $ij$th element represents how far apart or how dissimilar the $i$th and $j$th objects are.
Let $X$ be an $n$ by $p$ data matrix of observations of $p$ variables on $n$ objects, then the distance between object $j$ and object $k$, ${d}_{jk}$, can be defined as:
 $djk= ∑i=1pDxji/si,xki/si α ,$
where ${x}_{ji}$ and ${x}_{ki}$ are the $ji$th and $ki$th elements of $X$, ${s}_{i}$ is a standardization for the $i$th variable and $D\left(u,v\right)$ is a suitable function. Three functions are provided in G03EAF.
 (a) Euclidean distance: $D\left(u,v\right)={\left(u-v\right)}^{2}$ and $\alpha =\frac{1}{2}$. (b) Euclidean squared distance: $D\left(u,v\right)={\left(u-v\right)}^{2}$ and $\alpha =1$. (c) Absolute distance (city block metric): $D\left(u,v\right)=\left|u-v\right|$ and $\alpha =1$.
Three standardizations are available.
 (a) Standard deviation: ${s}_{i}=\sqrt{\sum _{j=1}^{n}{\left({x}_{ji}-\stackrel{-}{x}\right)}^{2}/\left(n-1\right)}$ (b) Range: ${s}_{i}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({x}_{1i},{x}_{2i},\dots ,{x}_{ni}\right)-\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({x}_{1i},{x}_{2i},\dots ,{x}_{ni}\right)$ (c) User-supplied values of ${s}_{i}$.
In addition to the above distances there are a large number of other dissimilarity measures, particularly for dichotomous variables (see Krzanowski (1990) and Everitt (1974)). For the dichotomous case these measures are simple to compute and can, if suitable scaling is used, be combined with the distances computed by G03EAF using the updating option.
Dissimilarity measures for variables can be based on the correlation coefficient for continuous variables and contingency table statistics for dichotomous data, see chapters G02 and G11 respectively.
G03EAF returns the strictly lower triangle of the distance matrix.

## 4  References

Everitt B S (1974) Cluster Analysis Heinemann
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press

## 5  Parameters

1:     UPDATE – CHARACTER(1)Input
On entry: indicates whether or not an existing matrix is to be updated.
${\mathbf{UPDATE}}=\text{'U'}$
The matrix $D$ is updated and distances are added to $D$.
${\mathbf{UPDATE}}=\text{'I'}$
The matrix $D$ is initialized to zero before the distances are added to $D$.
Constraint: ${\mathbf{UPDATE}}=\text{'U'}$ or $\text{'I'}$.
2:     DIST – CHARACTER(1)Input
On entry: indicates which type of distances are computed.
${\mathbf{DIST}}=\text{'A'}$
Absolute distances.
${\mathbf{DIST}}=\text{'E'}$
Euclidean distances.
${\mathbf{DIST}}=\text{'S'}$
Euclidean squared distances.
Constraint: ${\mathbf{DIST}}=\text{'A'}$, $\text{'E'}$ or $\text{'S'}$.
3:     SCAL – CHARACTER(1)Input
On entry: indicates the standardization of the variables to be used.
${\mathbf{SCAL}}=\text{'S'}$
Standard deviation.
${\mathbf{SCAL}}=\text{'R'}$
Range.
${\mathbf{SCAL}}=\text{'G'}$
Standardizations given in array S.
${\mathbf{SCAL}}=\text{'U'}$
Unscaled.
Constraint: ${\mathbf{SCAL}}=\text{'S'}$, $\text{'R'}$, $\text{'G'}$ or $\text{'U'}$.
4:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}\ge 2$.
5:     M – INTEGERInput
On entry: the total number of variables in array X.
Constraint: ${\mathbf{M}}>0$.
6:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of the $\mathit{j}$th variable for the $\mathit{i}$th object, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
7:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03EAF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
8:     ISX(M) – INTEGER arrayInput
On entry: ${\mathbf{ISX}}\left(j\right)$ indicates whether or not the $j$th variable in X is to be included in the distance computations.
If ${\mathbf{ISX}}\left(\mathit{j}\right)>0$ the $\mathit{j}$th variable is included, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$; otherwise it is not referenced.
Constraint: ${\mathbf{ISX}}\left(\mathit{j}\right)>0$ for at least one $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
9:     S(M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{SCAL}}=\text{'G'}$ and ${\mathbf{ISX}}\left(\mathit{j}\right)>0$ then ${\mathbf{S}}\left(\mathit{j}\right)$ must contain the scaling for variable $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
Constraint: if ${\mathbf{SCAL}}=\text{'G'}$ and ${\mathbf{ISX}}\left(j\right)>0$, ${\mathbf{S}}\left(\mathit{j}\right)>0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
On exit: if ${\mathbf{SCAL}}=\text{'S'}$ and ${\mathbf{ISX}}\left(j\right)>0$ then ${\mathbf{S}}\left(j\right)$ contains the standard deviation of the variable in the $j$th column of X.
If ${\mathbf{SCAL}}=\text{'R'}$ and ${\mathbf{ISX}}\left(j\right)>0$, ${\mathbf{S}}\left(j\right)$ contains the range of the variable in the $j$th column of X.
If ${\mathbf{SCAL}}=\text{'U'}$ and ${\mathbf{ISX}}\left(j\right)>0$, ${\mathbf{S}}\left(j\right)=1.0$.
If ${\mathbf{SCAL}}=\text{'G'}$, S is unchanged.
10:   D(${\mathbf{N}}×\left({\mathbf{N}}-1\right)/2$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{UPDATE}}=\text{'U'}$, D must contain the strictly lower triangle of the distance matrix $D$ to be updated. $D$ must be stored packed by rows, i.e., ${\mathbf{D}}\left(\left(i-1\right)\left(i-2\right)/2+j\right)$, $i>j$ must contain ${d}_{ij}$.
If ${\mathbf{UPDATE}}=\text{'I'}$, D need not be set.
Constraint: if ${\mathbf{UPDATE}}=\text{'U'}$, ${\mathbf{D}}\left(\mathit{j}\right)\ge 0.0$, for $\mathit{j}=1,2,\dots ,n\left(n-1\right)/2$.
On exit: the strictly lower triangle of the distance matrix $D$ stored packed by rows, i.e., ${d}_{ij}$ is contained in ${\mathbf{D}}\left(\left(i-1\right)\left(i-2\right)/2+j\right)$, $i>j$.
11:   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{N}}<2$, or ${\mathbf{LDX}}<{\mathbf{N}}$, or ${\mathbf{M}}\le 0$, or ${\mathbf{UPDATE}}\ne \text{'I'}$ or $\text{'U'}$, or ${\mathbf{DIST}}\ne \text{'A'}$, $\text{'E'}$ or $\text{'S'}$, or ${\mathbf{SCAL}}\ne \text{'S'}$, $\text{'R'}$, $\text{'G'}$ or $\text{'U'}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{ISX}}\left(\mathit{j}\right)\le 0$, for $\mathit{j}=1,2,\dots ,{\mathbf{M}}$, or ${\mathbf{UPDATE}}=\text{'U'}$ and ${\mathbf{D}}\left(j\right)<0.0$, for some $j=1,2,\dots ,n\left(n-1\right)/2$, or ${\mathbf{SCAL}}=\text{'S'}$ or $\text{'R'}$ and ${\mathbf{X}}\left(i,j\right)={\mathbf{X}}\left(i+1,j\right)$ for $i=1,2,\dots ,n-1$, for some $j$ with ${\mathbf{ISX}}\left(i\right)>0$. or ${\mathbf{S}}\left(j\right)\le 0.0$ for some $j$ when ${\mathbf{SCAL}}=\text{'G'}$ and ${\mathbf{ISX}}\left(j\right)>0$.

## 7  Accuracy

The computations are believed to be stable.

G03ECF can be used to perform cluster analysis on the computed distance matrix.

## 9  Example

A data matrix of five observations and three variables is read in and a distance matrix is calculated from variables $2$ and $3$ using squared Euclidean distance with no scaling. This matrix is then printed.

### 9.1  Program Text

Program Text (g03eafe.f90)

### 9.2  Program Data

Program Data (g03eafe.d)

### 9.3  Program Results

Program Results (g03eafe.r)