Skip to main content
Open In Colab The expectation-maximization (EM) algorithm finds maximum likelihood estimates for models with latent variables. This section develops it on the mixture of Gaussians, as an extension of the maximum likelihood estimate of a Gaussian. For one Gaussian the estimate has a closed form. With several components it does not, and EM replaces it with two steps that each have a closed form and are repeated until the estimate stops changing. The derivations follow Bishop, Chapter 9 (Bishop, n.d.), and the equation numbers cited are his. The symbols follow the rest of this book: data points are indexed by i=1,…,mi = 1, \ldots, m where Bishop writes n=1,…,Nn = 1, \ldots, N, so his responsibilities γ(znk)\gamma(z_{nk}) appear here as γ(zik)\gamma(z_{ik}) and his effective counts NkN_k as mkm_k. You need the marginal maximum likelihood principle and the multivariate Gaussian density. The previous section, k-means, is used twice here. Its output is a good starting point for EM, and k-means itself is the limit of EM when all components share a covariance that shrinks to zero.

One Gaussian in closed form

Take a data set X\mathbf X of mm observations x1,…,xm\mathbf x_1, \ldots, \mathbf x_m in nn dimensions, drawn independently from a Gaussian N(x∣μ,Σ)\mathcal N(\mathbf x \mid \boldsymbol\mu, \boldsymbol\Sigma). The log likelihood is a sum of logs of Gaussians (Bishop, Eq. 2.118): ln⁡p(X∣μ,Σ)=−mn2ln⁡(2π)−m2ln⁡∣Σ∣−12∑i=1m(xi−μ)⊤Σ−1(xi−μ).\ln p(\mathbf X \mid \boldsymbol\mu, \boldsymbol\Sigma) = -\frac{mn}{2}\ln(2\pi) - \frac{m}{2}\ln|\boldsymbol\Sigma| - \frac{1}{2}\sum_{i=1}^{m} (\mathbf x_i - \boldsymbol\mu)^\top \boldsymbol\Sigma^{-1} (\mathbf x_i - \boldsymbol\mu). The logarithm acts directly on each Gaussian, so every term is a quadratic in μ\boldsymbol\mu. Setting the derivatives to zero gives the sample mean and the sample covariance (Eqs. 2.121 and 2.122): μML=1m∑i=1mxi,ΣML=1m∑i=1m(xi−μML)(xi−μML)⊤.\boldsymbol\mu_{\text{ML}} = \frac{1}{m}\sum_{i=1}^{m}\mathbf x_i, \qquad \boldsymbol\Sigma_{\text{ML}} = \frac{1}{m}\sum_{i=1}^{m}(\mathbf x_i - \boldsymbol\mu_{\text{ML}})(\mathbf x_i - \boldsymbol\mu_{\text{ML}})^\top. The data below come from three Gaussians in the plane, 100 points each. You fit a single Gaussian to all 300 points with these two formulas.
Scatter of 300 points in three clusters with a single Gaussian's one and two standard deviation ellipses drawn over them. The mean falls between the clusters, where there are almost no data. One Gaussian fitted to data from three clusters. The ellipses mark one and two standard deviations. The mean sits between the clusters, in a region with almost no data. This is the best single Gaussian for the data, yet it puts its highest density where there are no points. One Gaussian per cluster would fit much better, but the data do not say which point came from which cluster.

The mixture of Gaussians

