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_gaussian_mixture (g03ga)

## Purpose

nag_mv_gaussian_mixture (g03ga) performs a mixture of Normals (Gaussians) for a given (co)variance structure.

## Syntax

[prob, niter, w, g, s, f, loglik, ifail] = g03ga(x, isx, nvar, ng, sopt, sds, tol, 'n', n, 'm', m, 'prob', prob, 'niter', niter, 'riter', riter)
[prob, niter, w, g, s, f, loglik, ifail] = nag_mv_gaussian_mixture(x, isx, nvar, ng, sopt, sds, tol, 'n', n, 'm', m, 'prob', prob, 'niter', niter, 'riter', riter)

## Description

A Normal (Gaussian) mixture model is a weighted sum of $k$ group Normal densities given by,
 $p x∣w,μ,Σ = ∑ j=1 k wj g x∣μj,Σj , x∈ℝp$
where:
• $x$ is a $p$-dimensional object of interest;
• ${w}_{j}$ is the mixture weight for the $j$th group and $\sum _{\mathit{j}=1}^{k}{w}_{j}=1$;
• ${\mu }_{j}$ is a $p$-dimensional vector of means for the $j$th group;
• ${\Sigma }_{j}$ is the covariance structure for the $j$th group;
• $g\left(·\right)$ is the $p$-variate Normal density:
 $g x∣μj,Σj = 1 2π p/2 Σj 1/2 exp - 12 x-μj Σ j -1 x-μj T .$
Optionally, the (co)variance structure may be pooled (common to all groups) or calculated for each group, and may be full or diagonal.

## References

Hartigan J A (1975) Clustering Algorithms Wiley

## Parameters

### Compulsory Input Parameters

