annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmQ_CPD/update_ess.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
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(end);
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(i_counts_ndx, kndx, jndx) = fmarginal(i_fmarg_ndx...)
wolffd@0 26 % where i_fmarg_ndx = 1 and i_counts_ndx = i if old_self is observed to have value i,
wolffd@0 27 % i_fmarg_ndx = 1:Qsz and i_counts_ndx = 1:Qsz if old_self is hidden, etc.
wolffd@0 28
wolffd@0 29
wolffd@0 30 if hidden_bitv(old_self)
wolffd@0 31 i_counts_ndx = 1:Qsz;
wolffd@0 32 eff_oldQsz = Qsz;
wolffd@0 33 else
wolffd@0 34 i_counts_ndx = evidence{old_self};
wolffd@0 35 eff_oldQsz = 1;
wolffd@0 36 end
wolffd@0 37
wolffd@0 38 if all(hidden_bitv(Qps)) % we assume all are hidden or all are observed
wolffd@0 39 k_counts_ndx = 1:Qpsz;
wolffd@0 40 eff_Qpsz = Qpsz;
wolffd@0 41 else
wolffd@0 42 k_counts_ndx = subv2ind(Qpsz, cat(1, evidence{Qps}));
wolffd@0 43 eff_Qpsz = 1;
wolffd@0 44 end
wolffd@0 45
wolffd@0 46 if hidden_bitv(self)
wolffd@0 47 j_counts_ndx = 1:Qsz;
wolffd@0 48 eff_Qsz = Qsz;
wolffd@0 49 else
wolffd@0 50 j_counts_ndx = evidence{self};
wolffd@0 51 eff_Qsz = 1;
wolffd@0 52 end
wolffd@0 53
wolffd@0 54 hor_counts = zeros(Qsz, Qpsz, Qsz);
wolffd@0 55 ver_counts = zeros(Qpsz, Qsz);
wolffd@0 56
wolffd@0 57 if ~isempty(CPD.Fbelow_ndx)
wolffd@0 58 if ~isempty(CPD.Fself_ndx) % general case
wolffd@0 59 fmarg = myreshape(fmarginal.T, [eff_oldQsz 2 2 eff_Qpsz eff_Qsz]);
wolffd@0 60 hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = fmarg(:, 2, 1, :, :);
wolffd@0 61 ver_counts(k_counts_ndx, j_counts_ndx) = ... % sum over Fbelow and Qold
wolffd@0 62 sumv(fmarg(:, :, 2, :, :), [1 2]); % require Fself=2
wolffd@0 63 else % no F from self, hence no startprob
wolffd@0 64 fmarg = myreshape(fmarginal.T, [eff_oldQsz 2 eff_Qpsz eff_Qsz]);
wolffd@0 65 hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = ...
wolffd@0 66 fmarg(:, 2, :, :); % require Fbelow = 2
wolffd@0 67 end
wolffd@0 68 else % no F signal from below
wolffd@0 69 if ~isempty(CPD.Fself_ndx) % self F
wolffd@0 70 fmarg = myreshape(fmarginal.T, [eff_oldQsz 2 eff_Qpsz eff_Qsz]);
wolffd@0 71 hor_counts(i_counts_ndx, k_counts_ndx, j_counts_ndx) = fmarg(:, 1, :, :);
wolffd@0 72 ver_counts(k_counts_ndx, j_counts_ndx) = ... % sum over Qold
wolffd@0 73 squeeze(sum(fmarg(:, 2, :, :), 1)); % Fself=2
wolffd@0 74 else % no F from self
wolffd@0 75 error('An hhmmQ node without any F parents is just a tabular_CPD')
wolffd@0 76 end
wolffd@0 77 end
wolffd@0 78
wolffd@0 79
wolffd@0 80 CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts);
wolffd@0 81
wolffd@0 82 if ~isempty(CPD.sub_CPD_start)
wolffd@0 83 CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts);
wolffd@0 84 end
wolffd@0 85
wolffd@0 86