Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_mv_discrim_group (g03dc)

## Purpose

nag_mv_discrim_group (g03dc) allocates observations to groups according to selected rules. It is intended for use after nag_mv_discrim (g03da).

## Syntax

[prior, p, iag, ati, ifail] = g03dc(typ, equal, priors, nig, gmn, gc, det, isx, x, prior, atiq, 'nvar', nvar, 'ng', ng, 'nobs', nobs, 'm', m)
[prior, p, iag, ati, ifail] = nag_mv_discrim_group(typ, equal, priors, nig, gmn, gc, det, isx, x, prior, atiq, 'nvar', nvar, 'ng', ng, 'nobs', nobs, 'm', m)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 22: nobs was made optional

## Description

Discriminant analysis is concerned with the allocation of observations to groups using information from other observations whose group membership is known, ${X}_{t}$; these are called the training set. Consider $p$ variables observed on ${n}_{g}$ populations or groups. Let ${\stackrel{-}{x}}_{j}$ be the sample mean and ${S}_{j}$ the within-group variance-covariance matrix for the $j$th group; these are calculated from a training set of $n$ observations with ${n}_{j}$ observations in the $j$th group, and let ${x}_{k}$ be the $k$th observation from the set of observations to be allocated to the ${n}_{g}$ groups. The observation can be allocated to a group according to a selected rule. The allocation rule or discriminant function will be based on the distance of the observation from an estimate of the location of the groups, usually the group means. A measure of the distance of the observation from the $j$th group mean is given by the Mahalanobis distance, ${D}_{kj}$:
 $Dkj2=xk-x-jTSj-1xk-x-j.$ (1)
If the pooled estimate 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.$ (2)
Instead of using the variance-covariance matrices $S$ and ${S}_{j}$, nag_mv_discrim_group (g03dc) uses the upper triangular matrices $R$ and ${R}_{j}$ supplied by nag_mv_discrim (g03da) such that $S={R}^{\mathrm{T}}R$ and ${S}_{j}={R}_{j}^{\mathrm{T}}{R}_{j}$. ${D}_{kj}^{2}$ can then be calculated as ${z}^{\mathrm{T}}z$ where ${{R}^{\mathrm{T}}}_{j}z=\left({x}_{k}-{x}_{j}\right)$ or ${R}^{\mathrm{T}}z=\left({x}_{k}-x\right)$ as appropriate.
In addition to the distances, a set of prior probabilities of group membership, ${\pi }_{j}$, for $j=1,2,\dots ,{n}_{g}$, may be used, with $\sum {\pi }_{j}=1$. The prior probabilities reflect your view as to the likelihood of the observations coming from the different groups. Two common cases for prior probabilities are ${\pi }_{1}={\pi }_{2}=\cdots ={\pi }_{{n}_{g}}$, that is, equal prior probabilities, and ${\pi }_{\mathit{j}}={n}_{\mathit{j}}/n$, for $\mathit{j}=1,2,\dots ,{n}_{g}$, that is, prior probabilities proportional to the number of observations in the groups in the training set.
nag_mv_discrim_group (g03dc) uses one of four allocation rules. In all four rules the $p$ variables are assumed to follow a multivariate Normal distribution with mean ${\mu }_{j}$ and variance-covariance matrix ${\Sigma }_{j}$ if the observation comes from the $j$th group. The different rules depend on whether or not the within-group variance-covariance matrices are assumed equal, i.e., ${\Sigma }_{1}={\Sigma }_{2}=\cdots ={\Sigma }_{{n}_{g}}$, and whether a predictive or estimative approach is used. If $p\left({x}_{k}\mid {\mu }_{j},{\Sigma }_{j}\right)$ is the probability of observing the observation ${x}_{k}$ from group $j$, then the posterior probability of belonging to group $j$ is:
 $p j∣xk,μj,Σj∝ p xk∣ μj ,Σj πj.$ (3)
