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:
Mark 22: n has been made optional
.

## Description

Given n$n$ objects, a distance or dissimilarity matrix is a symmetric matrix with zero diagonal elements such that the ij$ij$th element represents how far apart or how dissimilar the i$i$th and j$j$th objects are.
Let X$X$ be an n$n$ by p$p$ data matrix of observations of p$p$ variables on n$n$ objects, then the distance between object j$j$ and object k$k$, djk${d}_{jk}$, can be defined as:
djk =
 (p ) ∑ D(xji / si,xki / si)i = 1 α
,
$djk= {∑i=1pD(xji/si,xki/si)} α ,$
where xji${x}_{ji}$ and xki${x}_{ki}$ are the ji$ji$th and ki$ki$th elements of X$X$, si${s}_{i}$ is a standardization for the i$i$th variable and D(u,v)$D\left(u,v\right)$ is a suitable function. Three functions are provided in nag_mv_distance_mat (g03ea).
 (a) Euclidean distance: D(u,v) = (u − v)2$D\left(u,v\right)={\left(u-v\right)}^{2}$ and α = (1/2) $\alpha =\frac{1}{2}$. (b) Euclidean squared distance: D(u,v) = (u − v)2$D\left(u,v\right)={\left(u-v\right)}^{2}$ and α = 1$\alpha =1$. (c) Absolute distance (city block metric): D (u,v) = |u − v| $D\left(u,v\right)=|u-v|$ and α = 1$\alpha =1$.
