annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/dhmm1.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 discrete 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; % num observable symbols
wolffd@0 15
wolffd@0 16 ns = [Q O];
wolffd@0 17 dnodes = 1:2;
wolffd@0 18 onodes = [2];
wolffd@0 19 eclass1 = [1 2];
wolffd@0 20 eclass2 = [3 2];
wolffd@0 21 bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ...
wolffd@0 22 'observed', onodes);
wolffd@0 23
wolffd@0 24 rand('state', 0);
wolffd@0 25 prior1 = normalise(rand(Q,1));
wolffd@0 26 transmat1 = mk_stochastic(rand(Q,Q));
wolffd@0 27 obsmat1 = mk_stochastic(rand(Q,O));
wolffd@0 28 bnet.CPD{1} = tabular_CPD(bnet, 1, prior1);
wolffd@0 29 bnet.CPD{2} = tabular_CPD(bnet, 2, obsmat1);
wolffd@0 30 bnet.CPD{3} = tabular_CPD(bnet, 3, transmat1);
wolffd@0 31
wolffd@0 32
wolffd@0 33 T = 5; % fixed length sequences
wolffd@0 34
wolffd@0 35 engine = {};
wolffd@0 36 engine{end+1} = jtree_unrolled_dbn_inf_engine(bnet, T);
wolffd@0 37 engine{end+1} = hmm_inf_engine(bnet);
wolffd@0 38 engine{end+1} = smoother_engine(hmm_2TBN_inf_engine(bnet));
wolffd@0 39 engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet));
wolffd@0 40 if 1
wolffd@0 41 %engine{end+1} = frontier_inf_engine(bnet); % broken
wolffd@0 42 engine{end+1} = bk_inf_engine(bnet, 'clusters', {[1]});
wolffd@0 43 engine{end+1} = jtree_dbn_inf_engine(bnet);
wolffd@0 44 end
wolffd@0 45
wolffd@0 46 inf_time = cmp_inference_dbn(bnet, engine, T);
wolffd@0 47
wolffd@0 48 ncases = 2;
wolffd@0 49 max_iter = 2;
wolffd@0 50 [learning_time, CPD, LL, cases] = cmp_learning_dbn(bnet, engine, T, 'ncases', ncases, 'max_iter', max_iter);
wolffd@0 51
wolffd@0 52 % Compare to HMM toolbox
wolffd@0 53
wolffd@0 54 data = zeros(ncases, T);
wolffd@0 55 for i=1:ncases
wolffd@0 56 %data(i,:) = cat(2, cases{i}{onodes,:});
wolffd@0 57 data(i,:) = cell2num(cases{i}(onodes,:));
wolffd@0 58 end
wolffd@0 59 [LL2, prior2, transmat2, obsmat2] = dhmm_em(data, prior1, transmat1, obsmat1, 'max_iter', max_iter);
wolffd@0 60
wolffd@0 61 e = 1;
wolffd@0 62 assert(approxeq(prior2, CPD{e,1}.CPT))
wolffd@0 63 assert(approxeq(obsmat2, CPD{e,2}.CPT))
wolffd@0 64 assert(approxeq(transmat2, CPD{e,3}.CPT))
wolffd@0 65 assert(approxeq(LL2, LL{e}))
wolffd@0 66