# 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:
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.