Seyed Masoud Hosseini · Overview · Study log · Weekly summaries · Ideas · Search · Transcript · RSS feed

Machine Learning · Lecture 14 of 21 · 1:20:31

Lecture 13: Expectation-Maximization Algorithms

Lecture 13 - Expectation-Maximization Algorithms | Stanford CS229: Machine Learning (Autumn 2018) on YouTube

Study guide

What this lecture covers

This lecture opens the course's unsupervised learning unit, answering a different question from everything before it: given only unlabeled data x, what structure can you find without any labels y? It starts with k-means clustering as a concrete, intuitive algorithm, then moves to density estimation and anomaly detection, and finally derives the expectation-maximization (EM) algorithm, first informally for fitting a mixture of Gaussians and then rigorously using Jensen's inequality.

The lecture builds directly on the generative learning algorithms covered earlier in the course, particularly Gaussian discriminant analysis, since the mixture of Gaussians model is essentially GDA with the class label replaced by a hidden variable you never observe. After watching, you should be able to run k-means by hand, explain why it converges, set up the mixture of Gaussians model with a latent variable, and follow the E-step and M-step of EM both as a "guess and refit" heuristic and as a lower-bound maximization procedure.

Key ideas

  • Unsupervised learning: given a training set of only x_1 through x_m with no labels, the goal is to find interesting structure in the data.
  • K-means clustering: alternates between assigning each point to its nearest cluster centroid and moving each centroid to the mean of its assigned points, and this process is guaranteed to converge because it monotonically decreases a non-negative cost function.
  • Choosing k: there is no single correct number of clusters; Ng recommends choosing it based on the downstream use of the clusters (for example, how many marketing campaigns a team can realistically run) rather than an automatic criterion.
  • Anomaly detection via density estimation: model p(x) from unlabeled data and flag new examples with low probability as potential anomalies, useful when no single feature is unusual but a combination of features is.
  • Mixture of Gaussians: models complex, multi-modal densities by assuming each example was generated by one of several Gaussians, selected by a hidden (latent) variable z that is never observed.
  • EM as soft k-means: instead of hard-assigning each point to one cluster, the E-step computes a probability (w_ij) that each point belongs to each Gaussian, and the M-step re-estimates the Gaussians using these weights.
  • EM as lower-bound maximization: the rigorous derivation constructs a lower bound on the log-likelihood that touches it at the current parameters (E-step), then maximizes that lower bound (M-step), which guarantees the log-likelihood never decreases.
  • Jensen's inequality: for a concave function like log, the expectation of the function is less than or equal to the function of the expectation, with equality only when the underlying random variable is constant; this is the tool used to build EM's lower bound.

Walkthrough

From supervised to unsupervised learning and market segmentation (1:54)

The lecture contrasts supervised learning, where you have labeled positive and negative examples, with unsupervised learning, where the training set consists only of unlabeled points x_1 through x_m. Clustering is introduced as the first unsupervised algorithm, with market segmentation as the motivating example: a company with a large user database can run clustering to discover natural groups of customers by attributes like age, education, or region, without having predefined labels for those groups.

K-means clustering, step by step (5:37)

Using a live animation, the lecture walks through k-means: initialize two cluster centroids (commonly by picking k random training examples), then repeat two steps until nothing changes. First, assign each point to the closer of the two centroids. Second, move each centroid to the mean of the points currently assigned to it. The lecture writes this out formally with the assignment variable c_i and centroid variables mu_1 through mu_k, and clarifies that the standard distance measure is the L2 norm, with or without squaring giving the same result.

Why k-means converges, and how to choose k (12:10)

K-means minimizes a cost function equal to the sum of squared distances between each point and its assigned centroid. Because this cost is non-negative and provably decreases on every iteration of the two-step process, it must eventually stop decreasing, which is what convergence means here. In practice, k-means can also get stuck in local minima, so a common fix is to rerun it from many random initializations and keep the run with the lowest cost. Choosing the number of clusters k is treated as inherently ambiguous; rather than using automatic criteria, the lecture recommends choosing k based on what the clusters will be used for.

Density estimation and anomaly detection (16:32)

The lecture shifts to a related but different problem: given unlabeled data such as vibration and heat readings from aircraft engines, model the density p(x) and flag new examples with low probability as anomalies. This approach is described as used in practice for detecting unusual cell-tower network behavior and for computer security monitoring. The key illustration is a case where neither individual feature is out of range, but the specific combination of the two is unusual, which simple per-feature thresholds would miss.

The mixture of Gaussians model (20:40)

To model densities that no single textbook distribution can fit, such as an L-shaped region of probability mass, the lecture introduces a latent variable z that indicates which of several Gaussians generated each example. This closely parallels Gaussian discriminant analysis, except z is multinomial rather than binary, each Gaussian can have its own covariance Sigma_j, and, critically, z is never observed in the training data. If z were known, maximum likelihood estimation would reduce to fitting each Gaussian separately to its labeled points, using formulas nearly identical to GDA.

The EM algorithm's E-step and M-step (31:44)

Since the true values of z are unknown, EM alternates between guessing them and refitting the model. The E-step computes w_ij, the posterior probability that example i came from Gaussian j, using a Bayes' rule calculation that combines the Gaussian density and the multinomial prior over z. The lecture emphasizes that this is a soft assignment: unlike k-means' hard 0-or-1 assignment, each point can be partially assigned to multiple Gaussians, with its weights summing to 1. The M-step then reuses the maximum-likelihood formulas from the "if only we knew z" case, replacing the indicator of z_i = j with the soft weight w_ij.

