📚 The CoCalc Library - books, templates and other resources
License: OTHER
Density Estimation: Gaussian Mixture Models
Credits: Forked from PyCon 2015 Scikit-learn Tutorial by Jake VanderPlas
Here we'll explore Gaussian Mixture Models, which is an unsupervised clustering & density estimation technique.
We'll start with our standard set of initial imports
Introducing Gaussian Mixture Models
We previously saw an example of K-Means, which is a clustering algorithm which is most often fit using an expectation-maximization approach.
Here we'll consider an extension to this which is suitable for both clustering and density estimation.
For example, imagine we have some one-dimensional data in a particular distribution:
Gaussian mixture models will allow us to approximate this density:
Note that this density is fit using a mixture of Gaussians, which we can examine by looking at the means_
, covars_
, and weights_
attributes:
These individual Gaussian distributions are fit using an expectation-maximization method, much as in K means, except that rather than explicit cluster assignment, the posterior probability is used to compute the weighted mean and covariance. Somewhat surprisingly, this algorithm provably converges to the optimum (though the optimum is not necessarily global).
How many Gaussians?
Given a model, we can use one of several means to evaluate how well it fits the data. For example, there is the Aikaki Information Criterion (AIC) and the Bayesian Information Criterion (BIC)
Let's take a look at these as a function of the number of gaussians:
It appears that for both the AIC and BIC, 4 components is preferred.
Example: GMM For Outlier Detection
GMM is what's known as a Generative Model: it's a probabilistic model from which a dataset can be generated. One thing that generative models can be useful for is outlier detection: we can simply evaluate the likelihood of each point under the generative model; the points with a suitably low likelihood (where "suitable" is up to your own bias/variance preference) can be labeld outliers.
Let's take a look at this by defining a new dataset with some outliers:
Now let's evaluate the log-likelihood of each point under the model, and plot these as a function of y
:
The algorithm misses a few of these points, which is to be expected (some of the "outliers" actually land in the middle of the distribution!)
Here are the outliers that were missed:
And here are the non-outliers which were spuriously labeled outliers:
Finally, we should note that although all of the above is done in one dimension, GMM does generalize to multiple dimensions, as we'll see in the breakout session.
Other Density Estimators
The other main density estimator that you might find useful is Kernel Density Estimation, which is available via sklearn.neighbors.KernelDensity
. In some ways, this can be thought of as a generalization of GMM where there is a gaussian placed at the location of every training point!
All of these density estimators can be viewed as Generative models of the data: that is, that is, the model tells us how more data can be created which fits the model.