wolffd@0: %DEMGMM3 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 with a mixture model using diagonal wolffd@0: % covariance matrices. The priors are 0.3, 0.5 and 0.2; the centres wolffd@0: % are (2, 3.5), (0, 0) and (0,2); the covariances are all axis aligned wolffd@0: % (0.16, 0.64), (0.25, 1) and the identity matrix. The first figure wolffd@0: % 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), and centres (given wolffd@0: % as (x, y) pairs as the next two columns). The diagonal entries of wolffd@0: % the covariance matrices are printed separately. wolffd@0: % wolffd@0: % The second figure is a 3 dimensional view of the density function, wolffd@0: % while the third shows the axes of the 1-standard deviation circles wolffd@0: % for the three 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: ndata = 500; wolffd@0: wolffd@0: % Fix the seeds for reproducible results wolffd@0: randn('state', 42); wolffd@0: rand('state', 42); wolffd@0: data = randn(ndata, 2); wolffd@0: prior = [0.3 0.5 0.2]; wolffd@0: % Mixture model swaps clusters 1 and 3 wolffd@0: datap = [0.2 0.5 0.3]; wolffd@0: datac = [0 2; 0 0; 2 3.5]; wolffd@0: datacov = [1 1;1 0.25; 0.4*0.4 0.8*0.8]; wolffd@0: data1 = data(1:prior(1)*ndata,:); wolffd@0: data2 = data(prior(1)*ndata+1:(prior(2)+prior(1))*ndata, :); wolffd@0: data3 = data((prior(1)+prior(2))*ndata +1:ndata, :); wolffd@0: wolffd@0: % First cluster has axis aligned variance and centre (2, 3.5) wolffd@0: data1(:, 1) = data1(:, 1)*0.4 + 2.0; wolffd@0: data1(:, 2) = data1(:, 2)*0.8 + 3.5; wolffd@0: wolffd@0: % Second cluster has axis aligned variance and centre (0, 0) wolffd@0: data2(:,2) = data2(:, 2)*0.5; wolffd@0: wolffd@0: % Third cluster is at (0,2) with identity matrix for covariance wolffd@0: data3 = data3 + repmat([0 2], prior(3)*ndata, 1); wolffd@0: wolffd@0: % Put the dataset together again wolffd@0: data = [data1; data2; data3]; wolffd@0: wolffd@0: clc wolffd@0: disp('This demonstration illustrates the use of a Gaussian mixture model') wolffd@0: disp('with diagonal covariance matrices to approximate the unconditional') wolffd@0: disp('probability density of data in a two-dimensional space.') wolffd@0: disp('We begin by generating the data from a mixture of three Gaussians') wolffd@0: disp('with axis aligned covariance structure and plotting it.') wolffd@0: disp(' ') wolffd@0: disp('The first cluster has centre (0, 2).') wolffd@0: disp('The second cluster has centre (0, 0).') wolffd@0: disp('The third cluster has centre (2, 3.5).') 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: wolffd@0: % Set up mixture model wolffd@0: ncentres = 3; wolffd@0: input_dim = 2; wolffd@0: mix = gmm(input_dim, ncentres, 'diag'); 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: % Print out model wolffd@0: disp('The mixture model has three components and diagonal covariance') wolffd@0: disp('matrices. The model parameters after initialisation using the') wolffd@0: disp('k-means algorithm are as follows') wolffd@0: disp(' Priors Centres') wolffd@0: disp([mix.priors' mix.centres]) wolffd@0: disp('Covariance diagonals are') wolffd@0: disp(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) = 20; % Number of iterations. wolffd@0: wolffd@0: disp('We now train the model using the EM algorithm for 20 iterations.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: 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 priors and centres:') wolffd@0: disp(' Priors Centres') wolffd@0: disp([mix.priors' mix.centres]) wolffd@0: disp('The data generator has priors and centres') wolffd@0: disp(' Priors Centres') wolffd@0: disp([datap' datac]) wolffd@0: disp('Model covariance diagonals are') wolffd@0: disp(mix.covars) wolffd@0: disp('Data generator covariance diagonals are') wolffd@0: disp(datacov) wolffd@0: disp('Note the close correspondence between these parameters and those') wolffd@0: disp('of the distribution used to generate the data.') 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: 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: drawnow 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. The axes') wolffd@0: disp('of the ellipses of constant density are shown.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: 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('Position', fig2_pos); wolffd@0: wolffd@0: h = plot(data(:, 1), data(:, 2), 'bo'); wolffd@0: hold on wolffd@0: axis('equal'); wolffd@0: title('Plot of data and covariances') wolffd@0: for i = 1:ncentres wolffd@0: v = [1 0]; wolffd@0: for j = 1:2 wolffd@0: start=mix.centres(i,:)-sqrt(mix.covars(i,:).*v); wolffd@0: endpt=mix.centres(i,:)+sqrt(mix.covars(i,:).*v); wolffd@0: linex = [start(1) endpt(1)]; wolffd@0: liney = [start(2) endpt(2)]; wolffd@0: line(linex, liney, 'Color', 'k', 'LineWidth', 3) wolffd@0: v = [0 1]; wolffd@0: end wolffd@0: % Plot ellipses of one standard deviation wolffd@0: theta = 0:0.02:2*pi; wolffd@0: x = sqrt(mix.covars(i,1))*cos(theta) + mix.centres(i,1); wolffd@0: y = sqrt(mix.covars(i,2))*sin(theta) + mix.centres(i,2); wolffd@0: plot(x, y, 'r-'); wolffd@0: end wolffd@0: hold off wolffd@0: 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: