hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_mv_discrim_mahal (g03db)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_mv_discrim_mahal (g03db) computes Mahalanobis squared distances for group or pooled variance-covariance matrices. It is intended for use after nag_mv_discrim (g03da).

Syntax

[d, ifail] = g03db(equal, mode, gmn, gc, nobs, isx, x, 'nvar', nvar, 'ng', ng, 'm', m)
[d, ifail] = nag_mv_discrim_mahal(equal, mode, gmn, gc, nobs, isx, x, 'nvar', nvar, 'ng', ng, 'm', m)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 22: ng was made optional

Description

Consider p variables observed on ng populations or groups. Let x-j be the sample mean and Sj the within-group variance-covariance matrix for the jth group and let xk be the kth sample point in a dataset. A measure of the distance of the point from the jth population or group is given by the Mahalanobis distance, Dkj:
Dkj2=xk-x-jTSj-1xk-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:
Dkj2=xk-x-jTS-1xk-x-j.  
Instead of using the variance-covariance matrices S and Sj, nag_mv_discrim_mahal (g03db) uses the upper triangular matrices R and Rj supplied by nag_mv_discrim (g03da) such that S=RTR and Sj=RjTRj. Dkj2 can then be calculated as zTz where Rjz=xk-x-j or Rz=xk-x-j as appropriate.
A particular case is when the distance between the group or population means is to be estimated. The Mahalanobis squared distance between the ith and jth groups is:
Dij2=x-i-x-jTSj-1x-i-x-j  
or
Dij2=x-i-x-jTS-1x-i-x-j.  
Note:  Djj2=0 and that in the case when the pooled variance-covariance matrix is used Dij2=Dji2 so in this case only the lower triangular values of Dij2, i>j, are computed.

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

Parameters

Compulsory Input Parameters

1:     equal – string (length ≥ 1)
Indicates whether or not the within-group variance-covariance matrices are assumed to be equal and the pooled variance-covariance matrix used.
equal='E'
The within-group variance-covariance matrices are assumed equal and the matrix R stored in the first pp+1/2 elements of gc is used.
equal='U'
The within-group variance-covariance matrices are assumed to be unequal and the matrices Rj, for j=1,2,,ng, stored in the remainder of gc are used.
Constraint: equal='E' or 'U'.
2:     mode – string (length ≥ 1)
Indicates whether distances from sample points are to be calculated or distances between the group means.
mode='S'
The distances between the sample points given in x and the group means are calculated.
mode='M'
The distances between the group means will be calculated.
Constraint: mode='M' or 'S'.
3:     gmnldgmnnvar – double array
ldgmn, the first dimension of the array, must satisfy the constraint ldgmnng.
The jth row of gmn contains the means of the p selected variables for the jth group, for j=1,2,,ng. These are returned by nag_mv_discrim (g03da).
4:     gcng+1×nvar×nvar+1/2 – double array
The first pp+1/2 elements of gc should contain the upper triangular matrix R and the next ng blocks of pp+1/2 elements should contain the upper triangular matrices Rj. All matrices must be stored packed by column. These matrices are returned by nag_mv_discrim (g03da). If equal='E' only the first pp+1/2 elements are referenced, if equal='U' only the elements pp+1/2+1 to ng+1pp+1/2 are referenced.
Constraints:
  • if equal='E', R0.0;
  • if equal='U', the diagonal elements of the Rj0.0, for j=1,2,,ng.
5:     nobs int64int32nag_int scalar
If mode='S', the number of sample points in x for which distances are to be calculated.
If mode='M', nobs is not referenced.
Constraint: if nobs1, mode='S'.
6:     isx: int64int32nag_int array
The dimension of the array isx must be at least max1,m
If mode='S', isxl indicates if the lth variable in x is to be included in the distance calculations. If isxl>0 the lth variable is included, for l=1,2,,m; otherwise the lth variable is not referenced.
If mode='M', isx is not referenced.
Constraint: if mode='S', isxl>0 for nvar values of l.
7:     xldx: – double array
The first dimension, ldx, of the array x must satisfy
  • if mode='S', ldxnobs;
  • otherwise 1.
