hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_mv_distance_mat (g03ea)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 ijth element represents how far apart or how dissimilar the ith and jth 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, djk, can be defined as:
djk= i=1pDxji/si,xki/si α ,  
where xji and xki are the jith and kith elements of X, si is a standardization for the ith variable and Du,v is a suitable function. Three functions are provided in nag_mv_distance_mat (g03ea).
(a) Euclidean distance: Du,v= u-v 2 and α=12 .
(b) Euclidean squared distance: Du,v= u-v 2 and α=1.
(c) Absolute distance (city block metric): D u,v= u-v  and α=1.
Three standardizations are available.
(a) Standard deviation: si=j=1n xji-x- 2/n-1
(b) Range: si = maxx1i,x2i,,xni - minx1i,x2i,,xni
(c) User-supplied values of si.
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'
The matrix D is updated and distances are added to D.
update='I'
The matrix D is initialized to zero before the distances are added to D.
Constraint: update='U' or 'I'.
2:     dist – string (length ≥ 1)
Indicates which type of distances are computed.
dist='A'
Absolute distances.
dist='E'
Euclidean distances.
dist='S'
Euclidean squared distances.
Constraint: dist='A', 'E' or 'S'.
3:     scal – string (length ≥ 1)
Indicates the standardization of the variables to be used.
scal='S'
Standard deviation.
scal='R'
Range.
scal='G'
Standardizations given in array s.
scal='U'
Unscaled.
Constraint: scal='S', 'R', 'G' or 'U'.
4:     xldxm – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn.
xij must contain the value of the jth variable for the ith object, for i=1,2,,n and j=1,2,,m.
5:     isxm int64int32nag_int array
isxj indicates whether or not the jth variable in x is to be included in the distance computations.
If isxj>0 the jth variable is included, for j=1,2,,m; otherwise it is not referenced.
Constraint: isxj>0 for at least one j, for j=1,2,,m.
6:     sm – double array
If scal='G' and isxj>0 then sj must contain the scaling for variable j, for j=1,2,,m.
Constraint: if scal='G' and isxj>0, sj>0.0, for j=1,2,,m.
7:     dn×n-1/2 – double array
If update='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., d i-1 i-2 /2+j , i>j must contain dij.
If update='I', d need not be set.
Constraint: if update='U', dj0.0, for j=1,2,,nn-1/2.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the first dimension of the array x.
n, the number of observations.
Constraint: n2.
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.

Output Parameters

1:     sm – double array
If scal='S' and isxj>0 then sj contains the standard deviation of the variable in the jth column of x.
If scal='R' and isxj>0, sj contains the range of the variable in the jth column of x.
If scal='U' and isxj>0, sj=1.0.
If scal='G', s is unchanged.
2:     dn×n-1/2 – double array
The strictly lower triangle of the distance matrix D stored packed by rows, i.e., dij is contained in d i-1 i-2 /2+j , i>j.
3:     ifail int64int32nag_int scalar
ifail=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
On entry,n<2,
orldx<n,
orm0,
orupdate'I' or 'U',
ordist'A', 'E' or 'S',
orscal'S', 'R', 'G' or 'U'.
   ifail=2
On entry,isxj0, for j=1,2,,m,
orupdate='U' and dj<0.0, for some j=1,2,,nn-1/2,
orscal='S' or 'R' and xij=xi+1j for i=1,2,,n-1, for some j with isxi>0.
orsj0.0 for some j when scal='G' and isxj>0.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

The computations are believed to be stable.

Further Comments

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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015