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