Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/netlabKPM/demgmm1_movie.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 EM for Gaussian mixtures. | |
2 % | |
3 % Description | |
4 % This script demonstrates the use of the EM algorithm to fit a mixture | |
5 % of Gaussians to a set of data using maximum likelihood. A colour | |
6 % coding scheme is used to illustrate the evaluation of the posterior | |
7 % probabilities in the E-step of the EM algorithm. | |
8 % | |
9 % See also | |
10 % DEMGMM2, DEMGMM3, DEMGMM4, GMM, GMMEM, GMMPOST | |
11 % | |
12 | |
13 % Copyright (c) Ian T Nabney (1996-2001) | |
14 | |
15 mov = avifile('movies/gmm1.avi','fps',1 ); | |
16 | |
17 % Generate the data | |
18 randn('state', 0); rand('state', 0); | |
19 gmix = gmm(2, 2, 'spherical'); | |
20 ndat1 = 20; ndat2 = 20; ndata = ndat1+ndat2; | |
21 gmix.centres = [0.3 0.3; 0.7 0.7]; | |
22 gmix.covars = [0.01 0.01]; | |
23 x = gmmsamp(gmix, ndata); | |
24 | |
25 h = figure; | |
26 hd = plot(x(:, 1), x(:, 2), '.g', 'markersize', 30); | |
27 hold on; axis([0 1 0 1]); axis square; set(gca, 'box', 'on'); | |
28 ht = text(0.5, 1.05, 'Data', 'horizontalalignment', 'center'); | |
29 | |
30 | |
31 % Set up mixture model | |
32 ncentres = 2; input_dim = 2; | |
33 mix = gmm(input_dim, ncentres, 'spherical'); | |
34 | |
35 % Initialise the mixture model | |
36 mix.centres = [0.2 0.8; 0.8, 0.2]; | |
37 mix.covars = [0.01 0.01]; | |
38 | |
39 % Plot the initial model | |
40 ncirc = 30; theta = linspace(0, 2*pi, ncirc); | |
41 xs = cos(theta); ys = sin(theta); | |
42 xvals = mix.centres(:, 1)*ones(1,ncirc) + sqrt(mix.covars')*xs; | |
43 yvals = mix.centres(:, 2)*ones(1,ncirc) + sqrt(mix.covars')*ys; | |
44 hc(1)=line(xvals(1,:), yvals(1,:), 'color', 'r'); | |
45 hc(2)=line(xvals(2,:), yvals(2,:), 'color', 'b'); | |
46 set(ht, 'string', 'Initial Configuration'); | |
47 figure(h); | |
48 mov = addframe(mov, getframe(gcf)); | |
49 mov = addframe(mov, getframe(gcf)); | |
50 | |
51 % Initial E-step. | |
52 set(ht, 'string', 'E-step'); | |
53 post = gmmpost(mix, x); | |
54 dcols = [post(:,1), zeros(ndata, 1), post(:,2)]; | |
55 delete(hd); | |
56 for i = 1 : ndata | |
57 hd(i) = plot(x(i, 1), x(i, 2), 'color', dcols(i,:), ... | |
58 'marker', '.', 'markersize', 30); | |
59 end | |
60 | |
61 % M-step. | |
62 set(ht, 'string', 'M-step'); | |
63 options = foptions; | |
64 options(14) = 1; % A single iteration | |
65 options(1) = -1; % Switch off all messages, including warning | |
66 mix = gmmem(mix, x, options); | |
67 delete(hc); | |
68 xvals = mix.centres(:, 1)*ones(1,ncirc) + sqrt(mix.covars')*xs; | |
69 yvals = mix.centres(:, 2)*ones(1,ncirc) + sqrt(mix.covars')*ys; | |
70 hc(1)=line(xvals(1,:), yvals(1,:), 'color', 'r'); | |
71 hc(2)=line(xvals(2,:), yvals(2,:), 'color', 'b'); | |
72 figure(h); | |
73 mov = addframe(mov, getframe(gcf)); | |
74 mov = addframe(mov, getframe(gcf)); | |
75 | |
76 % Loop over EM iterations. | |
77 numiters = 9; | |
78 for n = 1 : numiters | |
79 | |
80 set(ht, 'string', 'E-step'); | |
81 post = gmmpost(mix, x); | |
82 dcols = [post(:,1), zeros(ndata, 1), post(:,2)]; | |
83 delete(hd); | |
84 for i = 1 : ndata | |
85 hd(i) = plot(x(i, 1), x(i, 2), 'color', dcols(i,:), ... | |
86 'marker', '.', 'markersize', 30); | |
87 end | |
88 %pause(1) | |
89 | |
90 set(ht, 'string', 'M-step'); | |
91 [mix, options] = gmmem(mix, x, options); | |
92 fprintf(1, 'Cycle %4d Error %11.6f\n', n, options(8)); | |
93 delete(hc); | |
94 xvals = mix.centres(:, 1)*ones(1,ncirc) + sqrt(mix.covars')*xs; | |
95 yvals = mix.centres(:, 2)*ones(1,ncirc) + sqrt(mix.covars')*ys; | |
96 hc(1)=line(xvals(1,:), yvals(1,:), 'color', 'r'); | |
97 hc(2)=line(xvals(2,:), yvals(2,:), 'color', 'b'); | |
98 pause(1) | |
99 | |
100 mov = addframe(mov, getframe(gcf)); | |
101 end | |
102 | |
103 mov = close(mov); |