A mixture of KK Gaussians is the density (Bishop, Eq. 9.7) p(x)=∑k=1Kπk N(x∣μk,Σk),p(\mathbf x) = \sum_{k=1}^{K} \pi_k\, \mathcal N(\mathbf x \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k), where the mixing coefficients satisfy 0≤πk≤10 \le \pi_k \le 1 and ∑kπk=1\sum_k \pi_k = 1 (Eqs. 9.8 and 9.9). Each component has its own mean μk\boldsymbol\mu_k and covariance Σk\boldsymbol\Sigma_k. The mixture can also be read as a model of how the data are generated. Introduce a latent variable z\mathbf z in the one-of-KK coding: exactly one element zkz_k equals 1 and the others are 0. Draw z\mathbf z with p(zk=1)=πkp(z_k = 1) = \pi_k, then draw x\mathbf x from the Gaussian that z\mathbf z selects, p(x∣zk=1)=N(x∣μk,Σk)p(\mathbf x \mid z_k = 1) = \mathcal N(\mathbf x \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k). Summing the joint p(z)p(x∣z)p(\mathbf z)p(\mathbf x \mid \mathbf z) over the KK values of z\mathbf z gives back the mixture (Eqs. 9.10 to 9.12). Every observation xi\mathbf x_i has its own latent zi\mathbf z_i, with elements zikz_{ik}. Once you have observed x\mathbf x, Bayes’ theorem gives the posterior probability that component kk generated it (Eq. 9.13): γ(zk)≡p(zk=1∣x)=πk N(x∣μk,Σk)∑j=1Kπj N(x∣μj,Σj).\gamma(z_k) \equiv p(z_k = 1 \mid \mathbf x) = \frac{\pi_k\, \mathcal N(\mathbf x \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k)}{\sum_{j=1}^{K} \pi_j\, \mathcal N(\mathbf x \mid \boldsymbol\mu_j, \boldsymbol\Sigma_j)}. Bishop calls γ(zk)\gamma(z_k) the responsibility of component kk for x\mathbf x. The mixing coefficient πk\pi_k is the prior probability of zk=1z_k = 1, and γ(zk)\gamma(z_k) is the posterior. For data point xi\mathbf x_i it is written γ(zik)\gamma(z_{ik}). The log likelihood of the data set is (Eq. 9.14) ln⁡p(X∣π,μ,Σ)=∑i=1mln⁡{∑k=1Kπk N(xi∣μk,Σk)}.\ln p(\mathbf X \mid \boldsymbol\pi, \boldsymbol\mu, \boldsymbol\Sigma) = \sum_{i=1}^{m} \ln \bigl\{ \sum_{k=1}^{K} \pi_k\, \mathcal N(\mathbf x_i \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k) \bigr\}. In the single-Gaussian case the logarithm acted directly on each Gaussian. Here the sum over kk sits inside the logarithm, so the terms are not quadratic in the means and setting the derivatives to zero does not give a closed form. The log likelihood is evaluated with the log-sum-exp identity, which computes ln⁡∑keak\ln \sum_k e^{a_k} without underflow when the densities are very small. Its inputs aik=ln⁡πk+ln⁡N(xi∣μk,Σk)a_{ik} = \ln \pi_k + \ln \mathcal N(\mathbf x_i \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k) are also the log numerators of the responsibilities.

Stationarity conditions weighted by responsibilities

