wolffd@0: function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv) wolffd@0: % UPDATE_ESS Update the Expected Sufficient Statistics of a hhmm Q node. wolffd@0: % function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, idden_bitv) wolffd@0: % wolffd@0: % we assume if one of the Qps is observed, all of them are wolffd@0: % We assume the F nodes are already hidden wolffd@0: wolffd@0: % Figure out the node numbers associated with each parent wolffd@0: dom = fmarginal.domain; wolffd@0: self = dom(CPD.self_ndx); wolffd@0: old_self = dom(CPD.old_self_ndx); wolffd@0: %Fself = dom(CPD.Fself_ndx); wolffd@0: %Fbelow = dom(CPD.Fbelow_ndx); wolffd@0: Qps = dom(CPD.Qps_ndx); wolffd@0: wolffd@0: Qsz = CPD.Qsz; wolffd@0: Qpsz = CPD.Qpsz; wolffd@0: wolffd@0: wolffd@0: % hor_counts(old_self, Qps, self), wolffd@0: % fmarginal(old_self, Fbelow, Fself, Qps, self) wolffd@0: % hor_counts(i,k,j) = fmarginal(i,2,1,k,j) % below has finished, self has not wolffd@0: % ver_counts(i,k,j) = fmarginal(i,2,2,k,j) % below has finished, and so has self (reset) wolffd@0: % Since any of i,j,k may be observed, we write wolffd@0: % hor_counts(ndx{:}) = fmarginal(...) wolffd@0: % where e.g., ndx = {1, ':', 2} if Qps is hidden but we observe old_self=1, self=2. wolffd@0: wolffd@0: % ndx{i,k,j} wolffd@0: if hidden_bitv(old_self) wolffd@0: ndx{1} = ':'; wolffd@0: else wolffd@0: ndx{1} = evidence{old_self}; wolffd@0: end wolffd@0: if hidden_bitv(Qps) wolffd@0: ndx{2} = ':'; wolffd@0: else wolffd@0: ndx{2} = subv2ind(Qpsz, cat(1, evidence{Qps})); wolffd@0: end wolffd@0: if hidden_bitv(self) wolffd@0: ndx{3} = ':'; wolffd@0: else wolffd@0: ndx{3} = evidence{self}; wolffd@0: end wolffd@0: wolffd@0: fmarg = add_ev_to_dmarginal(fmarginal, evidence, ns); wolffd@0: % marg(Qold(t-1), Fbelow(t-1), Fself(t-1), Qps(t), Qself(t)) wolffd@0: hor_counts = zeros(Qsz, Qpsz, Qsz); wolffd@0: ver_counts = zeros(Qpsz, Qsz); wolffd@0: wolffd@0: if ~isempty(CPD.Fbelow_ndx) wolffd@0: if ~isempty(CPD.Fself_ndx) % general case wolffd@0: fmarg.T = myreshape(fmarg.T, [Qsz 2 2 Qpsz Qsz]); wolffd@0: marg_ndx = {ndx{1}, 2, 1, ndx{2}, ndx{3}}; wolffd@0: hor_counts(ndx{:}) = fmarg.T(marg_ndx{:}); wolffd@0: ver_counts(ndx{2:3}) = ... % sum over Fbelow and Qold=i wolffd@0: sum(fmarg.T({ndx{1}, 1, 2, ndx{2}, ndx{3}}),1) + .. wolffd@0: sum(fmarg.T({ndx{1}, 2, 2, ndx{2}, ndx{3}}),1); wolffd@0: else % no F from self, hence no startprob wolffd@0: fmarg.T = myreshape(fmarg.T, [Qsz 2 Qpsz Qsz]); wolffd@0: hor_counts(ndx{:}) = fmarg.T({ndx{1}, 2, ndx{2}, ndx{3}}); wolffd@0: end wolffd@0: else % no F signal from below wolffd@0: if ~isempty(CPD.Fself_ndx) % self F wolffd@0: fmarg.T = myreshape(fmarg.T, [Qsz 2 Qpsz Qsz]); wolffd@0: hor_counts(ndx{:}) = fmarg.T({ndx{1}, 1, ndx{2}, ndx{3}}); wolffd@0: ver_counts(ndx{2:3}) = ... % sum over Qold=i wolffd@0: sum(fmarg.T({ndx{1}, 2, ndx{2}, ndx{3}}),1); wolffd@0: else % no F from self wolffd@0: error('An hhmmQ node without any F parents is just a tabular_CPD') wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts); wolffd@0: wolffd@0: if ~isempty(CPD.sub_CPD_start) wolffd@0: CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts); wolffd@0: end wolffd@0: wolffd@0: