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, LPROB, NITER, RITER, W, G, SOPT, S, LDS, SDS, F, TOL, LOGLIK, IFAIL)
 INTEGER N, M, LDX, ISX(M), NVAR, NG, POPT, LPROB, NITER, RITER, SOPT, LDS, SDS, IFAIL REAL (KIND=nag_wp) X(LDX,M), PROB(LPROB,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 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.

## 4  References

Hartigan J A (1975) Clustering Algorithms Wiley

## 5  Parameters

1:     $\mathrm{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:     $\mathrm{M}$ – INTEGERInput
On entry: the total number of variables in array X.
Constraint: ${\mathbf{M}}\ge 1$.
3:     $\mathrm{X}\left({\mathbf{LDX}},{\mathbf{M}}\right)$ – 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 ,n$ and $\mathit{j}=1,2,\dots ,{\mathbf{M}}$.
4:     $\mathrm{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:     $\mathrm{ISX}\left({\mathbf{M}}\right)$ – 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}}$.
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}}$.
6:     $\mathrm{NVAR}$ – INTEGERInput
On entry: $p$, the number of variables included in the calculations.
Constraint: $1\le {\mathbf{NVAR}}\le {\mathbf{M}}$.
7:     $\mathrm{NG}$ – INTEGERInput
On entry: $k$, the number of groups in the mixture model.
Constraint: ${\mathbf{NG}}\ge 1$.
8:     $\mathrm{POPT}$ – INTEGERInput
On entry: if ${\mathbf{POPT}}=1$, the initial membership probabilities in PROB are set internally; otherwise these probabilities must be supplied.
9:     $\mathrm{PROB}\left({\mathbf{LPROB}},{\mathbf{NG}}\right)$ – 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:   $\mathrm{LPROB}$ – INTEGERInput
On entry: the first dimension of the array PROB as declared in the (sub)program from which G03GAF is called.
Constraint: ${\mathbf{LPROB}}\ge {\mathbf{N}}$.
11:   $\mathrm{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:   $\mathrm{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:   $\mathrm{W}\left({\mathbf{NG}}\right)$ – REAL (KIND=nag_wp) arrayOutput
On exit: ${w}_{j}$, the mixing probability for the $j$th group.
14:   $\mathrm{G}\left({\mathbf{NVAR}},{\mathbf{NG}}\right)$ – 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:   $\mathrm{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:   $\mathrm{S}\left({\mathbf{LDS}},{\mathbf{SDS}},*\right)$ – 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:   $\mathrm{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:   $\mathrm{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:   $\mathrm{F}\left({\mathbf{N}},{\mathbf{NG}}\right)$ – 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:   $\mathrm{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:   $\mathrm{LOGLIK}$ – REAL (KIND=nag_wp)Output
On exit: the log-likelihood for the fitted mixture model.
22:   $\mathrm{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: $1\le {\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, ${\mathbf{POPT}}\ne 1$ or $2$.
${\mathbf{IFAIL}}=9$
On entry, row $〈\mathit{\text{value}}〉$ of supplied PROB does not sum to $1$.
${\mathbf{IFAIL}}=10$
On entry, ${\mathbf{LPROB}}=〈\mathit{\text{value}}〉$ and ${\mathbf{N}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{LPROB}}\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}}=-99$
See Section 3.8 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

Not applicable.

## 8  Parallelism and Performance

G03GAF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
G03GAF makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

None.

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

### 10.1  Program Text

Program Text (g03gafe.f90)

### 10.2  Program Data

Program Data (g03gafe.d)

### 10.3  Program Results

Program Results (g03gafe.r)