Setting the derivatives to zero still gives useful conditions. The derivative with respect to μk\boldsymbol\mu_k is (Bishop, Eq. 9.16) 0=∑i=1mπk N(xi∣μk,Σk)∑jπj N(xi∣μj,Σj)⏟γ(zik)Σk−1(xi−μk).0 = \sum_{i=1}^{m} \underbrace{\frac{\pi_k\, \mathcal N(\mathbf x_i \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k)}{\sum_j \pi_j\, \mathcal N(\mathbf x_i \mid \boldsymbol\mu_j, \boldsymbol\Sigma_j)}}_{\gamma(z_{ik})} \boldsymbol\Sigma_k^{-1} (\mathbf x_i - \boldsymbol\mu_k). The fraction inside the sum is the responsibility of Eq. 9.13. Multiply by Σk\boldsymbol\Sigma_k and rearrange to get (Eqs. 9.17 and 9.18) μk=1mk∑i=1mγ(zik) xi,mk=∑i=1mγ(zik).\boldsymbol\mu_k = \frac{1}{m_k}\sum_{i=1}^{m} \gamma(z_{ik})\, \mathbf x_i, \qquad m_k = \sum_{i=1}^{m} \gamma(z_{ik}). This is the sample mean of the single Gaussian with each point weighted by the responsibility of component kk for it. mkm_k is the effective number of points assigned to component kk. The same steps for Σk\boldsymbol\Sigma_k give a weighted sample covariance (Eq. 9.19): Σk=1mk∑i=1mγ(zik)(xi−μk)(xi−μk)⊤.\boldsymbol\Sigma_k = \frac{1}{m_k}\sum_{i=1}^{m} \gamma(z_{ik})(\mathbf x_i - \boldsymbol\mu_k)(\mathbf x_i - \boldsymbol\mu_k)^\top. The mixing coefficients must sum to one. Maximizing ln⁡p(X∣π,μ,Σ)+λ(∑kπk−1)\ln p(\mathbf X \mid \boldsymbol\pi, \boldsymbol\mu, \boldsymbol\Sigma) + \lambda\bigl(\sum_k \pi_k - 1\bigr) with a Lagrange multiplier λ\lambda gives λ=−m\lambda = -m and (Eqs. 9.20 to 9.22) πk=mkm,\pi_k = \frac{m_k}{m}, the average responsibility that component kk takes for the data. The three equations do not give the parameters directly, because the responsibilities on the right-hand side depend on π\boldsymbol\pi, μ\boldsymbol\mu and Σ\boldsymbol\Sigma through Eq. 9.13. Each equation has the unknowns on both sides. Given the responsibilities, the parameters follow in closed form, and given the parameters, the responsibilities follow from Bayes’ theorem.

The E step and the M step

EM alternates between these two computations. Start from some values of the parameters and evaluate the log likelihood. Then repeat two steps (Bishop, Section 9.2.2). In the E step, evaluate the responsibilities with the current parameters (Eq. 9.23): γ(zik)=πk N(xi∣μk,Σk)∑j=1Kπj N(xi∣μj,Σj).\gamma(z_{ik}) = \frac{\pi_k\, \mathcal N(\mathbf x_i \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k)}{\sum_{j=1}^{K} \pi_j\, \mathcal N(\mathbf x_i \mid \boldsymbol\mu_j, \boldsymbol\Sigma_j)}. In the M step, hold the responsibilities fixed and re-estimate the parameters with the weighted maximum likelihood formulas (Eqs. 9.24 to 9.27): μknew=1mk∑i=1mγ(zik) xi,\boldsymbol\mu_k^{\text{new}} = \frac{1}{m_k}\sum_{i=1}^{m} \gamma(z_{ik})\, \mathbf x_i, Σknew=1mk∑i=1mγ(zik)(xi−μknew)(xi−μknew)⊤,\boldsymbol\Sigma_k^{\text{new}} = \frac{1}{m_k}\sum_{i=1}^{m} \gamma(z_{ik})(\mathbf x_i - \boldsymbol\mu_k^{\text{new}})(\mathbf x_i - \boldsymbol\mu_k^{\text{new}})^\top, πknew=mkm.\pi_k^{\text{new}} = \frac{m_k}{m}. After each M step, evaluate the log likelihood (Eq. 9.28) and stop when it no longer changes. The new means are computed first and then used in the covariance, as for the single Gaussian. The E step works in log space. It subtracts the log-sum-exp of each row before exponentiating, which is the division in Eq. 9.23.
The run below starts from a deliberately poor guess. All three means sit in a row near the middle of the data, every covariance is the identity, and the mixing coefficients are equal.
Four panels of the same 300 points after 0, 1, 5 and the final number of EM iterations. Points are colored by a mix of three colors in proportion to their responsibilities, and each component is drawn as a one standard deviation ellipse. The ellipses start small and close together and end on the three clusters. EM iterations on the three-cluster data. Each point’s color mixes the three component colors in proportion to its responsibilities γ(zik)\gamma(z_{ik}), and the ellipses mark one standard deviation of each component. Panel 0 shows the starting parameters. After the first M step each mean has moved to the responsibility-weighted center of the points, and each covariance has grown to the weighted spread. Points between components take mixed colors, because their responsibilities are split. By the last iteration each component sits on one cluster and almost every point belongs to one component with responsibility near 1. Log likelihood against EM iteration. The curve rises steeply over the first few iterations and then flattens, staying above the dashed line of the single Gaussian fit once it crosses it. The log likelihood after each EM iteration. The dashed line is the log likelihood of the single Gaussian. The log likelihood increases at every iteration. Bishop shows that an E step followed by an M step cannot decrease it (Section 9.4), and the section EM in general below gives the argument. The check below confirms it for this run, up to floating-point rounding.
The data were generated by a known mixture, so you can compare the estimate with the truth. Each component is matched to the true cluster whose mean is nearest.
Filled contours of the fitted three-component mixture density over the data. Three density peaks sit on the three clusters, and the EM means (black crosses) fall inside the circles that mark the true means. Density of the fitted mixture, p(x)=∑kπkN(x∣μk,Σk)p(\mathbf x) = \sum_k \pi_k \mathcal N(\mathbf x \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k), with the true means (circles) and the EM means (crosses).

