NAG CL Interface
g03dbc (discrim_​mahal)

Settings help

CL Name Style:


1 Purpose

g03dbc computes Mahalanobis squared distances for group or pooled variance-covariance matrices. It is intended for use after g03dac.

2 Specification

#include <nag.h>
void  g03dbc (Nag_GroupCovars equal, Nag_MahalDist mode, Integer nvar, Integer ng, const double gmean[], Integer tdg, const double gc[], Integer nobs, Integer m, const Integer isx[], const double x[], Integer tdx, double d[], Integer tdd, NagError *fail)
The function may be called by the names: g03dbc, nag_mv_discrim_mahal or nag_mv_discrim_mahaldist.

3 Description

Consider p variables observed on n g populations or groups. Let x ¯ j be the sample mean and S j the within-group variance-covariance matrix for the j th group and let x k be the k th sample point in a dataset. A measure of the distance of the point from the j th population or group is given by the Mahalanobis distance, D kj 2 :
D kj 2 = ( x k - x ¯ j ) T S j −1 ( x k - x ¯ j ) .  
If the pooled estimated of the variance-covariance matrix S is used rather than the within-group variance-covariance matrices, then the distance is:
D kj 2 = ( x k - x ¯ j ) T S −1 ( x k - x ¯ j ) .  
Instead of using the variance-covariance matrices S and S j , g03dbc uses the upper triangular matrices R and R j supplied by g03dac such that S = RT R and S j = RjT R j . D kj 2 can then be calculated as zT z where R j z = ( x k - x ¯ j ) or Rz = ( x k - x ¯ j ) as appropriate.
A particular case is when the distance between the group or population means is to be estimated. The Mahalanobis distance between the i th and j th groups is:
D ij 2 = ( x ¯ i - x ¯ j ) T S j −1 ( x ¯ i - x ¯ j )  
or
D ij 2 = ( x ¯ i - x ¯ j ) T S −1 ( x ¯ i - x ¯ j ) .  
Note: D jj 2 = 0 and that in the case when the pooled variance-covariance matrix is used D ij 2 = D ji 2 so in this case only the lower triangular values of D ij 2 , i>j , are computed.

4 References

Aitchison J and Dunsmore I R (1975) Statistical Prediction Analysis Cambridge
Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press

5 Arguments

1: equal Nag_GroupCovars Input
On entry: indicates whether or not the within-group variance-covariance matrices are assumed to be equal and the pooled variance-covariance matrix used.
equal=Nag_EqualCovar
The within-group variance-covariance matrices are assumed equal and the matrix R stored in the first p (p+1) / 2 elements of gc is used.
equal=Nag_NotEqualCovar
The within-group variance-covariance matrices are assumed to be unequal and the matrices R j , for j=1,2,, n g , stored in the remainder of gc are used.
Constraint: equal=Nag_EqualCovar or Nag_NotEqualCovar.
2: mode Nag_MahalDist Input
On entry: indicates whether distances from sample points are to be calculated or distances between the group means.
mode=Nag_SamplePoints
The distances between the sample points given in x and the group means are calculated.
mode=Nag_GroupMeans
The distances between the group means will be calculated.
Constraint: mode=Nag_SamplePoints or Nag_GroupMeans.
3: nvar Integer Input
On entry: the number of variables, p , in the variance-covariance matrices as specified to g03dac.
Constraint: nvar1 .
4: ng Integer Input
On entry: the number of groups, n g .
Constraint: ng2 .
5: gmean[ng×tdg] const double Input
Note: the (i,j)th element of the matrix is stored in gmean[(i-1)×tdg+j-1].
On entry: the j th row of gmean contains the means of the p selected variables for the j th group, for j=1,2,, n g . These are returned by g03dac.
6: tdg Integer Input
On entry: the stride separating matrix column elements in the array gmean.
Constraint: tdgnvar .
7: gc[dim] const double Input
Note: the dimension, dim, of the array gc must be at least (ng+1)×nvar×(nvar+1)/2.
On entry: the first p (p+1) / 2 elements of gc should contain the upper triangular matrix R and the next n g blocks of p (p+1) / 2 elements should contain the upper triangular matrices R j . All matrices must be stored packed by column. These matrices are returned by g03dac.
If equal=Nag_EqualCovar only the first p (p+1) / 2 elements are referenced.
If equal=Nag_NotEqualCovar only the elements p (p+1) / 2 to ( n g +1) p (p+1) / 2 - 1 are referenced.
Constraints:
  • if equal=Nag_EqualCovar, the diagonal elements of R0.0 ;
  • if equal=Nag_NotEqualCovar, the diagonal elements of the R j 0.0 , for j=1,2,,ng.
