NAG FL Interface
g03eaf (distance_​mat)

Settings help

FL Name Style:

FL Specification Language:

1 Purpose

g03eaf computes a distance (dissimilarity) matrix.

2 Specification

Fortran Interface
Subroutine g03eaf ( update, dist, scal, n, m, x, ldx, isx, s, d, ifail)
Integer, Intent (In) :: n, m, ldx, isx(m)
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: x(ldx,m)
Real (Kind=nag_wp), Intent (Inout) :: s(m), d(n*(n-1)/2)
Character (1), Intent (In) :: update, dist, scal
C Header Interface
#include <nag.h>
void  g03eaf_ (const char *update, const char *dist, const char *scal, const Integer *n, const Integer *m, const double x[], const Integer *ldx, const Integer isx[], double s[], double d[], Integer *ifail, const Charlen length_update, const Charlen length_dist, const Charlen length_scal)
The routine may be called by the names g03eaf or nagf_mv_distance_mat.

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×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=1pD(xji/si,xki/si)} α ,  
where xji and xki are the jith and kith elements of X, si is a standardization for the ith variable and D(u,v) is a suitable function. Three functions are provided in g03eaf.
  1. (a)Euclidean distance: D(u,v)= (u-v) 2 and α=12 .
  2. (b)Euclidean squared distance: D(u,v)= (u-v) 2 and α=1.
  3. (c)Absolute distance (city block metric): D (u,v)= |u-v| and α=1.
Three standardizations are available.
  1. (a)Standard deviation: si=j=1n (xji-x¯) 2/(n-1)
  2. (b)Range: si = max(x1i,x2i,,xni) - min(x1i,x2i,,xni)
  3. (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 Arguments

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 Integer Input
On entry: n, the number of observations.
Constraint: n2.
5: m Integer Input
On entry: the total number of variables in array x.
Constraint: m>0.
6: x(ldx,m) Real (Kind=nag_wp) array Input
On entry: x(i,j) must contain the value of the jth variable for the ith object, for i=1,2,,n and j=1,2,,m.
7: ldx Integer Input
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 array Input
On entry: isx(j) indicates whether or not the jth variable in x is to be included in the distance computations.
If isx(j)>0 the jth variable is included, for j=1,2,,m; otherwise it is not referenced.
Constraint: isx(j)>0 for at least one j, for j=1,2,,m.
9: s(m) Real (Kind=nag_wp) array Input/Output
On entry: if scal='G' and isx(j)>0 then s(j) must contain the scaling for variable j, for j=1,2,,m.
Constraint: if scal='G' and isx(j)>0, s(j)>0.0, for j=1,2,,m.
On exit: if scal='S' and isx(j)>0 then s(j) contains the standard deviation of the variable in the jth column of x.
If scal='R' and isx(j)>0, s(j) contains the range of the variable in the jth column of x.
If scal='U' and isx(j)>0, s(j)=1.0.
If scal='G', s is unchanged.
10: d(n×(n-1)/2) Real (Kind=nag_wp) array Input/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., d( (i-1) (i-2) /2+j ) , i>j must contain dij.
If update='I', d need not be set.
Constraint: if update='U', d(j)0.0, for j=1,2,,n(n-1)/2.
On exit: 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.
11: ifail Integer Input/Output
On entry: ifail must be set to 0, −1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of −1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value −1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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, dist=value.
Constraint: dist='A', 'E' or 'S'
On entry, ldx=value and n=value.
Constraint: ldxn.
On entry, m=value.
Constraint: m>0.
On entry, n=value.
Constraint: n2.
On entry, scal=value.
Constraint: scal='S', 'R', 'G' or 'U'.
On entry, update=value.
Constraint: update='U' or 'I'.
On entry, at least one element of d<0.0.
On entry, at least one element of s0.0.
On entry, isx does not contain a positive element.
Variable value is constant.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

The computations are believed to be stable.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g03eaf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g03eaf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

g03ecf can be used to perform cluster analysis on the computed distance matrix.

10 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.

10.1 Program Text

Program Text (g03eafe.f90)

10.2 Program Data

Program Data (g03eafe.d)

10.3 Program Results

Program Results (g03eafe.r)