annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/ghmm1.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 % Make an HMM with Gaussian observations
wolffd@0 2 % X1 -> X2
wolffd@0 3 % | |
wolffd@0 4 % v v
wolffd@0 5 % Y1 Y2
wolffd@0 6
wolffd@0 7 intra = zeros(2);
wolffd@0 8 intra(1,2) = 1;
wolffd@0 9 inter = zeros(2);
wolffd@0 10 inter(1,1) = 1;
wolffd@0 11 n = 2;
wolffd@0 12
wolffd@0 13 Q = 2; % num hidden states
wolffd@0 14 O = 2; % size of observed vector
wolffd@0 15 ns = [Q O];
wolffd@0 16 bnet = mk_dbn(intra, inter, ns, 'discrete', 1, 'observed', 2);
wolffd@0 17
wolffd@0 18 prior0 = normalise(rand(Q,1));
wolffd@0 19 transmat0 = mk_stochastic(rand(Q,Q));
wolffd@0 20 mu0 = rand(O,Q);
wolffd@0 21 Sigma0 = repmat(eye(O), [1 1 Q]);
wolffd@0 22 bnet.CPD{1} = tabular_CPD(bnet, 1, prior0);
wolffd@0 23 %% we set the cov prior to 0 to give same results as HMM toolbox
wolffd@0 24 %bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', mu0, 'cov', Sigma0, 'cov_prior_weight', 0);
wolffd@0 25 bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', mu0, 'cov', Sigma0);
wolffd@0 26 bnet.CPD{3} = tabular_CPD(bnet, 3, transmat0);
wolffd@0 27
wolffd@0 28
wolffd@0 29 T = 5; % fixed length sequences
wolffd@0 30
wolffd@0 31 engine = {};
wolffd@0 32 engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet));
wolffd@0 33 engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet));
wolffd@0 34 engine{end+1} = hmm_inf_engine(bnet);
wolffd@0 35 engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T);
wolffd@0 36 %engine{end+1} = frontier_inf_engine(bnet);
wolffd@0 37 engine{end+1} = bk_inf_engine(bnet, 'clusters', {[1]});
wolffd@0 38 engine{end+1} = jtree_dbn_inf_engine(bnet);
wolffd@0 39
wolffd@0 40
wolffd@0 41 inf_time = cmp_inference_dbn(bnet, engine, T);
wolffd@0 42
wolffd@0 43 ncases = 2;
wolffd@0 44 max_iter = 2;
wolffd@0 45 [learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter);
wolffd@0 46
wolffd@0 47 % Compare to HMM toolbox
wolffd@0 48
wolffd@0 49 data = zeros(O, T, ncases);
wolffd@0 50 for i=1:ncases
wolffd@0 51 data(:,:,i) = cell2num(cases{i}(bnet.observed, :));
wolffd@0 52 end
wolffd@0 53
wolffd@0 54 tic
wolffd@0 55 [LL2, prior2, transmat2, mu2, Sigma2] = mhmm_em(data, prior0, transmat0, mu0, Sigma0, [], 'max_iter', max_iter);
wolffd@0 56 t=toc;
wolffd@0 57 disp(['HMM toolbox took ' num2str(t) ' seconds '])
wolffd@0 58
wolffd@0 59 e = 1;
wolffd@0 60 assert(approxeq(prior2, CPD{e,1}.CPT))
wolffd@0 61 assert(approxeq(mu2, CPD{e,2}.mean))
wolffd@0 62 assert(approxeq(Sigma2, CPD{e,2}.cov))
wolffd@0 63 assert(approxeq(transmat2, CPD{e,3}.CPT))
wolffd@0 64 assert(approxeq(LL2, LL{e}))