8: nobs Integer Input
On entry: if mode=Nag_SamplePoints the number of sample points in x for which distances are to be calculated.
If mode=Nag_GroupMeans, nobs is not referenced.
Constraint: if mode=Nag_SamplePoints, nobs1 .
9: m Integer Input
On entry: if mode=Nag_SamplePoints the number of variables in the data array x.
If mode=Nag_GroupMeans, then m is not referenced.
Constraint: if mode=Nag_SamplePoints, mnvar .
10: isx[m] const Integer Input
On entry: if mode=Nag_SamplePoints, isx[l-1] indicates if the l th variable in x is to be included in the distance calculations. If isx[l-1] > 0 , the l th variable is included, for l=1,2,,m; otherwise the l th variable is not referenced.
If mode=Nag_GroupMeans, then isx is not referenced and may be set to the NULL pointer (Integer *)0.
Constraint: if mode=Nag_SamplePoints, isx[l-1] > 0 for nvar values of l .
11: x[nobs×tdx] const double Input
On entry: if mode=Nag_SamplePoints, the k th row of x must contain x k . That is, x[(k-1)×tdx+l-1] must contain the k th sample value for the l th variable for k = 1 , 2 , , nobs and l = 1 , 2 , , m . Otherwise x is not referenced and may be set to the NULL pointer (double *)0.
12: tdx Integer Input
On entry: the stride separating matrix column elements in the array x.
Constraint: tdxmax(1,m).
13: d[dim1×tdd] double Output
On exit: the squared distances.
If mode=Nag_SamplePoints, d[(k-1)×tdd+j-1] contains the squared distance of the k th sample point from the j th group mean, D kj 2 , for k=1,2,,nobs and j=1,2,, n g .
If mode=Nag_GroupMeans and equal=Nag_NotEqualCovar, d[(i-1)×tdd+j-1] contains the squared distance between the i th mean and the j th mean, D ij 2 , for i=1,2,, n g and j=1,2,,i - 1 , i + 1 , , n g . The elements d[(i-1)×tdd+i-1] are not referenced, for i=1,2,, n g .
If mode=Nag_GroupMeans and equal=Nag_EqualCovar, d[(i-1)×tdd+j-1] contains the squared distance between the i th mean and the j th mean, D ij 2 , for i=1,2,, n g and j=1,2,,i - 1. Since D ij = D ji the elements d[(i-1)×tdd+j-1] are not referenced, for i=1,2,, n g and j=i,, n g .
14: tdd Integer Input
On entry: the stride separating matrix column elements in the array d.
Constraint: tddng .
15: 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_2_INT_ARG_ENUM_CONS
On entry, m=value while nvar=value and mode=Nag_SamplePoints. These arguments must satisfy mnvar when mode=Nag_SamplePoints.
On entry, tdx=value while m=value and mode=Nag_SamplePoints. These arguments must satisfy tdxmax (1,m) when mode=Nag_SamplePoints.
NE_2_INT_ARG_LT
On entry, tdd=value while ng=value . These arguments must satisfy tddng .
On entry, tdg=value while nvar=value . These arguments must satisfy tdgnvar .
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_BAD_PARAM
On entry, argument equal had an illegal value.
On entry, argument mode had an illegal value.
NE_DIAG_0_COND
A diagonal element of R is zero when equal=Nag_EqualCovar.
NE_DIAG_0_J_COND
A diagonal element of R is zero for some j , when equal=Nag_NotEqualCovar.
NE_INT_ARG_ENUM_CONS
On entry, nobs=value while mode=Nag_SamplePoints. These arguments must satisfy nobs1 when mode=Nag_SamplePoints.
NE_INT_ARG_LT
On entry, ng=value.
Constraint: ng2.
On entry, nvar=value.
Constraint: nvar1.
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_VAR_INCL_COND
The number of variables, nvar in the analysis =value , while number of variables included in the analysis via array isx=value .
Constraint: These two numbers must be the same when mode=Nag_SamplePoints.

7 Accuracy

The accuracy will depend upon the accuracy of the input R or R j matrices.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
g03dbc is not threaded in any implementation.

9 Further Comments

If the distances are to be used for discrimination, see also g03dcc.

10 Example

The data, taken from Aitchison and Dunsmore (1975), is concerned with the diagnosis of three ‘types’ of Cushing's syndrome. The variables are the logarithms of the urinary excretion rates (mg/24hr) of two steroid metabolites. Observations for a total of 21 patients are input and the group means and R matrices are computed by g03dac. A further six observations of unknown type are input, and the distances from the group means of the 21 patients of known type are computed under the assumption that the within-group variance-covariance matrices are not equal. These results are printed and indicate that the first four are close to one of the groups while observations 5 and 6 are some distance from any group.

10.1 Program Text

Program Text (g03dbce.c)

10.2 Program Data

Program Data (g03dbce.d)

10.3 Program Results

Program Results (g03dbce.r)