JWS C Library
C language utility library
gmm.c File Reference

Gaussian mixture model and parameter fitting using EM. More...

#include <jwsc/config.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <inttypes.h>
#include "jwsc/base/limits.h"
#include "jwsc/vector/vector.h"
#include "jwsc/vector/vector_math.h"
#include "jwsc/matrix/matrix.h"
#include "jwsc/matrix/matrix_math.h"
#include "jwsc/matblock/matblock.h"
#include "jwsc/prob/mv_pdf.h"
#include "jwsc/stat/stat.h"
#include "jwsc/stat/kmeans.h"
#include "jwsc/stat/gmm.h"
Include dependency graph for gmm.c:

Go to the source code of this file.

Functions

static void ensure_non_singular_covariance_d (Matrix_d **m, double min)
 Sets minimum eigenvalues of the matrix to non-negative to ensure matrix non-singularity.
static void gmm_e_step_d (const Matrix_d *mu, const Matblock_d *sigma, const Vector_d *pi, Matrix_d *gamma, const Matrix_d *x)
 E-step in the EM algorithm.
static void gmm_m_step_d (Matrix_d *mu, Matblock_d *sigma, Vector_d *pi, const Matrix_d *gamma, const Matrix_d *x)
 M-step in the EM algorithm.
double gmm_log_likelihood_d (const Matrix_d *mu, const Matblock_d *sigma, const Vector_d *pi, const Matrix_d *x)
 Computes the double precision Gaussian mixture model log-likelihood.
void gmm_kmeans_init_d (Matrix_d **mu_out, Matblock_d **sigma_out, Vector_d **pi_out, const Matrix_d *x, uint32_t K)
 Initializes the double precision Gaussian mixture model means, covariances, and mixture weights using the kmeans clustering algorithm.
void gmm_stats_init_d (Matrix_d **mu_out, Matblock_d **sigma_out, Vector_d **pi_out, const Matrix_d *x, uint32_t K)
 Initializes the double precision Gaussian mixture model means, covariances, and mixture weights with sample statistics.
void train_gmm_d (Matrix_d **mu_out, Matblock_d **sigma_out, Vector_d **pi_out, Matrix_d **gamma_out, const Matrix_d *x, uint32_t K)
 Trains a double precision Gaussian mixture model.

Detailed Description

Gaussian mixture model and parameter fitting using EM.

Author:
Joseph Schlecht
License:
Creative Commons BY-NC-SA 3.0

Definition in file gmm.c.


Function Documentation

static void ensure_non_singular_covariance_d ( Matrix_d **  m,
double  min 
) [static]

Sets minimum eigenvalues of the matrix to non-negative to ensure matrix non-singularity.

Definition at line 70 of file gmm.c.

static void gmm_e_step_d ( const Matrix_d mu,
const Matblock_d sigma,
const Vector_d pi,
Matrix_d gamma,
const Matrix_d x 
) [static]

E-step in the EM algorithm.

Definition at line 118 of file gmm.c.

static void gmm_m_step_d ( Matrix_d mu,
Matblock_d sigma,
Vector_d pi,
const Matrix_d gamma,
const Matrix_d x 
) [static]

M-step in the EM algorithm.

Definition at line 177 of file gmm.c.

double gmm_log_likelihood_d ( const Matrix_d mu,
const Matblock_d sigma,
const Vector_d pi,
const Matrix_d x 
)

Computes the double precision Gaussian mixture model log-likelihood.

Parameters:
muMultivariate Gaussian means.
sigmaMultivariate Gaussian covariance matrices.
piMixture component weights.
xData.

Definition at line 263 of file gmm.c.

void gmm_kmeans_init_d ( Matrix_d **  mu_out,
Matblock_d **  sigma_out,
Vector_d **  pi_out,
const Matrix_d x,
uint32_t  K 
)

Initializes the double precision Gaussian mixture model means, covariances, and mixture weights using the kmeans clustering algorithm.

If one of the arguments is already allocated, it is assumed to be initialized as well.

Minimum cluster membership is 2 samples.

Parameters:
mu_outMultivariate Gaussian means.
sigma_outMultivariate Gaussian Covariance matrices.
pi_outMixture component weights.
xData.
KNumber of mixture components.

Definition at line 332 of file gmm.c.

void gmm_stats_init_d ( Matrix_d **  mu_out,
Matblock_d **  sigma_out,
Vector_d **  pi_out,
const Matrix_d x,
uint32_t  K 
)

Initializes the double precision Gaussian mixture model means, covariances, and mixture weights with sample statistics.

All cluster covariance matrices are set to the sample covariance. One mean is set from the sample mean, and the rest are random samples from a Gaussian using the sample mean and covariance matrix.

If one of the arguments is already allocated, it is assumed to be initialized as well.

Parameters:
mu_outMultivariate Gaussian means.
sigma_outMultivariate Gaussian covariance matrices.
pi_outMixture component weights.
xData.
KNumber of mixture components.

Definition at line 414 of file gmm.c.

void train_gmm_d ( Matrix_d **  mu_out,
Matblock_d **  sigma_out,
Vector_d **  pi_out,
Matrix_d **  gamma_out,
const Matrix_d x,
uint32_t  K 
)

Trains a double precision Gaussian mixture model.

If *mu_out, *sigma_out and *pi_out are not initialized, gmm_kmeans_init_d() is used to do so.

Parameters:
mu_outResult parameter. Means for each mixture component.
sigma_outResult parameter. Covariance matrices for each mixture component.
pi_outResult parameter. Mixture component weights.
gamma_outOptional result parameter. Responsibilities of each mixture component per sample. If NULL, the variable is ignored.
xData to train the GMM with.
KNumber of mixture components.

Definition at line 506 of file gmm.c.