comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/HHMM/Old/mk_hhmm3_args.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 % Q1 (slice 1) is clamped to be uniform.
35 % Q2 (slice 1) is clamped to always start in state 1.
36 % Q3 (slice 1) is clamped to always start in state 1.
37 % F3 by default will only finish if Q3 is in its last state (F3 is a tabular_CPD)
38 % F2 by default gets the default hhmmF_CPD params.
39 % Q1:Q3 (slice 2) by default gets the default hhmmQ_CPD params.
40 % O by default gets the default tabular/Gaussian params.
41 %
42 % Optional arguments in name/value format [default]
43 %
44 % Qsizes - sizes at each level [ none ]
45 % Osize - size of O node [ none ]
46 % discrete_obs - 1 means O is tabular_CPD, 0 means O is gaussian_CPD [0]
47 % Oargs - cell array of args to pass to the O CPD [ {} ]
48 % Q1args - args to be passed to constructor for Q1 (slice 2) [ {} ]
49 % Q2args - args to be passed to constructor for Q2 (slice 2) [ {} ]
50 % Q3args - args to be passed to constructor for Q3 (slice 2) [ {} ]
51 % F2args - args to be passed to constructor for F2 [ {} ]
52 % F3args - args to be passed to constructor for F3 [ {'CPT', finish in last Q3 state} ]
53 %
54
55 ss = 6; D = 3;
56 Q1 = 1; Q2 = 2; Q3 = 3; F3 = 4; F2 = 5; obs = 6;
57 Qnodes = [Q1 Q2 Q3]; Fnodes = [F2 F3];
58 names = {'Q1', 'Q2', 'Q3', 'F3', 'F2', 'obs'};
59
60 intra = zeros(ss);
61 intra(Q1, Q2) = 1;
62 intra(Q2, [F2 Q3 obs]) = 1;
63 intra(Q3, [F3 obs]) = 1;
64 intra(F3, F2) = 1;
65
66 inter = zeros(ss);
67 inter(Q1,Q1) = 1;
68 inter(Q2,Q2) = 1;
69 inter(Q3,Q3) = 1;
70 inter(F2,[Q1 Q2]) = 1;
71 inter(F3,[Q2 Q3]) = 1;
72
73
74 % get sizes of nodes
75 args = varargin;
76 nargs = length(args);
77 Qsizes = [];
78 Osize = 0;
79 for i=1:2:nargs
80 switch args{i},
81 case 'Qsizes', Qsizes = args{i+1};
82 case 'Osize', Osize = args{i+1};
83 end
84 end
85 if isempty(Qsizes), error('must specify Qsizes'); end
86 if Osize==0, error('must specify Osize'); end
87
88 % set default params
89 discrete_obs = 0;
90 Oargs = {};
91 Q1args = {};
92 Q2args = {};
93 Q3args = {};
94 F2args = {};
95
96 % P(Q3, F3)
97 CPT = zeros(Qsizes(3), 2);
98 % Each model can only terminate in its final state.
99 % 0 params will remain 0 during EM, thus enforcing this constraint.
100 CPT(:, 1) = 1.0; % all states turn F off ...
101 p = 0.5;
102 CPT(Qsizes(3), 2) = p; % except the last one
103 CPT(Qsizes(3), 1) = 1-p;
104 F3args = {'CPT', CPT};
105
106 for i=1:2:nargs
107 switch args{i},
108 case 'discrete_obs', discrete_obs = args{i+1};
109 case 'Oargs', Oargs = args{i+1};
110 case 'Q1args', Q1args = args{i+1};
111 case 'Q2args', Q2args = args{i+1};
112 case 'Q3args', Q3args = args{i+1};
113 case 'F2args', F2args = args{i+1};
114 case 'F3args', F3args = args{i+1};
115 end
116 end
117
118 ns = zeros(1,ss);
119 ns(Qnodes) = Qsizes;
120 ns(obs) = Osize;
121 ns(Fnodes) = 2;
122
123 dnodes = [Qnodes Fnodes];
124 if discrete_obs
125 dnodes = [dnodes obs];
126 end
127 onodes = [obs];
128
129 bnet = mk_dbn(intra, inter, ns, 'observed', onodes, 'discrete', dnodes, 'names', names);
130 eclass = bnet.equiv_class;
131
132 % SLICE 1
133
134 % We clamp untied nodes in the first slice, since their params can't be estimated
135 % from just one sequence
136
137 % uniform prior on initial model
138 CPT = normalise(ones(1,ns(Q1)));
139 bnet.CPD{eclass(Q1,1)} = tabular_CPD(bnet, Q1, 'CPT', CPT, 'adjustable', 0);
140
141 % each model always starts in state 1
142 CPT = zeros(ns(Q1), ns(Q2));
143 CPT(:, 1) = 1.0;
144 bnet.CPD{eclass(Q2,1)} = tabular_CPD(bnet, Q2, 'CPT', CPT, 'adjustable', 0);
145
146 % each model always starts in state 1
147 CPT = zeros(ns(Q2), ns(Q3));
148 CPT(:, 1) = 1.0;
149 bnet.CPD{eclass(Q3,1)} = tabular_CPD(bnet, Q3, 'CPT', CPT, 'adjustable', 0);
150
151 bnet.CPD{eclass(F2,1)} = hhmmF_CPD(bnet, F2, Qnodes, 2, D, F2args{:});
152
153 bnet.CPD{eclass(F3,1)} = tabular_CPD(bnet, F3, F3args{:});
154
155 if discrete_obs
156 bnet.CPD{eclass(obs,1)} = tabular_CPD(bnet, obs, Oargs{:});
157 else
158 bnet.CPD{eclass(obs,1)} = gaussian_CPD(bnet, obs, Oargs{:});
159 end
160
161 % SLICE 2
162
163 bnet.CPD{eclass(Q1,2)} = hhmmQ_CPD(bnet, Q1+ss, Qnodes, 1, D, Q1args{:});
164 bnet.CPD{eclass(Q2,2)} = hhmmQ_CPD(bnet, Q2+ss, Qnodes, 2, D, Q2args{:});
165 bnet.CPD{eclass(Q3,2)} = hhmmQ_CPD(bnet, Q3+ss, Qnodes, 3, D, Q3args{:});