NAG FL Interface
g03gaf (gaussian_​mixture)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

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

2 Specification

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

3 Description

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  
where:
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 Arguments

1: n Integer Input
On entry: n, the number of objects. There must be more objects than parameters in the model.
Constraints:
  • 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 Integer Input
On entry: the total number of variables in array x.
Constraint: m1.
3: x(ldx,m) Real (Kind=nag_wp) array Input
On entry: x(i,j) must contain the value of the jth variable for the ith object, for i=1,2,,n and j=1,2,,m.
4: ldx Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g03gaf is called.
Constraint: ldxn.
5: isx(m) Integer array Input
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 isx(j)=1 and excluded if isx(j)=0, for j=1,2,,m.
Constraint: if nvarm, isx(j)=1 for nvar values of j and isx(j)=0 for the remaining m-nvar values of j, for j=1,2,,m.
6: nvar Integer Input
On entry: p, the number of variables included in the calculations.
Constraint: 1nvarm.
7: ng Integer Input
On entry: k, the number of groups in the mixture model.
Constraint: ng1.
8: popt Integer Input
On entry: if popt=1, the initial membership probabilities in prob are set internally; otherwise these probabilities must be supplied.
9: prob(lprob,ng) Real (Kind=nag_wp) array Input/Output
On entry: if popt1, prob(i,j) is the probability that the ith object belongs to the jth group. (These probabilities are normalised internally.)
On exit: prob(i,j) is the probability of membership of the ith object to the jth group for the fitted model.
10: lprob Integer Input
On entry: the first dimension of the array prob as declared in the (sub)program from which g03gaf is called.
Constraint: lprobn.
11: niter Integer Input/Output
On entry: the maximum number of iterations.
Suggested value: 15
On exit: the number of completed iterations.
Constraint: niter1.
12: riter Integer Input
On entry: if riter>0, membership probabilities are rounded to 0.0 or 1.0 after the completion of the first riter iterations.
Suggested value: 0
13: w(ng) Real (Kind=nag_wp) array Output
On exit: wj, the mixing probability for the jth group.
14: g(nvar,ng) Real (Kind=nag_wp) array Output
On exit: g(i,j) gives the estimated mean of the ith variable in the jth group.
15: sopt Integer Input
On entry: determines the (co)variance structure:
sopt=1
Groupwise covariance matrices.
sopt=2
Pooled covariance matrix.
sopt=3
Groupwise variances.
sopt=4
Pooled variances.
sopt=5
Overall variance.
Constraint: sopt=1, 2, 3, 4 or 5.
16: s(lds,sds,*) Real (Kind=nag_wp) array Output
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, s(i,j,k) gives the (i,j)th element of the kth group.
If sopt=2, s(i,j,1) gives the (i,j)th element of the pooled covariance.
If sopt=3, s(j,k,1) gives the jth variance in the kth group.
If sopt=4, s(j,1,1) gives the jth pooled variance.
If sopt=5, s(1,1,1) gives the overall variance.
17: lds Integer Input
On entry: the first dimension of the (co)variance structure s.
Constraints:
  • if sopt=5, lds=1;
  • otherwise lds=nvar.
18: sds Integer Input
On entry: the second dimension of the (co)variance structure s.
Constraints:
  • if sopt=1 or 2, sdsnvar;
  • if sopt=3, sdsng;
  • if sopt=4 or 5, sds1.
19: f(n,ng) Real (Kind=nag_wp) array Output
On exit: f(i,j) 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 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 -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).

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:
ifail=1
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.
ifail=2
On entry, m=value.
Constraint: m1.
ifail=4
On entry, ldx=value and n=value.
Constraint: ldxn.
ifail=5
On entry, nvar=value and m=value.
Constraint: 1nvarm.
ifail=6
On entry, nvarm and isx is invalid.
ifail=7
On entry, ng=value.
Constraint: ng1.
ifail=8
On entry, popt1 or 2.
ifail=9
On entry, row value of supplied prob does not sum to 1.
ifail=10
On entry, lprob=value and n=value.
Constraint: lprobn.
ifail=11
On entry, niter=value.
Constraint: niter1.
ifail=16
On entry, sopt1, 2, 3, 4 or 5.
ifail=18
On entry, lds=value was invalid.
ifail=19
On entry, sds=value was invalid.
ifail=44
A covariance matrix is not positive definite, try a different initial allocation.
ifail=45
An iteration cannot continue due to an empty group, try a different initial allocation.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
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.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

Not applicable.

8 Parallelism and Performance

Background information to multithreading can be found in the Multithreading documentation.
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.

9 Further Comments

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)