comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/HHMM/Old/mk_hhmm3.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_hhmm3(varargin)
2 % MK_HHMM3 Make a 3 level Hierarchical HMM
3 % bnet = mk_hhmm3(...)
4 %
5 % 3-layer hierarchical HMM where level 1 only connects to level 2, not 3 or obs.
6 % This enforces sub-models (which differ only in their Q1 index) to be shared.
7 % Also, we enforce the fact that each model always starts in its initial state
8 % and only finishes in its final state. However, the prob. of finishing (as opposed to
9 % self-transitioning to the final state) can be learned.
10 % The fact that we always finish from the same state means we do not need to condition
11 % F(i) on Q(i-1), since finishing prob is indep of calling context.
12 %
13 % The DBN is the same as Fig 10 in my tech report.
14 %
15 % Q1 ----------> Q1
16 % | / |
17 % | / |
18 % | F2 ------- |
19 % | ^ \ |
20 % | /| \ |
21 % v | v v
22 % Q2-| --------> Q2
23 % /| | ^
24 % / | | /|
25 % | | F3 ---------/ |
26 % | | ^ \ |
27 % | v / v
28 % | Q3 -----------> Q3
29 % | |
30 % \ |
31 % v v
32 % O
33 %
34 %
35 % Optional arguments in name/value format [default]
36 %
37 % Qsizes - sizes at each level [ none ]
38 % Osize - size of O node [ none ]
39 % discrete_obs - 1 means O is tabular_CPD, 0 means O is gaussian_CPD [0]
40 % Oargs - cell array of args to pass to the O CPD [ {} ]
41 % transprob1 - transprob1(i,j) = P(Q1(t)=j|Q1(t-1)=i) ['ergodic']
42 % startprob1 - startprob1(j) = P(Q1(t)=j) ['leftstart']
43 % transprob2 - transprob2(i,k,j) = P(Q2(t)=j|Q2(t-1)=i,Q1(t)=k) ['leftright']
44 % startprob2 - startprob2(k,j) = P(Q2(t)=j|Q1(t)=k) ['leftstart']
45 % termprob2 - termprob2(j,f) = P(F2(t)=f|Q2(t)=j) ['rightstop']
46 % transprob3 - transprob3(i,k,j) = P(Q3(t)=j|Q3(t-1)=i,Q2(t)=k) ['leftright']
47 % startprob3 - startprob3(k,j) = P(Q3(t)=j|Q2(t)=k) ['leftstart']
48 % termprob3 - termprob3(j,f) = P(F3(t)=f|Q3(t)=j) ['rightstop']
49 %
50 % leftstart means the model always starts in state 1.
51 % rightstop means the model always finished in its last state (Qsize(d)).
52 %
53 % Q1:Q3 in slice 1 are of type tabular_CPD
54 % Q1:Q3 in slice 2 are of type hhmmQ_CPD.
55 % F2 is of type hhmmF_CPD, F3 is of type tabular_CPD.
56
57 ss = 6; D = 3;
58 Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; obs = 6;
59 Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3];
60 names = {'Q1', 'Q2', 'Q3', 'F3', 'F2', 'obs'};
61
62 intra = zeros(ss);
63 intra(Q1, Q2) = 1;
64 intra(Q2, [F2 Q3 obs]) = 1;
65 intra(Q3, [F3 obs]) = 1;
66 intra(F3, F2) = 1;
67
68 inter = zeros(ss);
69 inter(Q1,Q1) = 1;
70 inter(Q2,Q2) = 1;
71 inter(Q3,Q3) = 1;
72 inter(F2,[Q1 Q2]) = 1;
73 inter(F3,[Q2 Q3]) = 1;
74
75
76 % get sizes of nodes
77 args = varargin;
78 nargs = length(args);
79 Qsizes = [];
80 Osize = 0;
81 for i=1:2:nargs
82 switch args{i},
83 case 'Qsizes', Qsizes = args{i+1};
84 case 'Osize', Osize = args{i+1};
85 end
86 end
87 if isempty(Qsizes), error('must specify Qsizes'); end
88 if Osize==0, error('must specify Osize'); end
89
90 % set default params
91 discrete_obs = 0;
92 Oargs = {};
93 startprob1 = 'ergodic';
94 startprob2 = 'leftstart';
95 startprob3 = 'leftstart';
96 transprob1 = 'ergodic';
97 transprob2 = 'leftright';
98 transprob3 = 'leftright';
99 termprob2 = 'rightstop';
100 termprob3 = 'rightstop';
101
102
103 for i=1:2:nargs
104 switch args{i},
105 case 'discrete_obs', discrete_obs = args{i+1};
106 case 'Oargs', Oargs = args{i+1};
107 case 'Q1args', Q1args = args{i+1};
108 case 'Q2args', Q2args = args{i+1};
109 case 'Q3args', Q3args = args{i+1};
110 case 'F2args', F2args = args{i+1};
111 case 'F3args', F3args = args{i+1};
112 end
113 end
114
115
116 ns = zeros(1,ss);
117 ns(Qnodes) = Qsizes;
118 ns(obs) = Osize;
119 ns(Fnodes) = 2;
120
121 dnodes = [Qnodes Fnodes];
122 if discrete_obs
123 dnodes = [dnodes obs];
124 end
125 onodes = [obs];
126
127 bnet = mk_dbn(intra, inter, ns, 'observed', onodes, 'discrete', dnodes, 'names', names);
128 eclass = bnet.equiv_class;
129
130 if strcmp(startprob1, 'ergodic')
131 startprob1 = normalise(ones(1,ns(Q1)));
132 end
133 if strcmp(startprob2, 'leftstart')
134 startprob2 = zeros(ns(Q1), ns(Q2));
135 starpbrob2(:, 1) = 1.0;
136 end
137 if strcmp(startprob3, 'leftstart')
138 startprob3 = zeros(ns(Q2), ns(Q3));
139 starpbrob3(:, 1) = 1.0;
140 end
141
142 if strcmp(termprob2, 'rightstop')
143 p = 0.9;
144 termprob2 = zeros(Qsize(2),2);
145 termprob2(:, 2) = p;
146 termprob2(:, 1) = 1-p;
147 termprob2(1:(Qsize(2)-1), 1) = 1;
148 end
149 if strcmp(termprob3, 'rightstop')
150 p = 0.9;
151 termprob3 = zeros(Qsize(3),2);
152 termprob3(:, 2) = p;
153 termprob3(:, 1) = 1-p;
154 termprob3(1:(Qsize(3)-1), 1) = 1;
155 end
156
157
158 % SLICE 1
159
160 % We clamp untied nodes in the first slice, since their params can't be estimated
161 % from just one sequence
162
163 bnet.CPD{eclass(Q1,1)} = tabular_CPD(bnet, Q1, 'CPT', startprob1, 'adjustable', 0);
164 bnet.CPD{eclass(Q2,1)} = tabular_CPD(bnet, Q2, 'CPT', startprob2, 'adjustable', 0);
165 bnet.CPD{eclass(Q3,1)} = tabular_CPD(bnet, Q3, 'CPT', startprob3, 'adjustable', 0);
166
167 bnet.CPD{eclass(F2,1)} = hhmmF_CPD(bnet, F2, Qnodes, 2, D, 'termprob', termprob2);
168 bnet.CPD{eclass(F3,1)} = tabular_CPD(bnet, F3, 'CPT', termprob3);
169
170 if discrete_obs
171 bnet.CPD{eclass(obs,1)} = tabular_CPD(bnet, obs, Oargs{:});
172 else
173 bnet.CPD{eclass(obs,1)} = gaussian_CPD(bnet, obs, Oargs{:});
174 end
175
176 % SLICE 2
177
178 bnet.CPD{eclass(Q1,2)} = hhmmQ_CPD(bnet, Q1+ss, Qnodes, 1, D, 'transprob', transprob1, 'startprob', startprob1);
179 bnet.CPD{eclass(Q2,2)} = hhmmQ_CPD(bnet, Q2+ss, Qnodes, 2, D, 'transprob', transprob2, 'startprob', startprob2);
180 bnet.CPD{eclass(Q3,2)} = hhmmQ_CPD(bnet, Q3+ss, Qnodes, 3, D, 'transprob', transprob3, 'startprob', startprob3);
181