wolffd@0
|
1 function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv)
|
wolffd@0
|
2 % UPDATE_ESS Update the Expected Sufficient Statistics of a hhmm Q node.
|
wolffd@0
|
3 % function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, idden_bitv)
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % we assume if one of the Qps is observed, all of them are
|
wolffd@0
|
6 % We assume the F nodes are already hidden
|
wolffd@0
|
7
|
wolffd@0
|
8 % Figure out the node numbers associated with each parent
|
wolffd@0
|
9 dom = fmarginal.domain;
|
wolffd@0
|
10 self = dom(CPD.self_ndx);
|
wolffd@0
|
11 old_self = dom(CPD.old_self_ndx);
|
wolffd@0
|
12 %Fself = dom(CPD.Fself_ndx);
|
wolffd@0
|
13 %Fbelow = dom(CPD.Fbelow_ndx);
|
wolffd@0
|
14 Qps = dom(CPD.Qps_ndx);
|
wolffd@0
|
15
|
wolffd@0
|
16 Qsz = CPD.Qsz;
|
wolffd@0
|
17 Qpsz = CPD.Qpsz;
|
wolffd@0
|
18
|
wolffd@0
|
19
|
wolffd@0
|
20 % hor_counts(old_self, Qps, self),
|
wolffd@0
|
21 % fmarginal(old_self, Fbelow, Fself, Qps, self)
|
wolffd@0
|
22 % hor_counts(i,k,j) = fmarginal(i,2,1,k,j) % below has finished, self has not
|
wolffd@0
|
23 % ver_counts(i,k,j) = fmarginal(i,2,2,k,j) % below has finished, and so has self (reset)
|
wolffd@0
|
24 % Since any of i,j,k may be observed, we write
|
wolffd@0
|
25 % hor_counts(ndx{:}) = fmarginal(...)
|
wolffd@0
|
26 % where e.g., ndx = {1, ':', 2} if Qps is hidden but we observe old_self=1, self=2.
|
wolffd@0
|
27
|
wolffd@0
|
28 % ndx{i,k,j}
|
wolffd@0
|
29 if hidden_bitv(old_self)
|
wolffd@0
|
30 ndx{1} = ':';
|
wolffd@0
|
31 else
|
wolffd@0
|
32 ndx{1} = evidence{old_self};
|
wolffd@0
|
33 end
|
wolffd@0
|
34 if hidden_bitv(Qps)
|
wolffd@0
|
35 ndx{2} = ':';
|
wolffd@0
|
36 else
|
wolffd@0
|
37 ndx{2} = subv2ind(Qpsz, cat(1, evidence{Qps}));
|
wolffd@0
|
38 end
|
wolffd@0
|
39 if hidden_bitv(self)
|
wolffd@0
|
40 ndx{3} = ':';
|
wolffd@0
|
41 else
|
wolffd@0
|
42 ndx{3} = evidence{self};
|
wolffd@0
|
43 end
|
wolffd@0
|
44
|
wolffd@0
|
45 fmarg = add_ev_to_dmarginal(fmarginal, evidence, ns);
|
wolffd@0
|
46 % marg(Qold(t-1), Fbelow(t-1), Fself(t-1), Qps(t), Qself(t))
|
wolffd@0
|
47 hor_counts = zeros(Qsz, Qpsz, Qsz);
|
wolffd@0
|
48 ver_counts = zeros(Qpsz, Qsz);
|
wolffd@0
|
49
|
wolffd@0
|
50 if ~isempty(CPD.Fbelow_ndx)
|
wolffd@0
|
51 if ~isempty(CPD.Fself_ndx) % general case
|
wolffd@0
|
52 fmarg.T = myreshape(fmarg.T, [Qsz 2 2 Qpsz Qsz]);
|
wolffd@0
|
53 marg_ndx = {ndx{1}, 2, 1, ndx{2}, ndx{3}};
|
wolffd@0
|
54 hor_counts(ndx{:}) = fmarg.T(marg_ndx{:});
|
wolffd@0
|
55 ver_counts(ndx{2:3}) = ... % sum over Fbelow and Qold=i
|
wolffd@0
|
56 sum(fmarg.T({ndx{1}, 1, 2, ndx{2}, ndx{3}}),1) + ..
|
wolffd@0
|
57 sum(fmarg.T({ndx{1}, 2, 2, ndx{2}, ndx{3}}),1);
|
wolffd@0
|
58 else % no F from self, hence no startprob
|
wolffd@0
|
59 fmarg.T = myreshape(fmarg.T, [Qsz 2 Qpsz Qsz]);
|
wolffd@0
|
60 hor_counts(ndx{:}) = fmarg.T({ndx{1}, 2, ndx{2}, ndx{3}});
|
wolffd@0
|
61 end
|
wolffd@0
|
62 else % no F signal from below
|
wolffd@0
|
63 if ~isempty(CPD.Fself_ndx) % self F
|
wolffd@0
|
64 fmarg.T = myreshape(fmarg.T, [Qsz 2 Qpsz Qsz]);
|
wolffd@0
|
65 hor_counts(ndx{:}) = fmarg.T({ndx{1}, 1, ndx{2}, ndx{3}});
|
wolffd@0
|
66 ver_counts(ndx{2:3}) = ... % sum over Qold=i
|
wolffd@0
|
67 sum(fmarg.T({ndx{1}, 2, ndx{2}, ndx{3}}),1);
|
wolffd@0
|
68 else % no F from self
|
wolffd@0
|
69 error('An hhmmQ node without any F parents is just a tabular_CPD')
|
wolffd@0
|
70 end
|
wolffd@0
|
71 end
|
wolffd@0
|
72
|
wolffd@0
|
73
|
wolffd@0
|
74 CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts);
|
wolffd@0
|
75
|
wolffd@0
|
76 if ~isempty(CPD.sub_CPD_start)
|
wolffd@0
|
77 CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts);
|
wolffd@0
|
78 end
|
wolffd@0
|
79
|
wolffd@0
|
80
|