Three standardizations are available.
 (a) Standard deviation: si = sqrt( ∑ j = 1n(xji − x)2 / (n − 1))${s}_{i}=\sqrt{\sum _{j=1}^{n}{\left({x}_{ji}-\stackrel{-}{x}\right)}^{2}/\left(n-1\right)}$ (b) Range: si = max (x1i,x2i, … ,xni) − min (x1i,x2i, … ,xni) ${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 si${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:     update – string (length ≥ 1)
Indicates whether or not an existing matrix is to be updated.
update = 'U'${\mathbf{update}}=\text{'U'}$
The matrix D$D$ is updated and distances are added to D$D$.
update = 'I'${\mathbf{update}}=\text{'I'}$
The matrix D$D$ is initialized to zero before the distances are added to D$D$.
Constraint: update = 'U'${\mathbf{update}}=\text{'U'}$ or 'I'$\text{'I'}$.
2:     dist – string (length ≥ 1)
Indicates which type of distances are computed.
dist = 'A'${\mathbf{dist}}=\text{'A'}$
Absolute distances.
dist = 'E'${\mathbf{dist}}=\text{'E'}$
Euclidean distances.
dist = 'S'${\mathbf{dist}}=\text{'S'}$
Euclidean squared distances.
Constraint: dist = 'A'${\mathbf{dist}}=\text{'A'}$, 'E'$\text{'E'}$ or 'S'$\text{'S'}$.
3:     scal – string (length ≥ 1)
Indicates the standardization of the variables to be used.
scal = 'S'${\mathbf{scal}}=\text{'S'}$
Standard deviation.
scal = 'R'${\mathbf{scal}}=\text{'R'}$
Range.
scal = 'G'${\mathbf{scal}}=\text{'G'}$
Standardizations given in array s.
scal = 'U'${\mathbf{scal}}=\text{'U'}$
Unscaled.
Constraint: scal = 'S'${\mathbf{scal}}=\text{'S'}$, 'R'$\text{'R'}$, 'G'$\text{'G'}$ or 'U'$\text{'U'}$.
4:     x(ldx,m) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
x(i,j)${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of the j$\mathit{j}$th variable for the i$\mathit{i}$th object, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
5:     isx(m) – int64int32nag_int array
m, the dimension of the array, must satisfy the constraint m > 0${\mathbf{m}}>0$.
isx(j)${\mathbf{isx}}\left(j\right)$ indicates whether or not the j$j$th variable in x is to be included in the distance computations.
If isx(j) > 0${\mathbf{isx}}\left(\mathit{j}\right)>0$ the j$\mathit{j}$th variable is included, for j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$; otherwise it is not referenced.
Constraint: isx(j) > 0${\mathbf{isx}}\left(\mathit{j}\right)>0$ for at least one j$\mathit{j}$, for j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
6:     s(m) – double array
m, the dimension of the array, must satisfy the constraint m > 0${\mathbf{m}}>0$.
If scal = 'G'${\mathbf{scal}}=\text{'G'}$ and isx(j) > 0${\mathbf{isx}}\left(\mathit{j}\right)>0$ then s(j)${\mathbf{s}}\left(\mathit{j}\right)$ must contain the scaling for variable j$\mathit{j}$, for j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
Constraint: if scal = 'G'${\mathbf{scal}}=\text{'G'}$ and isx(j) > 0${\mathbf{isx}}\left(j\right)>0$, s(j) > 0.0${\mathbf{s}}\left(\mathit{j}\right)>0.0$, for j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
7:     d(n × (n1) / 2${\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$) – double array
If update = 'U'${\mathbf{update}}=\text{'U'}$, d must contain the strictly lower triangle of the distance matrix D$D$ to be updated. D$D$ must be stored packed by rows, i.e., d((i1)(i2) / 2 + j)${\mathbf{d}}\left(\left(i-1\right)\left(i-2\right)/2+j\right)$, i > j$i>j$ must contain dij${d}_{ij}$.
If update = 'I'${\mathbf{update}}=\text{'I'}$, d need not be set.
Constraint: if update = 'U'${\mathbf{update}}=\text{'U'}$, d(j)0.0${\mathbf{d}}\left(\mathit{j}\right)\ge 0.0$, for j = 1,2,,n(n1) / 2$\mathit{j}=1,2,\dots ,n\left(n-1\right)/2$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The first dimension of the array x.
n$n$, the number of observations.
Constraint: n2${\mathbf{n}}\ge 2$.
2:     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: m > 0${\mathbf{m}}>0$.

ldx

### Output Parameters

1:     s(m) – double array
If scal = 'S'${\mathbf{scal}}=\text{'S'}$ and isx(j) > 0${\mathbf{isx}}\left(j\right)>0$ then s(j)${\mathbf{s}}\left(j\right)$ contains the standard deviation of the variable in the j$j$th column of x.
If scal = 'R'${\mathbf{scal}}=\text{'R'}$ and isx(j) > 0${\mathbf{isx}}\left(j\right)>0$, s(j)${\mathbf{s}}\left(j\right)$ contains the range of the variable in the j$j$th column of x.
If scal = 'U'${\mathbf{scal}}=\text{'U'}$ and isx(j) > 0${\mathbf{isx}}\left(j\right)>0$, s(j) = 1.0${\mathbf{s}}\left(j\right)=1.0$.
If scal = 'G'${\mathbf{scal}}=\text{'G'}$, s is unchanged.
2:     d(n × (n1) / 2${\mathbf{n}}×\left({\mathbf{n}}-1\right)/2$) – double array
The strictly lower triangle of the distance matrix D$D$ stored packed by rows, i.e., dij${d}_{ij}$ is contained in d((i1)(i2) / 2 + j)${\mathbf{d}}\left(\left(i-1\right)\left(i-2\right)/2+j\right)$, i > j$i>j$.
3:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 2${\mathbf{n}}<2$, or ldx < n$\mathit{ldx}<{\mathbf{n}}$, or m ≤ 0${\mathbf{m}}\le 0$, or update ≠ 'I'${\mathbf{update}}\ne \text{'I'}$ or 'U'$\text{'U'}$, or dist ≠ 'A'${\mathbf{dist}}\ne \text{'A'}$, 'E'$\text{'E'}$ or 'S'$\text{'S'}$, or scal ≠ 'S'${\mathbf{scal}}\ne \text{'S'}$, 'R'$\text{'R'}$, 'G'$\text{'G'}$ or 'U'$\text{'U'}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, isx(j) ≤ 0${\mathbf{isx}}\left(\mathit{j}\right)\le 0$, for j = 1,2, … ,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$, or update = 'U'${\mathbf{update}}=\text{'U'}$ and d(j) < 0.0${\mathbf{d}}\left(j\right)<0.0$, for some j = 1,2, … ,n(n − 1) / 2$j=1,2,\dots ,n\left(n-1\right)/2$, or scal = 'S'${\mathbf{scal}}=\text{'S'}$ or 'R'$\text{'R'}$ and x(i,j) = x(i + 1,j)${\mathbf{x}}\left(i,j\right)={\mathbf{x}}\left(i+1,j\right)$ for i = 1,2, … ,n − 1$i=1,2,\dots ,n-1$, for some j$j$ with isx(i) > 0${\mathbf{isx}}\left(i\right)>0$. or s(j) ≤ 0.0${\mathbf{s}}\left(j\right)\le 0.0$ for some j$j$ when scal = 'G'${\mathbf{scal}}=\text{'G'}$ and isx(j) > 0${\mathbf{isx}}\left(j\right)>0$.

## 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

```function nag_mv_distance_mat_example
update = 'I';
dist = 'S';
scal = 'U';
x = [1, 1, 1;
2, 1, 2;
3, 6, 3;
4, 8, 2;
5, 8, 0];
isx = [int64(0);1;1];
s = [1;
1;
1];
d = zeros(10,1);
[sOut, dOut, ifail] = nag_mv_distance_mat(update, dist, scal, x, isx, s, d)
```
```

sOut =

1
1
1

dOut =

1
29
26
50
49
5
50
53
13
4

ifail =

0

```
```function g03ea_example
update = 'I';
dist = 'S';
scal = 'U';
x = [1, 1, 1;
2, 1, 2;
3, 6, 3;
4, 8, 2;
5, 8, 0];
isx = [int64(0);1;1];
s = [1;
1;
1];
d = zeros(10,1);
[sOut, dOut, ifail] = g03ea(update, dist, scal, x, isx, s, d)
```
```

sOut =

1
1
1

dOut =

1
29
26
50
49
5
50
53
13
4

ifail =

0

```