{"id":173,"date":"2026-11-13T12:33:32","date_gmt":"2026-11-13T04:33:32","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-expectation-maximization-alternates-two-steps\/"},"modified":"2026-11-13T21:51:48","modified_gmt":"2026-11-13T13:51:48","slug":"how-expectation-maximization-alternates-two-steps","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-expectation-maximization-alternates-two-steps\/","title":{"rendered":"How expectation-maximization alternates two steps"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Expectation-maximization<\/strong> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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&#8217;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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Initialize the component weights, means, and covariances. A k-means run is a common start for a Gaussian mixture.<\/li>\n<li>E-step: for each point and component, compute the responsibility, proportional to weight times Gaussian density, normalized over components.<\/li>\n<li>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.<\/li>\n<li>Evaluate the observed log-likelihood. Stop when the increase is negligible or a step budget ends.<\/li>\n<li>If a covariance is collapsing, apply the variance floor the implementation chose, or restart. An unbounded likelihood is not a useful fit.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the responsibilities are replaced by hard assignments<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Assign each point to the single best component and then update<\/td>\n<td>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.<\/td>\n<\/tr>\n<tr>\n<td>Claim the highest possible likelihood after convergence<\/td>\n<td>The sequence of likelihoods is nondecreasing and can stop at a poor local maximum or head toward a singularity.<\/td>\n<\/tr>\n<tr>\n<td>Let a Gaussian variance go to zero and keep the point<\/td>\n<td>The density at that one point diverges. The likelihood is no longer a useful number. Constrain the variance.<\/td>\n<\/tr>\n<tr>\n<td>Update the means with unweighted averages of hard clusters while quoting the mixture likelihood<\/td>\n<td>The M-step weights points by responsibility. An unweighted mean is the maximizer of a different expected log-likelihood.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>0.10 5.00<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import math\npoints = [0.0, 0.2, 0.1, 5.0, 5.2, 4.8]\nmeans = [0.0, 5.0]\nvariances = [1.0, 1.0]\nweights = [0.5, 0.5]\n\ndef density(x, mean, variance):\n    return math.exp(-0.5 * (x - mean) ** 2 \/ variance) \/ math.sqrt(2 * math.pi * variance)\n\nprevious = None\nfor _ in range(30):\n    resp, loglike = [], 0.0\n    for x in points:\n        raw = [weights[k] * density(x, means[k], variances[k]) for k in range(2)]\n        total = sum(raw)\n        resp.append([value \/ total for value in raw])\n        loglike += math.log(total)\n    if previous is not None:\n        assert loglike &gt;= previous - 1e-9\n    previous = loglike\n    for k in range(2):\n        mass = sum(row[k] for row in resp)\n        weights[k] = mass \/ len(points)\n        means[k] = sum(row[k] * x for row, x in zip(resp, points)) \/ mass\n        variances[k] = max(1e-3, sum(row[k] * (x - means[k]) ** 2 for row, x in zip(resp, points)) \/ mass)\nmeans.sort()\nassert abs(means[0] - 0.1) &lt; 1e-6 and abs(means[1] - 5) &lt; 1e-6\nprint(f&quot;{means[0]:.2f}&quot;, f&quot;{means[1]:.2f}&quot;)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Expectation-maximization fits a <strong>Gaussian mixture<\/strong> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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. <strong>Expectation-maximization alternates a posterior over latent variables with a parameter update that raises the observed likelihood to a local maximum.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,48],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-clustering"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":572,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions\/572"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}