CS229 Lecture notes 
Andrew Ng 
Part IX 
The EM algorithm 
In the previous set of notes, we talked about the EM algorithm as applied to 
fitting a mixture of Gaussians. In this set of notes, we give a broader view 
of the EM algorithm, and show how it can be applied to a large family of 
estimation problems with latent variables. We begin our discussion with a 
very useful result called Jensen’s inequality 
1 Jensen’s inequality 
Let f be a function whose domain is the set of real numbers. Recall that 
f is a convex function if f′′(x) ≥ 0 (for all x ∈ R). In the case of f taking 
vector-valued inputs, this is generalized to the condition that its hessian H 
is positive semi-definite (H ≥ 0). If f′′(x) > 0 for all x, then we say f is 
strictly convex (in the vector-valued case, the corresponding statement is 
that H must be positive definite, written H > 0). Jensen’s inequality can 
then be stated as follows: 
Theorem. Let f be a convex function, and let X be a random variable. 
Then: 
E[f(X)] ≥ f(EX). 
Moreover, if f is strictly convex, then E[f(X)] = f(EX) holds true if and 
only if X = E[X] with probability 1 (i.e., if X is a constant). 
Recall our convention of occasionally dropping the parentheses when writ- 
ing expectations, so in the theorem above, f(EX) = f(E[X]). 
For an interpretation of the theorem, consider the figure below. 
1
2 
a E[X] b 
f(a) 
E[f(X)] 
f(b) 
f(EX) 
f 
Here, f is a convex function shown by the solid line. Also, X is a random 
variable that has a 0.5 chance of taking the value a, and a 0.5 chance of 
taking the value b (indicated on the x-axis). Thus, the expected value of X 
is given by the midpoint between a and b. 
We also see the values f(a), f(b) and f(E[X]) indicated on the y-axis. 
Moreover, the value E[f(X)] is now the midpoint on the y-axis between f(a) 
and f(b). From our example, we see that because f is convex, it must be the 
case that E[f(X)] ≥ f(EX). 
Incidentally, quite a lot of people have trouble remembering which way 
the inequality goes, and remembering a picture like this is a good way to 
quickly figure out the answer. 
Remark. Recall that f is [strictly] concave if and only if −f is [strictly] 
convex (i.e., f′′(x) ≤ 0 or H ≤ 0). Jensen’s inequality also holds for concave 
functions f, but with the direction of all the inequalities reversed (E[f(X)] ≤ 
f(EX), etc.). 
2 The EM algorithm 
Suppose we have an estimation problem in which we have a training set 
{x(1), . . . , x(m)} consisting of m independent examples. We wish to fit the 
parameters of a model p(x, z) to the data, where the likelihood is given by 
ℓ(θ) = 
m 
Xi=1 
log p(x; θ) 
= 
m 
Xi=1 
logXz 
p(x, z; θ).
3 
But, explicitly finding the maximum likelihood estimates of the parameters θ 
may be hard. Here, the z(i)’s are the latent random variables; and it is often 
the case that if the z(i)’s were observed, then maximum likelihood estimation 
would be easy. 
In such a setting, the EM algorithm gives an efficient method for max- 
imum likelihood estimation. Maximizing ℓ(θ) explicitly might be difficult, 
and our strategy will be to instead repeatedly construct a lower-bound on ℓ 
(E-step), and then optimize that lower-bound (M-step). 
For each i, let Qi be some distribution over the z’s (Pz Qi(z) = 1, Qi(z) ≥ 
0). Consider the following:1 
Xi 
log p(x(i); θ) = Xi 
log(i) 
z 
Xp(x(i), z(i); θ) (1) 
= Xi 
log(i) 
z 
XQi(z(i)) 
p(x(i), z(i); θ) 
Qi(z(i)) 
(2) 
≥ Xi (i) 
z 
XQi(z(i)) log 
p(x(i), z(i); θ) 
Qi(z(i)) 
(3) 
The last step of this derivation used Jensen’s inequality. Specifically, f(x) = 
log x is a concave function, since f′′(x) = −1/x2 < 0 over its domain x ∈ R+. 
Also, the term 
Xz 
(i) 
Qi(z(i)) p(x(i), z(i); θ) 
Qi(z(i))  
in the summation is just an expectation of the quantity p(x(i), z(i); θ)/Qi(z(i)) with respect to z(i) drawn according to the distribution given by Qi. By 
Jensen’s inequality, we have 
f Ez(i)∼Qi p(x(i), z(i); θ) 
Qi(z(i))  ≥ Ez(i)∼Qi f p(x(i), z(i); θ) 
Qi(z(i)) , 
where the “z(i) ∼ Qi” subscripts above indicate that the expectations are 
with respect to z(i) drawn from Qi. This allowed us to go from Equation (2) 
to Equation (3). 
Now, for any set of distributions Qi, the formula (3) gives a lower-bound 
on ℓ(θ). There’re many possible choices for the Qi’s. Which should we 
choose? Well, if we have some current guess θ of the parameters, it seems 
1If z were continuous, then Qi would be a density, and the summations over z in our 
discussion are replaced with integrals over z.
4 
natural to try to make the lower-bound tight at that value of θ. I.e., we’ll 
make the inequality above hold with equality at our particular value of θ. 
(We’ll see later how this enables us to prove that ℓ(θ) increases monotonically 
with successsive iterations of EM.) 
To make the bound tight for a particular value of θ, we need for the step 
involving Jensen’s inequality in our derivation above to hold with equality. 
For this to be true, we know it is sufficient that that the expectation be taken 
over a “constant”-valued random variable. I.e., we require that 
p(x(i), z(i); θ) 
Qi(z(i)) 
= c 
for some constant c that does not depend on z(i). This is easily accomplished 
by choosing 
Qi(z(i)) ∝ p(x(i), z(i); θ). 
Actually, since we know Pz Qi(z(i)) = 1 (because it is a distribution), this 
further tells us that 
Qi(z(i)) = 
p(x(i), z(i); θ) 
Pz p(x(i), z; θ) 
= 
p(x(i), z(i); θ) 
p(x(i); θ) 
= p(z(i)|x(i); θ) 
Thus, we simply set the Qi’s to be the posterior distribution of the z(i)’s 
given x(i) and the setting of the parameters θ. 
Now, for this choice of the Qi’s, Equation (3) gives a lower-bound on the 
loglikelihood ℓ that we’re trying to maximize. This is the E-step. In the 
M-step of the algorithm, we then maximize our formula in Equation (3) with 
respect to the parameters to obtain a new setting of the θ’s. Repeatedly 
carrying out these two steps gives us the EM algorithm, which is as follows: 
Repeat until convergence { 
(E-step) For each i, set 
Qi(z(i)) := p(z(i)|x(i); θ). 
(M-step) Set 
 Xi Xz 
θ := argmax 
(i) 
Qi(z(i)) log 
p(x(i), z(i); θ) 
Qi(z(i)) 
.
5 
} 
How we we know if this algorithm will converge? Well, suppose θ(t) 
and θ(t+1) are the parameters from two successive iterations of EM. We will 
now prove that ℓ(θ(t)) ≤ ℓ(θ(t+1)), which shows EM always monotonically 
improves the log-likelihood. The key to showing this result lies in our choice 
of the Qi’s. Specifically, on the iteration of EM in which the parameters had 
started out as θ(t), we would have chosen Q(t) 
i (z(i)) := p(z(i)|x(i); θ(t)). We 
saw earlier that this choice ensures that Jensen’s inequality, as applied to get 
Equation (3), holds with equality, and hence 
z 
ℓ(θ(t)) =Xi (i) 
XQ(t) 
i (z(i)) log 
p(x(i), z(i); θ(t)) 
Q(t) 
i (z(i)) 
. 
The parameters θ(t+1) are then obtained by maximizing the right hand side 
of the equation above. Thus, 
z 
ℓ(θ(t+1)) ≥ Xi (i) 
XQ(t) 
i (z(i)) log 
p(x(i), z(i); θ(t+1)) 
Q(t) 
i (z(i)) 
(4) 
≥ Xi (i) 
z 
XQ(t) 
i (z(i)) log 
p(x(i), z(i); θ(t)) 
Q(t) 
i (z(i)) 
(5) 
= ℓ(θ(t)) (6) 
This first inequality comes from the fact that 
ℓ(θ) ≥Xi Xz(i) 
Qi(z(i)) log 
p(x(i), z(i); θ) 
Qi(z(i)) 
holds for any values of Qi and θ, and in particular holds for Qi = Q(t) 
i , 
θ = θ(t+1). To get Equation (5), we used the fact that θ(t+1) is chosen 
explicitly to be 
 Xi Xz 
