NAG Library Routine Document

g03gaf (gaussian_mixture)


    1  Purpose
    7  Accuracy


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


Fortran Interface
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, 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
C Header Interface
#include nagmk26.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)


A Normal (Gaussian) mixture model is a weighted sum of k group Normal densities given by,
p xw,μ,Σ = j=1 k wj g xμj,Σj ,  xp  
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


1:     n – IntegerInput
On entry: n, the number of objects. There must be more objects than parameters in the model.
  • if sopt=1, n>ng× nvar×nvar+nvar;
  • if sopt=2, n>nvar× ng+nvar ;
  • if sopt=3, n>2×ng×nvar ;
  • if sopt=4, n>nvar× ng+1 ;
  • if sopt=5, n>nvar×ng+1 .
2:     m – IntegerInput
On entry: the total number of variables in array x.
Constraint: m1.
3:     xldxm – Real (Kind=nag_wp) arrayInput
On entry: xij must contain the value of the jth variable for the ith object, for i=1,2,,n and j=1,2,,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: ldxn.
5:     isxm – Integer arrayInput
On entry: if nvar=m all available variables are included in the model and isx is not referenced; otherwise the jth variable will be included in the analysis if isxj=1 and excluded if isxj=0, for j=1,2,,m.
Constraint: if nvarm, isxj=1 for nvar values of j and isxj=0 for the remaining m-nvar values of j, for j=1,2,,m.
6:     nvar – IntegerInput
On entry: p, the number of variables included in the calculations.
Constraint: 1nvarm.
7:     ng – IntegerInput
On entry: k, the number of groups in the mixture model.
Constraint: ng1.
8:     popt – IntegerInput
On entry: if popt=1, the initial membership probabilities in prob are set internally; otherwise these probabilities must be supplied.
9:     problprobng – Real (Kind=nag_wp) arrayInput/Output
On entry: if popt1, probij is the probability that the ith object belongs to the jth group. (These probabilities are normalised internally.)
On exit: probij is the probability of membership of the ith object to the jth group for the fitted model.
10:   lprob – IntegerInput
On entry: the first dimension of the array prob as declared in the (sub)program from which g03gaf is called.
Constraint: lprobn.
11:   niter – IntegerInput/Output
On entry: the maximum number of iterations.
Suggested value: 15
On exit: the number of completed iterations.
Constraint: niter1.
12:   riter – IntegerInput
On entry: if riter>0, membership probabilities are rounded to 0.0 or 1.0 after the completion of every riter iterations.
Suggested value: 5
13:   wng – Real (Kind=nag_wp) arrayOutput
On exit: wj, the mixing probability for the jth group.
14:   gnvarng – Real (Kind=nag_wp) arrayOutput
On exit: gij gives the estimated mean of the ith variable in the jth group.
15:   sopt – IntegerInput
On entry: determines the (co)variance structure:
Groupwise covariance matrices.
Pooled covariance matrix.
Groupwise variances.
Pooled variances.
Overall variance.
Constraint: sopt=1, 2, 3, 4 or 5.
16:   sldssds* – Real (Kind=nag_wp) arrayOutput
Note: the last dimension of the array s must be at least ng if sopt=1, and at least 1 otherwise.
On exit: if sopt=1, sijk gives the i,jth element of the kth group.
If sopt=2, sij1 gives the i,jth element of the pooled covariance.
If sopt=3, sjk1 gives the jth variance in the kth group.
If sopt=4, sj11 gives the jth pooled variance.
If sopt=5, s111 gives the overall variance.
17:   lds – IntegerInput
On entry: the first dimension of the (co)variance structure s.
  • if sopt=5, lds=1;
  • otherwise lds=nvar.
18:   sds – IntegerInput
On entry: the second dimension of the (co)variance structure s.
  • if sopt=1 or 2, sdsnvar;
  • if sopt=3, sdsng;
  • if sopt=4 or 5, sds1.
19:   fnng – Real (Kind=nag_wp) arrayOutput
On exit: fij gives the p-variate Normal (Gaussian) density of the ith object in the jth 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 tol0 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​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ 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 argument, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

Error Indicators and Warnings

If on entry 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:
On entry, n=value and p=value.
Constraint: n>p, the number of parameters, i.e., too few objects have been supplied for the model.
On entry, m=value.
Constraint: m1.
On entry, ldx=value and n=value.
Constraint: ldxn.
On entry, nvar=value and m=value.
Constraint: 1nvarm.
On entry, nvarm and isx is invalid.
On entry, ng=value.
Constraint: ng1.
On entry, popt1 or 2.
On entry, row value of supplied prob does not sum to 1.
On entry, lprob=value and n=value.
Constraint: lprobn.
On entry, niter=value.
Constraint: niter1.
On entry, sopt<1 or sopt>5.
On entry, lds=value was invalid.
On entry, sds=value was invalid.
A covariance matrix is not positive definite, try a different initial allocation.
An iteration cannot continue due to an empty group, try a different initial allocation.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.


Not applicable.

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.

Further Comments



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

Program Text

Program Text (g03gafe.f90)

Program Data

Program Data (g03gafe.d)

Program Results

Program Results (g03gafe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017