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