Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_mv_distance_mat (g03ea)

## Purpose

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

## Syntax

[s, d, ifail] = g03ea(update, dist, scal, x, isx, s, d, 'n', n, 'm', m)
[s, d, ifail] = nag_mv_distance_mat(update, dist, scal, x, isx, s, d, 'n', n, 'm', m)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 22: n was made optional

## 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 nag_mv_distance_mat (g03ea).
 (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 nag_mv_distance_mat (g03ea) 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.
nag_mv_distance_mat (g03ea) returns the strictly lower triangle of the distance matrix.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{update}$ – string (length ≥ 1)
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:     $\mathrm{dist}$ – string (length ≥ 1)
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:     $\mathrm{scal}$ – string (length ≥ 1)
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:     $\mathrm{x}\left(\mathit{ldx},{\mathbf{m}}\right)$ – double array
ldx, the first dimension of the array, must satisfy the constraint $\mathit{ldx}\ge {\mathbf{n}}$.
${\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}}$.
5:     $\mathrm{isx}\left({\mathbf{m}}\right)$int64int32nag_int array
${\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}}$.
6:     $\mathrm{s}\left({\mathbf{m}}\right)$ – double array
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}}$.
7:     $\mathrm{d}\left({\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right)$ – double array
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$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the first dimension of the array x.
$n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
2:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the arrays isx, s and the second dimension of the array x. (An error is raised if these dimensions are not equal.)
The total number of variables in array x.
Constraint: ${\mathbf{m}}>0$.

### Output Parameters

1:     $\mathrm{s}\left({\mathbf{m}}\right)$ – double array
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.
2:     $\mathrm{d}\left({\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right)$ – double array
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$.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$, or $\mathit{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$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The computations are believed to be stable.

nag_mv_cluster_hier (g03ec) can be used to perform cluster analysis on the computed distance matrix.

## 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.
```function g03ea_example

fprintf('g03ea example results\n\n');

x = [1, 1, 1;
2, 1, 2;
3, 6, 3;
4, 8, 2;
5, 8, 0];
[n,m]  = size(x);

isx    = ones(m,1,'int64');
isx(1) = int64(0);
s      = ones(m,1);
ld     = (n*(n-1))/2;
d      = zeros(ld,1);

% Compute the distance matrix
update = 'I';
dist = 'S';
scal = 'U';
[s, d, ifail] = g03ea( ...
update, dist, scal, x, isx, s, d);

fprintf(' Distance Matrix\n  ');
fprintf('   %5d', [1:n-1]);
for i = 2:n
lj = (i-1)*(i-2)/2 + 1;
uj = i*(i-1)/2;
fprintf('\n%2d  ', i);
fprintf('   %5.2f', d(lj:uj));
end
fprintf('\n');

```
```g03ea example results

Distance Matrix
1       2       3       4
2      1.00
3     29.00   26.00
4     50.00   49.00    5.00
5     50.00   53.00   13.00    4.00
```