Singularities and initialization

Maximum likelihood for a mixture has a problem that does not arise for a single Gaussian. Suppose component jj has covariance σj2I\sigma_j^2 \mathbf I and its mean lands exactly on one data point, μj=xi\boldsymbol\mu_j = \mathbf x_i. That point contributes the density N(xi∣xi,σj2I)=1(2π)n/21σjn,\mathcal N(\mathbf x_i \mid \mathbf x_i, \sigma_j^2 \mathbf I) = \frac{1}{(2\pi)^{n/2}}\frac{1}{\sigma_j^n}, which grows without bound as σj→0\sigma_j \to 0. Bishop’s Eq. 9.15 gives the case n=1n = 1. A single Gaussian that collapses onto one point drives the density of every other point to zero, so its likelihood goes to zero. In a mixture, another component can keep a finite variance and cover the other points while component jj shrinks. The log likelihood then goes to infinity (Bishop, Section 9.2.1). The code builds such a mixture. One component is the single Gaussian fitted above, with mixing coefficient 0.99. The other has mixing coefficient 0.01 and sits on the first data point with a shrinking σj\sigma_j.
Below σj=10−2\sigma_j = 10^{-2}, each further factor of 100 adds nln⁡100≈9.2n \ln 100 \approx 9.2 to the log likelihood, and the growth has no limit. (At σj=0.1\sigma_j = 0.1 the narrow component still covers a few neighbors of x1\mathbf x_1, which is why the first value is higher than the second.) The global maximum of the log likelihood is therefore one of these degenerate solutions, and what EM should find is a well-behaved local maximum. Practical implementations guard against collapse. They add a small multiple of the identity to every covariance in the M step (the reg argument of m_step), or they detect a collapsing component and reset its mean and covariance. A prior on the parameters also removes the singularities. EM then maximizes Q(θ,θold)+ln⁡p(θ)Q(\boldsymbol\theta, \boldsymbol\theta^{\text{old}}) + \ln p(\boldsymbol\theta) in the M step (Bishop, Section 9.3). A second issue is that the log likelihood has several local maxima, and EM climbs to the one nearest its start. The run below starts EM ten times, each from three data points drawn at random as means, the overall sample covariance for every component, and equal mixing coefficients.
Starts that reach the same final value have found the same solution, up to relabeling the components. A mixture of KK components has K!K! equivalent maxima of this kind, one for each way of assigning the parameter sets to the labels. Starts that end lower have stopped at a worse local maximum. The usual remedy is to start EM from the output of k-means (Bishop, Section 9.2.2). K-means alternates two steps that are cheaper than EM’s: assign each point to its nearest center, rik=1r_{ik} = 1 for k=arg⁡min⁡j∥xi−μj∥2k = \arg\min_j \lVert \mathbf x_i - \boldsymbol\mu_j \rVert^2 and 0 otherwise (Eq. 9.2), then move each center to the mean of its points (Eq. 9.4). The cluster means, the cluster sample covariances and the cluster fractions then serve as the starting μk\boldsymbol\mu_k, Σk\boldsymbol\Sigma_k and πk\pi_k.
Started from k-means, EM reaches the best value of the random starts in fewer iterations than any of them.