The rigorous derivation via Jensen's inequality (48:12)

Because the earlier derivation is described as hand-wavy, the lecture builds a formal justification. Jensen's inequality states that for a concave function such as log, E[log(X)] <= log(E[X]), with equality only when X is constant. Applying this to the log-likelihood by introducing an auxiliary distribution Q_i(z_i) over the latent variable produces a lower bound on the log-likelihood as a function of the parameters. Choosing Q_i to make this bound tight at the current parameter value recovers exactly the E-step weights w_ij, and maximizing the resulting lower bound recovers exactly the M-step update, showing that each EM iteration provably does not decrease the log-likelihood and converges to a local optimum.

Before you watch

  • Review Gaussian discriminant analysis and Bayes' rule, since the mixture of Gaussians model and the E-step's posterior calculation build directly on them.
  • Be comfortable with maximum likelihood estimation and taking derivatives of a log-likelihood, as used in problem set 1's supervised setting.
  • A basic notion of convexity or concavity of a function will make the Jensen's inequality derivation easier to follow.

Check your understanding

  1. Why is the k-means cost function guaranteed to decrease on every iteration, and why does that guarantee convergence?
  2. How does the mixture of Gaussians model extend Gaussian discriminant analysis, and what role does the latent variable z play?
  3. What is the difference between the "hard assignment" in k-means and the "soft assignment" computed in EM's E-step?
  4. State Jensen's inequality for a concave function, and explain the condition under which it holds with equality.
  5. How does choosing Q_i(z_i) to make Jensen's inequality tight lead to the specific formula used in the E-step?

Vocabulary

unsupervised learning (noun)
Finding structure in data that has no labels.
Unsupervised learning finds groups in data without any given labels.
clustering (noun)
Grouping similar data points together based on shared characteristics.
Clustering can discover natural customer segments.
centroid (noun)
The center point representing the average position of a group of points.
Each cluster's centroid is the average of its assigned points.
k-means (noun)
An algorithm that groups data into k clusters by alternating assignment and centroid updates.
K-means assigns each point to its nearest centroid.
converge (verb)
To settle toward a stable final result.
K-means is guaranteed to converge because its cost keeps decreasing.
local minimum (noun)
A point that looks best nearby but might not be the overall best solution.
K-means can get stuck in a local minimum depending on initialization.
density estimation (noun)
Modeling how likely different values or regions of data are.
Density estimation models p(x) to detect unusual data points.
anomaly detection (noun)
Identifying data points that are unusual compared to the rest of the dataset.
Anomaly detection flags aircraft engine readings that look unusual.
multi-modal (adjective)
Having several separate peaks or clusters in its distribution.
A multi-modal density can't be fit by a single simple distribution.
latent variable (noun)
A hidden variable that is never directly observed in the data.
The latent variable z indicates which Gaussian generated each point.
mixture of Gaussians (noun)
A model assuming data comes from a combination of several Gaussian distributions.
A mixture of Gaussians can fit complex, multi-modal data.
expectation-maximization (EM) (noun)
An algorithm that alternates between guessing hidden values and refitting model parameters.
EM fits the mixture of Gaussians without knowing the true group labels.
E-step (noun)
The step in EM that estimates the probability of hidden assignments given current parameters.
The E-step computes each point's probability of belonging to each Gaussian.
M-step (noun)
The step in EM that updates model parameters based on the current estimated assignments.
The M-step re-estimates the Gaussians using the E-step's weights.
soft assignment (noun)
Assigning partial membership across multiple groups instead of a single definite choice.
EM uses soft assignment, unlike k-means' hard assignment.
posterior probability (noun)
The updated probability of something after taking new evidence into account.
The E-step computes the posterior probability of each hidden group.
Jensen's inequality (noun)
A rule stating that for a concave function, its average value is at least the function of the average input.
Jensen's inequality is used to build EM's lower bound.
lower bound (noun)
A guaranteed minimum value that a quantity cannot fall below.
EM maximizes a lower bound on the log-likelihood.
log-likelihood (noun)
The logarithm of how probable the observed data is under a model.
EM guarantees the log-likelihood never decreases.
L2 norm (noun)
The standard measure of a vector's length using the square root of summed squares.
K-means uses the L2 norm as its distance measure.
market segmentation (noun)
Dividing customers into groups based on shared characteristics.
Clustering supports market segmentation for a company's user base.
vibration reading (noun)
A measurement of mechanical shaking or oscillation from a sensor.
Vibration readings from aircraft engines feed the anomaly detector.

Chapters

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

0:00 Introduction
1:15 Unsupervised learning
1:38 First unsupervised learning algorithm
1:54 Market Segmentation
5:33 Clustering algorithm
5:37 K-means clustering
5:52 Initialize the cluster centroids
12:10 Cost function
16:32 Density Estimation
18:01 Anomaly Detection
20:40 Mixture of Gaussians Volatile
29:27 Maximum Likelihood Estimates
31:44 Bayes Rule
48:12 Jensen's Inequality
57:57 Density Estimation Problem
59:32 Maximum Likelihood Estimation
1:07:16 Concave form of Jensen's Inequality

← Lecture 12: Debugging ML Models and Error Analysis · Lecture 14: EM Algorithm and Factor Analysis →