In the estimative approach, the arguments ${\mu }_{j}$ and ${\Sigma }_{j}$ in (3) are replaced by their estimates calculated from ${X}_{t}$. In the predictive approach, a non-informative prior distribution is used for the arguments and a posterior distribution for the arguments, $p\left({\mu }_{j},{\Sigma }_{j}\mid {X}_{t}\right)$, is found. A predictive distribution is then obtained by integrating $p\left(j\mid {x}_{k},{\mu }_{j},{\Sigma }_{j}\right)p\left({\mu }_{j},{\Sigma }_{j}\mid X\right)$ over the argument space. This predictive distribution then replaces $p\left({x}_{k}\mid {\mu }_{j},{\Sigma }_{j}\right)$ in (3). See Aitchison and Dunsmore (1975), Aitchison et al. (1977) and Moran and Murphy (1979) for further details.
The observation is allocated to the group with the highest posterior probability. Denoting the posterior probabilities, $p\left(j\mid {x}_{k},{\mu }_{j},{\Sigma }_{j}\right)$, by ${q}_{j}$, the four allocation rules are:
(i) Estimative with equal variance-covariance matrices – Linear Discrimination
 $log⁡qj∝-12Dkj2+log⁡πj$
(ii) Estimative with unequal variance-covariance matrices – Quadratic Discrimination
 $log⁡qj∝-12Dkj2+log⁡πj-12logSj$
(iii) Predictive with equal variance-covariance matrices
 $q j - 1 ∝ n j +1 / n j p / 2 1 + n j / n - n g n j +1 D k j 2 n +1 - n g / 2$
(iv) Predictive with unequal variance-covariance matrices
 $q j - 1 ∝ C n j 2 - 1 / n j S j p / 2 1 + n j / n j 2 - 1 D k j 2 n j / 2 ,$
where
 $C=Γ12nj-p Γ12nj .$
In the above the appropriate value of ${D}_{kj}^{2}$ from (1) or (2) is used. The values of the ${q}_{j}$ are standardized so that,
 $∑j=1ngqj=1.$
Moran and Murphy (1979) show the similarity between the predictive methods and methods based upon likelihood ratio tests.
In addition to allocating the observation to a group, nag_mv_discrim_group (g03dc) computes an atypicality index, ${I}_{j}\left({x}_{k}\right)$. The predictive atypicality index is returned, irrespective of the value of the parameter typ. This represents the probability of obtaining an observation more typical of group $j$ than the observed ${x}_{k}$ (see Aitchison and Dunsmore (1975) and Aitchison et al. (1977)). The atypicality index is computed for unequal within-group variance-covariance matrices as:
 $Ijxk=PB≤z:12p,12nj-p$
where $P\left(B\le \beta :a,b\right)$ is the lower tail probability from a beta distribution and
 $z=Dkj2/Dkj2+nj2-1/nj,$
and for equal within-group variance-covariance matrices as:
 $Ijxk=PB≤z : 12p,12n-ng-p+ 1,$
with
 $z=Dkj2/Dkj2+n-ngnj+1/nj.$
If ${I}_{j}\left({x}_{k}\right)$ is close to $1$ for all groups it indicates that the observation may come from a grouping not represented in the training set. Moran and Murphy (1979) provide a frequentist interpretation of ${I}_{j}\left({x}_{k}\right)$.

## References