K-means as a limit of EM

K-means makes a hard assignment of each point to one cluster, and EM makes a soft one. The two are related exactly (Bishop, Section 9.3.2). Give every component the same covariance ϵI\epsilon \mathbf I, with ϵ\epsilon a fixed constant that EM does not re-estimate, so that p(x∣μk,Σk)=1(2πϵ)n/2exp⁡{−12ϵ∥x−μk∥2}.p(\mathbf x \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k) = \frac{1}{(2\pi\epsilon)^{n/2}} \exp\left\{-\frac{1}{2\epsilon}\lVert \mathbf x - \boldsymbol\mu_k \rVert^2\right\}. Bishop writes this as Eq. 9.41. The responsibilities become (Eq. 9.42) γ(zik)=πkexp⁡{−∥xi−μk∥2/2ϵ}∑jπjexp⁡{−∥xi−μj∥2/2ϵ}.\gamma(z_{ik}) = \frac{\pi_k \exp\{-\lVert \mathbf x_i - \boldsymbol\mu_k \rVert^2 / 2\epsilon\}}{\sum_j \pi_j \exp\{-\lVert \mathbf x_i - \boldsymbol\mu_j \rVert^2 / 2\epsilon\}}. As ϵ→0\epsilon \to 0, the term with the smallest distance ∥xi−μj∥2\lVert \mathbf x_i - \boldsymbol\mu_j \rVert^2 decays most slowly. Its responsibility goes to 1 and all others go to 0, whatever the values of πk\pi_k as long as none is zero. The responsibility γ(zik)\gamma(z_{ik}) becomes the k-means indicator rikr_{ik}, and the M step for the means, Eq. 9.17, becomes the k-means update, Eq. 9.4. In the same limit, the expected complete-data log likelihood introduced in the next section tends to −12∑i∑krik∥xi−μk∥2-\frac{1}{2}\sum_i \sum_k r_{ik} \lVert \mathbf x_i - \boldsymbol\mu_k \rVert^2 plus a constant (Eq. 9.43). Maximizing it is the same as minimizing the k-means loss L=∑i∑krik∥xi−μk∥2L = \sum_i \sum_k r_{ik} \lVert \mathbf x_i - \boldsymbol\mu_k \rVert^2 of the previous section, which Bishop calls the distortion JJ (Eq. 9.1). The code holds the means at the values EM found and shrinks ϵ\epsilon. For each ϵ\epsilon it reports the average of the largest responsibility of each point, and the largest gap over all points and components between γ(zik)\gamma(z_{ik}) and the k-means indicator rikr_{ik} of the nearest mean.
Three panels of the data colored by responsibilities under a shared covariance epsilon times the identity, for epsilon equal to 16, 4 and 0.1. At 16 the colors are blended across the plane; at 0.1 every point has one pure color, the color of its nearest mean. Responsibilities with a shared covariance ϵI\epsilon \mathbf I and the means held at the EM solution (gray crosses). As ϵ\epsilon shrinks, the blended colors turn into pure ones and each point takes the color of its nearest mean, which is the k-means assignment. K-means is therefore EM for a mixture whose components are round, equal in size and infinitely narrow. It estimates the means only. This is also why k-means is a good starting point for EM. It solves a simplified version of the same problem, and its iterations are fewer and cheaper.

