{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exploring the EM Algorithm \n",
"\n",
"> **tl;dr:** I explain, visualize, and code the EM Algorithm for Gaussian Mixture Models and explain how it is basically just an example of Variational Inference.\n",
"\n",
"In my AMLED class we recently talked about how Maximum Likelihood can help you find the best statistical model:\n",
"\n",
"1. Write down the formula for the Likelihood of the data $P(\\mathbf{X})$. This is how likely the data are with respect to your probabilistic models parameters.\n",
"2. Take that likelihood, and take the natural logarithm of it. This won't change the optimal parameters that maximize the likelihood, since the logarithm is a monotonically increasing function.\n",
"3. Take that log-likelihood, and optimize it, typically by setting the partial derivatives of the log-likelihood w.r.t. each parameter equal to 0 and solving for which parameters get you that.\n",
"\n",
"A typical example of this is [finding the $\\mu$ and $\\sigma$ of a Gaussian Distribution](https://en.wikipedia.org/wiki/Maximum_likelihood#Continuous_distribution.2C_continuous_parameter_space).\n",
"\n",
"This works fine for certain convex likelihoods (provided you are careful to take care of things like singularities, ill-conditioned matrices, etc.). However, for many types of models this optimization is not straight-forward: the optimal parameters depend on one another in complex, non-linear ways, and you can't just \"plug-in\" the right data to get the optimal answer. One case where this occurs is in Latent Variable models, such as [Gaussian Mixture Models](https://en.wikipedia.org/wiki/Mixture_model#Gaussian_mixture_model), that is, where you have data that are generated from multiple gaussian distributions, but you don't know which distribution generated which data point. While this might seem similar to the MLE of the Gaussian Distribution above, it is actually much more complex.\n",
"\n",
"One way to solve this problem is by first finding a \"simpler\" distribution, for which analytical closed form updates are possible, and then matching that simpler distribution as closely as possible to the \"harder\" distribution. This is the basic idea behind [Variational Inference](https://en.wikipedia.org/wiki/Variational_Bayesian_methods). Often, one can iteratively update simpler models to get closer and closer to the \"harder\" one. That iteration between simplier models is the basic idea of the [Expectation-Maximization Algorithm](https://en.wikipedia.org/wiki/Expectation%E2%80%93maximization_algorithm). And in some cases, with certain models and distributions, that iterative process of inching closer and close with simpler distributions can actually get you the exact solution to the harder distribution that you originally wanted. Below, we will see that Gaussian Mixture Models are one case where that happens. Most of the time, however, you will not be so lucky and the optimized simple models will be a little different than what an optimized hard model would be; in these cases you just hope that the diffence doesn't affect actual performance much.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" \n",
"