Aitchison J and Dunsmore I R (1975) Statistical Prediction Analysis Cambridge
Aitchison J, Habbema J D F and Kay J W (1977) A critical comparison of two methods of statistical discrimination Appl. Statist. 26 15–25
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
Moran M A and Murphy B J (1979) A closer look at two alternative methods of statistical discrimination Appl. Statist. 28 223–232
Morrison D F (1967) Multivariate Statistical Methods McGraw–Hill

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{typ}$ – string (length ≥ 1)
Whether the estimative or predictive approach is used.
${\mathbf{typ}}=\text{'E'}$
The estimative approach is used.
${\mathbf{typ}}=\text{'P'}$
The predictive approach is used.
Constraint: ${\mathbf{typ}}=\text{'E'}$ or $\text{'P'}$.
2:     $\mathrm{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.
${\mathbf{equal}}=\text{'E'}$
The within-group variance-covariance matrices are assumed equal and the matrix $R$ stored in the first $p\left(p+1\right)/2$ elements of gc is used.
${\mathbf{equal}}=\text{'U'}$
The within-group variance-covariance matrices are assumed to be unequal and the matrices ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{n}_{g}$, stored in the remainder of gc are used.
Constraint: ${\mathbf{equal}}=\text{'E'}$ or $\text{'U'}$.
3:     $\mathrm{priors}$ – string (length ≥ 1)
Indicates the form of the prior probabilities to be used.
${\mathbf{priors}}=\text{'E'}$
Equal prior probabilities are used.
${\mathbf{priors}}=\text{'P'}$
Prior probabilities proportional to the group sizes in the training set, ${n}_{j}$, are used.
${\mathbf{priors}}=\text{'I'}$
The prior probabilities are input in prior.
Constraint: ${\mathbf{priors}}=\text{'E'}$, $\text{'I'}$ or $\text{'P'}$.
4:     $\mathrm{nig}\left({\mathbf{ng}}\right)$int64int32nag_int array
The number of observations in each group in the training set, ${n}_{j}$.
Constraints:
• if ${\mathbf{equal}}=\text{'E'}$, ${\mathbf{nig}}\left(\mathit{j}\right)>0$ and $\sum _{\mathit{j}=1}^{{n}_{g}}{\mathbf{nig}}\left(\mathit{j}\right)>{\mathbf{ng}}+{\mathbf{nvar}}$, for $\mathit{j}=1,2,\dots ,{n}_{g}$;
• if ${\mathbf{equal}}=\text{'U'}$, ${\mathbf{nig}}\left(\mathit{j}\right)>{\mathbf{nvar}}$, for $\mathit{j}=1,2,\dots ,{n}_{g}$.
5:     $\mathrm{gmn}\left(\mathit{ldgmn},{\mathbf{nvar}}\right)$ – double array
ldgmn, the first dimension of the array, must satisfy the constraint $\mathit{ldgmn}\ge {\mathbf{ng}}$.
The $\mathit{j}$th row of gmn contains the means of the $p$ variables for the $\mathit{j}$th group, for $\mathit{j}=1,2,\dots ,{n}_{j}$. These are returned by nag_mv_discrim (g03da).
6:     $\mathrm{gc}\left(\left({\mathbf{ng}}+1\right)×{\mathbf{nvar}}×\left({\mathbf{nvar}}+1\right)/2\right)$ – double array
The first $p\left(p+1\right)/2$ elements of gc should contain the upper triangular matrix $R$ and the next ${n}_{g}$ blocks of $p\left(p+1\right)/2$ elements should contain the upper triangular matrices ${R}_{j}$.
All matrices must be stored packed by column. These matrices are returned by nag_mv_discrim (g03da). If ${\mathbf{equal}}=\text{'E'}$ only the first $p\left(p+1\right)/2$ elements are referenced, if ${\mathbf{equal}}=\text{'U'}$ only the elements $p\left(p+1\right)/2+1$ to $\left({n}_{g}+1\right)p\left(p+1\right)/2$ are referenced.
Constraints:
• if ${\mathbf{equal}}=\text{'E'}$, the diagonal elements of $R$ must be $\text{}\ne 0.0$;
• if ${\mathbf{equal}}=\text{'U'}$, the diagonal elements of the ${R}_{\mathit{j}}$ must be $\text{}\ne 0.0$, for $\mathit{j}=1,2,\dots ,{n}_{g}$.
7:     $\mathrm{det}\left({\mathbf{ng}}\right)$ – double array
If ${\mathbf{equal}}=\text{'U'}$. the logarithms of the determinants of the within-group variance-covariance matrices as returned by nag_mv_discrim (g03da). Otherwise det is not referenced.
8:     $\mathrm{isx}\left({\mathbf{m}}\right)$int64int32nag_int array
${\mathbf{isx}}\left(l\right)$ indicates if the $l$th variable in x is to be included in the distance calculations.
If ${\mathbf{isx}}\left(\mathit{l}\right)>0$, the $\mathit{l}$th variable is included, for $\mathit{l}=1,2,\dots ,{\mathbf{m}}$; otherwise the $\mathit{l}$th variable is not referenced.
Constraint: ${\mathbf{isx}}\left(l\right)>0$ for nvar values of $l$.
9:     $\mathrm{x}\left(\mathit{ldx},{\mathbf{m}}\right)$ – double array
ldx, the first dimension of the array, must satisfy the constraint $\mathit{ldx}\ge {\mathbf{nobs}}$.
${\mathbf{x}}\left(\mathit{k},\mathit{l}\right)$ must contain the $\mathit{k}$th observation for the $\mathit{l}$th variable, for $\mathit{k}=1,2,\dots ,{\mathbf{nobs}}$ and $\mathit{l}=1,2,\dots ,{\mathbf{m}}$.
10:   $\mathrm{prior}\left({\mathbf{ng}}\right)$ – double array
If ${\mathbf{priors}}=\text{'I'}$, the prior probabilities for the ${n}_{g}$ groups.
Constraint: if ${\mathbf{priors}}=\text{'I'}$, ${\mathbf{prior}}\left(\mathit{j}\right)>0.0$ and , for $\mathit{j}=1,2,\dots ,{n}_{g}$.
11:   $\mathrm{atiq}$ – logical scalar
atiq must be true if atypicality indices are required. If atiq is false the array ati is not set.

### Optional Input Parameters

1:     $\mathrm{nvar}$int64int32nag_int scalar
Default: the second dimension of the array gmn.
$p$, the number of variables in the variance-covariance matrices.
Constraint: ${\mathbf{nvar}}\ge 1$.
2:     $\mathrm{ng}$int64int32nag_int scalar
Default: the dimension of the arrays nig, det, prior and the first dimension of the array gmn. (An error is raised if these dimensions are not equal.)
The number of groups, ${n}_{g}$.
Constraint: ${\mathbf{ng}}\ge 2$.
3:     $\mathrm{nobs}$int64int32nag_int scalar
Default: the first dimension of the arrays gmn, x. (An error is raised if these dimensions are not equal.)
The number of observations in x which are to be allocated.
Constraint: ${\mathbf{nobs}}\ge 1$.
4:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the array isx and the second dimension of the array x. (An error is raised if these dimensions are not equal.)
The number of variables in the data array x.
Constraint: ${\mathbf{m}}\ge {\mathbf{nvar}}$.

### Output Parameters

1:     $\mathrm{prior}\left({\mathbf{ng}}\right)$ – double array
If ${\mathbf{priors}}=\text{'P'}$, the computed prior probabilities in proportion to group sizes for the ${n}_{g}$ groups.
If ${\mathbf{priors}}=\text{'I'}$, the input prior probabilities will be unchanged.
If ${\mathbf{priors}}=\text{'E'}$, prior is not set.
2:     $\mathrm{p}\left(\mathit{ldp},{\mathbf{ng}}\right)$ – double array
${\mathbf{p}}\left(\mathit{k},\mathit{j}\right)$ contains the posterior probability ${p}_{\mathit{k}\mathit{j}}$ for allocating the $\mathit{k}$th observation to the $\mathit{j}$th group, for $\mathit{k}=1,2,\dots ,{\mathbf{nobs}}$ and $\mathit{j}=1,2,\dots ,{n}_{g}$.
3:     $\mathrm{iag}\left({\mathbf{nobs}}\right)$int64int32nag_int array
The groups to which the observations have been allocated.
4:     $\mathrm{ati}\left(\mathit{ldp},:\right)$ – double array
The first dimension of the array ati will be ${\mathbf{nobs}}$.
The second dimension of the array ati will be ${\mathbf{ng}}$ if ${\mathbf{atiq}}=\mathit{true}$ and $1$ otherwise.
If atiq is true, ${\mathbf{ati}}\left(\mathit{k},\mathit{j}\right)$ will contain the predictive atypicality index for the $\mathit{k}$th observation with respect to the $\mathit{j}$th group, for $\mathit{k}=1,2,\dots ,{\mathbf{nobs}}$ and $\mathit{j}=1,2,\dots ,{n}_{g}$.
If atiq is false, ati is not set.
5:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{nvar}}<1$, or ${\mathbf{ng}}<2$, or ${\mathbf{nobs}}<1$, or ${\mathbf{m}}<{\mathbf{nvar}}$, or $\mathit{ldgmn}<{\mathbf{ng}}$, or $\mathit{ldx}<{\mathbf{nobs}}$, or $\mathit{ldp}<{\mathbf{nobs}}$, or ${\mathbf{typ}}\ne \text{'E'}$ or ‘p’, or ${\mathbf{equal}}\ne \text{'E'}$ or ‘U’, or ${\mathbf{priors}}\ne \text{'E'}$, ‘I’ or ‘p’.
${\mathbf{ifail}}=2$
 On entry, the number of variables indicated by isx is not equal to nvar, or ${\mathbf{equal}}=\text{'E'}$ and ${\mathbf{nig}}\left(j\right)\le 0$, for some $j$, or ${\mathbf{equal}}=\text{'E'}$ and $\sum _{j=1}^{{n}_{g}}{\mathbf{nig}}\left(j\right)\le {\mathbf{ng}}+{\mathbf{nvar}}$, or ${\mathbf{equal}}=\text{'U'}$ and ${\mathbf{nig}}\left(j\right)\le {\mathbf{nvar}}$ for some $j$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{priors}}=\text{'I'}$ and ${\mathbf{prior}}\left(j\right)\le 0.0$ for some $j$, or ${\mathbf{priors}}=\text{'I'}$ and $\sum _{j=1}^{{n}_{g}}{\mathbf{prior}}\left(j\right)$ is not within  of $1$.
