What is GMM?
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 Gaussian distributions:
where , , and are the mixing weight, mean, and covariance matrix of component . GMMs are widely used for density estimation, soft clustering (via posterior responsibilities ), 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 and covariance matrix .
- Mixing Weights (): Prior probabilities specifying the proportion of data drawn from each component.
- Responsibilities (): Posterior probability that sample originated from component .
- Covariance Matrix (): Controls the spread and orientation of component (
full,tied,diag, orspherical). - EM Algorithm: Alternates computing responsibilities (E-step) and updating (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 vs. ), 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()
Related Concepts
- K-means Clustering: Corresponds to the hard-assignment limit () of EM on a GMM with isotropic (spherical) equal covariances 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 .
- 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 on isolated points; covariance regularization (
reg_covar) or Bayesian GMMs prevent singularities.