NAG FL Interface
g03ebf (distance_​mat_​2)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

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

2 Specification

Fortran Interface
Subroutine g03ebf ( update, scal, stype, p, m, n, l, x, ldx, isv, y, ldy, sx, sy, d, ifail)
Integer, Intent (In) :: m, n, l, ldx, isv(l), ldy
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: p, x(ldx,l), y(ldy,l)
Real (Kind=nag_wp), Intent (Inout) :: sx(l), sy(*), d(m,n)
Character (1), Intent (In) :: update, scal, stype
C Header Interface
#include <nag.h>
void  g03ebf_ (const char *update, const char *scal, const char *stype, const double *p, const Integer *m, const Integer *n, const Integer *l, const double x[], const Integer *ldx, const Integer isv[], const double y[], const Integer *ldy, double sx[], double sy[], double d[], Integer *ifail, const Charlen length_update, const Charlen length_scal, const Charlen length_stype)
The routine may be called by the names g03ebf or nagf_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 g03ebf 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.
g03ebf 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 Character(1) Input
On entry: indicates whether or not an existing matrix is to be updated.
update='U'
The matrix D is updated and distances are added to D.
update='I'
The matrix D is initialized to zero before the distances are added to D.
Constraint: update='U' or 'I'.
2: scal Character(1) Input
On entry: indicates the standardization of the variables to be used.
scal='S'
Standard deviation.
scal='R'
Range.
scal='G'
Standardizations given in array sx (and posibly sy).
scal='U'
Unscaled.
Constraint: scal='S', 'R', 'G' or 'U'.
3: stype Character(1) Input
On entry: indicates how the standardization of the variables treats the two sets of observations.
stype='A'
Amalgamated.
stype='I'
Independent.
stype='X'
Standardization is based purley on observations in x.
Constraint: stype='A', 'I' or 'X'.
4: p Real (Kind=nag_wp) 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(ldx,l) Real (Kind=nag_wp) array Input
On entry: x(i,k) 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: ldx Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g03ebf is called.
Constraint: ldxm.
10: isv(l) Integer array Input
On entry: isv(j) indicates whether or not the jth variable in x and y is to be included in the distance computations.
If isv(k)=0 the jth variable is not included., for k=1,2,,l.
If isv(k)1 the jth variable is included, for k=1,2,,l
Constraint: isv(j)>0 for at least one j, for k=1,2,,l.
11: y(ldy,l) Real (Kind=nag_wp) array Input
On entry: y(j,k) 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: ldy Integer Input
On entry: the first dimension of the array y as declared in the (sub)program from which g03ebf is called.
Constraint: ldyn.
13: sx(l) Real (Kind=nag_wp) array Input/Output
On entry: if scal='G' and isv(k)>0 then sx(k) must contain the scaling for variable k, for k=1,2,,l.
Constraint: if scal='G' and isv(k)>0, sx(k)>0.0, for k=1,2,,l.
On exit: if scal='S' and isv(k)>0 then sx(k) contains the standard deviation of the variable in the kth column of x.
If scal='R' and isv(k)>0, sx(k) contains the range of the variable in the jth column of x.
If scal='U' and isv(k)>0, sx(k)=1.0.
If scal='G', sx is unchanged.
14: sy(*) Real (Kind=nag_wp) array Input/Output
Note: the dimension of the array sy must be at least l if stype='I' and scal'U', and at least 0 otherwise.
On entry: if scal='G' and stype='I' and isv(k)>0 then sy(k) must contain the scaling for variable k , for k=1,2,,l.
If stype'I', or scal='U' then sy is not referenced and need not be set.
Constraint: if scal='G' and stype='I' and isv(k)>0, sy(k)>0.0, for k=1,2,,l.
On exit: if scal='S' and stype='I' and isv(k)>0 then sy(k) contains the standard deviation of the variable in the kth column of x.
If scal='R' and stype='I' and isv(k)>0, sy(k) contains the range of the variable in the jth column of x.
If scal='U', sy is unchanged.
If scal='G', sy is unchanged.
15: d(m,n) Real (Kind=nag_wp) array Input/Output
On entry: the m×n distance matrix D.
If update='U', d need not be set.
On exit: the (possibly updated) distance matrix D.
16: 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:
ifail=1
On entry, update=value.
Constraint: update='U' or 'I'.
ifail=2
On entry, scal=value.
Constraint: scal='S', 'R', 'G' or 'U'.
ifail=3
On entry, stype=value.
Constraint: stype='A', 'I' or 'X'.
ifail=4
On entry, p=value.
Constraint: p1.0.
ifail=5
On entry, m=value.
Constraint: m1.
ifail=6
On entry, n=value.
Constraint: n1.
ifail=7
On entry, l=value.
Constraint: l1.
ifail=8
Variable value is constant.
ifail=9
On entry, ldx=value and m=value.
Constraint: ldxm.
ifail=10
On entry, isv does not contain a positive element.
ifail=12
On entry, ldy=value and n=value.
Constraint: ldyn.
ifail=13
On entry, at least one element of sx0.0.
On entry, at least one element of sx0.0 or sy0.0.
ifail=15
On entry, at least one element of d<0.0.
ifail=-99
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.
ifail=-399
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.
ifail=-999
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.
g03ebf 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

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 g03ebf 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 (g03ebfe.f90)

10.2 Program Data

Program Data (g03ebfe.d)

10.3 Program Results

Program Results (g03ebfe.r)