Seyed Masoud Hosseini · Overview · Study log · Ideas · Transcript · RSS feed
Machine Learning · Lecture 15 of 21 · 1:19:47
Lecture 14: EM Algorithm and Factor Analysis
Study guide
What this lecture covers
This lecture continues directly from the EM derivation in the previous session, first showing exactly how the abstract E-step and M-step turn into the concrete update equations for a mixture of Gaussians, and reframing EM as a coordinate ascent algorithm. It then spends most of its time on a new question: how do you model a probability distribution p(x) when your data is high-dimensional but you only have a handful of examples, so few that a standard Gaussian's covariance matrix becomes singular and unusable? The answer is the factor analysis model.
This builds on the previous lecture's mixture of Gaussians and EM derivation, and assumes you are comfortable with multivariate Gaussians, covariance matrices, and the E-step/M-step structure. After watching, you should be able to explain why a naive Gaussian fit fails when the number of examples is smaller than the dimension, set up the factor analysis model with its latent variable z, and follow the key steps used to derive EM for it, including computing Gaussian marginals and conditionals.
Key ideas
- Mapping EM to a concrete model: the abstract E-step (construct a tight lower bound) and M-step (maximize it) become specific formulas once you plug in a particular model, such as
w_ijfor the mixture of Gaussians E-step and closed-form updates formu,phi, andSigmain the M-step. - EM as coordinate ascent: defining
J(theta, Q)as the lower bound, the E-step maximizesJwith respect toQand the M-step maximizes it with respect totheta, alternating one variable at a time while never decreasing the log-likelihood. - When Gaussian mixtures don't apply: mixture of Gaussians works well when you have far more examples than dimensions; when the number of examples
mis close to or smaller than the number of dimensionsn, a single Gaussian's maximum likelihood covariance matrix becomes singular and non-invertible. - Restricting the covariance matrix: forcing the covariance to be diagonal (uncorrelated features) or a scalar multiple of the identity (equal variance, uncorrelated features) avoids the singularity but assumes away the correlations you may actually care about.
- The factor analysis model: assumes a low-dimensional latent Gaussian
z(dimensiond < n) generates the high-dimensional observationx = mu + Lambda*z + epsilon, whereepsilonis independent Gaussian noise with diagonal covariancePsi. - Geometric intuition: factor analysis models high-dimensional data as lying roughly on a lower-dimensional subspace (a line, plane, or higher-dimensional analogue) with a bit of noise off that subspace.
- Gaussian marginals and conditionals: deriving EM for factor analysis requires knowing that a subvector of a Gaussian is itself Gaussian, and that one subvector conditioned on another is also Gaussian, with mean and covariance given by standard formulas.
- E-step as a continuous distribution: because
zis continuous rather than discrete here, the E-step does not store a list of probabilities likew_ij, but instead computes the mean and covariance of a Gaussian distribution overzgivenx.
Walkthrough
Recapping EM and mapping it to the mixture of Gaussians updates (1:54)
The lecture reviews the general EM framework from the previous session, where the E-step constructs a lower bound tight at the current parameters and the M-step maximizes it. It then works through how this abstract framework produces the specific formulas seen for the mixture of Gaussians: the E-step computes w_ij, the posterior probability of each latent assignment via Bayes' rule, and the M-step takes derivatives of the resulting double sum with respect to phi, mu, and Sigma and sets them to zero to get closed-form updates.
EM as coordinate ascent (16:06)
Defining a function J(theta, Q) equal to the lower bound used in the E-step, the lecture shows that L(theta) >= J(theta, Q) for any Q, and that the E-step is exactly maximizing J with respect to Q while the M-step maximizes J with respect to theta. This reframes EM as coordinate ascent on J, alternating between the two variables, and gives a practical way to monitor convergence: plot the value of J or the log-likelihood across iterations and watch for it to plateau.
Why mixture of Gaussians fails on small, high-dimensional data (17:23)
The lecture introduces factor analysis by contrast: mixture of Gaussians works well when you have many more examples than dimensions, but fails when dimensions exceed or roughly match the number of examples. The motivating example is a Stanford building with around 100 temperature sensors but only around 30 days of readings, where the goal is to model the joint distribution of sensor readings to detect anomalies such as an open window. A second example, from the model's psychometric origins, involves scoring 100 survey questions from only 30 respondents.
The singular covariance problem and its partial fixes (24:03)
When the number of examples is at or below the number of dimensions, the maximum likelihood covariance matrix for a single Gaussian becomes singular, meaning its determinant is zero and it cannot be inverted, so the Gaussian density formula breaks down. The lecture illustrates this with a two-point, two-dimensional example where the fitted Gaussian collapses onto an infinitely thin line. Two workarounds are shown and both are treated as inadequate: forcing the covariance matrix to be diagonal removes off-diagonal correlations entirely, and forcing it to be a scalar multiple of the identity additionally forces every feature to have equal variance. Both avoid the singularity but assume features are uncorrelated, which is usually unrealistic and is presented as a stepping stone toward factor analysis rather than a real solution.
Defining the factor analysis model (40:34)
Factor analysis introduces a continuous latent variable z, drawn from a standard Gaussian in a lower dimension d, and models the observed data as x = mu + Lambda*z + epsilon, where Lambda is an n by d matrix and epsilon is Gaussian noise with a diagonal covariance Psi. The lecture works through low-dimensional geometric examples, mapping a one-dimensional z into two dimensions and a two-dimensional z into three dimensions, to show how this produces data clustered near a line or plane with added noise. This gives the intuition for how high-dimensional data (say, 100 sensor readings) can be explained by a small number of underlying driving factors plus independent per-sensor noise.
Deriving EM for factor analysis (42:29)
Because there is no closed-form maximum likelihood solution for factor analysis, the lecture derives EM for it, describing this as one of the trickier derivations in the course. It first establishes that a subvector of a jointly Gaussian vector is itself Gaussian (the marginal) and that one part conditioned on another is also Gaussian with standard formulas for the conditional mean and covariance. Using these, the joint distribution of z and x is shown to be Gaussian, letting the E-step represent the posterior over z given x as a Gaussian with a specific mean and covariance rather than a discrete list of probabilities. The lecture also highlights a general trick for the M-step: when an integral against a Gaussian density has a log in front, it usually simplifies to a quadratic function, and when it is really computing an expectation, it can often be replaced directly by an already-computed mean rather than expanded as an integral.
Before you watch
- Watch the previous lecture on the EM algorithm and mixture of Gaussians, since this lecture builds directly on its E-step and M-step derivation.
- Review the properties of the multivariate Gaussian distribution, particularly marginal and conditional distributions, since these are used as building blocks for the factor analysis derivation.
- Be comfortable with matrix notation for covariance matrices, including what it means for a matrix to be singular or non-invertible.
Check your understanding
- Why does the maximum likelihood covariance matrix for a single Gaussian become singular when the number of examples is smaller than the number of dimensions?
- In what sense is EM a form of coordinate ascent, and what two things does it alternate optimizing?
- What do the parameters
mu,Lambda, andPsirepresent in the factor analysis modelx = mu + Lambda*z + epsilon? - Why does restricting the covariance matrix to be diagonal or a scalar multiple of the identity solve the singularity problem but still produce an unsatisfying model?
- Why is the E-step for factor analysis represented as a Gaussian distribution over
zrather than a list of probabilities like in the mixture of Gaussians?
Chapters
- 0:00 <Untitled Chapter 1>
- 1:03 The Factor Analysis Model
- 1:17 Properties of Gaussian Distributions
- 1:54 Recap
- 3:20 Mixture of Gaussians Model
- 16:06 Coordinate Ascent
- 17:23 Factor Analysis
- 17:42 Factor Analysis Algorithm
- 21:51 Applying a Gaussian Model
- 24:03 Non Invertible Matrix
- 25:06 Contours of Gaussian Densities
- 26:55 Origins of the Factor Analysis Model
- 28:13 Alternatives
- 30:06 Covariance Matrix
- 40:34 Factor Analysis Model
- 42:29 Conditional Distribution
- 43:28 Examples
- 45:54 Examples of the Types of Data Factor Analysis Can Model
- 1:11:30 So How Do You Represent Qi of Zi in a Computer It Turns Out that Using the Formulas We Have for the Marginal Excuse Me for the Conditional Distribution of a Gaussian It Turns Out that if You Compute this Right Hand Side You'Ll Find that Zi Given Xi this Is Going To Be Gaussian with some Mean and some Covariance Right Where It's Basically those Formulas Mu of Zi Given Xi Is Equal to if You Kind Of Take that Foam and Then Apply It all Thing Here Is 0 Plus Lambda Transpose Okay so these Equations Are Exactly these Two Equations Right Maps To Map to that Big Gaussian Density That We Have Okay so What You Would Do in the East Step Is Compute this and Compute this Compute this Vector in Computers Matrix and Saw that Sorters in You Know Store these as Variables and Your Representation of the Qi Is that Qi Is a Gaussian Density Right with this Mean and Disco Beer so this Is What You Actually Compute To Represent Qi All Right So Step Two Was To Write the E Step
From the YouTube description
For more information about Stanford’s Artificial Intelligence professional and graduate programs, visit: https://stanford.io/ai
Andrew Ng
Adjunct Professor of Computer Science
https://www.andrewng.org/
To follow along with the course schedule and syllabus, visit:
http://cs229.stanford.edu/syllabus-autumn2018.html
← Lecture 13: Expectation-Maximization Algorithms · Lecture 15: PCA and ICA →
