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)

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 nn objects, a distance or dissimilarity matrix is a symmetric matrix with zero diagonal elements such that the ijijth element represents how far apart or how dissimilar the iith and jjth objects are.
Let XX be an nn by pp data matrix of observations of pp variables on nn objects, then the distance between object jj and object kk, djkdjk, can be defined as:
djk =
(p ) ∑ D(xji / si,xki / si)i = 1 α
,
djk= {i=1pD(xji/si,xki/si)} α ,
where xjixji and xkixki are the jijith and kikith elements of XX, sisi is a standardization for the iith variable and D(u,v)D(u,v) is a suitable function. Three functions are provided in nag_mv_distance_mat (g03ea).
(a) Euclidean distance: D(u,v) = (uv)2D(u,v)= (u-v) 2 and α = (1/2) α=12 .
(b) Euclidean squared distance: D(u,v) = (uv)2D(u,v)= (u-v) 2 and α = 1α=1.
(c) Absolute distance (city block metric): D (u,v) = |uv| D (u,v)= |u-v|  and α = 1α=1.
Three standardizations are available.
(a) Standard deviation: si = sqrt(j = 1n(xjix)2 / (n1))si=j=1n (xji-x-) 2/(n-1)
(b) Range: si = max (x1i,x2i,,xni) min (x1i,x2i,,xni) si = max(x1i,x2i,,xni) - min(x1i,x2i,,xni)
(c) User-supplied values of sisi.
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'update='U'
The matrix DD is updated and distances are added to DD.
update = 'I'update='I'
The matrix DD is initialized to zero before the distances are added to DD.
Constraint: update = 'U'update='U' or 'I''I'.
2:     dist – string (length ≥ 1)
Indicates which type of distances are computed.
dist = 'A'dist='A'
Absolute distances.
dist = 'E'dist='E'
Euclidean distances.
dist = 'S'dist='S'
Euclidean squared distances.
Constraint: dist = 'A'dist='A', 'E''E' or 'S''S'.
3:     scal – string (length ≥ 1)
Indicates the standardization of the variables to be used.
scal = 'S'scal='S'
Standard deviation.
scal = 'R'scal='R'
Range.
scal = 'G'scal='G'
Standardizations given in array s.
scal = 'U'scal='U'
Unscaled.
Constraint: scal = 'S'scal='S', 'R''R', 'G''G' or 'U''U'.
4:     x(ldx,m) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxnldxn.
x(i,j)xij must contain the value of the jjth variable for the iith object, for i = 1,2,,ni=1,2,,n and j = 1,2,,mj=1,2,,m.
5:     isx(m) – int64int32nag_int array
m, the dimension of the array, must satisfy the constraint m > 0m>0.
isx(j)isxj indicates whether or not the jjth variable in x is to be included in the distance computations.
If isx(j) > 0isxj>0 the jjth variable is included, for j = 1,2,,mj=1,2,,m; otherwise it is not referenced.
Constraint: isx(j) > 0isxj>0 for at least one jj, for j = 1,2,,mj=1,2,,m.
6:     s(m) – double array
m, the dimension of the array, must satisfy the constraint m > 0m>0.
If scal = 'G'scal='G' and isx(j) > 0isxj>0 then s(j)sj must contain the scaling for variable jj, for j = 1,2,,mj=1,2,,m.
Constraint: if scal = 'G'scal='G' and isx(j) > 0isxj>0, s(j) > 0.0sj>0.0, for j = 1,2,,mj=1,2,,m.
7:     d(n × (n1) / 2n×(n-1)/2) – double array
If update = 'U'update='U', d must contain the strictly lower triangle of the distance matrix DD to be updated. DD must be stored packed by rows, i.e., d((i1)(i2) / 2 + j)d((i-1)(i-2)/2+j), i > ji>j must contain dijdij.
If update = 'I'update='I', d need not be set.
Constraint: if update = 'U'update='U', d(j)0.0dj0.0, for j = 1,2,,n(n1) / 2j=1,2,,n(n-1)/2.

Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The first dimension of the array x.
nn, the number of observations.
Constraint: n2n2.
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 > 0m>0.

Input Parameters Omitted from the MATLAB Interface

ldx

Output Parameters

1:     s(m) – double array
If scal = 'S'scal='S' and isx(j) > 0isxj>0 then s(j)sj contains the standard deviation of the variable in the jjth column of x.
If scal = 'R'scal='R' and isx(j) > 0isxj>0, s(j)sj contains the range of the variable in the jjth column of x.
If scal = 'U'scal='U' and isx(j) > 0isxj>0, s(j) = 1.0sj=1.0.
If scal = 'G'scal='G', s is unchanged.
2:     d(n × (n1) / 2n×(n-1)/2) – double array
The strictly lower triangle of the distance matrix DD stored packed by rows, i.e., dijdij is contained in d((i1)(i2) / 2 + j)d((i-1)(i-2)/2+j), i > ji>j.
3:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
On entry,n < 2n<2,
orldx < nldx<n,
orm0m0,
orupdate'I'update'I' or 'U''U',
ordist'A'dist'A', 'E''E' or 'S''S',
orscal'S'scal'S', 'R''R', 'G''G' or 'U''U'.
  ifail = 2ifail=2
On entry,isx(j)0isxj0, for j = 1,2,,mj=1,2,,m,
orupdate = 'U'update='U' and d(j) < 0.0dj<0.0, for some j = 1,2,,n(n1) / 2j=1,2,,n(n-1)/2,
orscal = 'S'scal='S' or 'R''R' and x(i,j) = x(i + 1,j)xij=xi+1j for i = 1,2,,n1i=1,2,,n-1, for some jj with isx(i) > 0isxi>0.
ors(j)0.0sj0.0 for some jj when scal = 'G'scal='G' and isx(j) > 0isxj>0.

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

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



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–2013