${\mathbf{ifail}}=4$
 On entry, ${\mathbf{equal}}=\text{'E'}$ and a diagonal element of $R$ is zero, or ${\mathbf{equal}}=\text{'U'}$ and a diagonal element of ${R}_{j}$ for some $j$ is zero.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The accuracy of the returned posterior probabilities will depend on the accuracy of the input $R$ or ${R}_{j}$ matrices. The atypicality index should be accurate to four significant places.

The distances ${D}_{kj}^{2}$ can be computed using nag_mv_discrim_mahal (g03db) if other forms of discrimination are required.

## 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 allocations made using the predictive approach and under the assumption that the within-group covariance matrices are not equal. The posterior probabilities of group membership, ${q}_{j}$, and the atypicality index are printed along with the allocated group. The atypicality index shows that observations $5$ and $6$ do not seem to be typical of the three types present in the initial $21$ observations.
```function g03dc_example

fprintf('g03dc 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);

% Data to group
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];

% Grouping parameters
typ    = 'P';
equal  = 'U';
priors = 'Equal priors';
prior  = zeros(3, 1);
atiq   = true;

[prior, p, iag, ati, ifail] = ...
g03dc( ...
typ, equal, priors, nig, gmean, gc, det, isx, x, prior, atiq);

fprintf('   Obs       Posterior        Allocated     Atypicality\n');
fprintf('             probabilities    to group      index\n');
for i=1:6
fprintf('%6d     ', i);
fprintf('%6.3f', p(i,:));
fprintf('%6d     ', iag(i));
fprintf('%6.3f', ati(i,:));
fprintf('\n');
end

```
```g03dc example results

Obs       Posterior        Allocated     Atypicality
probabilities    to group      index
1      0.094 0.905 0.002     2      0.596 0.254 0.975
2      0.005 0.168 0.827     3      0.952 0.836 0.018
3      0.019 0.920 0.062     2      0.954 0.797 0.912
4      0.697 0.303 0.000     1      0.207 0.860 0.993
5      0.317 0.013 0.670     3      0.991 1.000 0.984
6      0.032 0.366 0.601     3      0.981 0.978 0.887
```