@kanaries/ml
API Reference/Mixture Models

Gaussian Mixture Models

Fit Gaussian Mixture and Variational Bayesian Mixture models in JavaScript and TypeScript with @kanaries/ml for soft clustering and density estimation.

View as Markdown

Module overview

Gaussian Mixture Models (GMMs) represent a dataset as a weighted combination of multivariate Gaussian distributions. Unlike hard-assignment partitioners such as K-Means, mixture models assign every observation a soft membership probability (predictProba) across components and estimate the full data log-density (scoreSamples), making them well suited for elliptical clusters, overlapping subpopulations, and density-based anomaly scoring.

Use Mixture models when:

  • Clusters have different sizes, variances, or diagonal/tilted orientations where spherical K-Means boundaries fail.
  • You need calibrated cluster responsibilities (predictProba) rather than a single hard label.
  • You want principled model selection via AIC and BIC (GaussianMixture) or automatic pruning of excess components via a Dirichlet-process prior (BayesianGaussianMixture).

JavaScript implementation

@kanaries/ml provides two scikit-learn-compatible mixture estimators under import { Mixture } from '@kanaries/ml', written in pure TypeScript for browsers and Node.js:

EstimatorFitting algorithmCovariance typesBest for
Mixture.GaussianMixtureExpectation-Maximization (EM) in the log domain'full', 'tied', 'diag', 'spherical'Soft clustering, density estimation, AIC/BIC selection of nComponents
Mixture.BayesianGaussianMixtureVariational Bayesian inference (Dirichlet Process / Dirichlet Distribution)'full', 'diag'Automatic pruning of redundant components when the true cluster count is unknown

Both classes extend ClusterBase, support reproducible randomState seeds, multiple restarts (nInit), and full JSON serialization with toJSON() / loadModel().

Quick start example

import { Mixture, Datasets } from '@kanaries/ml';

const { X } = Datasets.makeBlobs({ nSamples: 150, centers: 3, clusterStd: 0.7, randomState: 42 });

const gmm = new Mixture.GaussianMixture({
  nComponents: 3,
  covarianceType: 'full',
  nInit: 3,
  randomState: 42,
});

gmm.fit(X);
const labels = gmm.predict(X);
const probs = gmm.predictProba(X);
const bic = gmm.bic(X);

Choosing between GMM, Bayesian GMM, and K-Means