Mixtures of Gaussians
Earl F. Glynn
Data measurements of many properties are often normally distributed, but with heterogeneous populations, sometimes data measurements reflect a mixture of normal distributions. The following notes are intended to give some insight about such mixtures and how they can be "unmixed" into separate distributions. R code is provided to reproduce the calculations and figures below.
The normal or Gaussian probability density function (pdf)can be written as follows:
Below in Fig 1 the n(x,0,1), "Single Gaussian," has μ = 0 (mean) and σ = 1 (standard deviation).
Fig. 1. Single Gaussian with its 1st and 2nd Derivatives
y = n(x; 0.0, 1.0)
The peaks in the second derivative occur approximately at x = ± 1.73
Mixture of Two Gaussians.
Consider a population that is a mixture of two Gaussian distributions. Shown in Fig 2 is this mixture: y = n(x; 3.75, 0.75) + n(x; 6.00, 0.50)
Fig. 2. Sum of Two Gaussians along with 1st and 2nd Derivatives
y = n(x; 3.75, 0.75) + n(x; 6.00, 0.50)
Based on our observations of the Single Gaussian above, we can graphically find the mean and standard deviations of these two distributions. To create Fig. 2, or see all the R code mentioned below, see TwoGaussians.R with output in TwoGaussians.pdf.
The x distance from the peak of the first derivative to the valley of the first derivative should be 2σ, which can be used to find σ:
Graphically we find the σ of the first Gaussian is 0.70 (expected 0.75), while the σ of the second Gaussian is 0.48 (expected 0.50).
With some mixtures, all the peaks of the original distribution may not be obvious (see Heming Lake Pike problem in the next section). Instead, the valleys in the second derivative may be a better way to find the mean values.
Graphically we find the μ of the first Gaussian is 3.75 (expected 3.75), while the μ of the second Gaussian is 6.01 (expected 6.00). Overall, the graphical approach worked well, but a non-linear curve fitting algorithm will be useful in situations where we cannot find the answers by simply studying the derivative plots. The R nls (nonlinear least squares) function can be used to find the best fit.
Oddly, the nls function fails with "toy" problems like this with exact solutions. One must add a "small" amount of noise to the original function to get around this problem. (I think this design of nls is flawed and should be fixed, but I've never studied exactly what the problem is.)
This iterative technique requires "good" initial values, and there's no guarantee of convergence. Above, the μ (mean) values are c=3.74998 and f=6.00000; the σ values are b=0.75004 and e=0.50002. Overall, these are good results. The a and d values in this case should be 1/sqrt(2*pi) = 0.39894.
What if our mixtures are weighted, e.g.,
The "Sum of Two Gaussians" section of MixingGaussians.doc shows several examples of this.
The Heming Lake Pike Mixture.
As a final example, let's look at the Heming Lake Pike mixture. The figures below were created from summary μ, σ statistics from data in this file. These statistics were used to create an "exact" curve for analysis purposes.
These weights, means, and standard deviations are represented in this expression:
y = w1*n(x; μ1, σ1) + w2*n(x; μ2, σ2) + ... + w5*n(x; μ5, σ5)
The top panel in Fig 3 below shows a graph of this function in bold black, with each of the component distributions in various colors.
Fig. 3. Heming Lake Pike Mixture
The number of Gaussians in this mixture is not clear from the original plot. There are two obvious peaks in the original data, but the "bump" on the right side of the major peak hints there is a third Gaussian. The 1st derivative shows there are at least four peak/valley pairs, so there must be at least four Gaussians in the mixture. The peaks in the 2nd derivative show 4 clear peaks, with possibly 5 or 6 peaks. Here the 6th peak is erroneous. The five Gaussian distributions an be estimated from only graphical analysis.
The graphical analyis gives these results for the first four distributions:
These graphical results compare well with the actual values (see below):
However the graphical results do not give the weights of the origial probability density functions.
Remember, this fit included only four of the five terms used to compute the original function.
Means ( μ) :
Standard Deviations (σ):
Weights: the computed values multiplied by a constant, sqrt(2*pi), allows comparison to the original values:
The weights show general agreement but with less accuracy than the means or standard deviations. In general, the coefficients of each term in a mixture of distributions need to be comparable. Some "tweaking" of the convergence criterial in the nonlinear least squares call sometimes can improve results.
9 Feb 2007