argmax 
(i) 
Qi(z(i)) log 
p(x(i), z(i); θ) 
Qi(z(i)) 
, 
and thus this formula evaluated at θ(t+1) must be equal to or larger than the 
same formula evaluated at θ(t). Finally, the step used to get (6) was shown 
earlier, and follows from Q(t) 
i having been chosen to make Jensen’s inequality 
hold with equality at θ(t).
6 
Hence, EM causes the likelihood to converge monotonically. In our de- 
scription of the EM algorithm, we said we’d run it until convergence. Given 
the result that we just showed, one reasonable convergence test would be 
to check if the increase in ℓ(θ) between successive iterations is smaller than 
some tolerance parameter, and to declare convergence if EM is improving 
ℓ(θ) too slowly. 
Remark. If we define 
z 
J(Q, θ) =Xi (i) 
XQi(z(i)) log 
p(x(i), z(i); θ) 
Qi(z(i)) 
, 
then we know ℓ(θ) ≥ J(Q, θ) from our previous derivation. The EM can also 
be viewed a coordinate ascent on J, in which the E-step maximizes it with 
respect to Q (check this yourself), and the M-step maximizes it with respect 
to θ. 
3 Mixture of Gaussians revisited 
Armed with our general definition of the EM algorithm, let’s go back to our 
old example of fitting the parameters φ, μ and  in a mixture of Gaussians. 
For the sake of brevity, we carry out the derivations for the M-step updates 
only for φ and μj , and leave the updates for j as an exercise for the reader. 
The E-step is easy. Following our algorithm derivation above, we simply 
calculate 
w(i) 
j = Qi(z(i) = j) = P(z(i) = j|x(i); φ, μ,). 
Here, “Qi(z(i) = j)” denotes the probability of z(i) taking the value j under 
the distribution Qi. 
Next, in the M-step, we need to maximize, with respect to our parameters 
φ, μ,, the quantity 
m 
Xi=1(i) 
z 
XQi(z(i)) log 
p(x(i), z(i); φ, μ,) 
Qi(z(i)) 
= 
m 
Xi=1 
k 
Xj=1 
Qi(z(i) = j) log 
p(x(i)|z(i) = j; μ,)p(z(i) = j; φ) 
Qi(z(i) = j) 
= 
m 
Xi=1 
k 
Xj=1 
w(i) 
j log 
1 
2 (x(i) − μj)T−1 
(2)n/2|j |1/2 exp −1 
j (x(i) − μj) · φj 
w(i) 
j
7 
Let’s maximize this with respect to μl. If we take the derivative with respect 
to μl, we find 
∇μl 
m 
Xi=1 
k 
Xj=1 
w(i) 
j log 
1 
exp −1 
(2)n/2|j |1/2 2 (x(i) − μj)T−1 
j (x(i) − μj) · φj 
w(i) 
j 
= −∇μl 
m 
Xi=1 
k 
Xj=1 
w(i) 
j 
1 
2 
(x(i) − μj)T−1 
j (x(i) − μj) 
= 
1 
2 
m 
Xi=1 
w(i) 
l −1 
l ∇μl2μT 
l x(i) − μT 
l −1 
l μl 
= 
m 
Xi=1 
w(i) 
l −1 
l x(i) − −1 
l μl 
Setting this to zero and solving for μl therefore yields the update rule 
i=1 w(i) 
μl := Pm 
l x(i) 
i=1 w(i) 
Pm 
l 
, 
which was what we had in the previous set of notes. 
Let’s do one more example, and derive the M-step update for the param- 
eters φj . Grouping together only the terms that depend on φj , we find that 
we need to maximize 
m 
Xi=1 
k 
Xj=1 
w(i) 
j log φj . 
However, there is an additional constraint that the φj ’s sum to 1, since they 
represent the probabilities φj = p(z(i) = j; φ). To deal with the constraint 
that Pk 
φj = 1, we construct the Lagrangian 
j=1 L(φ) = 
m 
Xi=1 
k 
Xj=1 
w(i) 
j log φj + β( 
k 
Xj=1 
φj − 1), 
where β is the Lagrange multiplier.2 Taking derivatives, we find 
∂ 
∂φj 
L(φ) = 
m 
Xi=1 
w(i) 
j 
φj 
+ 1 
2We don’t need to worry about the constraint that j ≥ 0, because as we’ll shortly see, 
the solution we’ll find from this derivation will automatically satisfy that anyway.
8 
Setting this to zero and solving, we get 
i=1 w(i) 
φj = Pm 
j 
−β 
i=1 w(i) 
I.e., φj ∝ Pm 
j . Using the constraint that Pj φj = 1, we easily find 
j=1 w(i) 
that −β = Pm 
i=1Pk 
i=1 1 = m. (This used the fact that w(i) 
j = Pm 
j = 
Qi(z(i) = j), and since probabilities sum to 1, Pj w(i) 
j = 1.) We therefore 
have our M-step updates for the parameters φj : 
φj := 
1 
m 
m 
Xi=1 
w(i) 
j . 
The derivation for the M-step updates to j are also entirely straightfor- 
ward.

