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