NAG CL Interface
g03eac (distance_​mat)

Settings help

CL Name Style:

1 Purpose

g03eac computes a distance (dissimilarity) matrix.

2 Specification

#include <nag.h>
void  g03eac (Nag_MatUpdate update, Nag_DistanceType dist, Nag_VarScaleType scale, Integer n, Integer m, const double x[], Integer tdx, const Integer isx[], double s[], double d[], NagError *fail)
The function may be called by the names: g03eac or nag_mv_distance_mat.

3 Description

Given n objects, a distance or dissimilarity matrix, is a symmetric matrix with zero diagonal elements such that the ij th element represents how far apart or how dissimilar the i th and j th 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 , d jk , can be defined as:
d jk = { i=1 p D( x ji / s i , x ki / s i )} α ,  
where x ji and x ki are the (j,i) th and (k,i) th elements of X , s i is a standardization for the i th variable and D (u,v) is a suitable function. Three functions are provided in g03eac:
  1. (a)Euclidean distance: D (u,v) = (u-v) 2 and α = 1 2 .
  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. 1.Standard deviation: s i = j=1 n ( x ji - x ¯) 2 / (n-1)
  2. 2.Range: s i = max( x 1i , x 2i ,, x ni ) - min( x 1i , x 2i ,, x ni )
  3. 3.User-supplied values of 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 g03eac 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 the G02 Chapter Introduction and the G11 Chapter Introduction respectively.
g03eac 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 Nag_MatUpdate 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=Nag_MatUp or Nag_NoMatUp.
2: dist Nag_DistanceType Input
On entry: indicates which type of distances are computed.
Absolute distances.
Euclidean distances.
Euclidean squared distances.
Constraint: dist=Nag_DistAbs, Nag_DistEuclid or Nag_DistSquared.
3: scale Nag_VarScaleType Input
On entry: indicates the standardization of the variables to be used.
Standard deviation.
Standardizations given in array S.
Constraint: scale=Nag_VarScaleStd, Nag_VarScaleRange, Nag_VarScaleUser or Nag_NoVarScale.
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[n×tdx] const double Input
On entry: x[(i-1)×tdx+j-1] must contain the value of the j th variable for the i th object, for i=1,2,,n and j=1,2,,m.
7: tdx Integer Input
On entry: the stride separating matrix column elements in the array x.
Constraint: tdxm .
8: isx[m] const Integer Input
On entry: isx[j-1] indicates whether or not the j th variable in x is to be included in the distance computations.
If isx[j-1] > 0 the j th variable is included, for j=1,2,,m; otherwise it is not referenced.
Constraint: isx[j-1] > 0 for at least one j , for j=1,2,,m.
9: s[m] double Input/Output
On entry: if scale=Nag_VarScaleUser and isx[j-1] > 0 then s[j-1] must contain the scaling for variable j , for j=1,2,,m.
Constraint: if scale=Nag_VarScaleUser and isx[j-1] > 0 , s[j-1] > 0.0 , for j=1,2,,m.
On exit: if scale=Nag_VarScaleStd and isx[j-1] > 0 then s[j-1] contains the standard deviation of the variable in the j th column of x.
If scale=Nag_VarScaleRange and isx[j-1] > 0 then s[j-1] contains the range of the variable in the j th column of x.
If scale=Nag_NoVarScale and isx[j-1] > 0 then s[j-1] = 1.0 and if scale=Nag_VarScaleUser then s is unchanged.
10: d[n×(n-1)/2] double Input/Output
On entry: if update=Nag_MatUp then 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 - 1 ] , i>j must contain d ij .
Constraint: if update=Nag_MatUp, d[j-1] 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., d ij is contained in d[ (i-1) (i-2) / 2 + j - 1 ] , i>j .
11: fail NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

6 Error Indicators and Warnings

On entry, tdx=value while m=value . These arguments must satisfy tdxm .
On entry, argument dist had an illegal value.
On entry, argument scale had an illegal value.
On entry, argument update had an illegal value.
On entry, scale=Nag_VarScaleRange or scale=Nag_VarScaleStd, and x[(i-1)×tdx+j-1] = x[(i)×tdx+j-1] , for i=1,2,,n - 1, for some j with isx[i-1] > 0 .
On entry, m=value.
Constraint: m>0.
On entry, n=value.
Constraint: n2.
On entry, isx[value] = value.
Constraint: isx[i-1] > 0 , for at least one i , i = 1 , 2 , , m .
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
On entry, d[value] = value.
Constraint: d[i-1] 0 , for i=1,2,, n × (n-1) / 2, when update=Nag_MatUp.
On entry, s[value] = value.
Constraint: s[j-1] > 0 , for j=1,2,,m, when scale=Nag_VarScaleUser and isx[j-1] > 0 .

7 Accuracy

The computations are believed to be stable.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g03eac 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 function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9 Further Comments

g03ecc 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 (g03eace.c)

10.2 Program Data

Program Data (g03eace.d)

10.3 Program Results

Program Results (g03eace.r)