More Related Content

PDF
Cs229 notes8
PDF
Harmonic Analysis and Deep Learning
PDF
Matrix calculus
PDF
Paper finance hosseinkhan_remy
PDF
Polya recurrence
PDF
Fixed Point Results In Fuzzy Menger Space With Common Property (E.A.)
PPTX
Predicates and Quantifiers
PPTX
Project in Calcu
Cs229 notes8
Harmonic Analysis and Deep Learning
Matrix calculus
Paper finance hosseinkhan_remy
Polya recurrence
Fixed Point Results In Fuzzy Menger Space With Common Property (E.A.)
Predicates and Quantifiers
Project in Calcu

What's hot (19)

PPTX
2 integration and the substitution methods x
PDF
Lecture 2 predicates quantifiers and rules of inference
PDF
Slides mc gill-v3
PDF
Sildes buenos aires
PDF
PaperNo10-KaramiHabibiSafariZarrabi-IJCMS
PDF
Machine learning (1)
PDF
Slides simplexe
PDF
Notes on Intersection theory
PDF
Slides econ-lm
PPTX
5.3 areas, riemann sums, and the fundamental theorem of calaculus
PDF
Slides mc gill-v4
PPTX
Presentation mathmatic 3
DOC
PPTX
Limits, continuity, and derivatives
PDF
A common fixed point theorems in menger space using occationally weakly compa...
PDF
Testing for Extreme Volatility Transmission
PPTX
Limits and their applications
PDF
Convex Analysis and Duality (based on "Functional Analysis and Optimization" ...
PDF
masters-presentation
2 integration and the substitution methods x
Lecture 2 predicates quantifiers and rules of inference
Slides mc gill-v3
Sildes buenos aires
PaperNo10-KaramiHabibiSafariZarrabi-IJCMS
Machine learning (1)
Slides simplexe
Notes on Intersection theory
Slides econ-lm
5.3 areas, riemann sums, and the fundamental theorem of calaculus
Slides mc gill-v4
Presentation mathmatic 3
Limits, continuity, and derivatives
A common fixed point theorems in menger space using occationally weakly compa...
Testing for Extreme Volatility Transmission
Limits and their applications
Convex Analysis and Duality (based on "Functional Analysis and Optimization" ...
masters-presentation
Ad

Viewers also liked (11)

PDF
Programming methodology lecture24
PDF
Computer network (13)
PDF
Machine learning (13)
PDF
Computer network (6)
PDF
Programming methodology lecture18
PDF
Programming methodology lecture07
PPTX
PDF
Computer network (9)
PDF
Programming methodology lecture20
PPTX
Convicts In Australia
PPTX
Moneypad- the future wallet
Programming methodology lecture24
Computer network (13)
Machine learning (13)
Computer network (6)
Programming methodology lecture18
Programming methodology lecture07
Computer network (9)
Programming methodology lecture20
Convicts In Australia
Moneypad- the future wallet
Ad

Similar to Machine learning (9) (20)

PDF
Jensen's inequality, EM 알고리즘
PPTX
Learning group em - 20171025 - copy
PPTX
lecture_11.pptx
PDF
Cs229 notes7b
PDF
expectation maximization and Guassian Mixture.pdf
PDF
Machine learning (8)
PDF
Lecture9 xing
PDF
Machine Learning With MapReduce, K-Means, MLE
PDF
Expectation maximization
PDF
A Tutorial of the EM-algorithm and Its Application to Outlier Detection
PDF
07 Machine Learning - Expectation Maximization
PDF
2012 mdsp pr12 k means mixture of gaussian
PDF
SAS Homework Help
PPTX
lecture_16 Expectation – Maximization pptx
PDF
Comparing estimation algorithms for block clustering models
PDF
Estimationtheory2
PDF
A Solution Manual and Notes for The Elements of Statistical Learning.pdf
PDF
The dual geometry of Shannon information
PDF
On learning statistical mixtures maximizing the complete likelihood
PDF
k-MLE: A fast algorithm for learning statistical mixture models
Jensen's inequality, EM 알고리즘
Learning group em - 20171025 - copy
lecture_11.pptx
Cs229 notes7b
expectation maximization and Guassian Mixture.pdf
Machine learning (8)
Lecture9 xing
Machine Learning With MapReduce, K-Means, MLE
Expectation maximization
A Tutorial of the EM-algorithm and Its Application to Outlier Detection
07 Machine Learning - Expectation Maximization
2012 mdsp pr12 k means mixture of gaussian
SAS Homework Help
lecture_16 Expectation – Maximization pptx
Comparing estimation algorithms for block clustering models
Estimationtheory2
A Solution Manual and Notes for The Elements of Statistical Learning.pdf
The dual geometry of Shannon information
On learning statistical mixtures maximizing the complete likelihood
k-MLE: A fast algorithm for learning statistical mixture models

More from NYversity (20)

PDF
Programming methodology-1.1
PDF
3016 all-2007-dist
PDF
Programming methodology lecture28
PDF
Programming methodology lecture27
PDF
Programming methodology lecture26
PDF
Programming methodology lecture25
PDF
Programming methodology lecture23
PDF
Programming methodology lecture22
PDF
Programming methodology lecture19
PDF
Programming methodology lecture17
PDF
Programming methodology lecture16
PDF
Programming methodology lecture15
PDF
Programming methodology lecture14
PDF
Programming methodology lecture13
PDF
Programming methodology lecture12
PDF
Programming methodology lecture11
PDF
Programming methodology lecture10
PDF
Programming methodology lecture09
PDF
Programming methodology lecture08
PDF
Programming methodology lecture06
Programming methodology-1.1
3016 all-2007-dist
Programming methodology lecture28
Programming methodology lecture27
Programming methodology lecture26
Programming methodology lecture25
Programming methodology lecture23
Programming methodology lecture22
Programming methodology lecture19
Programming methodology lecture17
Programming methodology lecture16
Programming methodology lecture15
Programming methodology lecture14
Programming methodology lecture13
Programming methodology lecture12
Programming methodology lecture11
Programming methodology lecture10
Programming methodology lecture09
Programming methodology lecture08
Programming methodology lecture06

Recently uploaded (20)

PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
advance database management system book.pdf
PDF
My India Quiz Book_20210205121199924.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
International_Financial_Reporting_Standa.pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Cambridge-Practice-Tests-for-IELTS-12.docx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
What if we spent less time fighting change, and more time building what’s rig...
AI-driven educational solutions for real-life interventions in the Philippine...
Share_Module_2_Power_conflict_and_negotiation.pptx
IGGE1 Understanding the Self1234567891011
Introduction to pro and eukaryotes and differences.pptx
Environmental Education MCQ BD2EE - Share Source.pdf
TNA_Presentation-1-Final(SAVE)) (1).pptx
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
advance database management system book.pdf
My India Quiz Book_20210205121199924.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
History, Philosophy and sociology of education (1).pptx
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
International_Financial_Reporting_Standa.pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf

Machine learning (9)

  • 1. CS229 Lecture notes Andrew Ng Part IX The EM algorithm In the previous set of notes, we talked about the EM algorithm as applied to fitting a mixture of Gaussians. In this set of notes, we give a broader view of the EM algorithm, and show how it can be applied to a large family of estimation problems with latent variables. We begin our discussion with a very useful result called Jensen’s inequality 1 Jensen’s inequality Let f be a function whose domain is the set of real numbers. Recall that f is a convex function if f′′(x) ≥ 0 (for all x ∈ R). In the case of f taking vector-valued inputs, this is generalized to the condition that its hessian H is positive semi-definite (H ≥ 0). If f′′(x) > 0 for all x, then we say f is strictly convex (in the vector-valued case, the corresponding statement is that H must be positive definite, written H > 0). Jensen’s inequality can then be stated as follows: Theorem. Let f be a convex function, and let X be a random variable. Then: E[f(X)] ≥ f(EX). Moreover, if f is strictly convex, then E[f(X)] = f(EX) holds true if and only if X = E[X] with probability 1 (i.e., if X is a constant). Recall our convention of occasionally dropping the parentheses when writ- ing expectations, so in the theorem above, f(EX) = f(E[X]). For an interpretation of the theorem, consider the figure below. 1
  • 2. 2 a E[X] b f(a) E[f(X)] f(b) f(EX) f Here, f is a convex function shown by the solid line. Also, X is a random variable that has a 0.5 chance of taking the value a, and a 0.5 chance of taking the value b (indicated on the x-axis). Thus, the expected value of X is given by the midpoint between a and b. We also see the values f(a), f(b) and f(E[X]) indicated on the y-axis. Moreover, the value E[f(X)] is now the midpoint on the y-axis between f(a) and f(b). From our example, we see that because f is convex, it must be the case that E[f(X)] ≥ f(EX). Incidentally, quite a lot of people have trouble remembering which way the inequality goes, and remembering a picture like this is a good way to quickly figure out the answer. Remark. Recall that f is [strictly] concave if and only if −f is [strictly] convex (i.e., f′′(x) ≤ 0 or H ≤ 0). Jensen’s inequality also holds for concave functions f, but with the direction of all the inequalities reversed (E[f(X)] ≤ f(EX), etc.). 2 The EM algorithm Suppose we have an estimation problem in which we have a training set {x(1), . . . , x(m)} consisting of m independent examples. We wish to fit the parameters of a model p(x, z) to the data, where the likelihood is given by ℓ(θ) = m Xi=1 log p(x; θ) = m Xi=1 logXz p(x, z; θ).
  • 3. 3 But, explicitly finding the maximum likelihood estimates of the parameters θ may be hard. Here, the z(i)’s are the latent random variables; and it is often the case that if the z(i)’s were observed, then maximum likelihood estimation would be easy. In such a setting, the EM algorithm gives an efficient method for max- imum likelihood estimation. Maximizing ℓ(θ) explicitly might be difficult, and our strategy will be to instead repeatedly construct a lower-bound on ℓ (E-step), and then optimize that lower-bound (M-step). For each i, let Qi be some distribution over the z’s (Pz Qi(z) = 1, Qi(z) ≥ 0). Consider the following:1 Xi log p(x(i); θ) = Xi log(i) z Xp(x(i), z(i); θ) (1) = Xi log(i) z XQi(z(i)) p(x(i), z(i); θ) Qi(z(i)) (2) ≥ Xi (i) z XQi(z(i)) log p(x(i), z(i); θ) Qi(z(i)) (3) The last step of this derivation used Jensen’s inequality. Specifically, f(x) = log x is a concave function, since f′′(x) = −1/x2 < 0 over its domain x ∈ R+. Also, the term Xz (i) Qi(z(i)) p(x(i), z(i); θ) Qi(z(i)) in the summation is just an expectation of the quantity p(x(i), z(i); θ)/Qi(z(i)) with respect to z(i) drawn according to the distribution given by Qi. By Jensen’s inequality, we have f Ez(i)∼Qi p(x(i), z(i); θ) Qi(z(i)) ≥ Ez(i)∼Qi f p(x(i), z(i); θ) Qi(z(i)) , where the “z(i) ∼ Qi” subscripts above indicate that the expectations are with respect to z(i) drawn from Qi. This allowed us to go from Equation (2) to Equation (3). Now, for any set of distributions Qi, the formula (3) gives a lower-bound on ℓ(θ). There’re many possible choices for the Qi’s. Which should we choose? Well, if we have some current guess θ of the parameters, it seems 1If z were continuous, then Qi would be a density, and the summations over z in our discussion are replaced with integrals over z.
  • 4. 4 natural to try to make the lower-bound tight at that value of θ. I.e., we’ll make the inequality above hold with equality at our particular value of θ. (We’ll see later how this enables us to prove that ℓ(θ) increases monotonically with successsive iterations of EM.) To make the bound tight for a particular value of θ, we need for the step involving Jensen’s inequality in our derivation above to hold with equality. For this to be true, we know it is sufficient that that the expectation be taken over a “constant”-valued random variable. I.e., we require that p(x(i), z(i); θ) Qi(z(i)) = c for some constant c that does not depend on z(i). This is easily accomplished by choosing Qi(z(i)) ∝ p(x(i), z(i); θ). Actually, since we know Pz Qi(z(i)) = 1 (because it is a distribution), this further tells us that Qi(z(i)) = p(x(i), z(i); θ) Pz p(x(i), z; θ) = p(x(i), z(i); θ) p(x(i); θ) = p(z(i)|x(i); θ) Thus, we simply set the Qi’s to be the posterior distribution of the z(i)’s given x(i) and the setting of the parameters θ. Now, for this choice of the Qi’s, Equation (3) gives a lower-bound on the loglikelihood ℓ that we’re trying to maximize. This is the E-step. In the M-step of the algorithm, we then maximize our formula in Equation (3) with respect to the parameters to obtain a new setting of the θ’s. Repeatedly carrying out these two steps gives us the EM algorithm, which is as follows: Repeat until convergence { (E-step) For each i, set Qi(z(i)) := p(z(i)|x(i); θ). (M-step) Set Xi Xz θ := argmax (i) Qi(z(i)) log p(x(i), z(i); θ) Qi(z(i)) .
  • 5. 5 } How we we know if this algorithm will converge? Well, suppose θ(t) and θ(t+1) are the parameters from two successive iterations of EM. We will now prove that ℓ(θ(t)) ≤ ℓ(θ(t+1)), which shows EM always monotonically improves the log-likelihood. The key to showing this result lies in our choice of the Qi’s. Specifically, on the iteration of EM in which the parameters had started out as θ(t), we would have chosen Q(t) i (z(i)) := p(z(i)|x(i); θ(t)). We saw earlier that this choice ensures that Jensen’s inequality, as applied to get Equation (3), holds with equality, and hence z ℓ(θ(t)) =Xi (i) XQ(t) i (z(i)) log p(x(i), z(i); θ(t)) Q(t) i (z(i)) . The parameters θ(t+1) are then obtained by maximizing the right hand side of the equation above. Thus, z ℓ(θ(t+1)) ≥ Xi (i) XQ(t) i (z(i)) log p(x(i), z(i); θ(t+1)) Q(t) i (z(i)) (4) ≥ Xi (i) z XQ(t) i (z(i)) log p(x(i), z(i); θ(t)) Q(t) i (z(i)) (5) = ℓ(θ(t)) (6) This first inequality comes from the fact that ℓ(θ) ≥Xi Xz(i) Qi(z(i)) log p(x(i), z(i); θ) Qi(z(i)) holds for any values of Qi and θ, and in particular holds for Qi = Q(t) i , θ = θ(t+1). To get Equation (5), we used the fact that θ(t+1) is chosen explicitly to be Xi Xz argmax (i) Qi(z(i)) log p(x(i), z(i); θ) Qi(z(i)) , and thus this formula evaluated at θ(t+1) must be equal to or larger than the same formula evaluated at θ(t). Finally, the step used to get (6) was shown earlier, and follows from Q(t) i having been chosen to make Jensen’s inequality hold with equality at θ(t).
  • 6. 6 Hence, EM causes the likelihood to converge monotonically. In our de- scription of the EM algorithm, we said we’d run it until convergence. Given the result that we just showed, one reasonable convergence test would be to check if the increase in ℓ(θ) between successive iterations is smaller than some tolerance parameter, and to declare convergence if EM is improving ℓ(θ) too slowly. Remark. If we define z J(Q, θ) =Xi (i) XQi(z(i)) log p(x(i), z(i); θ) Qi(z(i)) , then we know ℓ(θ) ≥ J(Q, θ) from our previous derivation. The EM can also be viewed a coordinate ascent on J, in which the E-step maximizes it with respect to Q (check this yourself), and the M-step maximizes it with respect to θ. 3 Mixture of Gaussians revisited Armed with our general definition of the EM algorithm, let’s go back to our old example of fitting the parameters φ, μ and in a mixture of Gaussians. For the sake of brevity, we carry out the derivations for the M-step updates only for φ and μj , and leave the updates for j as an exercise for the reader. The E-step is easy. Following our algorithm derivation above, we simply calculate w(i) j = Qi(z(i) = j) = P(z(i) = j|x(i); φ, μ,). Here, “Qi(z(i) = j)” denotes the probability of z(i) taking the value j under the distribution Qi. Next, in the M-step, we need to maximize, with respect to our parameters φ, μ,, the quantity m Xi=1(i) z XQi(z(i)) log p(x(i), z(i); φ, μ,) Qi(z(i)) = m Xi=1 k Xj=1 Qi(z(i) = j) log p(x(i)|z(i) = j; μ,)p(z(i) = j; φ) Qi(z(i) = j) = m Xi=1 k Xj=1 w(i) j log 1 2 (x(i) − μj)T−1 (2)n/2|j |1/2 exp −1 j (x(i) − μj) · φj w(i) j
  • 7. 7 Let’s maximize this with respect to μl. If we take the derivative with respect to μl, we find ∇μl m Xi=1 k Xj=1 w(i) j log 1 exp −1 (2)n/2|j |1/2 2 (x(i) − μj)T−1 j (x(i) − μj) · φj w(i) j = −∇μl m Xi=1 k Xj=1 w(i) j 1 2 (x(i) − μj)T−1 j (x(i) − μj) = 1 2 m Xi=1 w(i) l −1 l ∇μl2μT l x(i) − μT l −1 l μl = m Xi=1 w(i) l −1 l x(i) − −1 l μl Setting this to zero and solving for μl therefore yields the update rule i=1 w(i) μl := Pm l x(i) i=1 w(i) Pm l , which was what we had in the previous set of notes. Let’s do one more example, and derive the M-step update for the param- eters φj . Grouping together only the terms that depend on φj , we find that we need to maximize m Xi=1 k Xj=1 w(i) j log φj . However, there is an additional constraint that the φj ’s sum to 1, since they represent the probabilities φj = p(z(i) = j; φ). To deal with the constraint that Pk φj = 1, we construct the Lagrangian j=1 L(φ) = m Xi=1 k Xj=1 w(i) j log φj + β( k Xj=1 φj − 1), where β is the Lagrange multiplier.2 Taking derivatives, we find ∂ ∂φj L(φ) = m Xi=1 w(i) j φj + 1 2We don’t need to worry about the constraint that j ≥ 0, because as we’ll shortly see, the solution we’ll find from this derivation will automatically satisfy that anyway.
  • 8. 8 Setting this to zero and solving, we get i=1 w(i) φj = Pm j −β i=1 w(i) I.e., φj ∝ Pm j . Using the constraint that Pj φj = 1, we easily find j=1 w(i) that −β = Pm i=1Pk i=1 1 = m. (This used the fact that w(i) j = Pm j = Qi(z(i) = j), and since probabilities sum to 1, Pj w(i) j = 1.) We therefore have our M-step updates for the parameters φj : φj := 1 m m Xi=1 w(i) j . The derivation for the M-step updates to j are also entirely straightfor- ward.