G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG03GAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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

## 2  Specification

 SUBROUTINE G03GAF ( N, M, X, LDX, ISX, NVAR, NG, POPT, PROB, LDPROB, NITER, RITER, W, G, SOPT, S, LDS, SDS, F, TOL, LOGLIK, IFAIL)
 INTEGER N, M, LDX, ISX(M), NVAR, NG, POPT, LDPROB, NITER, RITER, SOPT, LDS, SDS, IFAIL REAL (KIND=nag_wp) X(LDX,M), PROB(LDPROB,NG), W(NG), G(NVAR,NG), S(LDS,SDS,*), F(N,NG), TOL, LOGLIK

## 3  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 $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 covariance structure may be pooled (common to all groups) or calculated for each group, and may be full or diagonal.

## 4  References

Hartigan J A (1975) Clustering Algorithms Wiley

## 5  Parameters

1:     N – INTEGERInput
On entry: $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:     M – INTEGERInput
On entry: the total number of variables in array X.
Constraint: ${\mathbf{M}}\ge 1$.
3:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\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 ,{\mathbf{N}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
4:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03GAF is called.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
5:     ISX(M) – INTEGER arrayInput
On entry: 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}}$.
Constraints:
• if ${\mathbf{NVAR}}\ne {\mathbf{M}}$, ${\mathbf{ISX}}\left(j\right)=1$ for NVAR values of $j$;
• otherwise $0$.
6:     NVAR – INTEGERInput
On entry: $p$, the number of variables included in the calculations.
Constraint: $1\le {\mathbf{NVAR}}\le {\mathbf{M}}$.
7:     NG – INTEGERInput
On entry: $k$, the number of groups in the mixture model.
Constraint: ${\mathbf{NG}}\ge 1$.
8:     POPT – INTEGERInput
On entry: if ${\mathbf{POPT}}=1$, the initial membership probabilities in PROB are set internally; otherwise these probabilities must be supplied.
9:     PROB(LDPROB,NG) – REAL (KIND=nag_wp) arrayInput/Output
On entry: if ${\mathbf{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.)
On exit: ${\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.
10:   LDPROB – INTEGERInput
On entry: the first dimension of the array PROB as declared in the (sub)program from which G03GAF is called.
Constraint: ${\mathbf{LDPROB}}\ge {\mathbf{N}}$.
11:   NITER – INTEGERInput/Output
On entry: the maximum number of iterations.
Suggested value: $15$
On exit: the number of completed iterations.
Constraint: ${\mathbf{NITER}}\ge 1$.
12:   RITER – INTEGERInput
On entry: if ${\mathbf{RITER}}>0$, membership probabilities are rounded to $0.0$ or $1.0$ after the completion of every RITER iterations.
Suggested value: $5$
13:   W(NG) – REAL (KIND=nag_wp) arrayOutput
On exit: ${w}_{j}$, the mixing probability for the $j$th group.
14:   G(NVAR,NG) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{G}}\left(i,j\right)$ gives the estimated mean of the $i$th variable in the $j$th group.
15:   SOPT – INTEGERInput
On entry: 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$.
16:   S(LDS,SDS,$*$) – REAL (KIND=nag_wp) arrayOutput
Note: the last dimension of the array S must be at least ${\mathbf{NG}}$ if ${\mathbf{SOPT}}=1$, and at least $1$ otherwise.
On exit: 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.
17:   LDS – INTEGERInput
On entry: the first dimension of the (co)variance structure S.
Constraints:
• if ${\mathbf{SOPT}}=5$, ${\mathbf{LDS}}=1$;
• otherwise ${\mathbf{LDS}}={\mathbf{NVAR}}$.
18:   SDS – INTEGERInput
On entry: 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$.
19:   F(N,NG) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{F}}\left(i,j\right)$ gives the $p$-variate Normal (Gaussian) density of the $i$th object in the $j$th group.
20:   TOL – REAL (KIND=nag_wp)Input
On entry: 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.
21:   LOGLIK – REAL (KIND=nag_wp)Output
On exit: the log-likelihood for the fitted mixture model.
22:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
On entry, ${\mathbf{N}}=⟨\mathit{\text{value}}⟩$ and $p=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{N}}>p$, the number of parameters, i.e., too few objects have been supplied for the model.
${\mathbf{IFAIL}}=2$
On entry, ${\mathbf{M}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{M}}\ge 1$.
${\mathbf{IFAIL}}=4$
On entry, ${\mathbf{LDX}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{N}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
${\mathbf{IFAIL}}=5$
On entry, ${\mathbf{NVAR}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{M}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{NVAR}}\ge 1$ and ${\mathbf{NVAR}}\le {\mathbf{M}}$.
${\mathbf{IFAIL}}=6$
On entry, ${\mathbf{NVAR}}\ne {\mathbf{M}}$ and ISX is invalid.
${\mathbf{IFAIL}}=7$
On entry, ${\mathbf{NG}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{NG}}\ge 1$.
${\mathbf{IFAIL}}=8$
On entry, POPT is neither $1$ or $2$.
${\mathbf{IFAIL}}=9$
On entry, row $k$ of supplied PROB does not sum to $1$: $k=⟨\mathit{\text{value}}⟩$.
${\mathbf{IFAIL}}=10$
On entry, ${\mathbf{LDPROB}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{N}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{LDPROB}}\ge {\mathbf{N}}$.
${\mathbf{IFAIL}}=11$
On entry, ${\mathbf{NITER}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{NITER}}\ge 1$.
${\mathbf{IFAIL}}=16$
On entry, ${\mathbf{SOPT}}<1$ or ${\mathbf{SOPT}}>5$.
${\mathbf{IFAIL}}=18$
On entry, ${\mathbf{LDS}}=⟨\mathit{\text{value}}⟩$ was invalid.
${\mathbf{IFAIL}}=19$
On entry, ${\mathbf{SDS}}=⟨\mathit{\text{value}}⟩$ 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}}=-999$
Dynamic memory allocation failed.

Not applicable.

None.

## 9  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).

### 9.1  Program Text

Program Text (g03gafe.f90)

### 9.2  Program Data

Program Data (g03gafe.d)

### 9.3  Program Results

Program Results (g03gafe.r)