# NAG Library Routine Document

## 1Purpose

g03eaf computes a distance (dissimilarity) matrix.

## 2Specification

Fortran Interface
 Subroutine g03eaf ( dist, scal, n, m, x, ldx, isx, s, d,
 Integer, Intent (In) :: n, m, ldx, isx(m) Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(ldx,m) Real (Kind=nag_wp), Intent (Inout) :: s(m), d(n*(n-1)/2) Character (1), Intent (In) :: update, dist, scal
#include nagmk26.h
 void g03eaf_ (const char *update, const char *dist, const char *scal, const Integer *n, const Integer *m, const double x[], const Integer *ldx, const Integer isx[], double s[], double d[], Integer *ifail, const Charlen length_update, const Charlen length_dist, const Charlen length_scal)

## 3Description

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.

## 4References

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

## 5Arguments

1:     $\mathbf{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:     $\mathbf{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:     $\mathbf{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:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
5:     $\mathbf{m}$ – IntegerInput
On entry: the total number of variables in array x.
Constraint: ${\mathbf{m}}>0$.
6:     $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{m}}\right)$ – 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:     $\mathbf{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:     $\mathbf{isx}\left({\mathbf{m}}\right)$ – 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:     $\mathbf{s}\left({\mathbf{m}}\right)$ – 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:   $\mathbf{d}\left({\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right)$ – 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:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. 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 $-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 argument, 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

g03eaf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

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

## 10Example

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.

### 10.1Program Text

Program Text (g03eafe.f90)

### 10.2Program Data

Program Data (g03eafe.d)

### 10.3Program Results

Program Results (g03eafe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017