EM algorithm and GMM model

In statistics, EM (expectation maximization) algorithm handles latent variables, while GMM is the Gaussian mixture model.

Background
In the picture below, are shown the red blood cell hemoglobin concentration and the red blood cell volume data of two groups of people, the Anemia group and the Control Group (i.e. the group of people without Anemia). As expected, people with Anemia have lower red blood cell volume and lower red blood cell hemoglobin concentration than those without Anemia. $$x$$ is a random vector such as $$x:=\big(\text{red blood cell volume}, \text{red blood cell hemoglobin concentration}\big)$$, and from medical studies it is known that $$x$$ are normally distributed in each group, i.e. $$x \sim \mathcal N(\mu, \Sigma)$$.

$$z$$ is denoted as the group where $$x$$ belongs, with $$z_i = 0$$ when $$x_i$$ belongs to Anemia Group and $$z_i=1$$ when $$x_i$$ belongs to Control Group. Also $$z \sim \operatorname{Categorical}(k, \phi)$$ where $$k=2$$, $$\phi_j \geq 0,$$ and $$\sum_{j=1}^k\phi_j=1$$. See Categorical distribution.

The following procedure can be used to estimate $$\phi, \mu, \Sigma$$.

A maximum likelihood estimation can be applied:


 * $$\ell(\phi,\mu,\Sigma)=\sum_{i=1}^m \log (p(x^{(i)};\phi,\mu,\Sigma))

=\sum_{i=1}^m \log \sum_{z^{(i)}=1}^k p\left(x^{(i)} \mid z^{(i)} ; \mu, \Sigma\right) p(z^{(i)} ; \phi) $$

As the $$z_i$$ for each $$x_i$$ are known, the log likelihood function can be simplified as below:


 * $$\ell(\phi, \mu, \Sigma)=\sum_{i=1}^{m} \log p\left(x^{(i)} \mid z^{(i)} ; \mu, \Sigma\right)+\log p\left(z^{(i)} ; \phi\right)$$

Now the likelihood function can be maximized by making partial derivative over $$\mu, \Sigma, \phi$$, obtaining:


 * $$\phi_{j} =\frac{1}{m} \sum_{i=1}^m 1\{z^{(i)}=j\}$$


 * $$\mu_j =\frac{\sum_{i=1}^m 1\{z^{(i)}=j\} x^{(i)}}{\sum_{i=1}^{m} 1\left\{z^{(i)}=j\right\}}$$


 * $$\Sigma_j =\frac{\sum_{i=1}^m 1\{z^{(i)}=j\} (x^{(i)}-\mu_j)(x^{(i)}-\mu_j)^T}{\sum_{i=1}^m 1\{z^{(i)}=j\}}$$

If $$z_i$$ is known, the estimation of the parameters results to be quite simple with maximum likelihood estimation. But if $$z_i$$ is unknown it is much more complicated.

Being $$z$$ a latent variable (i.e. not observed), with unlabeled scenario, the Expectation Maximization Algorithm is needed to estimate $$z$$ as well as other parameters. Generally, this problem is set as a GMM since the data in each group is normally distributed.

In machine learning, the latent variable $$z$$ is considered as a latent pattern lying under the data, which the observer is not able to see very directly. $$x_i$$ is the known data, while $$\phi, \mu, \Sigma$$ are the parameter of the model. With the EM algorithm, some underlying pattern $$z$$ in the data $$x_i$$ can be found, along with the estimation of the parameters. The wide application of this circumstance in machine learning is what makes EM algorithm so important.

EM algorithm in GMM
The EM algorithm consists of two steps: the E-step and the M-step. Firstly, the model parameters and the $$z^{(i)}$$ can be randomly initialized. In the E-step, the algorithm tries to guess the value of $$z^{(i)}$$ based on the parameters, while in the M-step, the algorithm updates the value of the model parameters based on the guess of $$z^{(i)}$$of the E-step. These two steps are repeated until convergence is reached.

The algorithm in GMM is:

Repeat until convergence:

1. (E-step) For each $$i, j$$, set

$$w_{j}^{(i)}:=p\left(z^{(i)}=j | x^{(i)} ; \phi, \mu, \Sigma\right)$$

2. (M-step) Update the parameters $$\phi_{j} :=\frac{1}{m} \sum_{i=1}^{m} w_{j}^{(i)}$$ $$\mu_{j} :=\frac{\sum_{i=1}^{m} w_{j}^{(i)} x^{(i)}}{\sum_{i=1}^{m} w_{j}^{(i)}}$$ $$\Sigma_{j} :=\frac{\sum_{i=1}^{m} w_{j}^{(i)}\left(x^{(i)}-\mu_{j}\right)\left(x^{(i)}-\mu_{j}\right)^{T}}{\sum_{i=1}^{m} w_{j}^{(i)}}$$

With Bayes Rule, the following result is obtained by the E-step:

$$p\left(z^{(i)}=j | x^{(i)} ; \phi, \mu, \Sigma\right)=\frac{p\left(x^{(i)} | z^{(i)}=j ; \mu, \Sigma\right) p\left(z^{(i)}=j ; \phi\right)}{\sum_{l=1}^{k} p\left(x^{(i)} | z^{(i)}=l ; \mu, \Sigma\right) p\left(z^{(i)}=l ; \phi\right)}$$

According to GMM setting, these following formulas are obtained:

$$p\left(x^{(i)} | z^{(i)}=j ; \mu, \Sigma\right)=\frac{1}{(2 \pi)^{n / 2}\left|\Sigma_{j}\right|^{1 / 2}} \exp \left(-\frac{1}{2}\left(x^{(i)}-\mu_{j}\right)^{T} \Sigma_{j}^{-1}\left(x^{(i)}-\mu_{j}\right)\right)$$

$$p\left(z^{(i)}=j ; \phi\right)=\phi_j$$

In this way, a switch between the E-step and the M-step is possible, according to the randomly initialized parameters.