wolffd@0: %DEMGMM1 Demonstrate density modelling with a Gaussian mixture model. wolffd@0: % wolffd@0: % Description wolffd@0: % The problem consists of modelling data generated by a mixture of wolffd@0: % three Gaussians in 2 dimensions. The priors are 0.3, 0.5 and 0.2; wolffd@0: % the centres are (2, 3.5), (0, 0) and (0,2); the variances are 0.2, wolffd@0: % 0.5 and 1.0. The first figure contains a scatter plot of the data. wolffd@0: % wolffd@0: % A Gaussian mixture model with three components is trained using EM. wolffd@0: % The parameter vector is printed before training and after training. wolffd@0: % The user should press any key to continue at these points. The wolffd@0: % parameter vector consists of priors (the column), centres (given as wolffd@0: % (x, y) pairs as the next two columns), and variances (the last wolffd@0: % column). wolffd@0: % wolffd@0: % The second figure is a 3 dimensional view of the density function, wolffd@0: % while the third shows the 1-standard deviation circles for the three wolffd@0: % components of the mixture model. wolffd@0: % wolffd@0: % See also wolffd@0: % GMM, GMMINIT, GMMEM, GMMPROB, GMMUNPAK wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: % Generate the data wolffd@0: % Fix seeds for reproducible results wolffd@0: randn('state', 42); wolffd@0: rand('state', 42); wolffd@0: wolffd@0: ndata = 500; wolffd@0: [data, datac, datap, datasd] = dem2ddat(ndata); wolffd@0: wolffd@0: clc wolffd@0: disp('This demonstration illustrates the use of a Gaussian mixture model') wolffd@0: disp('to approximate the unconditional probability density of data in') wolffd@0: disp('a two-dimensional space. We begin by generating the data from') wolffd@0: disp('a mixture of three Gaussians and plotting it.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue') wolffd@0: pause wolffd@0: wolffd@0: fh1 = figure; wolffd@0: plot(data(:, 1), data(:, 2), 'o') wolffd@0: set(gca, 'Box', 'on') wolffd@0: % Set up mixture model wolffd@0: ncentres = 3; wolffd@0: input_dim = 2; wolffd@0: mix = gmm(input_dim, ncentres, 'spherical'); wolffd@0: wolffd@0: options = foptions; wolffd@0: options(14) = 5; % Just use 5 iterations of k-means in initialisation wolffd@0: % Initialise the model parameters from the data wolffd@0: mix = gmminit(mix, data, options); wolffd@0: wolffd@0: clc wolffd@0: disp('The data is drawn from a mixture with parameters') wolffd@0: disp(' Priors Centres Variances') wolffd@0: disp([datap' datac (datasd.^2)']) wolffd@0: disp(' ') wolffd@0: disp('The mixture model has three components and spherical covariance') wolffd@0: disp('matrices. The model parameters after initialisation using the') wolffd@0: disp('k-means algorithm are as follows') wolffd@0: % Print out model wolffd@0: disp(' Priors Centres Variances') wolffd@0: disp([mix.priors' mix.centres mix.covars']) wolffd@0: disp('Press any key to continue') wolffd@0: pause wolffd@0: wolffd@0: % Set up vector of options for EM trainer wolffd@0: options = zeros(1, 18); wolffd@0: options(1) = 1; % Prints out error values. wolffd@0: options(14) = 10; % Max. Number of iterations. wolffd@0: wolffd@0: disp('We now train the model using the EM algorithm for 10 iterations') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue') wolffd@0: pause wolffd@0: [mix, options, errlog] = gmmem(mix, data, options); wolffd@0: wolffd@0: % Print out model wolffd@0: disp(' ') wolffd@0: disp('The trained model has parameters ') wolffd@0: disp(' Priors Centres Variances') wolffd@0: disp([mix.priors' mix.centres mix.covars']) wolffd@0: disp('Note the close correspondence between these parameters and those') wolffd@0: disp('of the distribution used to generate the data, which are repeated here.') wolffd@0: disp(' Priors Centres Variances') wolffd@0: disp([datap' datac (datasd.^2)']) wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue') wolffd@0: pause wolffd@0: wolffd@0: clc wolffd@0: disp('We now plot the density given by the mixture model as a surface plot') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue') wolffd@0: pause wolffd@0: % Plot the result wolffd@0: x = -4.0:0.2:5.0; wolffd@0: y = -4.0:0.2:5.0; wolffd@0: [X, Y] = meshgrid(x,y); wolffd@0: X = X(:); wolffd@0: Y = Y(:); wolffd@0: grid = [X Y]; wolffd@0: Z = gmmprob(mix, grid); wolffd@0: Z = reshape(Z, length(x), length(y)); wolffd@0: c = mesh(x, y, Z); wolffd@0: hold on wolffd@0: title('Surface plot of probability density') wolffd@0: hold off wolffd@0: wolffd@0: clc wolffd@0: disp('The final plot shows the centres and widths, given by one standard') wolffd@0: disp('deviation, of the three components of the mixture model.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: % Try to calculate a sensible position for the second figure, below the first wolffd@0: fig1_pos = get(fh1, 'Position'); wolffd@0: fig2_pos = fig1_pos; wolffd@0: fig2_pos(2) = fig2_pos(2) - fig1_pos(4); wolffd@0: fh2 = figure; wolffd@0: set(fh2, 'Position', fig2_pos) wolffd@0: wolffd@0: hp1 = plot(data(:, 1), data(:, 2), 'bo'); wolffd@0: axis('equal'); wolffd@0: hold on wolffd@0: hp2 = plot(mix.centres(:, 1), mix.centres(:,2), 'g+'); wolffd@0: set(hp2, 'MarkerSize', 10); wolffd@0: set(hp2, 'LineWidth', 3); wolffd@0: wolffd@0: title('Plot of data and mixture centres') wolffd@0: angles = 0:pi/30:2*pi; wolffd@0: for i = 1 : mix.ncentres wolffd@0: x_circle = mix.centres(i,1)*ones(1, length(angles)) + ... wolffd@0: sqrt(mix.covars(i))*cos(angles); wolffd@0: y_circle = mix.centres(i,2)*ones(1, length(angles)) + ... wolffd@0: sqrt(mix.covars(i))*sin(angles); wolffd@0: plot(x_circle, y_circle, 'r') wolffd@0: end wolffd@0: hold off wolffd@0: disp('Note how the data cluster positions and widths are captured by') wolffd@0: disp('the mixture model.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to end.') wolffd@0: pause wolffd@0: wolffd@0: close(fh1); wolffd@0: close(fh2); wolffd@0: clear all; wolffd@0: