Matlab script to fit univariate Gaussian Mixture Model (GMM) to a distribution of an observable
This script uses fitgmdist function to fit the univariate Gaussian Mixture Model (GMM) to data, using a maximum likelihood estimator (MLE). GMM uses a superposition of k normal distributions, with mixing proportion xk, mean value µk and variance σk2. fitgmdist operates on raw and not on histogrammed data, and is therefore independent on the bin settings later used in the visualization of the results. As an otput, fitgmdist function returns GModel object, which contains the following properties:
- number of compoenent in a mixture model
- mean, variance and proportion of the components
- negative log likelihood at the found minimum
- information criteria, i.e. Akaike Information Criterion (AIC) and Bayesian Information Criterion (BIC)
Script is made to be used in interactive/explorative manner, and runs in sections. In the first section, no assumption on the fit model are made. Tested are GMM models with up to Ngaussians components, and the model with the minimum BIC value is taken as the one that describes data the best.
Alternatively, if there are presumptions about the distribution, in the following three code sections it also possible to fit specific GMM models to data, with up to three components. Script saves png and svg file of a histogram, overlayed with fit model as well as individual components in the mixture model. Fit results can be easily transfered to a spreadsheet: in the workspace right-clik on the observable of interest and select Copy. Alternatively a line of a code can be added to automatize export of the fit results, or an entire workspace can be saved for convenient handling of data at later point.
Currently script load a .txt file of a size Nx1 using importdata function. For input file extensions which are not compatible with importdata, consider other import functions. Exemplary input file is provided in example_data directory.
Fit_1D_GMM.m is a MATLAB script built on MATLAB R2019a, which requires fitgmdist function introduced in R2014a.
- McLachlan, G., and D. Peel. Finite Mixture Models. Hoboken, NJ: John Wiley & Sons, Inc., 2000.
- Milana Popara