g03 Chapter Contents
g03 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_mv_distance_mat (g03eac)

## 1  Purpose

nag_mv_distance_mat (g03eac) computes a distance (dissimilarity) matrix.

## 2  Specification

 #include #include
 void nag_mv_distance_mat (Nag_MatUpdate update, Nag_DistanceType dist, Nag_VarScaleType scale, Integer n, Integer m, const double x[], Integer tdx, const Integer isx[], double s[], double d[], NagError *fail)

## 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:
 $d jk = ∑ i=1 p D x ji / s i , x ki / s i α ,$
where ${x}_{ji}$ and ${x}_{ki}$ are the $\left(j,i\right)$th and $\left(k,i\right)$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 nag_mv_distance_mat (g03eac):
 (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:
1. Standard deviation: ${s}_{i}=\sqrt{{\sum }_{j=1}^{n}{\left({x}_{ji}-\stackrel{-}{x}\right)}^{2}/\left(n-1\right)}$
2. 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)$
3. 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 nag_mv_distance_mat (g03eac) 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 the g02 Chapter Introduction and the g11 Chapter Introduction respectively.
nag_mv_distance_mat (g03eac) 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  Arguments

1:     updateNag_MatUpdateInput
On entry: indicates whether or not an existing matrix is to be updated.
${\mathbf{update}}=\mathrm{Nag_MatUp}$
The matrix $D$ is updated and distances are added to $D$.
${\mathbf{update}}=\mathrm{Nag_NoMatUp}$
The matrix $D$ is initialized to zero before the distances are added to $D$.
Constraint: ${\mathbf{update}}=\mathrm{Nag_MatUp}$ or $\mathrm{Nag_NoMatUp}$.
2:     distNag_DistanceTypeInput
On entry: indicates which type of distances are computed.
${\mathbf{dist}}=\mathrm{Nag_DistAbs}$
Absolute distances.
${\mathbf{dist}}=\mathrm{Nag_DistEuclid}$
Euclidean distances.
${\mathbf{dist}}=\mathrm{Nag_DistSquared}$
Euclidean squared distances.
Constraint: ${\mathbf{dist}}=\mathrm{Nag_DistAbs}$, $\mathrm{Nag_DistEuclid}$ or $\mathrm{Nag_DistSquared}$.
3:     scaleNag_VarScaleTypeInput
On entry: indicates the standardization of the variables to be used.
${\mathbf{scale}}=\mathrm{Nag_VarScaleStd}$
Standard deviation.
${\mathbf{scale}}=\mathrm{Nag_VarScaleRange}$
Range.
${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$
Standardizations given in array $S$.
${\mathbf{scale}}=\mathrm{Nag_NoVarScale}$
Unscaled.
Constraint: ${\mathbf{scale}}=\mathrm{Nag_VarScaleStd}$, $\mathrm{Nag_VarScaleRange}$, $\mathrm{Nag_VarScaleUser}$ or $\mathrm{Nag_NoVarScale}$.
4:     nIntegerInput
On entry: the number of observations, $n$.
Constraint: ${\mathbf{n}}\ge 2$.
5:     mIntegerInput
On entry: the total number of variables in array x.
Constraint: ${\mathbf{m}}>0$.
6:     x[${\mathbf{n}}×{\mathbf{tdx}}$]const doubleInput
On entry: ${\mathbf{x}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdx}}+\mathit{j}-1\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:     tdxIntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: ${\mathbf{tdx}}\ge {\mathbf{m}}$.
8:     isx[m]const IntegerInput
On entry: ${\mathbf{isx}}\left[j-1\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}-1\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}-1\right]>0$ for at least one $\mathit{j}$, , for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
9:     s[m]doubleInput/Output
On entry: if ${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{isx}}\left[\mathit{j}-1\right]>0$ then ${\mathbf{s}}\left[\mathit{j}-1\right]$ must contain the scaling for variable $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
Constraint: if ${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{isx}}\left[j-1\right]>0$, ${\mathbf{s}}\left[\mathit{j}-1\right]>0.0$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
On exit: if ${\mathbf{scale}}=\mathrm{Nag_VarScaleStd}$ and ${\mathbf{isx}}\left[j-1\right]>0$ then ${\mathbf{s}}\left[j-1\right]$ contains the standard deviation of the variable in the $j$th column of x.
If ${\mathbf{scale}}=\mathrm{Nag_VarScaleRange}$ and ${\mathbf{isx}}\left[j-1\right]>0$ then ${\mathbf{s}}\left[j-1\right]$ contains the range of the variable in the $j$th column of x.
If ${\mathbf{scale}}=\mathrm{Nag_NoVarScale}$ and ${\mathbf{isx}}\left[j-1\right]>0$ then ${\mathbf{s}}\left[j-1\right]=1.0$ and if ${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$ then s is unchanged.
10:   d[${\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$]doubleInput/Output
On entry: if ${\mathbf{update}}=\mathrm{Nag_MatUp}$ then 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-1\right]$, $i>j$ must contain ${d}_{ij}$.
Constraint: if ${\mathbf{update}}=\mathrm{Nag_MatUp}$, ${\mathbf{d}}\left[\mathit{j}-1\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-1\right]$, $i>j$.
11:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, ${\mathbf{tdx}}=〈\mathit{\text{value}}〉$ while ${\mathbf{m}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{tdx}}\ge {\mathbf{m}}$.
On entry, argument dist had an illegal value.
On entry, argument scale had an illegal value.
On entry, argument update had an illegal value.
NE_IDEN_ELEM_COND
On entry, ${\mathbf{scale}}=\mathrm{Nag_VarScaleRange}$ or ${\mathbf{scale}}=\mathrm{Nag_VarScaleStd}$, and ${\mathbf{x}}\left[\left(\mathit{i}-1\right)×{\mathbf{tdx}}+j-1\right]={\mathbf{x}}\left[\left(\mathit{i}\right)×{\mathbf{tdx}}+j-1\right]$, for $\mathit{i}=1,2,\dots ,n-1$, for some $j$ with ${\mathbf{isx}}\left[i-1\right]>0$.
NE_INT_ARG_LE
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}>0$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 2$.
NE_INTARR
On entry, ${\mathbf{isx}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{isx}}\left[i-1\right]>0$, for at least one $i,i=1,2,\dots ,{\mathbf{m}}$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_REALARR
On entry, ${\mathbf{d}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{d}}\left[\mathit{i}-1\right]\ge 0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$, when ${\mathbf{update}}=\mathrm{Nag_MatUp}$.
On entry, ${\mathbf{s}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{s}}\left[\mathit{j}-1\right]>0$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$, when ${\mathbf{scale}}=\mathrm{Nag_VarScaleUser}$ and ${\mathbf{isx}}\left[j-1\right]>0$.

## 7  Accuracy

The computations are believed to be stable.

nag_mv_hierar_cluster_analysis (g03ecc) 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 (g03eace.c)

### 9.2  Program Data

Program Data (g03eace.d)

### 9.3  Program Results

Program Results (g03eace.r)