In many settings, we have data that is generated by some underlying causes that we are actually interested in. Because we do not observe these causes, we cannot model them directly. Instead, we model them as latent variables and use the data we have to learn them or infer their parameters.
Fig. 1: Graphical representation of latent variable models
Consider a latent variable model: Z → X Z \rightarrow X Z → X . Here, X X X is our observed data, and we assume that it comes from the latent variable Z Z Z . In particular, the likelihood for X X X is
p ( X ∣ θ ) = ∑ z p ( X , Z = z ∣ θ ) = ∑ z p ( X ∣ Z = z , θ ) p ( Z = z ∣ θ ) , \begin{aligned} p(X|\theta) &= \sum_{z} p(X, Z=z| \theta) \\ &= \sum_{z} {p(X| Z=z, \theta)} p(Z=z| \theta), \end{aligned} p ( X ∣ θ ) = z ∑ p ( X , Z = z ∣ θ ) = z ∑ p ( X ∣ Z = z , θ ) p ( Z = z ∣ θ ) , that is we marginalize Z out. Denote p ( z ) : = p ( Z = z ∣ θ ) p(z) := p(Z=z| \theta) p ( z ) : = p ( Z = z ∣ θ ) and p ( x ∣ z , θ ) = p ( X = x ∣ Z = z , θ ) p ( x ∣ z , θ ) = p ( X = x ∣ Z = z , θ ) p( x|z, \theta) = p(X=x | Z = z, \theta)p(x|z, \theta) = p(X=x | Z = z, \theta) p ( x ∣ z , θ ) = p ( X = x ∣ Z = z , θ ) p ( x ∣ z , θ ) = p ( X = x ∣ Z = z , θ ) where x ∈ X x \in \mathcal X x ∈ X . Consider now a dataset D = { x i } i = 1 n \mathcal D = \{ x_i \}_{i=1}^n D = { x i } i = 1 n . Its log-likelihood is
log p ( D ∣ θ ) = ∑ i = 1 n log p ( x i ∣ θ ) = ∑ i = 1 n log ( ∑ z p ( x ∣ z , θ ) p ( z ) ) . \begin{aligned} \log p(\mathcal D| \theta) &= \sum_{i=1}^n \log p(x_i | \theta) \\ &= \sum_{i=1}^n \log \bigg( \sum_{z} p(x| z,\theta) p(z) \bigg). \end{aligned} log p ( D ∣ θ ) = i = 1 ∑ n log p ( x i ∣ θ ) = i = 1 ∑ n log ( z ∑ p ( x ∣ z , θ ) p ( z ) ) . We can see that the equation above is non-linear w.r.t. to the parameters of the model θ \theta θ ; hence, we do not have a closed-form solution.
Expectation Maximization (EM) To find the model's parameters θ \theta θ , we instead use an iterative procedure. It consists of two steps, namely expectation and maximization.
First, we consider the complete log likelihood log p ( D , z ∣ θ ) \log p(\mathcal D, z | \theta) log p ( D , z ∣ θ ) . We then define the loss function
L ( θ , θ t ) : = E Z [ log p ( D , Z ∣ θ ) = ∑ z p ( z ∣ D , θ t ) log p ( D , z ∣ θ ) , \begin{aligned} \mathcal{L}(\theta, \theta^t) &:= \mathbb{E}_Z [ \log p(\mathcal D, Z| \theta) \\ &= \sum_{z} p(z| \mathcal D, \theta^t) \log p(\mathcal D, z | \theta), \end{aligned} L ( θ , θ t ) : = E Z [ log p ( D , Z ∣ θ ) = z ∑ p ( z ∣ D , θ t ) log p ( D , z ∣ θ ) , where θ t \theta_t θ t is the parameters of the previous iteration. We know that
p ( D , z ∣ θ ) = ∏ i = 1 n p ( x i , z ∣ θ ) = ∏ i = 1 n p ( x i ∣ z , θ ) p ( z ) . \begin{aligned} p(\mathcal D , z | \theta) &= \prod_{i=1}^n p(x_i, z| \theta) \\ &= \prod_{i=1}^n p( x_i|z, \theta) p(z). \end{aligned} p ( D , z ∣ θ ) = i = 1 ∏ n p ( x i , z ∣ θ ) = i = 1 ∏ n p ( x i ∣ z , θ ) p ( z ) . Combining L ( θ , θ t ) \mathcal L (\theta, \theta_t) L ( θ , θ t ) and p ( D , z ∣ θ ) p(\mathcal D, z| \theta) p ( D , z ∣ θ ) yields
L ( θ , θ t ) = ∑ z p ( z ∣ D , θ t ) log ( ∏ i = 1 n p ( x i ∣ z , θ ) p ( z ) ) = ∑ x p ( z ∣ D , θ t ) ∑ i = 1 n [ log p ( x i ∣ z , θ ) + log p ( z ) ] . \begin{aligned} \mathcal{L}(\theta, \theta^t) &= \sum_z p(z | \mathcal D, \theta^t) \log \bigg( \prod_{i=1}^n p(x_i|z,\theta) p(z) \bigg) \\ &= \sum_x p(z|\mathcal D, \theta^t) \sum_{i=1}^n \bigg[ \log p(x_i|z, \theta) + \log p(z) \bigg]. \end{aligned} L ( θ , θ t ) = z ∑ p ( z ∣ D , θ t ) log ( i = 1 ∏ n p ( x i ∣ z , θ ) p ( z ) ) = x ∑ p ( z ∣ D , θ t ) i = 1 ∑ n [ log p ( x i ∣ z , θ ) + log p ( z ) ] . To maximize L ( θ , θ t ) \mathcal L(\theta, \theta^t) L ( θ , θ t ) , we alternate between
Expectation step computing the posterior p ( z ∣ D , θ t ) , ∀ z ∈ Z p(z| \mathcal D, \theta^t), \forall z \in \mathcal Z p ( z ∣ D , θ t ) , ∀ z ∈ Z .Maximization step finding the model's parameters θ \theta θ that maximize L ( θ , θ t ) \mathcal L (\theta, \theta^t) L ( θ , θ t ) ; this can be done by ∇ θ L ( θ , θ t ) = ! 0 \nabla_\theta \mathcal L(\theta, \theta^t) \overset{!}{=} 0 ∇ θ L ( θ , θ t ) = ! 0 and solve for each θ \theta θ .It can be shown that the loss function increases every step; that is L ( θ , θ t + 1 ) ≥ L ( θ , θ t ) \mathcal{L}(\theta, \theta^{t+1}) \ge \mathcal{L}(\theta, \theta^t) L ( θ , θ t + 1 ) ≥ L ( θ , θ t ) .
Mixture Models Mixture of Gaussians We assume that our data D = { x i ∈ R d } i = 1 n \mathcal D = \{ \mathbf x_i \in \reals^d \}_{i=1}^n D = { x i ∈ R d } i = 1 n is generated from some latent factor z ∈ [ 1 , K ] z \in [1, K] z ∈ [ 1 , K ] . In particular, each sample's likelihood is
p ( x i ∣ θ ) = ∑ k = 1 K p ( z i = k ) ⏟ = : π k p ( x i ∣ θ k ) , p(\mathbf x_i | \theta) = \sum_{k=1}^K \underbrace{p(z_i=k)}_{=:\pi_k} p(\mathbf x_i| \theta _k), p ( x i ∣ θ ) = k = 1 ∑ K = : π k p ( z i = k ) p ( x i ∣ θ k ) , where π k \pi_k π k 's are mixing weights and ∑ k π k = 1 \sum_k \pi_k = 1 ∑ k π k = 1 . Here we assume that each component is a Gaussian distribution with θ k = { μ k , Σ k } \theta_k= \{ \mu_k, \Sigma_k \} θ k = { μ k , Σ k } ; hence, we have
p ( x ∣ θ k ) = N ( x ∣ μ k , Σ k ) . p(\mathbf x | \theta_k ) = \mathcal N ({\mathbf x}| \mathbf \mu_k, \Sigma_k). p ( x ∣ θ k ) = N ( x ∣ μ k , Σ k ) . Consider z i ∈ { 0 , 1 } K \mathbf z_i \in \{0, 1\}^K z i ∈ { 0 , 1 } K be the true mixture assignment of x i \mathbf x_i x i (i.e., it is a one-hot vector whose is one at the cluster that x i \mathbf x_i x i belongs to). The complete log likelihood is
l c ( θ ) : = ∑ i = 1 n log p ( x i , z i ∣ θ ) \begin{aligned} l_c(\theta) &:= \sum_{i=1}^n \log p(\mathbf x_i, \mathbf{z}_i | \theta) \end{aligned} l c ( θ ) : = i = 1 ∑ n log p ( x i , z i ∣ θ ) Let z i z_i z i be the cluster index for x i \mathbf x_i x i (the index of the one entry). We compute the expected complete log likelihood w.r.t to the approximate posterior q ( z ∣ x , θ t ) q(z|\mathbf x, \theta^{t}) q ( z ∣ x , θ t ) :
L ( θ , θ t ) = E q ( z ∣ x , θ t ) [ ∑ i = 1 n log p ( x i , Z i ∣ θ ) ] = ∑ i = 1 n E q ( z ∣ x , θ t ) [ log p ( x i , Z i ∣ θ ) ] = ∑ i = 1 n E q ( z ∣ x , θ t ) [ log ∏ k = 1 K ( p ( x i ∣ θ k ) π k ) 1 [ Z i = k ] ] = ∑ i = 1 n E q ( z ∣ x , θ t ) [ ∑ k 1 [ z i = k ] log p ( x i ∣ θ k ) π k ] = ∑ i = 1 n ∑ k E q ( z ∣ x , θ t ) 1 [ Z i = k ] log p ( x i ∣ θ k ) π k = ∑ i = 1 n ∑ k p ( z i = k ∣ x i , θ t ) log p ( x i ∣ θ k ) π k \begin{aligned} \mathcal{L}(\theta, \theta^{t}) &= \mathbb{E}_{q(z|\mathbf x, \theta^{t})}\bigg[ \sum_{i=1}^n \log p(\mathbf x_i, Z_i|\theta)\bigg] \\ &= \sum_{i=1}^n \mathbb{E}_{q(z|\mathbf x, \theta^{t})} \bigg[ \log p(\mathbf x_i, Z_i | \theta) \bigg] \\ &= \sum_{i=1}^n \mathbb{E}_{q(z|\mathbf x, \theta^{t})} \bigg[ \log \prod_{k=1}^K ( p(\mathbf x_i | \theta_k ) \pi_k )^{1[Z_i = k ]}\bigg] \\ &= \sum_{i=1}^n \mathbb{E}_{q(z|\mathbf x, \theta^{t})} \bigg[ \sum_k {1[z_i = k ]} \log p(\mathbf x_i | \theta_k ) \pi_k \bigg] \\ &= \sum_{i=1}^n \sum_k \mathbb{E}_{q(z|\mathbf x, \theta^{t})} {1[Z_i = k ]} \log p(\mathbf x_i | \theta_k ) \pi_k \\ &= \sum_{i=1}^n \sum_k p(z_i=k| \mathbf x_i, \theta^{t}) \log p(\mathbf x_i | \theta_k ) \pi_k \end{aligned} L ( θ , θ t ) = E q ( z ∣ x , θ t ) [ i = 1 ∑ n log p ( x i , Z i ∣ θ ) ] = i = 1 ∑ n E q ( z ∣ x , θ t ) [ log p ( x i , Z i ∣ θ ) ] = i = 1 ∑ n E q ( z ∣ x , θ t ) [ log k = 1 ∏ K ( p ( x i ∣ θ k ) π k ) 1 [ Z i = k ] ] = i = 1 ∑ n E q ( z ∣ x , θ t ) [ k ∑ 1 [ z i = k ] log p ( x i ∣ θ k ) π k ] = i = 1 ∑ n k ∑ E q ( z ∣ x , θ t ) 1 [ Z i = k ] log p ( x i ∣ θ k ) π k = i = 1 ∑ n k ∑ p ( z i = k ∣ x i , θ t ) log p ( x i ∣ θ k ) π k Define the responsibility (or posterior) r i k : = p ( z i = k ∣ x i , θ t ) r_{ik} := p(z_i=k|\mathbf x_i, \theta^{t}) r i k : = p ( z i = k ∣ x i , θ t ) . We arrive at
L ( θ , θ t ) = ∑ i = 1 n ∑ k = 1 K r i k [ log p ( x i ∣ θ k ) + log π k ] \mathcal{L}(\theta, \theta^{t}) = \sum_{i=1}^n \sum_{k=1}^K r_{ik} [\log p(\mathbf x_i | \theta_k) + \log \pi_k] L ( θ , θ t ) = i = 1 ∑ n k = 1 ∑ K r i k [ log p ( x i ∣ θ k ) + log π k ] Expectation Step Due to the fact that
p ( Z ∣ X , θ t ) = p ( X , Z ∣ θ t ) p ( X ∣ θ t ) , p(Z|X, \theta^{t}) = \frac{p(X, Z| \theta^{t})}{p(X| \theta^{t})}, p ( Z ∣ X , θ t ) = p ( X ∣ θ t ) p ( X , Z ∣ θ t ) , we can then write
r i k = π k p ( x i ∣ θ k t ) ∑ k ′ π k ′ p ( x i ∣ θ k ′ t ) . r_{ik} = \frac{\pi_k p(\mathbf x_i | \theta_k^{t})}{\sum_{k'} \pi_{k'} p(\mathbf x_i | \theta_{k'}^{t})}. r i k = ∑ k ′ π k ′ p ( x i ∣ θ k ′ t ) π k p ( x i ∣ θ k t ) . Maximization Step We compute ∀ k , π k , θ k : = ( μ k , Σ k ) \forall _k, \pi_k, \theta_k := (\mu_k, \Sigma_k) ∀ k , π k , θ k : = ( μ k , Σ k ) that maximize L ( θ , θ t ) \mathcal{L}(\theta, \theta^{t}) L ( θ , θ t ) .
Updating π k \pi_k π k
We know that ∑ k π k = 1 \sum_k \pi_k = 1 ∑ k π k = 1 . Using Lagrange's multiplier, we get
L = L ( θ , θ t ) − λ ( ∑ k π k − 1 ) L = \mathcal{L} (\theta, \theta^{t}) - \lambda \bigg(\sum_k \pi_k - 1\bigg) L = L ( θ , θ t ) − λ ( k ∑ π k − 1 ) Computing ∂ L ∂ π k \frac{\partial L}{\partial \pi_k} ∂ π k ∂ L and set it to zero yields
∑ i r i k λ = π k . \sum_i \frac{r_{ik}}\lambda = \pi_k. i ∑ λ r i k = π k . Combing this into the constraint, we have
∑ k ∑ i r i k = λ ∑ i ∑ k r i k ⏟ = 1 = λ \begin{aligned} \sum_k \sum_i r_{ik} &= \lambda \\ \sum_i \underbrace{\sum_k r_{ik}}_{=1} &= \lambda \end{aligned} k ∑ i ∑ r i k i ∑ = 1 k ∑ r i k = λ = λ Hence, λ = N \lambda = N λ = N and π k = 1 N ∑ i r i k \pi_k = \frac{1}{N} \sum_i r_{ik} π k = N 1 ∑ i r i k .
Updating μ k \mu_k μ k
Recall that N ( x ∣ μ k , Σ k ) = 1 ( ( 2 π ) d ∣ Σ k ∣ ) exp ( − 1 2 ( x − μ k ) T Σ k − 1 ( x − μ k ) ) \mathcal{N}(\mathbf x| \mu_k, \Sigma_k) = \frac{1}{(\sqrt{(2\pi)^d|\Sigma_k|}) }\exp\bigg(-\frac{1}{2}(\mathbf x - \mu_k)^T\Sigma_k^{-1}(\mathbf x - \mu_k)\bigg) N ( x ∣ μ k , Σ k ) = ( ( 2 π ) d ∣ Σ k ∣ ) 1 exp ( − 2 1 ( x − μ k ) T Σ k − 1 ( x − μ k ) ) ; hence, we have
L ( θ , θ t − 1 ) = ∑ i ∑ k r i k [ log π k + log ( 1 ( 2 π ) d ∣ Σ k ∣ ) + 1 2 ( x i − μ k ) T Σ k − 1 ( x i − μ k ) ] . \begin{aligned} \mathcal{L}(\theta, \theta^{t-1}) = \sum_i \sum_k r_{ik}\bigg[ \log \pi_k + \log \bigg( \frac{1}{\sqrt{(2\pi)^d|\Sigma_k|}} \bigg) + \frac{1}{2} (\mathbf x_i - \mu_k)^T\Sigma_k^{-1}(\mathbf x_i - \mu_k)\bigg]. \end{aligned} L ( θ , θ t − 1 ) = i ∑ k ∑ r i k [ log π k + log ( ( 2 π ) d ∣ Σ k ∣ 1 ) + 2 1 ( x i − μ k ) T Σ k − 1 ( x i − μ k ) ] . Taking the derivative w.r.t μ k \mu_k μ k we get
∇ μ k L ( θ , θ t − 1 ) = ∑ i r i k Σ k − 1 ( x i − μ k ) . \nabla_{\mu_k} \mathcal L (\theta, \theta^{t-1}) = \sum_i r_{ik} \Sigma_k^{-1} (\mathbf x_i - \mu_k). ∇ μ k L ( θ , θ t − 1 ) = i ∑ r i k Σ k − 1 ( x i − μ k ) . Setting it to zero yields
μ k = ∑ i r i k x i ∑ i r i k \mu_k = \frac{\sum_i r_{ik} \mathbf x_i}{\sum_i r_{ik}} μ k = ∑ i r i k ∑ i r i k x i Updating Σ k \Sigma_k Σ k
Taking the derivative yields
∇ Σ k log 1 ( 2 π ) d ∣ Σ k ∣ = ( 2 π ) d ∣ Σ k ∣ 1 ( 2 π ) d ∇ Σ k ∣ Σ k ∣ − 1 / 2 = ∣ Σ k ∣ [ − 1 2 ( ∣ Σ k ∣ ) − 1 Σ k − 1 ] = − Σ k − 1 2 . \begin{aligned} \nabla_{\Sigma_k} \log \frac{1}{\sqrt{{(2\pi)^d} |\Sigma_k|} } &= \sqrt{\cancel{(2\pi)^d} |\Sigma_k|} \frac{1}{\cancel{\sqrt{(2\pi)^d}}} \nabla_{\Sigma_k} |\Sigma_k|^{-1/2} \\ &= \cancel{\sqrt{|\Sigma_k|}} \bigg[ -\frac{1}{2} \cancel{(\sqrt{|\Sigma_k|})^{-1}} \Sigma_k^{-1} \bigg] \\ &= - \frac{\Sigma_k^{-1}}{2}. \end{aligned} ∇ Σ k log ( 2 π ) d ∣ Σ k ∣ 1 = ( 2 π ) d ∣ Σ k ∣ ( 2 π ) d 1 ∇ Σ k ∣ Σ k ∣ − 1 / 2 = ∣ Σ k ∣ [ − 2 1 ( ∣ Σ k ∣ ) − 1 Σ k − 1 ] = − 2 Σ k − 1 . We also have that
∇ Σ k ( x − μ k ) Σ k − 1 ( x − μ k ) = − Σ k − 1 ( x − μ k ) ( x − μ k ) T Σ k − 1 . \nabla_{\Sigma_k} (\mathbf x - \mu_k) \Sigma_k^{-1} (\mathbf x - \mu_k)= -\Sigma_k^{-1}(\mathbf x - \mu_k)(\mathbf x - \mu_k)^T \Sigma_k^{-1}. ∇ Σ k ( x − μ k ) Σ k − 1 ( x − μ k ) = − Σ k − 1 ( x − μ k ) ( x − μ k ) T Σ k − 1 . Combining the two terms and set the derivative to zero, we arrive at
∑ i r i k Σ k − 1 2 = ∑ i r i k 2 Σ k − 1 ( x i − μ k ) ( x i − μ k ) T Σ k − 1 Σ k − 1 ∑ i r i k = Σ k − 1 ∑ i r i k ( x i − μ k ) ( x i − μ k ) T Σ k − 1 . \begin{aligned} \sum_i \frac{r_{ik} \Sigma_k^{-1}}{2} &= \sum_i \frac{r_{ik}}{2} \Sigma_k^{-1}(\mathbf x_i - \mu_k)(\mathbf x_i - \mu_k)^T \Sigma_k^{-1} \\ \Sigma_k^{-1}\sum_i r_{ik} &= \Sigma_k^{-1} \sum_i r_{ik} (\mathbf x_i - \mu_k)(\mathbf x_i - \mu_k)^T \Sigma_k^{-1}. \end{aligned} i ∑ 2 r i k Σ k − 1 Σ k − 1 i ∑ r i k = i ∑ 2 r i k Σ k − 1 ( x i − μ k ) ( x i − μ k ) T Σ k − 1 = Σ k − 1 i ∑ r i k ( x i − μ k ) ( x i − μ k ) T Σ k − 1 . Therefore, we have
Σ k = ∑ i r i k ( x i − μ k ) ( x i − μ k ) T ∑ i r i k . \Sigma_k = \frac{\sum_i r_{ik}(\mathbf x_i - \mu_k)(\mathbf x_i - \mu_k)^T}{\sum_i r_{ik}}. Σ k = ∑ i r i k ∑ i r i k ( x i − μ k ) ( x i − μ k ) T . Fig. 2: Expectation-Maximization steps for a mixture of Gaussians
K-Means Algorithm: Special Case of GMMs One can view K-means as a special case of GMMs that greedily chooses the closest centroid for each x i \mathbf x_i x i . In this case, we assume that π k = 1 K \pi_k = \frac{1}{K} π k = K 1 and Σ k = σ 2 I d \Sigma_k = \sigma^2I_d Σ k = σ 2 I d for σ ∈ R + \sigma \in \R_+ σ ∈ R + . The posterior is
p ( z i = k ∣ x i , θ ) = 1 [ k = z i ∗ ] , p(z_i = k | \mathbf x_i, \theta) = 1[k = z_i^*], p ( z i = k ∣ x i , θ ) = 1 [ k = z i ∗ ] , where z i ∗ = arg max k p ( z i = k ∣ x i , θ ) z^*_i = \argmax_k p(z_i =k| \mathbf x_i, \theta) z i ∗ = a r g m a x k p ( z i = k ∣ x i , θ ) . Equivalently, this is similar to
z i ∗ = arg min k ∥ x i − μ k ∥ 2 . z_i^* = \argmin_k \| \mathbf x_i - \mu_k \|^2. z i ∗ = k a r g m i n ∥ x i − μ k ∥ 2 . As you can see from the derivation, this is a hard assignment assigning a cluster to the sample. Consider D k ⊂ D \mathcal D_k \subset \mathcal D D k ⊂ D be the samples belong to the k k k -th cluster. the maximization step is then just
μ k = 1 ∣ D k ∣ ∑ x ∈ D k x \mu_k = \frac{1}{|\mathcal D_k|} \sum_{\mathbf x \in \mathcal D_k} \mathbf x μ k = ∣ D k ∣ 1 x ∈ D k ∑ x Mixture of Binomials Consider z ∈ { H , T } z \in\{ H, T\} z ∈ { H , T } and we assume that it comes from a Bernoulli distribution:
p ( z = H ∣ θ ) = π H p ( z = T ∣ θ ) = π T , \begin{aligned} p(z=H | \theta) &= \pi_H \\ p(z=T | \theta) &= \pi_T, \end{aligned} p ( z = H ∣ θ ) p ( z = T ∣ θ ) = π H = π T , where π H + π T = 1 \pi_H + \pi_T = 1 π H + π T = 1 . Our setting has two Binomial mixture components, namely
p ( x ∣ z = H , θ ) = ( m x ) μ H m ( 1 − μ H ) m − x p ( x ∣ z = T , θ ) = ( m x ) μ T m ( 1 − μ T ) m − x , \begin{aligned} p(x | z=H, \theta) &= {m \choose x} \mu_H^m (1-\mu_H)^{m-x} \\ p(x | z=T, \theta) &= {m \choose x} \mu_T^m (1-\mu_T)^{m-x}, \end{aligned} p ( x ∣ z = H , θ ) p ( x ∣ z = T , θ ) = ( x m ) μ H m ( 1 − μ H ) m − x = ( x m ) μ T m ( 1 − μ T ) m − x , where x ∈ [ 0 , m ] x \in [0, m] x ∈ [ 0 , m ] . In our setting, we have θ : = { π H , π T , μ H , μ T } \theta := \{ \pi_H, \pi_T, \mu_H, \mu_T\} θ : = { π H , π T , μ H , μ T } . Let D = ( x 1 , … , x n ) . \mathcal{D} = (x_1, \dots, x_n). D = ( x 1 , … , x n ) . Recall the complete log-likelihood:
L ( θ , θ t ) = ∑ i = 1 n ∑ z = H , T r i z [ log π z + log p ( x i ∣ z , θ z ) ] . \begin{aligned} \mathcal{L}(\theta, \theta^{t}) = \sum_{i=1}^n \sum_{z=H, T} r_{iz} [ \log \pi_z + \log p(x_i|z, \theta_z) ]. \end{aligned} L ( θ , θ t ) = i = 1 ∑ n z = H , T ∑ r i z [ log π z + log p ( x i ∣ z , θ z ) ] . Expectation Step We first recall and write
r i H = π H p ( x i ∣ θ H t ) ∑ z = H , T π z p ( x i ∣ θ z t ) = π H μ H m ( 1 − μ H ) m − x i ∑ z = H , T π z p ( x i ∣ θ z t ) . \begin{aligned} r_{iH} &= \frac{\pi_H p(x_i | \theta_H^t)}{\sum_{z=H,T} \pi_z p(x_i | \theta_z^t) } \\ &= \frac{\pi_H \mu_H^m ( 1- \mu_H)^{m-x_i} }{\sum_{z=H,T} \pi_z p(x_i | \theta_z^t) }. \end{aligned} r i H = ∑ z = H , T π z p ( x i ∣ θ z t ) π H p ( x i ∣ θ H t ) = ∑ z = H , T π z p ( x i ∣ θ z t ) π H μ H m ( 1 − μ H ) m − x i . Similarly, we have
r i H = π T μ T m ( 1 − μ T ) m − x i ∑ z = H , T π z p ( x i ∣ θ z t ) . \begin{aligned} r_{iH} &= \frac{\pi_T \mu_T^m ( 1- \mu_T)^{m-x_i} }{\sum_{z=H,T} \pi_z p(x_i | \theta_z^t) }. \end{aligned} r i H = ∑ z = H , T π z p ( x i ∣ θ z t ) π T μ T m ( 1 − μ T ) m − x i . Maximization Step Recall that θ = { π H , π T , μ H , μ T } \theta = \{ \pi_H, \pi_T, \mu_H, \mu_T\} θ = { π H , π T , μ H , μ T } . We compute the derivative of L ( θ , θ t ) \mathcal{L}( \theta, \theta^{t}) L ( θ , θ t ) w.r.t the parameters and set to zero.
Updating π H \pi_H π H and π T \pi_T π T
∂ L ( θ , θ t ) ∂ π H = ∑ i = 1 n r i H ∂ ∂ π H [ log π H ] + r i T ∂ ∂ π H [ log 1 − π H ] = ∑ i = 1 n r i H π H − r i T 1 − π H = ! 0 \begin{aligned} \frac{\partial \mathcal{L}(\theta, \theta^{t})}{\partial \pi_H } &= \sum_{i=1}^n r_{iH} \frac{\partial}{\partial \pi_H} \bigg [\log \pi_H \bigg] + r_{iT} \frac{\partial}{\partial \pi_H} \bigg [\log 1-\pi_H \bigg] \\ &= \sum_{i=1}^n \frac{r_{iH}}{\pi_H} - \frac{r_{iT}}{1-\pi_H} \\ &\overset{!}{=} 0 \end{aligned} ∂ π H ∂ L ( θ , θ t ) = i = 1 ∑ n r i H ∂ π H ∂ [ log π H ] + r i T ∂ π H ∂ [ log 1 − π H ] = i = 1 ∑ n π H r i H − 1 − π H r i T = ! 0 We get
( 1 − π H ) ∑ i = 1 n r i H = λ ∑ i = 1 n r i T ∑ i = 1 n r i H = λ ( ∑ i = 1 n r i T + r i H ⏟ 1 ) π H = 1 n ∑ i = 1 n r i H , \begin{aligned} (1-\pi_H)\sum_{i=1}^n r_{iH} &= \lambda\sum_{i=1}^n r_{iT} \\ \sum_{i=1}^n r_{iH} &= \lambda\bigg(\sum_{i=1}^n \underbrace{r_{iT} + r_{iH}}_1\bigg) \\ \pi_H &= \frac 1 n \sum_{i=1}^n r_{iH}, \end{aligned} ( 1 − π H ) i = 1 ∑ n r i H i = 1 ∑ n r i H π H = λ i = 1 ∑ n r i T = λ ( i = 1 ∑ n 1 r i T + r i H ) = n 1 i = 1 ∑ n r i H , and π T = 1 − π H \pi_T = 1 - \pi_H π T = 1 − π H .
Updating μ H \mu_H μ H and μ T \mu_T μ T
∂ L ( θ , θ t − 1 ) ∂ μ H = ∑ i = 1 n r i H ∂ ∂ μ H ( log ( m x i ) + x i log μ H + ( m − x i ) log ( 1 − μ H ) ) = ∑ i = 1 n r i H ( x i μ H − m − x i ( 1 − μ H ) ) = ! 0 \begin{aligned} \frac{\partial \mathcal{L}(\theta, \theta^{t-1})}{\partial \mu_H} &= \sum_{i=1}^n r_{iH} \frac {\partial } {\partial \mu_H}\bigg( \log {m \choose x_i} + x_i \log \mu_H + (m-x_i)\log(1-\mu_H) \bigg ) \\ &= \sum_{i=1}^n r_{iH} \bigg(\frac{x_i}{\mu_H} - \frac{m-x_i}{(1-\mu_H)} \bigg) \\ &\overset{!}{=} 0 \end{aligned} ∂ μ H ∂ L ( θ , θ t − 1 ) = i = 1 ∑ n r i H ∂ μ H ∂ ( log ( x i m ) + x i log μ H + ( m − x i ) log ( 1 − μ H ) ) = i = 1 ∑ n r i H ( μ H x i − ( 1 − μ H ) m − x i ) = ! 0 Therefore, we have
∑ i = 1 n r i H x i μ H = ∑ i = 1 n r i H ( m − x i ) ( 1 − μ H ) ( 1 − μ H ) ∑ i = 1 n r i H x i = μ H ∑ i = 1 n r i H ( m − x i ) μ H = ∑ i = 1 n r i H x i ∑ i = 1 n r i H m . \begin{aligned} \sum_{i=1}^n \frac{r_{iH}x_i}{\mu_H} &= \sum_{i=1}^n \frac{r_{iH}(m-x_i)}{(1-\mu_H)} \\ (1-\mu_H) \sum_{i=1}^n {r_{iH}x_i} &= \mu_H \sum_{i=1}^n r_{iH}(m-x_i) \\ \mu_H &= \frac{\sum_{i=1}^n {r_{iH}x_i} }{\sum_{i=1}^n {r_{iH}m} }. \end{aligned} i = 1 ∑ n μ H r i H x i ( 1 − μ H ) i = 1 ∑ n r i H x i μ H = i = 1 ∑ n ( 1 − μ H ) r i H ( m − x i ) = μ H i = 1 ∑ n r i H ( m − x i ) = ∑ i = 1 n r i H m ∑ i = 1 n r i H x i . Similarly, we have μ T = ∑ i = 1 n r i T x i ∑ i = 1 n r i T m \mu_T = \frac{\sum_{i=1}^n {r_{iT}x_i} }{\sum_{i=1}^n {r_{iT}m} } μ T = ∑ i = 1 n r i T m ∑ i = 1 n r i T x i .
Fig. 3: Expectation-Maximization steps for a mixture of binomials
Conclusions These are resources that I consulted while writing this article
Murphy (2012), "Machine Learning: A Probabilistic Perspective" (Section 11.4) for the general idea of the EM algorithm. Manfred Opper's Probabilistic Bayesian Modeling course, Lecture Laplace Approximation (TU Berlin, Summer 2020) for Proof of the EM step Woijeck Samek's Machine Learning 1 course, Lecture Latent Variable Models (TU Berlin, Winter 2020) for the Mixture of Binomials example This articles' figures are made in Google Colab .
Proof: the EM procedure increases the expected complete log-likelihood In the following, we will show that L ( θ , θ t + 1 ) ≥ L ( θ , θ t ) \mathcal{L}(\theta, \theta^{t+1}) \ge \mathcal{L}(\theta, \theta^t) L ( θ , θ t + 1 ) ≥ L ( θ , θ t ) .
Consider q ( ⋅ ) q(\cdot) q ( ⋅ ) be any distribution over the latent variable Z Z Z . We know that p ( D , z ∣ θ ) = p ( z ∣ D , θ ) p ( D ∣ θ ) p(\mathcal D, z| \theta) = p(z|\mathcal D, \theta) p(\mathcal D | \theta) p ( D , z ∣ θ ) = p ( z ∣ D , θ ) p ( D ∣ θ ) . We can then compute
KL ( q ( Z ) ∥ p ( Z ∣ D , θ ) ) = ∑ z q ( x ) log q ( z ) p ( z ∣ D , θ ) = ∑ z q ( z ) log q ( z ) p ( D ∣ θ ) p ( z , D ∣ θ ) \begin{aligned} \text{KL} ( q(Z) \| p(Z |\mathcal D, \theta)) &= \sum_{z} q(x) \log \frac{q(z)}{p(z|\mathcal D, \theta)}\\ &= \sum_{z} q(z) \log \frac{q(z) p(\mathcal D | \theta)}{p(z, \mathcal D| \theta)} \end{aligned} KL ( q ( Z ) ∥ p ( Z ∣ D , θ ) ) = z ∑ q ( x ) log p ( z ∣ D , θ ) q ( z ) = z ∑ q ( z ) log p ( z , D ∣ θ ) q ( z ) p ( D ∣ θ ) Because KL ( ⋅ ∥ ⋅ ) ≥ 0 \text{KL}(\cdot \| \cdot) \ge 0 KL ( ⋅ ∥ ⋅ ) ≥ 0 , we have
∑ z q ( z ) log q ( z ) p ( z , D ∣ θ ) ⏟ = : F ( q , θ ) ≥ − log p ( D ∣ θ ) \begin{aligned} \underbrace{\sum_z q(z) \log \frac{q(z)}{p(z, \mathcal D | \theta)}}_{=: F(q, \theta) } \ge - \log p(\mathcal D | \theta) \end{aligned} = : F ( q , θ ) z ∑ q ( z ) log p ( z , D ∣ θ ) q ( z ) ≥ − log p ( D ∣ θ ) Now consider q t ( z ) : = p ( z ∣ D , θ t ) q_t(z) := p(z | \mathcal D, \theta^t) q t ( z ) : = p ( z ∣ D , θ t ) . We have
L ( θ , θ t ) = ∑ z q t ( z ) log p ( z , D ∣ θ ) = ∑ z q t ( z ) log p ( z , D ∣ θ ) q t ( z ) q t ( z ) = − F ( q t , θ ) + ∑ z q t ( z ) log q t ( z ) \begin{aligned} \mathcal{L}(\theta, \theta^t) &= \sum_z q_t(z) \log p(z, \mathcal D | \theta) \\ &= \sum_z q_t(z) \log p(z, \mathcal D | \theta) \frac{q_t(z) }{q_t(z)} \\ &= - F(q_t, \theta) + \sum_z q_t(z) \log q_t(z) \end{aligned} L ( θ , θ t ) = z ∑ q t ( z ) log p ( z , D ∣ θ ) = z ∑ q t ( z ) log p ( z , D ∣ θ ) q t ( z ) q t ( z ) = − F ( q t , θ ) + z ∑ q t ( z ) log q t ( z ) Consider the fact that F ( q t , θ ) ≥ − log p ( D ∣ θ ) F(q_t, \theta) \ge - \log p(\mathcal D| \theta) F ( q t , θ ) ≥ − log p ( D ∣ θ ) . We have two cases, namely
F ( q t , θ ) > − log p ( D ∣ θ ) F(q_t, \theta) > - \log p(\mathcal D|\theta) F ( q t , θ ) > − log p ( D ∣ θ ) F ( q t , θ ) = − log p ( D ∣ θ ) ⟹ F ( q t , θ t ) = − log p ( D ∣ θ t ) F(q_t, \theta) = - \log p( \mathcal D | \theta) \implies F(q_t, \theta_t) = - \log p(\mathcal D |\theta_t) F ( q t , θ ) = − log p ( D ∣ θ ) ⟹ F ( q t , θ t ) = − log p ( D ∣ θ t ) .Subtract these two conditions lead to
F ( q t , θ ) − F ( q t , θ t ) ≥ − log p ( D ∣ θ ) + log p ( D ∣ θ t ) F(q_t, \theta) - F(q_t, \theta_t) \ge - \log p(\mathcal D | \theta) + \log p(\mathcal D| \theta_t) F ( q t , θ ) − F ( q t , θ t ) ≥ − log p ( D ∣ θ ) + log p ( D ∣ θ t ) In other words, we have
log p ( D ∣ θ ) − log p ( D ∣ θ t ) > − F ( q t , θ ) + F ( q t , θ t ) > − ( F ( q t , θ ) − F ( q t , θ t ) ⏟ ≤ ( ∗ ) 0 ) > 0. \begin{aligned} \log p(\mathcal D | \theta) - \log p(\mathcal D | \theta^t) &> - F(q_t, \theta) + F(q_t, \theta^t) \\ &> - ( \underbrace{ F(q_t, \theta) - F(q_t, \theta^t)}_{\overset{(*)}\le 0}) \\ &> 0. \end{aligned} log p ( D ∣ θ ) − log p ( D ∣ θ t ) > − F ( q t , θ ) + F ( q t , θ t ) > − ( ≤ ( ∗ ) 0 F ( q t , θ ) − F ( q t , θ t ) ) > 0 . ( ∗ ) (*) ( ∗ ) is due to the fact that we maximize L ( θ , θ t ) \mathcal{L}(\theta, \theta^t) L ( θ , θ t ) w.r.t θ \theta θ , which is equivalent to minimize F ( q t , θ ) F(q_t, \theta) F ( q t , θ ) .
This concludes that the EM procedure always increases the expected complete log likelihood. However, the procedure does not guarantee to reach the global optimum.