The second dimension of the array x must be at least max1,m.
If mode='S' the kth row of x must contain xk. That is xkl must contain the kth sample value for the lth variable, for k=1,2,,nobs and l=1,2,,m. Otherwise x is not referenced.

Optional Input Parameters

1:     nvar int64int32nag_int scalar
Default: the second dimension of the array gmn.
p, the number of variables in the variance-covariance matrices as specified to nag_mv_discrim (g03da).
Constraint: nvar1.
2:     ng int64int32nag_int scalar
Default: the first dimension of the array gmn.
The number of groups, ng.
Constraint: ng2.
3:     m int64int32nag_int scalar
Default: the dimension of the arrays isx, x.
If mode='S', the number of variables in the data array x.
If mode='M', m is not referenced.
Constraint: if mnvar, mode='S'.

Output Parameters

1:     dlddng – double array
The squared distances.
If mode='S', dkj contains the squared distance of the kth sample point from the jth group mean, Dkj2, for k=1,2,,nobs and j=1,2,,ng.
If mode='M' and equal='U', dij contains the squared distance between the ith mean and the jth mean, Dij2, for i=1,2,,ng and j=1,2,,i-1,i+1,,ng. The elements dii are not referenced, for i=1,2,,ng.
If mode='M' and equal='E', dij contains the squared distance between the ith mean and the jth mean, Dij2, for i=1,2,,ng and j=1,2,,i-1. Since Dij=Dji the elements dij are not referenced, for i=1,2,,ng and j=i+1,,ng.
2:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
   ifail=1
On entry,nvar<1,
orng<2,
orldgmn<ng,
ormode='S' and nobs<1,
ormode='S' and m<nvar,
ormode='S' and ldx<nobs,
ormode='S' and ldd<nobs,
ormode='M' and ldd<ng,
orequal'E' or ‘U’,
ormode'M' or ‘S’.
   ifail=2
On entry,mode='S' and the number of variables indicated by isx is not equal to nvar,
orequal='E' and a diagonal element of R is zero,
orequal='U' and a diagonal element of Rj for some j is zero.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

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

Further Comments

If the distances are to be used for discrimination, see also nag_mv_discrim_group (g03dc).

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 nag_mv_discrim (g03da). 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.
function g03db_example


fprintf('g03db example results\n\n');

x = [1.1314,  2.4596;
     1.0986,  0.2624;
     0.6419, -2.3026;
     1.3350, -3.2189;
     1.4110,  0.0953;
     0.6419, -0.9163;
     2.1163,  0.0000;
     1.3350, -1.6094;
     1.3610, -0.5108;
     2.0541,  0.1823;
     2.2083, -0.5108;
     2.7344,  1.2809;
     2.0412,  0.4700;
     1.8718, -0.9163;
     1.7405, -0.9163;
     2.6101,  0.4700;
     2.3224,  1.8563;
     2.2192,  2.0669;
     2.2618,  1.1314;
     3.9853,  0.9163;
     2.7600,  2.0281];
[n,m] = size(x);
isx  = ones(m,1,'int64');
nvar = int64(m);
ing  = ones(n,1,'int64');
ing(7:16) = int64(2);
ing(17:n) = int64(3);
ng        = int64(3);

% Compute covariance matrix
[nig, gmean, det, gc, stat, df, sig, ifail] = ...
  g03da( ...
	 x, isx, nvar, ing, ng);

equal = 'U';
mode = 'Sample points';
nobs = int64(6);

% Data from which to compute distances
x = [1.6292, -0.9163;
     2.5572, 1.6094;
     2.5649, -0.2231;
     0.9555, -2.3026;
     3.4012, -2.3026;
     3.0204, -0.2231];

% Compute distances
[d, ifail] = g03db( ...
		    equal, mode, gmean, gc, nobs, isx, x);

mtitle = 'Distances';
matrix = 'General';
diag   = ' ';
[ifail] = x04ca( ...
                 matrix, diag, d, mtitle);


g03db example results

 Distances
             1          2          3
 1      3.3393     0.7521    50.9283
 2     20.7771     5.6559     0.0597
 3     21.3631     4.8411    19.4978
 4      0.7184     6.2803   124.7323
 5     55.0003    88.8604    71.7852
 6     36.1703    15.7849    15.7489

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015