Code
Code
One Gaussian in closed form
Take a data set of observations in dimensions, drawn independently from a Gaussian . The log likelihood is a sum of logs of Gaussians (Bishop, Eq. 2.118): The logarithm acts directly on each Gaussian, so every term is a quadratic in . Setting the derivatives to zero gives the sample mean and the sample covariance (Eqs. 2.121 and 2.122): 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.Code
Code
The mixture of Gaussians
A mixture of Gaussians is the density (Bishop, Eq. 9.7) where the mixing coefficients satisfy and (Eqs. 9.8 and 9.9). Each component has its own mean and covariance . The mixture can also be read as a model of how the data are generated. Introduce a latent variable in the one-of- coding: exactly one element equals 1 and the others are 0. Draw with , then draw from the Gaussian that selects, . Summing the joint over the values of gives back the mixture (Eqs. 9.10 to 9.12). Every observation has its own latent , with elements . Once you have observed , Bayes’ theorem gives the posterior probability that component generated it (Eq. 9.13): Bishop calls the responsibility of component for . The mixing coefficient is the prior probability of , and is the posterior. For data point it is written . The log likelihood of the data set is (Eq. 9.14) In the single-Gaussian case the logarithm acted directly on each Gaussian. Here the sum over 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 without underflow when the densities are very small. Its inputs are also the log numerators of the responsibilities.Code
Code
Stationarity conditions weighted by responsibilities
Setting the derivatives to zero still gives useful conditions. The derivative with respect to is (Bishop, Eq. 9.16) The fraction inside the sum is the responsibility of Eq. 9.13. Multiply by and rearrange to get (Eqs. 9.17 and 9.18) This is the sample mean of the single Gaussian with each point weighted by the responsibility of component for it. is the effective number of points assigned to component . The same steps for give a weighted sample covariance (Eq. 9.19): The mixing coefficients must sum to one. Maximizing with a Lagrange multiplier gives and (Eqs. 9.20 to 9.22) the average responsibility that component takes for the data. The three equations do not give the parameters directly, because the responsibilities on the right-hand side depend on , and 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): 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): 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.Code
Code
Code
Code
Code
Code
Code
Code
Singularities and initialization
Maximum likelihood for a mixture has a problem that does not arise for a single Gaussian. Suppose component has covariance and its mean lands exactly on one data point, . That point contributes the density which grows without bound as . Bishop’s Eq. 9.15 gives the case . 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 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 .Code
Code
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 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.
Code
Code
Code
Code
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 , with a fixed constant that EM does not re-estimate, so that Bishop writes this as Eq. 9.41. The responsibilities become (Eq. 9.42) As , the term with the smallest distance decays most slowly. Its responsibility goes to 1 and all others go to 0, whatever the values of as long as none is zero. The responsibility becomes the k-means indicator , 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 plus a constant (Eq. 9.43). Maximizing it is the same as minimizing the k-means loss of the previous section, which Bishop calls the distortion (Eq. 9.1). The code holds the means at the values EM found and shrinks . For each it reports the average of the largest responsibility of each point, and the largest gap over all points and components between and the k-means indicator of the nearest mean.Code
Code
EM in general
The mixture of Gaussians is one instance of a general algorithm. Collect the observed data in , the latent variables in (row is ), and all parameters in . The log likelihood is (Bishop, Eq. 9.29) with the sum over the latent variables inside the logarithm, as in Eq. 9.14. If you were also given , the complete-data log likelihood would be easy to maximize. For the mixture it is (Eq. 9.36) 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 is unknown, and the only information about it is its posterior under the current parameters. EM therefore maximizes the expected complete-data log likelihood under that posterior (Eq. 9.30): The E step evaluates the posterior . The M step sets (Eq. 9.31). For the mixture, the posterior factorizes over the data points and the expected value of is the responsibility, (Eq. 9.39). Substituting it gives (Eq. 9.40) 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 (Bishop, Section 9.4, Eqs. 9.70 to 9.72): The KL divergence is non-negative, so is a lower bound on the log likelihood. The E step sets , which makes the KL term zero and the bound touch the log likelihood at . With this , equals plus a constant that does not depend on (Eq. 9.74). The M step raises the bound by maximizing . 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 as the responsibilities computed at the starting parameters and evaluates both terms at the parameters after the first M step.Code
Code
Inliers and outliers as a two-component mixture
A mixture can also model measurements that should not be trusted. Consider fitting a line with to pairs , in the notation of the linear regression section. Ordinary least squares assumes every target is the line plus Gaussian noise of variance . Suppose instead that a fraction of the points are gross errors, spread over the whole range of . 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: The mixing coefficient is the inlier fraction. It differs from the earlier mixture in two ways: the Gaussian component is conditional on , 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 , the targets as and the noise as a precision ; the equations below are his with those symbols replaced. The E step computes the responsibility of the inlier component for each point, The M step maximizes the expected complete-data log likelihood. For it is a weighted least squares problem, with the responsibilities as weights. With and the design matrix whose rows are , the solution is (Bishop, Eq. 14.42) The noise variance and the inlier fraction follow the same pattern as and : These are the single-component versions of Bishop’s Eqs. 14.44 and 14.38. With for every point, the weighted solution reduces to the ordinary least squares solution (Bishop, Eq. 3.15). The data below have 60 points. 42 lie on the line with noise of standard deviation 0.3, and 18 are outliers drawn uniformly over the range of .Code
Code
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.