1:     $\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{n}}$.
${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of the $\mathit{j}$th variable for the $\mathit{i}$th object, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
2:     $\mathrm{isx}\left({\mathbf{m}}\right)$int64int32nag_int array
If ${\mathbf{nvar}}={\mathbf{m}}$ all available variables are included in the model and isx is not referenced; otherwise the $j$th variable will be included in the analysis if ${\mathbf{isx}}\left(\mathit{j}\right)=1$ and excluded if ${\mathbf{isx}}\left(\mathit{j}\right)=0$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
Constraint: if ${\mathbf{nvar}}\ne {\mathbf{m}}$, ${\mathbf{isx}}\left(\mathit{j}\right)=1$ for nvar values of $\mathit{j}$ and ${\mathbf{isx}}\left(\mathit{j}\right)=0$ for the remaining ${\mathbf{m}}-{\mathbf{nvar}}$ values of $\mathit{j}$, for $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
3:     $\mathrm{nvar}$int64int32nag_int scalar
$p$, the number of variables included in the calculations.
Constraint: $1\le {\mathbf{nvar}}\le {\mathbf{m}}$.
4:     $\mathrm{ng}$int64int32nag_int scalar
$k$, the number of groups in the mixture model.
Constraint: ${\mathbf{ng}}\ge 1$.
5:     $\mathrm{sopt}$int64int32nag_int scalar
Determines the (co)variance structure:
${\mathbf{sopt}}=1$
Groupwise covariance matrices.
${\mathbf{sopt}}=2$
Pooled covariance matrix.
${\mathbf{sopt}}=3$
Groupwise variances.
${\mathbf{sopt}}=4$
Pooled variances.
${\mathbf{sopt}}=5$
Overall variance.
Constraint: ${\mathbf{sopt}}=1$, $2$, $3$, $4$ or $5$.
6:     $\mathrm{sds}$int64int32nag_int scalar
The second dimension of the (co)variance structure s.
Constraints:
• if ${\mathbf{sopt}}=1$ or $2$, sds must be at least nvar;
• if ${\mathbf{sopt}}=3$, sds must be at least ng;
• if ${\mathbf{sopt}}=4$ or $5$, sds must be at least $1$.
7:     $\mathrm{tol}$ – double scalar
Iterations cease the first time an improvement in log-likelihood is less than tol. If ${\mathbf{tol}}\le 0$ a value of ${10}^{-3}$ is used.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the first dimension of the arrays x, prob. (An error is raised if these dimensions are not equal.)
$n$, the number of objects. There must be more objects than parameters in the model.
Constraints:
• if ${\mathbf{sopt}}=1$, ${\mathbf{n}}>{\mathbf{ng}}×\left({\mathbf{nvar}}×{\mathbf{nvar}}+{\mathbf{nvar}}\right)$;
• if ${\mathbf{sopt}}=2$, ${\mathbf{n}}>{\mathbf{nvar}}×\left({\mathbf{ng}}+{\mathbf{nvar}}\right)$;
• if ${\mathbf{sopt}}=3$, ${\mathbf{n}}>2×{\mathbf{ng}}×{\mathbf{nvar}}$;
• if ${\mathbf{sopt}}=4$, ${\mathbf{n}}>{\mathbf{nvar}}×\left({\mathbf{ng}}+1\right)$;
• if ${\mathbf{sopt}}=5$, ${\mathbf{n}}>{\mathbf{nvar}}×{\mathbf{ng}}+1$.
2:     $\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 total number of variables in array x.
Constraint: ${\mathbf{m}}\ge 1$.
3:     $\mathrm{prob}\left(\mathit{lprob},{\mathbf{ng}}\right)$ – double array
If $\mathit{popt}\ne 1$, ${\mathbf{prob}}\left(i,j\right)$ is the probability that the $i$th object belongs to the $j$th group. (These probabilities are normalised internally.)
4:     $\mathrm{niter}$int64int32nag_int scalar
Default: $15$
The maximum number of iterations.
Constraint: ${\mathbf{niter}}\ge 1$.
5:     $\mathrm{riter}$int64int32nag_int scalar
Default: $5$
If ${\mathbf{riter}}>0$, membership probabilities are rounded to $0.0$ or $1.0$ after the completion of every riter iterations.

### Output Parameters

1:     $\mathrm{prob}\left(\mathit{lprob},{\mathbf{ng}}\right)$ – double array
${\mathbf{prob}}\left(i,j\right)$ is the probability of membership of the $i$th object to the $j$th group for the fitted model.
2:     $\mathrm{niter}$int64int32nag_int scalar
Default: $15$
The number of completed iterations.
3:     $\mathrm{w}\left({\mathbf{ng}}\right)$ – double array
${w}_{j}$, the mixing probability for the $j$th group.
4:     $\mathrm{g}\left({\mathbf{nvar}},{\mathbf{ng}}\right)$ – double array
${\mathbf{g}}\left(i,j\right)$ gives the estimated mean of the $i$th variable in the $j$th group.
5:     $\mathrm{s}\left(\mathit{lds},{\mathbf{sds}},:\right)$ – double array
The last dimension of the array s will be ${\mathbf{ng}}$ if ${\mathbf{sopt}}=1$ and $1$ otherwise
If ${\mathbf{sopt}}=1$, ${\mathbf{s}}\left(i,j,k\right)$ gives the $\left(i,j\right)$th element of the $k$th group.
If ${\mathbf{sopt}}=2$, ${\mathbf{s}}\left(i,j,1\right)$ gives the $\left(i,j\right)$th element of the pooled covariance.
If ${\mathbf{sopt}}=3$, ${\mathbf{s}}\left(j,k,1\right)$ gives the $j$th variance in the $k$th group.
If ${\mathbf{sopt}}=4$, ${\mathbf{s}}\left(j,1,1\right)$ gives the $j$th pooled variance.
If ${\mathbf{sopt}}=5$, ${\mathbf{s}}\left(1,1,1\right)$ gives the overall variance.
6:     $\mathrm{f}\left({\mathbf{n}},{\mathbf{ng}}\right)$ – double array
${\mathbf{f}}\left(i,j\right)$ gives the $p$-variate Normal (Gaussian) density of the $i$th object in the $j$th group.
7:     $\mathrm{loglik}$ – double scalar
The log-likelihood for the fitted mixture model.
8:     $\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$
Constraint: ${\mathbf{n}}>p$, the number of parameters, i.e., too few objects have been supplied for the model.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{m}}\ge 1$.
${\mathbf{ifail}}=4$
Constraint: $\mathit{ldx}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=5$
Constraint: $1\le {\mathbf{nvar}}\le {\mathbf{m}}$.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{nvar}}\ne {\mathbf{m}}$ and isx is invalid.
${\mathbf{ifail}}=7$
Constraint: ${\mathbf{ng}}\ge 1$.
${\mathbf{ifail}}=8$
On entry, $\mathit{popt}\ne 1$ or $2$.
${\mathbf{ifail}}=9$
On entry, row $_$ of supplied prob does not sum to $1$.
${\mathbf{ifail}}=10$
Constraint: $\mathit{lprob}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=11$
Constraint: ${\mathbf{niter}}\ge 1$.
${\mathbf{ifail}}=16$
On entry, ${\mathbf{sopt}}<1$ or ${\mathbf{sopt}}>5$.
${\mathbf{ifail}}=18$
On entry, $\mathit{lds}=_$ was invalid.
${\mathbf{ifail}}=19$
On entry, ${\mathbf{sds}}=_$ was invalid.
${\mathbf{ifail}}=44$
A covariance matrix is not positive definite, try a different initial allocation.
${\mathbf{ifail}}=45$
An iteration cannot continue due to an empty group, try a different initial allocation.
${\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.

Not applicable.

None.

## Example

This example fits a Gaussian mixture model with pooled covariance structure to New Haven schools test data, see Table 5.1 (p. 118) in Hartigan (1975).
```function g03ga_example

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

x = [2.7, 3.2, 4.5, 4.8;
3.9, 3.8, 5.9, 6.2;
4.8, 4.1, 6.8, 5.5;
3.1, 3.5, 4.3, 4.6;
3.4, 3.7, 5.1, 5.6;
3.1, 3.4, 4.1, 4.7;
4.6, 4.4, 6.6, 6.1;
3.1, 3.3, 4.0, 4.9;
3.8, 3.7, 4.7, 4.9;
5.2, 4.9, 8.2, 6.9;
3.9, 3.8, 5.2, 5.4;
4.1, 4.0, 5.6, 5.6;
5.7, 5.1, 7.0, 6.3;
3.0, 3.2, 4.5, 5.0;
2.9, 3.3, 4.5, 5.1;
3.4, 3.3, 4.4, 5.0;
4.0, 4.2, 5.2, 5.4;
3.0, 3.0, 4.6, 5.0;
4.0, 4.1, 5.9, 5.8;
3.0, 3.2, 4.4, 5.1;
3.6, 3.6, 5.3, 5.4;
3.1, 3.2, 4.6, 5.0;
3.2, 3.3, 5.4, 5.3;
3.0, 3.4, 4.2, 4.7;
3.8, 4.0, 6.9, 6.7];

[m,n] = size(x);

ng    = int64(2);
prob = zeros(m,ng);
prob(1:12,1) = 1;
prob(13:m,2) = 1;
isx = zeros(n, 1, 'int64');

nvar  = int64(n);
sopt  = int64(2);
sds   = nvar;
tol   = 0;
[prob, niter, w, g, s, f, loglik, ifail] = ...
g03ga( ...
x, isx, nvar, ng, sopt, sds, tol, 'prob', prob);

mtitle = 'Mixing proportions';
matrix = 'General';
diag   = ' ';
[ifail] = x04ca( ...
matrix, diag, w', mtitle);

fprintf('\n');
mtitle = 'Group means';
[ifail] = x04ca( ...
matrix, diag, g, mtitle);

fprintf('\n');
mtitle = 'Pooled Variance-covariance matrix';
[ifail] = x04ca( ...
matrix, diag, s, mtitle);

fprintf('\n');
mtitle = 'Densities';
[ifail] = x04ca( ...
matrix, diag, f, mtitle);

fprintf('\n');
mtitle = 'Membership probabilities';
[ifail] = x04ca( ...
matrix, diag, prob, mtitle);
fprintf('\nNumber of iterations = %5d\n', niter);
fprintf(  'Log-likelihood       = %10.4f\n:', loglik);

```
```g03ga example results

Mixing proportions
1       2
1   0.4798  0.5202

Group means
1          2
1      4.0041     3.3350
2      3.9949     3.4434
3      5.5894     4.9870
4      5.4432     5.3602

Pooled Variance-covariance matrix
1          2          3          4
1      0.4539     0.2891     0.6075     0.3413
2      0.2891     0.2048     0.4101     0.2490
3      0.6075     0.4101     1.0648     0.6011
4      0.3413     0.2490     0.6011     0.3759

Densities
1            2
1    2.5836E-01   1.1853E-02
2    3.7065E-07   1.1241E-01
3    5.3069E-03   1.8080E-06
4    4.2461E-01   2.8584E-05
5    5.0387E-02   1.1544E+00
6    1.1260E+00   7.2224E-02
7    2.0911E+00   2.1224E-02
8    5.7856E-03   1.3227E+00
9    1.1609E+00   2.9411E-02
10    8.9826E-02   2.4260E-05
11    3.0170E-01   1.0106E+00
12    1.2930E+00   3.5422E-01
13    2.8644E-02   6.7851E-07
14    2.0759E-02   3.1690E+00
15    7.6461E-02   1.5231E+00
16    3.0279E-04   8.4017E-01
17    5.6101E-01   4.6699E-05
18    2.6573E-05   6.4442E-01
19    2.1250E+00   5.1006E-02
20    8.6822E-04   2.7626E+00
21    1.9223E-01   2.3971E+00
22    1.2469E-02   2.8179E+00
23    1.8389E-02   5.3572E-01
24    1.2409E+00   9.6489E-03
25    2.1037E-05   4.8674E-02

Membership probabilities
1            2
1    9.5018E-01   4.9823E-02
2    3.3259E-06   1.0000E+00
3    9.9961E-01   3.8664E-04
4    9.9992E-01   7.9913E-05
5    3.8999E-02   9.6100E-01
6    9.3270E-01   6.7295E-02
7    9.8881E-01   1.1190E-02
8    4.1252E-03   9.9587E-01
9    9.7252E-01   2.7479E-02
10    9.9969E-01   3.0805E-04
11    2.1722E-01   7.8278E-01
12    7.6938E-01   2.3062E-01
13    9.9997E-01   2.6937E-05
14    6.1133E-03   9.9389E-01
15    4.4189E-02   9.5581E-01
16    3.5006E-04   9.9965E-01
17    9.9990E-01   9.7029E-05
18    4.0270E-05   9.9996E-01
19    9.7380E-01   2.6202E-02
20    3.0204E-04   9.9970E-01
21    6.9471E-02   9.3053E-01
22    4.1603E-03   9.9584E-01
23    3.0839E-02   9.6916E-01
24    9.9116E-01   8.8421E-03
25    4.1534E-04   9.9958E-01

Number of iterations =    14
Log-likelihood       =   -29.6831
:```