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$k$ group Normal densities given by,
 k p(x ∣ w,μ,Σ) = ∑ wjg(x ∣ μj,Σj), x ∈ ℝp j = 1
$p (x∣w,μ,Σ) = ∑ j=1 k wj g (x∣μj,Σj) , x∈ℝp$
where:
• x$x$ is a p$p$-dimensional object of interest;
• wj${w}_{j}$ is the mixture weight for the j$j$th group and j = 1k wj = 1$\sum _{\mathit{j}=1}^{k}{w}_{j}=1$;
• μj${\mu }_{j}$ is p$p$-dimensional vector of means for the j$j$th group;
• Σj${\Sigma }_{j}$ is the covariance structure for the j$j$th group;
• g( · )$g\left(·\right)$ is the p$p$-variate Normal density:
 g (x ∣ μj,Σj) = 1/( (2π)p / 2 |Σj|1 / 2 ) exp[ − (1/2)(x − μj)Σj − 1(x − μj)T] . $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:     x(ldx,m) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
x(i,j)${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of the j$\mathit{j}$th variable for the i$\mathit{i}$th object, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$ and j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
2:     isx(m) – int64int32nag_int array
m, the dimension of the array, must satisfy the constraint m1${\mathbf{m}}\ge 1$.
If ${\mathbf{nvar}}={\mathbf{m}}$ all available variables are included in the model and isx is not referenced; otherwise the j$j$th variable will be included in the analysis if isx(j) = 1${\mathbf{isx}}\left(\mathit{j}\right)=1$ and excluded if isx(j) = 0${\mathbf{isx}}\left(\mathit{j}\right)=0$, for j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
Constraints:
• if ${\mathbf{nvar}}\ne {\mathbf{m}}$, isx(j) = 1${\mathbf{isx}}\left(j\right)=1$ for nvar values of j$j$;
• otherwise 0$0$.
3:     nvar – int64int32nag_int scalar
p$p$, the number of variables included in the calculations.
Constraint: 1nvarm$1\le {\mathbf{nvar}}\le {\mathbf{m}}$.
4:     ng – int64int32nag_int scalar
k$k$, the number of groups in the mixture model.
Constraint: ng1${\mathbf{ng}}\ge 1$.
5:     sopt – int64int32nag_int scalar
Determines the (co)variance structure:
sopt = 1${\mathbf{sopt}}=1$
Groupwise covariance matrices.
sopt = 2${\mathbf{sopt}}=2$
Pooled covariance matrix.
sopt = 3${\mathbf{sopt}}=3$
Groupwise variances.
sopt = 4${\mathbf{sopt}}=4$
Pooled variances.
sopt = 5${\mathbf{sopt}}=5$
Overall variance.
Constraint: sopt = 1${\mathbf{sopt}}=1$, 2$2$, 3$3$, 4$4$ or 5$5$.
6:     sds – int64int32nag_int scalar
The second dimension of the (co)variance structure s.
Constraints:
• if sopt = 1${\mathbf{sopt}}=1$ or 2$2$, sds must be at least nvar;
• if sopt = 3${\mathbf{sopt}}=3$, sds must be at least ng;
• if sopt = 4${\mathbf{sopt}}=4$ or 5$5$, sds must be at least 1$1$.
7:     tol – double scalar
Iterations cease the first time an improvement in log-likelihood is less than tol. If tol0${\mathbf{tol}}\le 0$ a value of 103${10}^{-3}$ is used.

### Optional Input Parameters

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

### Input Parameters Omitted from the MATLAB Interface

ldx popt lprob lds

### Output Parameters

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

Not applicable.

None.

## Example

```function nag_mv_gaussian_mixture_example
nvar  = int64(4);
sds   = int64(4);
sopt  = int64(2);
tol   = 0;
ng    = int64(2);

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];
prob = [1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1];
isx = zeros(4, 1, 'int64');

[prob, niter, w, g, s, f, loglik, ifail] = ...
nag_mv_gaussian_mixture(x, isx, nvar, ng, sopt, sds, tol, 'prob', prob)
```
```

prob =

0.9502    0.0498
0.0000    1.0000
0.9996    0.0004
0.9999    0.0001
0.0390    0.9610
0.9327    0.0673
0.9888    0.0112
0.0041    0.9959
0.9725    0.0275
0.9997    0.0003
0.2172    0.7828
0.7694    0.2306
1.0000    0.0000
0.0061    0.9939
0.0442    0.9558
0.0004    0.9996
0.9999    0.0001
0.0000    1.0000
0.9738    0.0262
0.0003    0.9997
0.0695    0.9305
0.0042    0.9958
0.0308    0.9692
0.9912    0.0088
0.0004    0.9996

niter =

14

w =

0.4798
0.5202

g =

4.0041    3.3350
3.9949    3.4434
5.5894    4.9870
5.4432    5.3602

s =

0.4539    0.2891    0.6075    0.3413
0.2891    0.2048    0.4101    0.2490
0.6075    0.4101    1.0648    0.6011
0.3413    0.2490    0.6011    0.3759

f =

0.2584    0.0119
0.0000    0.1124
0.0053    0.0000
0.4246    0.0000
0.0504    1.1544
1.1260    0.0722
2.0911    0.0212
0.0058    1.3227
1.1609    0.0294
0.0898    0.0000
0.3017    1.0106
1.2930    0.3542
0.0286    0.0000
0.0208    3.1690
0.0765    1.5231
0.0003    0.8402
0.5610    0.0000
0.0000    0.6444
2.1250    0.0510
0.0009    2.7626
0.1922    2.3971
0.0125    2.8179
0.0184    0.5357
1.2409    0.0096
0.0000    0.0487

loglik =

-29.6831

ifail =

0

```
```function g03ga_example
nvar  = int64(4);
sds   = int64(4);
sopt  = int64(2);
tol   = 0;
ng    = int64(2);

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];
prob = [1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
1, 0;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1;
0, 1];
isx = zeros(4, 1, 'int64');

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

prob =

0.9502    0.0498
0.0000    1.0000
0.9996    0.0004
0.9999    0.0001
0.0390    0.9610
0.9327    0.0673
0.9888    0.0112
0.0041    0.9959
0.9725    0.0275
0.9997    0.0003
0.2172    0.7828
0.7694    0.2306
1.0000    0.0000
0.0061    0.9939
0.0442    0.9558
0.0004    0.9996
0.9999    0.0001
0.0000    1.0000
0.9738    0.0262
0.0003    0.9997
0.0695    0.9305
0.0042    0.9958
0.0308    0.9692
0.9912    0.0088
0.0004    0.9996

niter =

14

w =

0.4798
0.5202

g =

4.0041    3.3350
3.9949    3.4434
5.5894    4.9870
5.4432    5.3602

s =

0.4539    0.2891    0.6075    0.3413
0.2891    0.2048    0.4101    0.2490
0.6075    0.4101    1.0648    0.6011
0.3413    0.2490    0.6011    0.3759

f =

0.2584    0.0119
0.0000    0.1124
0.0053    0.0000
0.4246    0.0000
0.0504    1.1544
1.1260    0.0722
2.0911    0.0212
0.0058    1.3227
1.1609    0.0294
0.0898    0.0000
0.3017    1.0106
1.2930    0.3542
0.0286    0.0000
0.0208    3.1690
0.0765    1.5231
0.0003    0.8402
0.5610    0.0000
0.0000    0.6444
2.1250    0.0510
0.0009    2.7626
0.1922    2.3971
0.0125    2.8179
0.0184    0.5357
1.2409    0.0096
0.0000    0.0487

loglik =

-29.6831

ifail =

0

```