NAG CL Interface
g03ebc (distance_​mat_​2)

Settings help

CL Name Style:


1 Purpose

g03ebc computes a distance (dissimilarity) matrix between two sets of observations.

2 Specification

#include <nag.h>
void  g03ebc (Nag_MatUpdate update, Nag_VarScaleType scal, Nag_VarScaleYType stype, double p, Integer m, Integer n, Integer l, const double x[], Integer pdx, const Integer isv[], const double y[], Integer pdy, double sx[], double sy[], double d[], NagError *fail)
The function may be called by the names: g03ebc or nag_mv_distance_mat_2.

3 Description

Given two sets of observations on l variables, a distance matrix is such that the ijth element represents how far apart or how dissimilar the ith observation from the first set and the jth observation from the second set are.
Let X and Y be m×l and n×l data matrices of m and n observations, respectively, on l variables. The distance between observation i from X and observation j from Y, dij, is most commonly defined in terms of the scaled Minkowski p-norm:
dij= { k=1p (|xik/sxi-yjk/syj|) p } 1/p ,  
where xik and yjk are the ikth and jkth elements of X and Y respectively, sxi is a standardization for the ith variable in X, syj is a standardization for the jth variable in Y, and p is the order of the Minkowski norm.
Three standardizations (scalings) for the variables are available.
  1. (a)Standard deviation: sxi=k=1n (xji-x¯) 2/(n-1)
  2. (b)Range: sxi = max(x1i,x2i,,xni) - min(x1i,x2i,,xni)
  3. (c)User-supplied values of sxi.
In addition to the Minkowski measure 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 g03ebc 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.
g03ebc returns the full rectangular 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.
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: scal Nag_VarScaleType Input
On entry: indicates the standardization of the variables to be used.
scal=Nag_VarScaleStd
Standard deviation.
scal=Nag_VarScaleRange
Range.
scal=Nag_VarScaleUser
Standardizations given in array sx (and posibly sy).
scal=Nag_NoVarScale
Unscaled.
Constraint: scal=Nag_VarScaleStd, Nag_VarScaleRange, Nag_VarScaleUser or Nag_NoVarScale.
3: stype Nag_VarScaleYType Input
On entry: indicates how the standardization of the variables treats the two sets of observations.
stype=Nag_VarScaleAmalg
Amalgamated.
stype=Nag_VarScaleIndep
Independent.
stype=Nag_VarScaleX
Standardization is based purley on observations in x.
Constraint: stype=Nag_VarScaleAmalg, Nag_VarScaleIndep or Nag_VarScaleX.
4: p double Input
On entry: the order p of the Minkowski distance metric.
Constraint: p1.0.
5: m Integer Input
On entry: m, the number of observations in the data array x.
Constraint: m1.
6: n Integer Input
On entry: n, the number of observations in the data array y.
Constraint: n1.
7: l Integer Input
On entry: l, the total number of variables in arrays x and y.
Constraint: l>0.
8: x[pdx×l] const double Input
Note: the (i,j)th element of the matrix X is stored in x[(j-1)×pdx+i-1].
On entry: x[(k-1)×pdx+i-1] must contain the value of the kth variable for the ith observation in the first set of observations, for i=1,2,,m and k=1,2,,l.
9: pdx Integer Input
On entry: the stride separating matrix row elements in the array x.
Constraint: pdxm.
10: isv[l] const Integer Input
On entry: isv[j-1] indicates whether or not the jth variable in x and y is to be included in the distance computations.
If isv[k-1]=0 the jth variable is not included., for k=1,2,,l.
If isv[k-1]1 the jth variable is included, for k=1,2,,l
Constraint: isv[j-1]>0 for at least one j, for k=1,2,,l.
11: y[pdy×l] const double Input
Note: the (i,j)th element of the matrix Y is stored in y[(j-1)×pdy+i-1].
On entry: y[(k-1)×pdy+j-1] must contain the value of the kth variable for the jth observation in the second set of observations, for j=1,2,,n and k=1,2,,l.
12: pdy Integer Input
On entry: the stride separating matrix row elements in the array y.
Constraint: pdyn.
13: sx[l] double Input/Output
On entry: if scal=Nag_VarScaleUser and isv[k-1]>0 then sx[k-1] must contain the scaling for variable k, for k=1,2,,l.
Constraint: if scal=Nag_VarScaleUser and isv[k]>0, sx[k]>0.0, for k=0,1,,l-1.
On exit: if scal=Nag_VarScaleStd and isv[k-1]>0 then sx[k-1] contains the standard deviation of the variable in the kth column of x.
If scal=Nag_VarScaleRange and isv[k-1]>0, sx[k-1] contains the range of the variable in the jth column of x.
If scal=Nag_NoVarScale and isv[k-1]>0, sx[k-1]=1.0.
If scal=Nag_VarScaleUser, sx is unchanged.
14: sy[dim] double Input/Output
Note: the dimension, dim, of the array sy must be at least
  • l when stype=Nag_VarScaleIndep and scalNag_NoVarScale;
  • otherwise sy may be NULL.
On entry: if scal=Nag_VarScaleUser and stype=Nag_VarScaleIndep and isv[k-1]>0 then sy[k-1] must contain the scaling for variable k , for k=1,2,,l.
If stypeNag_VarScaleIndep, or scal=Nag_NoVarScale then sy is not referenced and may be NULL.
Constraint: if scal=Nag_VarScaleUser and stype=Nag_VarScaleIndep and isv[k]>0, sy[k]>0.0, for k=0,1,,l-1.
On exit: if scal=Nag_VarScaleStd and stype=Nag_VarScaleIndep and isv[k-1]>0 then sy[k-1] contains the standard deviation of the variable in the kth column of x.
If scal=Nag_VarScaleRange and stype=Nag_VarScaleIndep and isv[k-1]>0, sy[k-1] contains the range of the variable in the jth column of x.
If scal=Nag_NoVarScale, sy is unchanged.
If scal=Nag_VarScaleUser, sy is unchanged.
15: d[m×n] double Input/Output
Note: the (i,j)th element of the matrix D is stored in d[(j-1)×m+i-1].
On entry: the m×n distance matrix D.
If update=Nag_MatUp, d need not be set.
On exit: the (possibly updated) distance matrix D.
16: 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

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_ARRAY_CONS
On entry, at least one element of sx0.0.
On entry, at least one element of sx0.0 or sy0.0.
On entry, isv does not contain a positive element.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_CONST_COL
Variable value is constant.
NE_INT
On entry, l=value.
Constraint: l1.
On entry, m=value.
Constraint: m1.
On entry, n=value.
Constraint: n1.
NE_INT_2
On entry, pdx=value and m=value.
Constraint: pdxm.
On entry, pdy=value and n=value.
Constraint: pdyn.
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.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NEG_ELEMENT
On entry, at least one element of d<0.0.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL
On entry, p=value.
Constraint: p1.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.
g03ebc 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

When there is a large number of observations and these do not require scaling factors to be computed internally, then the distance matrix for all observations can be computed in stages by using g03ebc to systematically calculate the distance matrix for pairs of row blocks of observations. When there are a large number of variables, the distance matrix can be updated in stages using observations on blocks of variables at each update.

10 Example

Two data matrices of five and three observations on three variables is read in and a distance matrix is calculated from variables 2 and 3 using Euclidean distance with no scaling. This matrix is then printed.

10.1 Program Text

Program Text (g03ebce.c)

10.2 Program Data

Program Data (g03ebce.d)

10.3 Program Results

Program Results (g03ebce.r)