Daniel@0: function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv) Daniel@0: % UPDATE_ESS Update the Expected Sufficient Statistics of a hhmmF node. Daniel@0: % function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv) Daniel@0: Daniel@0: % Figure out the node numbers associated with each parent Daniel@0: % so we extract evidence from the right place Daniel@0: dom = fmarginal.domain; % Q(1) .. Q(d) F(d+1) F(d) Daniel@0: Qps = fmarginal.domain(1:end-2); Daniel@0: Q = Qps(end); Daniel@0: Qps = Qps(1:end-1); Daniel@0: Daniel@0: Qsz = CPD.Qsizes(CPD.Q); Daniel@0: Qpsz = prod(CPD.Qsizes(CPD.Qps)); % may be 1 Daniel@0: Daniel@0: % We assume the F node are always hidden, but allow some of the Q nodes Daniel@0: % to be observed. We do case analysis for speed. Daniel@0: %We only extract prob from fmarginal.T when F(d+1)=2 i.e., model below has finished. Daniel@0: % wrong -> % We sum over the possibilities that F(d+1) = 1 or 2 Daniel@0: Daniel@0: obs_self = ~hidden_bitv(Q); Daniel@0: if obs_self Daniel@0: self_val = evidence{Q}; Daniel@0: end Daniel@0: Daniel@0: if isempty(Qps) % independent of parent context Daniel@0: counts = zeros(Qsz, 2); Daniel@0: %fmarginal.T(Q(d), F(d+1), F(d)) Daniel@0: if obs_self Daniel@0: marg = myreshape(fmarginal.T, [1 2 2]); Daniel@0: counts(self_val,:) = marg(1,2,:); Daniel@0: %counts(self_val,:) = marg(1,1,:) + marg(1,2,:); Daniel@0: else Daniel@0: marg = myreshape(fmarginal.T, [Qsz 2 2]); Daniel@0: counts = squeeze(marg(:,2,:)); Daniel@0: %counts = squeeze(marg(:,2,:)) + squeeze(marg(:,1,:)); Daniel@0: end Daniel@0: else Daniel@0: counts = zeros(Qpsz, Qsz, 2); Daniel@0: %fmarginal.T(Q(1:d-1), Q(d), F(d+1), F(d)) Daniel@0: obs_Qps = ~any(hidden_bitv(Qps)); % we assume that all or none of the Q parents are observed Daniel@0: if obs_Qps Daniel@0: Qps_val = subv2ind(Qpsz, cat(1, evidence{Qps})); Daniel@0: end Daniel@0: if obs_self & obs_Qps Daniel@0: marg = myreshape(fmarginal.T, [1 1 2 2]); Daniel@0: counts(Qps_val, self_val, :) = squeeze(marg(1,1,2,:)); Daniel@0: %counts(Qps_val, self_val, :) = squeeze(marg(1,1,2,:)) + squeeze(marg(1,1,1,:)); Daniel@0: elseif ~obs_self & obs_Qps Daniel@0: marg = myreshape(fmarginal.T, [1 Qsz 2 2]); Daniel@0: counts(Qps_val, :, :) = squeeze(marg(1,:,2,:)); Daniel@0: %counts(Qps_val, :, :) = squeeze(marg(1,:,2,:)) + squeeze(marg(1,:,1,:)); Daniel@0: elseif obs_self & ~obs_Qps Daniel@0: error('not yet implemented') Daniel@0: else Daniel@0: marg = myreshape(fmarginal.T, [Qpsz Qsz 2 2]); Daniel@0: counts(:, :, :) = squeeze(marg(:,:,2,:)); Daniel@0: %counts(:, :, :) = squeeze(marg(:,:,2,:)) + squeeze(marg(:,:,1,:)); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: CPD.sub_CPD_term = update_ess_simple(CPD.sub_CPD_term, counts);