Skip to content
E
Educatum

What is GMM?

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

A Gaussian Mixture Model (GMM) is a probabilistic latent-variable model that represents a data distribution as a convex combination of K multivariate Gaussian components, each with its own weight, mean vector, and covariance matrix. Unlike hard clustering methods such as K-means, GMMs compute soft posterior probabilities assigning every point partially to each cluster and can capture elliptical cluster shapes. Model parameters are typically learned via the Expectation-Maximization (EM) algorithm.

Study Card: Gaussian Mixture Models (GMMs)

Direct Answer

A Gaussian Mixture Model (GMM) is a generative probabilistic model that represents a complex, potentially multimodal distribution as a weighted sum of KK Gaussian distributions:

p(x)=∑k=1Kπk N(x∣μk,Σk),∑k=1Kπk=1,πk≥0p(\mathbf{x}) = \sum_{k=1}^{K} \pi_k \, \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k), \qquad \sum_{k=1}^{K} \pi_k = 1, \quad \pi_k \ge 0

where πk\pi_k, μk\boldsymbol{\mu}_k, and Σk\boldsymbol{\Sigma}_k are the mixing weight, mean, and covariance matrix of component kk. GMMs are widely used for density estimation, soft clustering (via posterior responsibilities γik=P(zi=k∣xi)\gamma_{ik} = P(z_i = k \mid \mathbf{x}_i)), and likelihood-based anomaly detection. Parameters are typically estimated with the Expectation-Maximization (EM) algorithm, using covariance regularization (reg_covar) or Bayesian priors to avoid degenerate zero-variance singularities where a Gaussian collapses onto a single data point.

Key Terms

  • Gaussian Distribution: Continuous distribution parameterized by mean μ\boldsymbol{\mu} and covariance matrix Σ\boldsymbol{\Sigma}.
  • Mixing Weights (πk\pi_k): Prior probabilities specifying the proportion of data drawn from each component.
  • Responsibilities (γik\gamma_{ik}): Posterior probability P(zi=k∣xi)P(z_i = k \mid \mathbf{x}_i) that sample xi\mathbf{x}_i originated from component kk.
  • Covariance Matrix (Σk\boldsymbol{\Sigma}_k): Controls the spread and orientation of component kk (full, tied, diag, or spherical).
  • EM Algorithm: Alternates computing responsibilities (E-step) and updating πk,μk,Σk\pi_k, \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k (M-step).

Example

Consider modeling customer purchase amounts that cluster into budget buyers (centered around $20 with tight variance) and occasional bulk buyers (centered around $150 with wider variance). A single Gaussian would average both groups and misrepresent the data. A 2-component GMM learns both modes, their mixing weights (say 70%70\% vs. 30%30\%), and different spreads, and assigns each transaction a soft membership probability across both segments.

Code Implementation

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from sklearn.mixture import GaussianMixture

# Generate sample data from a mixture of two 1D Gaussians
rng = np.random.default_rng(0)
n_samples = 300
X = np.concatenate([
    rng.normal(0.0, 1.0, int(0.3 * n_samples)),
    rng.normal(5.0, 1.0, int(0.7 * n_samples)),
]).reshape(-1, 1)

# Fit a 2-component Gaussian Mixture Model via EM
gmm = GaussianMixture(n_components=2, random_state=0, n_init=10)
gmm.fit(X)

means = gmm.means_
covariances = gmm.covariances_
weights = gmm.weights_

print("Means:\n", means)
print("\nCovariances:\n", covariances)
print("\nWeights:\n", weights)

# Evaluate total mixture PDF and each weighted Gaussian component:
# p(x) = sum_k pi_k * N(x | mu_k, sigma_k^2)
x = np.linspace(-5, 10, 1000).reshape(-1, 1)
pdf_total = np.exp(gmm.score_samples(x))

plt.figure(figsize=(8, 5))
plt.hist(X.ravel(), bins=30, density=True, histtype="stepfilled", alpha=0.4, label="Data")
plt.plot(x, pdf_total, "-k", lw=2, label="Total GMM PDF")

for i in range(gmm.n_components):
    pdf_component = weights[i] * norm(means[i, 0], np.sqrt(covariances[i, 0, 0])).pdf(x[:, 0])
    plt.plot(x, pdf_component, "--", lw=1.8, label=f"Weighted Gaussian {i + 1}")

plt.title("Gaussian Mixture Model Density Fit")
plt.xlabel("x")
plt.ylabel("Density")
plt.legend()
plt.grid(True)
plt.show()
  • K-means Clustering: Corresponds to the hard-assignment limit (σ2→0\sigma^2 \to 0) of EM on a GMM with isotropic (spherical) equal covariances σ2I\sigma^2 I and uniform mixing weights.
  • EM Algorithm: The iterative algorithm fitting GMM parameters to maximize marginal log-likelihood.
  • Model Selection (BIC / AIC): Criteria penalizing parameter count against log-likelihood to select the number of mixture components KK.
  • Anomaly Detection: Points with very low log-likelihood gmm.score_samples(x) lie outside high-density clusters and can be flagged as outliers.
  • Covariance Collapse: Pure MLE can push det⁡(Σk)→0\det(\boldsymbol{\Sigma}_k) \to 0 on isolated points; covariance regularization (reg_covar) or Bayesian GMMs prevent singularities.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.