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(end); 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(i_counts_ndx, kndx, jndx) = fmarginal(i_fmarg_ndx...) wolffd@0: % where i_fmarg_ndx = 1 and i_counts_ndx = i if old_self is observed to have value i, wolffd@0: % i_fmarg_ndx = 1:Qsz and i_counts_ndx = 1:Qsz if old_self is hidden, etc. wolffd@0: wolffd@0: wolffd@0: if hidden_bitv(old_self) wolffd@0: i_counts_ndx = 1:Qsz; wolffd@0: eff_oldQsz = Qsz; wolffd@0: else wolffd@0: i_counts_ndx = evidence{old_self}; wolffd@0: eff_oldQsz = 1; wolffd@0: end wolffd@0: wolffd@0: if all(hidden_bitv(Qps)) % we assume all are hidden or all are observed wolffd@0: k_counts_ndx = 1:Qpsz; wolffd@0: eff_Qpsz = Qpsz; wolffd@0: else wolffd@0: k_counts_ndx = subv2ind(Qpsz, cat(1, evidence{Qps})); wolffd@0: eff_Qpsz = 1; wolffd@0: end wolffd@0: wolffd@0: if hidden_bitv(self) wolffd@0: j_counts_ndx = 1:Qsz; wolffd@0: eff_Qsz = Qsz; wolffd@0: else wolffd@0: j_counts_ndx = evidence{self}; wolffd@0: eff_Qsz = 1; wolffd@0: end wolffd@0: 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 = myreshape(fmarginal.T, [eff_oldQsz 2 2 eff_Qpsz eff_Qsz]); wolffd@0: hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = fmarg(:, 2, 1, :, :); wolffd@0: ver_counts(k_counts_ndx, j_counts_ndx) = ... % sum over Fbelow and Qold wolffd@0: sumv(fmarg(:, :, 2, :, :), [1 2]); % require Fself=2 wolffd@0: else % no F from self, hence no startprob wolffd@0: fmarg = myreshape(fmarginal.T, [eff_oldQsz 2 eff_Qpsz eff_Qsz]); wolffd@0: hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = ... wolffd@0: fmarg(:, 2, :, :); % require Fbelow = 2 wolffd@0: end wolffd@0: else % no F signal from below wolffd@0: if ~isempty(CPD.Fself_ndx) % self F wolffd@0: fmarg = myreshape(fmarginal.T, [eff_oldQsz 2 eff_Qpsz eff_Qsz]); wolffd@0: hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = fmarg(:, 1, :, :); wolffd@0: ver_counts(k_counts_ndx, j_counts_ndx) = ... % sum over Qold wolffd@0: squeeze(sum(fmarg(:, 2, :, :), 1)); % Fself=2 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: