annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmF_CPD/Old/update_ess.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv)
Daniel@0 2 % UPDATE_ESS Update the Expected Sufficient Statistics of a hhmmF node.
Daniel@0 3 % function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv)
Daniel@0 4
Daniel@0 5 % Figure out the node numbers associated with each parent
Daniel@0 6 % so we extract evidence from the right place
Daniel@0 7 dom = fmarginal.domain; % Q(1) .. Q(d) F(d+1) F(d)
Daniel@0 8 Qps = fmarginal.domain(1:end-2);
Daniel@0 9 Q = Qps(end);
Daniel@0 10 Qps = Qps(1:end-1);
Daniel@0 11
Daniel@0 12 Qsz = CPD.Qsizes(CPD.Q);
Daniel@0 13 Qpsz = prod(CPD.Qsizes(CPD.Qps)); % may be 1
Daniel@0 14
Daniel@0 15 % We assume the F node are always hidden, but allow some of the Q nodes
Daniel@0 16 % to be observed. We do case analysis for speed.
Daniel@0 17 %We only extract prob from fmarginal.T when F(d+1)=2 i.e., model below has finished.
Daniel@0 18 % wrong -> % We sum over the possibilities that F(d+1) = 1 or 2
Daniel@0 19
Daniel@0 20 obs_self = ~hidden_bitv(Q);
Daniel@0 21 if obs_self
Daniel@0 22 self_val = evidence{Q};
Daniel@0 23 end
Daniel@0 24
Daniel@0 25 if isempty(Qps) % independent of parent context
Daniel@0 26 counts = zeros(Qsz, 2);
Daniel@0 27 %fmarginal.T(Q(d), F(d+1), F(d))
Daniel@0 28 if obs_self
Daniel@0 29 marg = myreshape(fmarginal.T, [1 2 2]);
Daniel@0 30 counts(self_val,:) = marg(1,2,:);
Daniel@0 31 %counts(self_val,:) = marg(1,1,:) + marg(1,2,:);
Daniel@0 32 else
Daniel@0 33 marg = myreshape(fmarginal.T, [Qsz 2 2]);
Daniel@0 34 counts = squeeze(marg(:,2,:));
Daniel@0 35 %counts = squeeze(marg(:,2,:)) + squeeze(marg(:,1,:));
Daniel@0 36 end
Daniel@0 37 else
Daniel@0 38 counts = zeros(Qpsz, Qsz, 2);
Daniel@0 39 %fmarginal.T(Q(1:d-1), Q(d), F(d+1), F(d))
Daniel@0 40 obs_Qps = ~any(hidden_bitv(Qps)); % we assume that all or none of the Q parents are observed
Daniel@0 41 if obs_Qps
Daniel@0 42 Qps_val = subv2ind(Qpsz, cat(1, evidence{Qps}));
Daniel@0 43 end
Daniel@0 44 if obs_self & obs_Qps
Daniel@0 45 marg = myreshape(fmarginal.T, [1 1 2 2]);
Daniel@0 46 counts(Qps_val, self_val, :) = squeeze(marg(1,1,2,:));
Daniel@0 47 %counts(Qps_val, self_val, :) = squeeze(marg(1,1,2,:)) + squeeze(marg(1,1,1,:));
Daniel@0 48 elseif ~obs_self & obs_Qps
Daniel@0 49 marg = myreshape(fmarginal.T, [1 Qsz 2 2]);
Daniel@0 50 counts(Qps_val, :, :) = squeeze(marg(1,:,2,:));
Daniel@0 51 %counts(Qps_val, :, :) = squeeze(marg(1,:,2,:)) + squeeze(marg(1,:,1,:));
Daniel@0 52 elseif obs_self & ~obs_Qps
Daniel@0 53 error('not yet implemented')
Daniel@0 54 else
Daniel@0 55 marg = myreshape(fmarginal.T, [Qpsz Qsz 2 2]);
Daniel@0 56 counts(:, :, :) = squeeze(marg(:,:,2,:));
Daniel@0 57 %counts(:, :, :) = squeeze(marg(:,:,2,:)) + squeeze(marg(:,:,1,:));
Daniel@0 58 end
Daniel@0 59 end
Daniel@0 60
Daniel@0 61 CPD.sub_CPD_term = update_ess_simple(CPD.sub_CPD_term, counts);