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