# NAG FL Interfaceg03gaf (gaussian_​mixture)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine g03gaf ( n, m, x, ldx, isx, nvar, ng, popt, prob, w, g, sopt, s, lds, sds, f, tol,
 Integer, Intent (In) :: n, m, ldx, isx(m), nvar, ng, popt, lprob, riter, sopt, lds, sds Integer, Intent (Inout) :: niter, ifail Real (Kind=nag_wp), Intent (In) :: x(ldx,m), tol Real (Kind=nag_wp), Intent (Inout) :: prob(lprob,ng), g(nvar,ng), s(lds,sds,*), f(n,ng) Real (Kind=nag_wp), Intent (Out) :: w(ng), loglik
#include <nag.h>
 void g03gaf_ (const Integer *n, const Integer *m, const double x[], const Integer *ldx, const Integer isx[], const Integer *nvar, const Integer *ng, const Integer *popt, double prob[], const Integer *lprob, Integer *niter, const Integer *riter, double w[], double g[], const Integer *sopt, double s[], const Integer *lds, const Integer *sds, double f[], const double *tol, double *loglik, Integer *ifail)
The routine may be called by the names g03gaf or nagf_mv_gaussian_mixture.

## 3Description

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.
Hartigan J A (1975) Clustering Algorithms Wiley

## 5Arguments

1: $\mathbf{n}$Integer Input
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: $\mathbf{m}$Integer Input
On entry: the total number of variables in array x.
Constraint: ${\mathbf{m}}\ge 1$.
3: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
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: $\mathbf{ldx}$Integer Input
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: $\mathbf{isx}\left({\mathbf{m}}\right)$Integer array Input
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: $\mathbf{nvar}$Integer Input
On entry: $p$, the number of variables included in the calculations.
Constraint: $1\le {\mathbf{nvar}}\le {\mathbf{m}}$.
7: $\mathbf{ng}$Integer Input
On entry: $k$, the number of groups in the mixture model.
Constraint: ${\mathbf{ng}}\ge 1$.
8: $\mathbf{popt}$Integer Input
On entry: if ${\mathbf{popt}}=1$, the initial membership probabilities in prob are set internally; otherwise these probabilities must be supplied.
9: $\mathbf{prob}\left({\mathbf{lprob}},{\mathbf{ng}}\right)$Real (Kind=nag_wp) array Input/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: $\mathbf{lprob}$Integer Input
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: $\mathbf{niter}$Integer Input/Output
On entry: the maximum number of iterations.
Suggested value: $15$
On exit: the number of completed iterations.
Constraint: ${\mathbf{niter}}\ge 1$.
12: $\mathbf{riter}$Integer Input
On entry: if ${\mathbf{riter}}>0$, membership probabilities are rounded to $0.0$ or $1.0$ after the completion of the first riter iterations.
Suggested value: $0$
13: $\mathbf{w}\left({\mathbf{ng}}\right)$Real (Kind=nag_wp) array Output
On exit: ${w}_{j}$, the mixing probability for the $j$th group.
14: $\mathbf{g}\left({\mathbf{nvar}},{\mathbf{ng}}\right)$Real (Kind=nag_wp) array Output
On exit: ${\mathbf{g}}\left(i,j\right)$ gives the estimated mean of the $i$th variable in the $j$th group.
15: $\mathbf{sopt}$Integer Input
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: $\mathbf{s}\left({\mathbf{lds}},{\mathbf{sds}},*\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{lds}$Integer Input
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: $\mathbf{sds}$Integer Input
On entry: the second dimension of the (co)variance structure s.
Constraints:
• if ${\mathbf{sopt}}=1$ or $2$, ${\mathbf{sds}}\ge {\mathbf{nvar}}$;
• if ${\mathbf{sopt}}=3$, ${\mathbf{sds}}\ge {\mathbf{ng}}$;
• if ${\mathbf{sopt}}=4$ or $5$, ${\mathbf{sds}}\ge 1$.
19: $\mathbf{f}\left({\mathbf{n}},{\mathbf{ng}}\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{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: $\mathbf{loglik}$Real (Kind=nag_wp) Output
On exit: the log-likelihood for the fitted mixture model.
22: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-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}}\ne 1$, $2$, $3$, $4$ or $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 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

Not applicable.

## 8Parallelism 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.

## 10Example

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.1Program Text

Program Text (g03gafe.f90)

### 10.2Program Data

Program Data (g03gafe.d)

### 10.3Program Results

Program Results (g03gafe.r)