comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/HHMM/Old/mk_hhmm2.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 function bnet = mk_hhmm2(varargin)
2 % MK_HHMM2 Make a 2 level Hierarchical HMM
3 % bnet = mk_hhmm2(...)
4 %
5 % 2-layer hierarchical HMM (node numbers in parens)
6 %
7 % Q1(1) ---------> Q1(5)
8 % / | \ / |
9 % | | v / |
10 % | | F2(3) --- / |
11 % | | ^ \ |
12 % | | / \ |
13 % | v \ v
14 % | Q2(2)--------> Q2 (6)
15 % | |
16 % \ |
17 % v v
18 % O(4)
19 %
20 %
21 % Optional arguments [default]
22 %
23 % discrete_obs - 1 means O is tabular_CPD, 0 means O is gaussian_CPD [0]
24 % obsCPT - CPT(o,q1,q2) params for O ['rnd']
25 % mu - mu(:,q1,q2) params for O [ [] ]
26 % Sigma - Sigma(:,q1,q2) params for O [ [] ]
27 %
28 % F2toQ1 - 1 if Q2 is an hhmm_CPD, 0 if F2 -> Q2 arc is absent, so level 2 never resets [1]
29 % Q1args - arguments to be passed to the constructors for Q1(t=2) [ {} ]
30 % Q2args - arguments to be passed to the constructors for Q2(t=2) [ {} ]
31 %
32 % F2 only turns on (wp 0.5) when Q2 enters its final state.
33 % Q1 (slice 1) is clamped to be uniform.
34 % Q2 (slice 1) is clamped to always start in state 1.
35
36 [os nmodels nstates] = size(mu);
37
38 ss = 4;
39 Q1 = 1; Q2 = 2; F2 = 3; obs = 4;
40 Qnodes = [Q1 Q2];
41 names = {'Q1', 'Q2', 'F2', 'obs'};
42 intra = zeros(ss);
43 intra(Q1, [Q2 F2 obs]) = 1;
44 intra(Q2, [F2 obs]) = 1;
45
46 inter = zeros(ss);
47 inter(Q1,Q1) = 1;
48 inter(F2,Q1) = 1;
49 if F2toQ2
50 inter(F2,Q2)=1;
51 end
52 inter(Q2,Q2) = 1;
53
54 ns = zeros(1,ss);
55
56 ns(Q1) = nmodels;
57 ns(Q2) = nstates;
58 ns(F2) = 2;
59 ns(obs) = os;
60
61 dnodes = [Q1 Q2 F2];
62 if discrete_obs
63 dnodes = [dnodes obs];
64 end
65 onodes = [obs];
66
67 bnet = mk_dbn(intra, inter, ns, 'observed', onodes, 'discrete', dnodes, 'names', names);
68 eclass = bnet.equiv_class;
69
70 % SLICE 1
71
72 % We clamp untied nodes in the first slice, since their params can't be estimated
73 % from just one sequence
74
75 % uniform prior on initial model
76 CPT = normalise(ones(1,nmodels));
77 bnet.CPD{eclass(Q1,1)} = tabular_CPD(bnet, Q1, 'CPT', CPT, 'adjustable', 0);
78
79 % each model always starts in state 1
80 CPT = zeros(ns(Q1), ns(Q2));
81 CPT(:, 1) = 1.0;
82 bnet.CPD{eclass(Q2,1)} = tabular_CPD(bnet, Q2, 'CPT', CPT, 'adjustable', 0);
83
84 % Termination probability
85 CPT = zeros(ns(Q1), ns(Q2), 2);
86 if 1
87 % Each model can only terminate in its final state.
88 % 0 params will remain 0 during EM, thus enforcing this constraint.
89 CPT(:, :, 1) = 1.0; % all states turn F off ...
90 p = 0.5;
91 CPT(:, ns(Q2), 2) = p; % except the last one
92 CPT(:, ns(Q2), 1) = 1-p;
93 end
94 bnet.CPD{eclass(F2,1)} = tabular_CPD(bnet, F2, 'CPT', CPT);
95
96 if discrete_obs
97 bnet.CPD{eclass(obs,1)} = tabular_CPD(bnet, obs, obs_args{:});
98 else
99 bnet.CPD{eclass(obs,1)} = gaussian_CPD(bnet, obs, obs_args{:});
100 end
101
102 % SLICE 2
103
104
105 bnet.CPD{eclass(Q1,2)} = hhmm_CPD(bnet, Q1+ss, Qnodes, 1, D, 'args', Q1args);
106
107 if F2toQ2
108 bnet.CPD{eclass(Q2,2)} = hhmmQD_CPD(bnet, Q2+ss, Qnodes, 2, D, Q2args{:});
109 else
110 bnet.CPD{eclass(Q2,2)} = tabular_CPD(bnet, Q2+ss, Q2args{:});
111 end