annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/viterbi1.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 % Compute Viterbi path discrete HMM by different methods
wolffd@0 2
wolffd@0 3 intra = zeros(2);
wolffd@0 4 intra(1,2) = 1;
wolffd@0 5 inter = zeros(2);
wolffd@0 6 inter(1,1) = 1;
wolffd@0 7 n = 2;
wolffd@0 8
wolffd@0 9 Q = 2; % num hidden states
wolffd@0 10 O = 2; % num observable symbols
wolffd@0 11
wolffd@0 12 ns = [Q O];
wolffd@0 13 dnodes = 1:2;
wolffd@0 14 onodes = [2];
wolffd@0 15 eclass1 = [1 2];
wolffd@0 16 eclass2 = [3 2];
wolffd@0 17 bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2, ...
wolffd@0 18 'observed', onodes);
wolffd@0 19
wolffd@0 20 for seed=1:10
wolffd@0 21 rand('state', seed);
wolffd@0 22 prior = normalise(rand(Q,1));
wolffd@0 23 transmat = mk_stochastic(rand(Q,Q));
wolffd@0 24 obsmat = mk_stochastic(rand(Q,O));
wolffd@0 25 bnet.CPD{1} = tabular_CPD(bnet, 1, prior);
wolffd@0 26 bnet.CPD{2} = tabular_CPD(bnet, 2, obsmat);
wolffd@0 27 bnet.CPD{3} = tabular_CPD(bnet, 3, transmat);
wolffd@0 28
wolffd@0 29
wolffd@0 30 % Create a sequence
wolffd@0 31 T = 5;
wolffd@0 32 ev = sample_dbn(bnet, T);
wolffd@0 33 evidence = cell(2,T);
wolffd@0 34 evidence(2,:) = ev(2,:); % extract observed component
wolffd@0 35 data = cell2num(ev(2,:));
wolffd@0 36
wolffd@0 37 %obslik = mk_dhmm_obs_lik(data, obsmat);
wolffd@0 38 obslik = multinomial_prob(data, obsmat);
wolffd@0 39 path = viterbi_path(prior, transmat, obslik);
wolffd@0 40
wolffd@0 41 engine = {};
wolffd@0 42 engine{end+1} = smoother_engine(jtree_2TBN_inf_engine(bnet));
wolffd@0 43
wolffd@0 44 mpe = find_mpe(engine{1}, evidence);
wolffd@0 45
wolffd@0 46 assert(isequal(cell2num(mpe(1,:)), path)) % extract values of hidden nodes
wolffd@0 47 end