annotate toolboxes/FullBNT-1.0.7/netlab3.3/demgmm2.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 %DEMGMM1 Demonstrate density modelling with a Gaussian mixture model.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 % The problem consists of modelling data generated by a mixture of
Daniel@0 5 % three Gaussians in 2 dimensions. The priors are 0.3, 0.5 and 0.2;
Daniel@0 6 % the centres are (2, 3.5), (0, 0) and (0,2); the variances are 0.2,
Daniel@0 7 % 0.5 and 1.0. The first figure contains a scatter plot of the data.
Daniel@0 8 %
Daniel@0 9 % A Gaussian mixture model with three components is trained using EM.
Daniel@0 10 % The parameter vector is printed before training and after training.
Daniel@0 11 % The user should press any key to continue at these points. The
Daniel@0 12 % parameter vector consists of priors (the column), centres (given as
Daniel@0 13 % (x, y) pairs as the next two columns), and variances (the last
Daniel@0 14 % column).
Daniel@0 15 %
Daniel@0 16 % The second figure is a 3 dimensional view of the density function,
Daniel@0 17 % while the third shows the 1-standard deviation circles for the three
Daniel@0 18 % components of the mixture model.
Daniel@0 19 %
Daniel@0 20 % See also
Daniel@0 21 % GMM, GMMINIT, GMMEM, GMMPROB, GMMUNPAK
Daniel@0 22 %
Daniel@0 23
Daniel@0 24 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 25
Daniel@0 26 % Generate the data
Daniel@0 27 % Fix seeds for reproducible results
Daniel@0 28 randn('state', 42);
Daniel@0 29 rand('state', 42);
Daniel@0 30
Daniel@0 31 ndata = 500;
Daniel@0 32 [data, datac, datap, datasd] = dem2ddat(ndata);
Daniel@0 33
Daniel@0 34 clc
Daniel@0 35 disp('This demonstration illustrates the use of a Gaussian mixture model')
Daniel@0 36 disp('to approximate the unconditional probability density of data in')
Daniel@0 37 disp('a two-dimensional space. We begin by generating the data from')
Daniel@0 38 disp('a mixture of three Gaussians and plotting it.')
Daniel@0 39 disp(' ')
Daniel@0 40 disp('Press any key to continue')
Daniel@0 41 pause
Daniel@0 42
Daniel@0 43 fh1 = figure;
Daniel@0 44 plot(data(:, 1), data(:, 2), 'o')
Daniel@0 45 set(gca, 'Box', 'on')
Daniel@0 46 % Set up mixture model
Daniel@0 47 ncentres = 3;
Daniel@0 48 input_dim = 2;
Daniel@0 49 mix = gmm(input_dim, ncentres, 'spherical');
Daniel@0 50
Daniel@0 51 options = foptions;
Daniel@0 52 options(14) = 5; % Just use 5 iterations of k-means in initialisation
Daniel@0 53 % Initialise the model parameters from the data
Daniel@0 54 mix = gmminit(mix, data, options);
Daniel@0 55
Daniel@0 56 clc
Daniel@0 57 disp('The data is drawn from a mixture with parameters')
Daniel@0 58 disp(' Priors Centres Variances')
Daniel@0 59 disp([datap' datac (datasd.^2)'])
Daniel@0 60 disp(' ')
Daniel@0 61 disp('The mixture model has three components and spherical covariance')
Daniel@0 62 disp('matrices. The model parameters after initialisation using the')
Daniel@0 63 disp('k-means algorithm are as follows')
Daniel@0 64 % Print out model
Daniel@0 65 disp(' Priors Centres Variances')
Daniel@0 66 disp([mix.priors' mix.centres mix.covars'])
Daniel@0 67 disp('Press any key to continue')
Daniel@0 68 pause
Daniel@0 69
Daniel@0 70 % Set up vector of options for EM trainer
Daniel@0 71 options = zeros(1, 18);
Daniel@0 72 options(1) = 1; % Prints out error values.
Daniel@0 73 options(14) = 10; % Max. Number of iterations.
Daniel@0 74
Daniel@0 75 disp('We now train the model using the EM algorithm for 10 iterations')
Daniel@0 76 disp(' ')
Daniel@0 77 disp('Press any key to continue')
Daniel@0 78 pause
Daniel@0 79 [mix, options, errlog] = gmmem(mix, data, options);
Daniel@0 80
Daniel@0 81 % Print out model
Daniel@0 82 disp(' ')
Daniel@0 83 disp('The trained model has parameters ')
Daniel@0 84 disp(' Priors Centres Variances')
Daniel@0 85 disp([mix.priors' mix.centres mix.covars'])
Daniel@0 86 disp('Note the close correspondence between these parameters and those')
Daniel@0 87 disp('of the distribution used to generate the data, which are repeated here.')
Daniel@0 88 disp(' Priors Centres Variances')
Daniel@0 89 disp([datap' datac (datasd.^2)'])
Daniel@0 90 disp(' ')
Daniel@0 91 disp('Press any key to continue')
Daniel@0 92 pause
Daniel@0 93
Daniel@0 94 clc
Daniel@0 95 disp('We now plot the density given by the mixture model as a surface plot')
Daniel@0 96 disp(' ')
Daniel@0 97 disp('Press any key to continue')
Daniel@0 98 pause
Daniel@0 99 % Plot the result
Daniel@0 100 x = -4.0:0.2:5.0;
Daniel@0 101 y = -4.0:0.2:5.0;
Daniel@0 102 [X, Y] = meshgrid(x,y);
Daniel@0 103 X = X(:);
Daniel@0 104 Y = Y(:);
Daniel@0 105 grid = [X Y];
Daniel@0 106 Z = gmmprob(mix, grid);
Daniel@0 107 Z = reshape(Z, length(x), length(y));
Daniel@0 108 c = mesh(x, y, Z);
Daniel@0 109 hold on
Daniel@0 110 title('Surface plot of probability density')
Daniel@0 111 hold off
Daniel@0 112
Daniel@0 113 clc
Daniel@0 114 disp('The final plot shows the centres and widths, given by one standard')
Daniel@0 115 disp('deviation, of the three components of the mixture model.')
Daniel@0 116 disp(' ')
Daniel@0 117 disp('Press any key to continue.')
Daniel@0 118 pause
Daniel@0 119 % Try to calculate a sensible position for the second figure, below the first
Daniel@0 120 fig1_pos = get(fh1, 'Position');
Daniel@0 121 fig2_pos = fig1_pos;
Daniel@0 122 fig2_pos(2) = fig2_pos(2) - fig1_pos(4);
Daniel@0 123 fh2 = figure;
Daniel@0 124 set(fh2, 'Position', fig2_pos)
Daniel@0 125
Daniel@0 126 hp1 = plot(data(:, 1), data(:, 2), 'bo');
Daniel@0 127 axis('equal');
Daniel@0 128 hold on
Daniel@0 129 hp2 = plot(mix.centres(:, 1), mix.centres(:,2), 'g+');
Daniel@0 130 set(hp2, 'MarkerSize', 10);
Daniel@0 131 set(hp2, 'LineWidth', 3);
Daniel@0 132
Daniel@0 133 title('Plot of data and mixture centres')
Daniel@0 134 angles = 0:pi/30:2*pi;
Daniel@0 135 for i = 1 : mix.ncentres
Daniel@0 136 x_circle = mix.centres(i,1)*ones(1, length(angles)) + ...
Daniel@0 137 sqrt(mix.covars(i))*cos(angles);
Daniel@0 138 y_circle = mix.centres(i,2)*ones(1, length(angles)) + ...
Daniel@0 139 sqrt(mix.covars(i))*sin(angles);
Daniel@0 140 plot(x_circle, y_circle, 'r')
Daniel@0 141 end
Daniel@0 142 hold off
Daniel@0 143 disp('Note how the data cluster positions and widths are captured by')
Daniel@0 144 disp('the mixture model.')
Daniel@0 145 disp(' ')
Daniel@0 146 disp('Press any key to end.')
Daniel@0 147 pause
Daniel@0 148
Daniel@0 149 close(fh1);
Daniel@0 150 close(fh2);
Daniel@0 151 clear all;
Daniel@0 152