EM in general

The mixture of Gaussians is one instance of a general algorithm. Collect the observed data in X\mathbf X, the latent variables in Z\mathbf Z (row ii is zi⊤\mathbf z_i^\top), and all parameters in θ\boldsymbol\theta. The log likelihood is (Bishop, Eq. 9.29) ln⁡p(X∣θ)=ln⁡{∑Zp(X,Z∣θ)},\ln p(\mathbf X \mid \boldsymbol\theta) = \ln \bigl\{ \sum_{\mathbf Z} p(\mathbf X, \mathbf Z \mid \boldsymbol\theta) \bigr\}, with the sum over the latent variables inside the logarithm, as in Eq. 9.14. If you were also given Z\mathbf Z, the complete-data log likelihood would be easy to maximize. For the mixture it is (Eq. 9.36) ln⁡p(X,Z∣μ,Σ,π)=∑i=1m∑k=1Kzik{ln⁡πk+ln⁡N(xi∣μk,Σk)}.\ln p(\mathbf X, \mathbf Z \mid \boldsymbol\mu, \boldsymbol\Sigma, \boldsymbol\pi) = \sum_{i=1}^{m}\sum_{k=1}^{K} z_{ik}\{\ln \pi_k + \ln \mathcal N(\mathbf x_i \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k)\}. The logarithm now acts on each Gaussian, and the maximization splits into one single-Gaussian fit per component on the points assigned to it. In practice Z\mathbf Z is unknown, and the only information about it is its posterior p(Z∣X,θold)p(\mathbf Z \mid \mathbf X, \boldsymbol\theta^{\text{old}}) under the current parameters. EM therefore maximizes the expected complete-data log likelihood under that posterior (Eq. 9.30): Q(θ,θold)=∑Zp(Z∣X,θold)ln⁡p(X,Z∣θ).Q(\boldsymbol\theta, \boldsymbol\theta^{\text{old}}) = \sum_{\mathbf Z} p(\mathbf Z \mid \mathbf X, \boldsymbol\theta^{\text{old}}) \ln p(\mathbf X, \mathbf Z \mid \boldsymbol\theta). The E step evaluates the posterior p(Z∣X,θold)p(\mathbf Z \mid \mathbf X, \boldsymbol\theta^{\text{old}}). The M step sets θnew=arg⁡max⁡θQ(θ,θold)\boldsymbol\theta^{\text{new}} = \arg\max_{\boldsymbol\theta} Q(\boldsymbol\theta, \boldsymbol\theta^{\text{old}}) (Eq. 9.31). For the mixture, the posterior factorizes over the data points and the expected value of zikz_{ik} is the responsibility, E[zik]=γ(zik)\mathbb E[z_{ik}] = \gamma(z_{ik}) (Eq. 9.39). Substituting it gives (Eq. 9.40) EZ[ln⁡p(X,Z∣μ,Σ,π)]=∑i=1m∑k=1Kγ(zik){ln⁡πk+ln⁡N(xi∣μk,Σk)},\mathbb E_{\mathbf Z}[\ln p(\mathbf X, \mathbf Z \mid \boldsymbol\mu, \boldsymbol\Sigma, \boldsymbol\pi)] = \sum_{i=1}^{m}\sum_{k=1}^{K} \gamma(z_{ik})\{\ln \pi_k + \ln \mathcal N(\mathbf x_i \mid \boldsymbol\mu_k, \boldsymbol\Sigma_k)\}, and maximizing it gives the M-step formulas derived above. The guarantee that the log likelihood never decreases comes from a decomposition that holds for any distribution q(Z)q(\mathbf Z) (Bishop, Section 9.4, Eqs. 9.70 to 9.72): ln⁡p(X∣θ)=L(q,θ)+KL(q ∥ p),L(q,θ)=∑Zq(Z)ln⁡p(X,Z∣θ)q(Z),KL(q ∥ p)=−∑Zq(Z)ln⁡p(Z∣X,θ)q(Z).\ln p(\mathbf X \mid \boldsymbol\theta) = \mathcal L(q, \boldsymbol\theta) + \mathrm{KL}(q \,\|\, p), \qquad \mathcal L(q, \boldsymbol\theta) = \sum_{\mathbf Z} q(\mathbf Z) \ln \frac{p(\mathbf X, \mathbf Z \mid \boldsymbol\theta)}{q(\mathbf Z)}, \qquad \mathrm{KL}(q \,\|\, p) = -\sum_{\mathbf Z} q(\mathbf Z) \ln \frac{p(\mathbf Z \mid \mathbf X, \boldsymbol\theta)}{q(\mathbf Z)}. The KL divergence is non-negative, so L(q,θ)\mathcal L(q, \boldsymbol\theta) is a lower bound on the log likelihood. The E step sets q(Z)=p(Z∣X,θold)q(\mathbf Z) = p(\mathbf Z \mid \mathbf X, \boldsymbol\theta^{\text{old}}), which makes the KL term zero and the bound touch the log likelihood at θold\boldsymbol\theta^{\text{old}}. With this qq, L(q,θ)\mathcal L(q, \boldsymbol\theta) equals Q(θ,θold)Q(\boldsymbol\theta, \boldsymbol\theta^{\text{old}}) plus a constant that does not depend on θ\boldsymbol\theta (Eq. 9.74). The M step raises the bound by maximizing QQ. The log likelihood rises at least as much, because the KL term, zero before the M step, can only be non-negative after it. The cell below checks the decomposition numerically for the mixture. It takes qq as the responsibilities computed at the starting parameters and evaluates both terms at the parameters after the first M step.
EM applies to any model whose complete-data log likelihood is easy to maximize, with discrete or continuous latent variables. Dempster, Laird and Rubin (1977) gave the general formulation. The latent variable models section uses the same structure, a latent z\mathbf z and a conditional p(x∣z)p(\mathbf x \mid \mathbf z), for generative models whose latent space is continuous.

Inliers and outliers as a two-component mixture

A mixture can also model measurements that should not be trusted. Consider fitting a line g(x;θ)=θ⊤ϕ(x)g(x; \boldsymbol\theta) = \boldsymbol\theta^\top \boldsymbol\phi(x) with ϕ(x)=(1,x)⊤\boldsymbol\phi(x) = (1, x)^\top to pairs (x(i),y(i))(x^{(i)}, y^{(i)}), in the notation of the linear regression section. Ordinary least squares assumes every target is the line plus Gaussian noise of variance σ2\sigma^2. Suppose instead that a fraction of the points are gross errors, spread over the whole range of yy. Model each target as coming from one of two components, an inlier component around the line and an outlier component that is uniform over the range of the data: p(y(i)∣x(i))=π N(y(i)∣θ⊤ϕ(x(i)),σ2)+(1−π) U(y(i)).p(y^{(i)} \mid x^{(i)}) = \pi\, \mathcal N\big(y^{(i)} \mid \boldsymbol\theta^\top \boldsymbol\phi(x^{(i)}), \sigma^2\big) + (1 - \pi)\, \mathcal U(y^{(i)}). The mixing coefficient π\pi is the inlier fraction. It differs from the earlier mixture in two ways: the Gaussian component is conditional on x(i)x^{(i)}, as in Bishop’s mixture of linear regression models (Section 14.5.1, Eq. 14.34), and the second component is uniform and has no parameters. Bishop writes the weights as w\mathbf w, the targets as tnt_n and the noise as a precision β=1/σ2\beta = 1/\sigma^2; the equations below are his with those symbols replaced. The E step computes the responsibility of the inlier component for each point, γ(zi)=π N(y(i)∣θ⊤ϕ(x(i)),σ2)π N(y(i)∣θ⊤ϕ(x(i)),σ2)+(1−π) U(y(i)).\gamma(z_i) = \frac{\pi\, \mathcal N\big(y^{(i)} \mid \boldsymbol\theta^\top \boldsymbol\phi(x^{(i)}), \sigma^2\big)}{\pi\, \mathcal N\big(y^{(i)} \mid \boldsymbol\theta^\top \boldsymbol\phi(x^{(i)}), \sigma^2\big) + (1 - \pi)\, \mathcal U(y^{(i)})}. The M step maximizes the expected complete-data log likelihood. For θ\boldsymbol\theta it is a weighted least squares problem, with the responsibilities as weights. With R=diag⁡(γ(zi))\mathbf R = \operatorname{diag}(\gamma(z_i)) and the design matrix Φ\boldsymbol\Phi whose rows are ϕ(x(i))⊤\boldsymbol\phi(x^{(i)})^\top, the solution is (Bishop, Eq. 14.42) θ=(Φ⊤RΦ)−1Φ⊤R y.\boldsymbol\theta = (\boldsymbol\Phi^\top \mathbf R \boldsymbol\Phi)^{-1} \boldsymbol\Phi^\top \mathbf R\, \mathbf y. The noise variance and the inlier fraction follow the same pattern as Σk\boldsymbol\Sigma_k and πk\pi_k: σ2=∑iγ(zi){y(i)−θ⊤ϕ(x(i))}2∑iγ(zi),π=1m∑iγ(zi).\sigma^2 = \frac{\sum_i \gamma(z_i)\big\{y^{(i)} - \boldsymbol\theta^\top \boldsymbol\phi(x^{(i)})\big\}^2}{\sum_i \gamma(z_i)}, \qquad \pi = \frac{1}{m}\sum_i \gamma(z_i). These are the single-component versions of Bishop’s Eqs. 14.44 and 14.38. With γ(zi)=1\gamma(z_i) = 1 for every point, the weighted solution reduces to the ordinary least squares solution θ=(Φ⊤Φ)−1Φ⊤y\boldsymbol\theta = (\boldsymbol\Phi^\top \boldsymbol\Phi)^{-1}\boldsymbol\Phi^\top \mathbf y (Bishop, Eq. 3.15). The data below have 60 points. 42 lie on the line y=1+0.5xy = 1 + 0.5x with noise of standard deviation 0.3, and 18 are outliers drawn uniformly over the range of yy.
Scatter of 60 points colored by inlier responsibility, with three lines: the true line, the least squares fit pulled toward the outliers, and the EM mixture fit lying on the true line. Points far from the line are dark, meaning low responsibility. Line fitting with 18 outliers among 60 points. The color of each point is its inlier responsibility γ(zi)\gamma(z_i). Least squares gives every point the same weight. EM gives the outliers responsibilities near zero and fits the line to the rest. The EM line is almost exactly the least squares line of the 42 true inliers, which is the fit you would get if the labels were known. Its inlier fraction, 0.73, is above the true 0.70 because 3 of the 18 outliers happened to land close to the line. EM counts them as inliers, and nothing in the data distinguishes them from the true inliers. The responsibilities weight the fit and also label the points: the model attributes a point with γ(zi)\gamma(z_i) near 0 to the outlier component. Replace the soft weight with a hard test, inlier if the residual is below a threshold set by σ\sigma, and you get the inlier test of RANSAC. The inlier fraction π\pi is the quantity that sets how many random samples RANSAC needs. The next chapter, robust estimation, starts from this model.

Lecture video

Key references: (Bishop, n.d.; Dempster et al., n.d.)

References

  • Dempster, A., Laird, N., Rdin, D. (n.d.). Maximum Likelihood from Incomplete Data via the EM Algorithm.
  • Bishop (n.d.). Pattern Recognition and Machine Learning. Link

Found an error on this page? Report an issue.