G03EAF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

NAG Library Routine Document


Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

G03EAF computes a distance (dissimilarity) matrix.

2  Specification

REAL (KIND=nag_wp)  X(LDX,M), S(M), D(N*(N-1)/2)

3  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 G03EAF.
(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 G03EAF 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.
G03EAF 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  Parameters

1:     UPDATE – CHARACTER(1)Input
On entry: indicates whether or not an existing matrix is to be updated.
The matrix D is updated and distances are added to D.
The matrix D is initialized to zero before the distances are added to D.
Constraint: UPDATE='U' or 'I'.
2:     DIST – CHARACTER(1)Input
On entry: indicates which type of distances are computed.
Absolute distances.
Euclidean distances.
Euclidean squared distances.
Constraint: DIST='A', 'E' or 'S'.
3:     SCAL – CHARACTER(1)Input
On entry: indicates the standardization of the variables to be used.
Standard deviation.
Standardizations given in array S.
Constraint: SCAL='S', 'R', 'G' or 'U'.
4:     N – INTEGERInput
On entry: n, the number of observations.
Constraint: N2.
5:     M – INTEGERInput
On entry: the total number of variables in array X.
Constraint: M>0.
6:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: Xij must contain the value of the jth variable for the ith object, for i=1,2,,n and j=1,2,,M.
7:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03EAF is called.
Constraint: LDXN.
8:     ISX(M) – INTEGER arrayInput
On entry: 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.
9:     S(M) – REAL (KIND=nag_wp) arrayInput/Output
On entry: 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.
On exit: 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.
10:   D(N×N-1/2) – REAL (KIND=nag_wp) arrayInput/Output
On entry: 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., Di-1i-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.
On exit: the strictly lower triangle of the distance matrix D stored packed by rows, i.e., dij is contained in Di-1i-2/2+j, i>j.
11:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
On entry,N<2,
orUPDATE'I' or 'U',
orDIST'A', 'E' or 'S',
orSCAL'S', 'R', 'G' or 'U'.
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.

7  Accuracy

The computations are believed to be stable.

8  Further Comments

G03ECF 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 (g03eafe.f90)

9.2  Program Data

Program Data (g03eafe.d)

9.3  Program Results

Program Results (g03eafe.r)

G03EAF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012