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