Short answer: Expectation-maximization fits a probabilistic model with latent variables by alternating two steps. The E-step computes the posterior of the latent variables given the current parameters. The M-step maximizes the expected complete-data log-likelihood under that posterior, which for many models is a weighted closed-form update. Each full iteration increases the observed-data likelihood. The limit is a stationary point, often a local maximum, and it depends on the initialization.
What the problem actually asks
A mixture model explains each observation by a hidden component. The likelihood of the observations alone sums over every possible hidden assignment, and that sum sits inside a logarithm. Direct maximization is awkward. Jensen’s inequality, or an equivalent argument, produces a lower bound that touches the true likelihood at the current posterior. Maximizing the bound with respect to the parameters cannot decrease the true likelihood.
For a Gaussian mixture the E-step is the responsibility of each component for each point, a softmax of Gaussian densities. The M-step is a weighted mean, a weighted covariance, and a weight equal to the average responsibility. Those updates are the algorithm people mean by EM on a mixture.
What the algorithm guarantees
The observed likelihood is nondecreasing at every iteration. There is no guarantee of the global maximum. Singularities of a Gaussian mixture, a component collapsing onto one point with variance going to zero, can send the likelihood to infinity. A variance floor or a Bayesian prior is a practical fix and a change of the objective. Time per iteration is proportional to the number of points times the number of components times the cost of a density evaluation.
k-means is the hard-assignment, equal-variance, spherical limit of EM for a Gaussian mixture. It is not the same update. Soft responsibilities are the point of the E-step.
The working steps
- Initialize the component weights, means, and covariances. A k-means run is a common start for a Gaussian mixture.
- E-step: for each point and component, compute the responsibility, proportional to weight times Gaussian density, normalized over components.
- M-step: set each weight to the mean responsibility. Set each mean to the responsibility-weighted average of the points. Set each covariance to the weighted outer product of deviations.
- Evaluate the observed log-likelihood. Stop when the increase is negligible or a step budget ends.
- If a covariance is collapsing, apply the variance floor the implementation chose, or restart. An unbounded likelihood is not a useful fit.
Monotone likelihood is not global optimality. A different start can finish higher. The algorithm is not k-means, and hardening the responsibilities throws away the soft update that justifies the likelihood proof. EM for a different latent model replaces the E and M formulas and keeps the alternation.
Approaches that look simpler and fail
| Approach | Why it fails when the responsibilities are replaced by hard assignments |
|---|---|
| Assign each point to the single best component and then update | That is a hard clustering step, close to k-means for spherical Gaussians. It is not the E-step, and the likelihood proof used the posterior, not the mode. |
| Claim the highest possible likelihood after convergence | The sequence of likelihoods is nondecreasing and can stop at a poor local maximum or head toward a singularity. |
| Let a Gaussian variance go to zero and keep the point | The density at that one point diverges. The likelihood is no longer a useful number. Constrain the variance. |
| Update the means with unweighted averages of hard clusters while quoting the mixture likelihood | The M-step weights points by responsibility. An unweighted mean is the maximizer of a different expected log-likelihood. |
Hard spherical clusters are k-means. Density connectivity is DBSCAN. Expectation-maximization is the soft latent-variable ascent, and for mixtures it is the responsibility-weighted update.
A working example
This Python fragment alternates responsibilities and weighted means for two one-dimensional Gaussians. The points sit near 0.1 and near 5. The fitted means print 0.10 5.00.
import math
points = [0.0, 0.2, 0.1, 5.0, 5.2, 4.8]
means = [0.0, 5.0]
variances = [1.0, 1.0]
weights = [0.5, 0.5]
def density(x, mean, variance):
return math.exp(-0.5 * (x - mean) ** 2 / variance) / math.sqrt(2 * math.pi * variance)
previous = None
for _ in range(30):
resp, loglike = [], 0.0
for x in points:
raw = [weights[k] * density(x, means[k], variances[k]) for k in range(2)]
total = sum(raw)
resp.append([value / total for value in raw])
loglike += math.log(total)
if previous is not None:
assert loglike >= previous - 1e-9
previous = loglike
for k in range(2):
mass = sum(row[k] for row in resp)
weights[k] = mass / len(points)
means[k] = sum(row[k] * x for row, x in zip(resp, points)) / mass
variances[k] = max(1e-3, sum(row[k] * (x - means[k]) ** 2 for row, x in zip(resp, points)) / mass)
means.sort()
assert abs(means[0] - 0.1) < 1e-6 and abs(means[1] - 5) < 1e-6
print(f"{means[0]:.2f}", f"{means[1]:.2f}")
Where it is used
Expectation-maximization fits a Gaussian mixture in scikit-learn and trains a hidden Markov model as Baum-Welch. Each pass assigns soft memberships, then updates the parameters. K-means is the hard-assignment special case.
When you can drop this algorithm
Drop EM when a hard partition and squared error are actually the objective. Use k-means. Keep EM when the model is a mixture and the quantity to ascend is the observed likelihood, with a guard against collapsing components.
Why this stays published
We publish this so a search for expectation-maximization returns the responsibility E-step, the weighted M-step, and the local nature of the likelihood ascent. Expectation-maximization alternates a posterior over latent variables with a parameter update that raises the observed likelihood to a local maximum.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
