annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/mog1.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 % Fit a mixture of Gaussians using netlab and BNT
Daniel@0 2
Daniel@0 3 rand('state', 0);
Daniel@0 4 randn('state', 0);
Daniel@0 5
Daniel@0 6 % Q -> Y
Daniel@0 7 ncenters = 2; dim = 2;
Daniel@0 8 cov_type = 'full';
Daniel@0 9
Daniel@0 10 % Generate the data from a mixture of 2 Gaussians
Daniel@0 11 %mu = randn(dim, ncenters);
Daniel@0 12 mu = zeros(dim, ncenters);
Daniel@0 13 mu(:,1) = [-1 -1]';
Daniel@0 14 mu(:,1) = [1 1]';
Daniel@0 15 Sigma = repmat(0.1*eye(dim),[1 1 ncenters]);
Daniel@0 16 ndat1 = 8; ndat2 = 8;
Daniel@0 17 %ndat1 = 2; ndat2 = 2;
Daniel@0 18 ndata = ndat1+ndat2;
Daniel@0 19 x1 = gsamp(mu(:,1), Sigma(:,:,1), ndat1);
Daniel@0 20 x2 = gsamp(mu(:,2), Sigma(:,:,2), ndat2);
Daniel@0 21 data = [x1; x2];
Daniel@0 22 %plot(x1(:,1),x1(:,2),'ro', x2(:,1),x2(:,2),'bx')
Daniel@0 23
Daniel@0 24 % Fit using netlab
Daniel@0 25 max_iter = 3;
Daniel@0 26 mix = gmm(dim, ncenters, cov_type);
Daniel@0 27 options = foptions;
Daniel@0 28 options(1) = 1; % verbose
Daniel@0 29 options(14) = max_iter;
Daniel@0 30
Daniel@0 31 % extract initial params
Daniel@0 32 %mix = gmminit(mix, x, options); % Initialize with K-means
Daniel@0 33 mu0 = mix.centres';
Daniel@0 34 pi0 = mix.priors(:);
Daniel@0 35 Sigma0 = mix.covars; % repmat(eye(dim), [1 1 ncenters]);
Daniel@0 36
Daniel@0 37 [mix, options] = gmmem(mix, data, options);
Daniel@0 38
Daniel@0 39 % Final params
Daniel@0 40 ll1 = options(8);
Daniel@0 41 mu1 = mix.centres';
Daniel@0 42 pi1 = mix.priors(:);
Daniel@0 43 Sigma1 = mix.covars;
Daniel@0 44
Daniel@0 45
Daniel@0 46
Daniel@0 47
Daniel@0 48 % BNT
Daniel@0 49
Daniel@0 50 dag = zeros(2);
Daniel@0 51 dag(1,2) = 1;
Daniel@0 52 node_sizes = [ncenters dim];
Daniel@0 53 discrete_nodes = 1;
Daniel@0 54 onodes = 2;
Daniel@0 55
Daniel@0 56 bnet = mk_bnet(dag, node_sizes, 'discrete', discrete_nodes, 'observed', onodes);
Daniel@0 57 bnet.CPD{1} = tabular_CPD(bnet, 1, pi0);
Daniel@0 58 bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', mu0, 'cov', Sigma0, 'cov_type', cov_type, ...
Daniel@0 59 'cov_prior_weight', 0);
Daniel@0 60
Daniel@0 61 engine = jtree_inf_engine(bnet);
Daniel@0 62
Daniel@0 63 evidence = cell(2, ndata);
Daniel@0 64 evidence(2,:) = num2cell(data', 1);
Daniel@0 65
Daniel@0 66 [bnet2, LL] = learn_params_em(engine, evidence, max_iter);
Daniel@0 67
Daniel@0 68 ll2 = LL(end);
Daniel@0 69 s1 = struct(bnet2.CPD{1});
Daniel@0 70 pi2 = s1.CPT(:);
Daniel@0 71
Daniel@0 72 s2 = struct(bnet2.CPD{2});
Daniel@0 73 mu2 = s2.mean;
Daniel@0 74 Sigma2 = s2.cov;
Daniel@0 75
Daniel@0 76 % assert(approxeq(ll1, ll2)); % gmmem returns the value after the final M step, GMT before
Daniel@0 77 assert(approxeq(mu1, mu2));
Daniel@0 78 assert(approxeq(Sigma1, Sigma2))
Daniel@0 79 assert(approxeq(pi1, pi2))
Daniel@0 80
Daniel@0 81