Gaussian mixtures and EM
Quick answer
A Gaussian mixture model represents a density as a weighted sum of Gaussian component densities. Unlike hard nearest-center assignments, it estimates a probability of component membership for each observation under the fitted model. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Normalize weighted densities and update a mean
a,b=0.06,0.14print([round(v/(a+b),2) for v in [a,b]])print((0.8*0+0.2*10)/(0.8+0.2))Observed local output:
[0.3, 0.7]2.0The first result is an E-step calculation; the second is a separate M-step mean calculation.
How it works
Write p(x)=sum_j pi_jN(x|mu_j,Sigma_j), where nonnegative mixing weights pi_j sum to one, means locate components and covariance matrices determine their shape. The E-step computes responsibility r_ij=pi_jN(x_i|mu_j,Sigma_j)/p(x_i). If two weighted densities are 0.06 and 0.14, responsibilities are 0.3 and 0.7. The M-step forms N_j=sum_i r_ij, updates pi_j=N_j/n, mu_j=sum_i r_ijx_i/N_j, and covariance as the responsibility-weighted outer products of deviations around the updated mean divided by N_j. For 0 and 10 with weights 0.8 and 0.2 for one component, the updated mean is 2. EM alternates these steps; exact EM under regular conditions does not decrease observed log likelihood, but local optima, singular covariances and numerical issues remain. Use log-sum-exp when normalizing log densities. Full covariance allows oriented ellipses; diagonal covariance restricts orientation, tied shares a covariance, and spherical uses one variance per component. Regularize covariance when fitting nearly duplicate observations. Specify n_init, seed and covariance type; check converged_. Component labels can be permuted without changing density. BIC = -2 log likelihood + parameter_countlog(n) trades fit and complexity; smaller BIC is preferred among comparable candidate fits, not proof of a true grouping. A component may represent part of a density rather than a meaningful category.
Inspect fitted responsibilities
from sklearn.mixture import GaussianMixtureimport numpy as npx=np.array([[0.],[1.],[2.],[10.],[11.],[12.]])m=GaussianMixture(n_components=2,n_init=3,random_state=7).fit(x)p=m.predict_proba(x)print(sorted(np.round(m.means_.ravel(),3).tolist()))print(bool(np.allclose(p.sum(axis=1),1)),bool(m.converged_))Observed local output:
[1.0, 11.0]True TrueResponsibilities sum to one for each observation. Component order is irrelevant.
Common mistakes
Do not treat component membership as calibrated class probability or use the maximum responsibility alone as density-based anomaly score. Check tiny component weights, near singularity and convergence warnings. BIC comparisons require the same observations and feature representation.
Practice and next steps
Given responsibilities [1,3] expressed as relative integer weights for points [2,10], compute the weighted mean (8). Then explain how a full covariance differs from a spherical one for elongated points. Complete the knowledge check and staged exercise. Next: Cluster validation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gaussian mixtures and EM — knowledge check
Apply gaussian mixtures and em to new scenarios.
What is a responsibility?
What does the M-step update?
Which covariance can represent a rotated ellipse?
What can EM converge to?
What does a lower BIC suggest on comparable fits?