nag_mv_distance_mat (g03eac) (PDF version)
g03 Chapter Contents
g03 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_mv_distance_mat (g03eac)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_mv_distance_mat (g03eac) computes a distance (dissimilarity) matrix.

2  Specification

#include <nag.h>
#include <nagg03.h>
void  nag_mv_distance_mat (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)

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  by 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 nag_mv_distance_mat (g03eac):
(a) Euclidean distance: D u,v = u-v 2  and α = 1 2 .
(b) Euclidean squared distance: D u,v = u-v 2  and α=1 .
(c) Absolute distance (city block metric): D u,v = u-v  and α=1 .
Three standardizations are available:
1. Standard deviation: s i = j=1 n x ji - x - 2 / n-1
2. Range: s i = max x 1i , x 2i , , x ni - min x 1i , x 2i , , x ni
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 nag_mv_distance_mat (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.
nag_mv_distance_mat (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:     updateNag_MatUpdateInput
On entry: indicates whether or not an existing matrix is to be updated.
update=Nag_MatUp
The matrix D  is updated and distances are added to D .
update=Nag_NoMatUp
The matrix D  is initialized to zero before the distances are added to D .
Constraint: update=Nag_MatUp or Nag_NoMatUp.
2:     distNag_DistanceTypeInput
On entry: indicates which type of distances are computed.
dist=Nag_DistAbs
Absolute distances.
dist=Nag_DistEuclid
Euclidean distances.
dist=Nag_DistSquared
Euclidean squared distances.
Constraint: dist=Nag_DistAbs, Nag_DistEuclid or Nag_DistSquared.
3:     scaleNag_VarScaleTypeInput
On entry: indicates the standardization of the variables to be used.
scale=Nag_VarScaleStd
Standard deviation.
scale=Nag_VarScaleRange
Range.
scale=Nag_VarScaleUser
Standardizations given in array S.
scale=Nag_NoVarScale
Unscaled.
Constraint: scale=Nag_VarScaleStd, Nag_VarScaleRange, Nag_VarScaleUser or Nag_NoVarScale.
4:     nIntegerInput
On entry: the number of observations, n .
Constraint: n2 .
5:     mIntegerInput
On entry: the total number of variables in array x.
Constraint: m>0 .
6:     x[n×tdx]const doubleInput
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:     tdxIntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: tdxm .
8:     isx[m]const IntegerInput
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]doubleInput/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]doubleInput/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:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, tdx=value  while m=value . These arguments must satisfy tdxm .
NE_BAD_PARAM
On entry, argument dist had an illegal value.
On entry, argument scale had an illegal value.
On entry, argument update had an illegal value.
NE_IDEN_ELEM_COND
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 .
NE_INT_ARG_LE
On entry, m=value.
Constraint: m>0.
NE_INT_ARG_LT
On entry, n=value.
Constraint: n2.
NE_INTARR
On entry, isx[value] = value.
Constraint: isx[i-1] > 0 , for at least one i , i = 1 , 2 , , m .
NE_INTERNAL_ERROR
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.
NE_REALARR
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

Not applicable.

9  Further Comments

nag_mv_hierar_cluster_analysis (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)


nag_mv_distance_mat (g03eac) (PDF version)
g03 Chapter Contents
g03 Chapter Introduction
NAG Library Manual

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