Skip to contents

Mirrors the C# GaussianMixtureModel class: fits a mixture of k multivariate normals by expectation-maximization, initialized from a k-means fit. It generalizes ml_kmeans() by carrying a full covariance per component rather than only a centre.

Usage

ml_gaussian_mixture(
  x,
  k,
  seed = NULL,
  kmeans_plus_plus = TRUE,
  max_iterations = 1000,
  tolerance = 1e-08
)

Arguments

x

a numeric matrix or data frame with one row per observation, or a numeric vector.

k

the number of mixture components.

seed

integer PRNG seed; NULL (the default) uses the computer clock.

kmeans_plus_plus

use k-means++ initialization for the starting fit. Default TRUE.

max_iterations

the EM iteration cap. Default 1000.

tolerance

the relative convergence tolerance on the log-likelihood. Default 1e-8.

Value

a list with means (k by ncol(x)), sigmas (a length-k list of ncol(x) by ncol(x) covariance matrices), weights, labels (0-based), log_likelihood, and iterations.

Details

labels are 0-BASED, as in ml_kmeans(). log_likelihood follows the library's own definition, which OMITS the multivariate-normal normalizing constant -0.5 * ncol(x) * log(2 * pi) per observation – so it is short of the true mixture log-likelihood by nrow(x) * ncol(x) / 2 * log(2 * pi). That constant cancels when comparing two fits of the same data (which is what the library uses it for), but do not feed this value to an information criterion without adding it back. A run that hits max_iterations without converging reports log_likelihood as 0.

Examples

x <- c(1, 1.2, 0.9, 1.1, 1.05, 8, 8.3, 7.9, 8.1, 8.2, 1.0, 8.0)
fit <- ml_gaussian_mixture(x, k = 2, seed = 12345)
round(fit$weights, 3)
#> [1] 0.5 0.5