Clustering / Unsupervised Learning
Gaussian Mixture Model, GMM
2023-12-21
While the K-means clustering model works on the simple rule of assigning clusters based on the nearest geometric Centroid, the Gaussian model uses a statistical approach. It considers not only the Centroid, but also the distribution when determining clusters. If the distribution between the clusters is large, some data points may belong to a cluster with larger variance even if they are physically closer to another one. For instance, considering the example in the illustration below, the red data sample point may be closer to the Centroid of cluster C1 in terms of physical distance, but statistically considering (i.e., when taking into account the variance), it may belong to cluster C2.

The GMM signifies that the entire data is a result of a mixture of several Gaussian distributions.
The process of the GMM uses the EM (Expectation-Maximization) algorithm. In this Blog, a detail derivation of the EM is not provided but readers can grasp of the concept quickly by learning by doing. The key idea is to introduce a variable called a mixture ratio that represents how much each data is influenced by the center of the scatter and the standard deviation of the corresponding cluster. The mixture ration so-called "mixing coefficients" will be iteratively updated until there is no further cluster change. Like K-means Clustering, GMM needs users' input for the number of clusters.
For the indexing purpose, let's assume the size of the total samples = n, the number of features = d, and the number of clusters = c, and associate index is i=(1,...,n), j=(1,...,d), k=(1,...,c)
Detail steps for GMM are quite simple as shown below:
Step 1. Initialize the mean, \(\mu\) and variance, \(\sigma^2\) per feature per Cluster, and the mixing coefficient, \(\phi\) per Cluster. The sum of all the mixing coefficients must be 1. The size of the mean and variance matrix is [dxc] and the size of the mixing coefficients is [1xc].
Step 2-1. Compute the ϕ weighted Gaussian Probability, P.
Step 2-2. Then normalize it with respect to the sum of all P per sample to calculate, w. The w is the normalized and weighted from the mixture towards the centre of each cluster for each data.
Step 2-3. Sample is assigned to the cluster with the highest w value.
The formula for the Gaussian Probability, P, is
\(P_{ik}=\dfrac{1}{(2\pi )^{d/2}\sqrt{|\sigma_k^2|}}\exp \begin{pmatrix}\ \sum _{j=1}^d -\dfrac {\phi _k}{2} \dfrac{(x_{ij}-\mu _{jk})^2}{\sigma_{jk}^2}\end{pmatrix}\)
and its size of the matrix is [nxc].
The total variance \(|\sigma^2|\) is
\(\left|\sigma _k^2\right|=\sum _{_{j=1}}^d\sigma _{jk}^2\).
Then, the normalized Gaussian probability, w, becomes
\(w_{ik}=\dfrac{P_{ik}}{\sum _{k=1}^cP_{ik}}\)
which satisfies the followings:
\(\sum _{k=1}^cw_{ik}=1 \)
\(\sum _{i=1}^nw_{ik}=n_k\)
What these mean (1) the sum of the probability over the Clusters per sample is 1 and (2) the sum of the probability over the all the samples per Cluster is the number samples belong to the Cluster.
Step 3-1 Update Mixture Ratio for each Cluster
It is the ratio toward the total sum of the Gaussian probability of each Cluster. The mixture ratio φ for each cluster is
\(\phi_k=\dfrac{\sum _{i=1}^nw_{ik}}{n}\)
Step 3-2 Update of the average weighted by the mixture ratio
\(\mu_{jk}=\dfrac{\sum _{i=1\ }^{\ n}(w_{ik}x_{ij})}{\sum _{i=1}^nw_{ik}}\)
Step 3-3 Update the variance weighted by the mixture ratio
\(\sigma_{jk}=\dfrac{\sum _{\ i=1}^{\ n}w_{ik}(x_{ij}-\mu_{jk})^2}{\sum _{\ i=1}^{\ n}w_{ik}}\)
Repeat Step 2 and Step 3 until there is no member change in the Cluster
Gaussian Mixtures can be used for anomaly detection. An anomaly is the sample that deviates significantly from the standard deviation so that it cannot be considered part of the same statistical group.
The optimal number of clusters in GMM (in K-means clustering, Elbow using Inertia or Silhouette was used) can be determined by minimizing the theoretical information criteria such as BIC (Bayesian information criterion) or AIC (Akaike information criterion). They are defined by
\(\text{BIC}=\log (n)p-2\log (L)\)
\(\text{AIC}=2p-2\log (L)\)
where n is the number of samples, p is the number of model parameters to be learned, that is, the number of source parameters, and L is the maximum value of the model's likelihood function. These are obtained from the weighted probability value calculated after the clustering is completed (i.e. after maximization, in other words, after E-M's M is achieved).
\(\log (L)=\sum _n^{\ }\ln \sum _{\ }^{\ }\phi_kN(x|\mu_k,\sigma_k)\)
That is, it becomes the sum of the log values of the weighted probabilities for each cluster of all data.
In Excel, you can easily replicate the above process using the SUMPRODUCT function. For the example, we used iris data as in the K-means clustering. The size of the dataset is 150 (i.e. n=150), and there are 4 features, (i.e. d=4)
Step 1. Set the number of Clusters=3, and take the value from any random sample as an initial mean per Cluster, and assume all the Cluster has the same variance per feature, and set all of mixing coefficients (start evenly so that the total sum is 1) equal to 1/(number of Cluster).

Step 2. Calculate the weighted Gaussian probabilities for each cluster for each sample, using the given means, variances, and mixtures, to ensure that the sum of all clusters is normalized to 1.
Step 2.1 The weighted Gaussian probability is
\(P=\dfrac{1}{(2\pi )^{2}\sqrt{|v|}}\exp \begin{pmatrix}-\dfrac{\phi}{2}\dfrac{(x-\mu)^T(x-\mu)}{v}\end{pmatrix}\)
Step 2.2 Normalized probability w per sample is
\(w_i=\dfrac{P_i}{\sum _{k=1}^{c}P_k}\)
Step 2.3 Choose the Cluster with the highest w value

Step 3.1 Update the mixture ratio, \(\phi\) for each cluster.
W1 = SUM(w1 column)
W2 = SUM(w2 column)
W3 = SUM(w3 column)
\(\phi_k = \dfrac {W_k}{W_1+W_2+W_3}\)
Step 3.2 Update the mean, \(\mu\).
μ=SUMPRODUCT(X,w)/W
Step 3.3 Update the variance, \(\sigma^2\).
variance = SUMPRODUCT(X-μ,X-μ,w)/W

Repeat Step 2 and 3 until the Clustering converge.
Following shows the BIC example for the cases of number of clusters 2,3,4.
