annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@hhmmQ_CPD/Old/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 % Figure out the node numbers associated with each parent
wolffd@0 6 % e.g., D=4, d=3, Qps = all Qs above, so dom = [Q3(t-1) F4(t-1) F3(t-1) Q1(t) Q2(t) Q3(t)].
wolffd@0 7 % so self = Q3(t), old_self = Q3(t-1), CPD.Qps = [1 2], Qps = [Q1(t) Q2(t)]
wolffd@0 8 dom = fmarginal.domain;
wolffd@0 9 self = dom(end);
wolffd@0 10 old_self = dom(1);
wolffd@0 11 Qps = dom(length(dom)-length(CPD.Qps):end-1);
wolffd@0 12
wolffd@0 13 Qsz = CPD.Qsizes(CPD.d);
wolffd@0 14 Qpsz = prod(CPD.Qsizes(CPD.Qps));
wolffd@0 15
wolffd@0 16 % If some of the Q nodes are observed (which happens during supervised training)
wolffd@0 17 % the counts will only be non-zero in positions
wolffd@0 18 % consistent with the evidence. We put the computed marginal responsibilities
wolffd@0 19 % into the appropriate slots of the big counts array.
wolffd@0 20 % (Recall that observed discrete nodes only have a single effective value.)
wolffd@0 21 % (A more general, but much slower, way is to call add_evidence_to_dmarginal.)
wolffd@0 22 % We assume the F nodes are never observed.
wolffd@0 23
wolffd@0 24 obs_self = ~hidden_bitv(self);
wolffd@0 25 obs_Qps = (~isempty(Qps)) & (~any(hidden_bitv(Qps))); % we assume that all or none of the Q parents are observed
wolffd@0 26
wolffd@0 27 if obs_self
wolffd@0 28 self_val = evidence{self};
wolffd@0 29 oldself_val = evidence{old_self};
wolffd@0 30 end
wolffd@0 31
wolffd@0 32 if obs_Qps
wolffd@0 33 Qps_val = subv2ind(Qpsz, cat(1, evidence{Qps}));
wolffd@0 34 if Qps_val == 0
wolffd@0 35 keyboard
wolffd@0 36 end
wolffd@0 37 end
wolffd@0 38
wolffd@0 39 if CPD.d==1 % no Qps from above
wolffd@0 40 if ~CPD.F1toQ1 % no F from self
wolffd@0 41 % marg(Q1(t-1), F2(t-1), Q1(t))
wolffd@0 42 % F2(t-1) P(Q1(t)=j | Q1(t-1)=i)
wolffd@0 43 % 1 delta(i,j)
wolffd@0 44 % 2 transprob(i,j)
wolffd@0 45 if obs_self
wolffd@0 46 hor_counts = zeros(Qsz, Qsz);
wolffd@0 47 hor_counts(oldself_val, self_val) = fmarginal.T(2);
wolffd@0 48 else
wolffd@0 49 marg = reshape(fmarginal.T, [Qsz 2 Qsz]);
wolffd@0 50 hor_counts = squeeze(marg(:,2,:));
wolffd@0 51 end
wolffd@0 52 else
wolffd@0 53 % marg(Q1(t-1), F2(t-1), F1(t-1), Q1(t))
wolffd@0 54 % F2(t-1) F1(t-1) P(Qd(t)=j| Qd(t-1)=i)
wolffd@0 55 % ------------------------------------------------------
wolffd@0 56 % 1 1 delta(i,j)
wolffd@0 57 % 2 1 transprob(i,j)
wolffd@0 58 % 1 2 impossible
wolffd@0 59 % 2 2 startprob(j)
wolffd@0 60 if obs_self
wolffd@0 61 marg = myreshape(fmarginal.T, [1 2 2 1]);
wolffd@0 62 hor_counts = zeros(Qsz, Qsz);
wolffd@0 63 hor_counts(oldself_val, self_val) = marg(1,2,1,1);
wolffd@0 64 ver_counts = zeros(Qsz, 1);
wolffd@0 65 %ver_counts(self_val) = marg(1,2,2,1);
wolffd@0 66 ver_counts(self_val) = marg(1,2,2,1) + marg(1,1,2,1);
wolffd@0 67 else
wolffd@0 68 marg = reshape(fmarginal.T, [Qsz 2 2 Qsz]);
wolffd@0 69 hor_counts = squeeze(marg(:,2,1,:));
wolffd@0 70 %ver_counts = squeeze(sum(marg(:,2,2,:),1)); % sum over i
wolffd@0 71 ver_counts = squeeze(sum(marg(:,2,2,:),1)) + squeeze(sum(marg(:,1,2,:),1)); % sum i,b
wolffd@0 72 end
wolffd@0 73 end % F1toQ1
wolffd@0 74 else % d ~= 1
wolffd@0 75 if CPD.d < CPD.D % general case
wolffd@0 76 % marg(Qd(t-1), Fd+1(t-1), Fd(t-1), Qps(t), Qd(t))
wolffd@0 77 % Fd+1(t-1) Fd(t-1) P(Qd(t)=j| Qd(t-1)=i, Qps(t)=k)
wolffd@0 78 % ------------------------------------------------------
wolffd@0 79 % 1 1 delta(i,j)
wolffd@0 80 % 2 1 transprob(i,k,j)
wolffd@0 81 % 1 2 impossible
wolffd@0 82 % 2 2 startprob(k,j)
wolffd@0 83 if obs_Qps & obs_self
wolffd@0 84 marg = myreshape(fmarginal.T, [1 2 2 1 1]);
wolffd@0 85 k = 1;
wolffd@0 86 hor_counts = zeros(Qsz, Qpsz, Qsz);
wolffd@0 87 hor_counts(oldself_val, Qps_val, self_val) = marg(1, 2,1, k,1);
wolffd@0 88 ver_counts = zeros(Qpsz, Qsz);
wolffd@0 89 %ver_counts(Qps_val, self_val) = marg(1, 2,2, k,1);
wolffd@0 90 ver_counts(Qps_val, self_val) = marg(1, 2,2, k,1) + marg(1, 1,2, k,1);
wolffd@0 91 elseif obs_Qps & ~obs_self
wolffd@0 92 marg = myreshape(fmarginal.T, [Qsz 2 2 1 Qsz]);
wolffd@0 93 k = 1;
wolffd@0 94 hor_counts = zeros(Qsz, Qpsz, Qsz);
wolffd@0 95 hor_counts(:, Qps_val, :) = marg(:, 2,1, k,:);
wolffd@0 96 ver_counts = zeros(Qpsz, Qsz);
wolffd@0 97 %ver_counts(Qps_val, :) = sum(marg(:, 2,2, k,:), 1);
wolffd@0 98 ver_counts(Qps_val, :) = sum(marg(:, 2,2, k,:), 1) + sum(marg(:, 1,2, k,:), 1);
wolffd@0 99 elseif ~obs_Qps & obs_self
wolffd@0 100 error('not yet implemented')
wolffd@0 101 else % everything is hidden
wolffd@0 102 marg = reshape(fmarginal.T, [Qsz 2 2 Qpsz Qsz]);
wolffd@0 103 hor_counts = squeeze(marg(:,2,1,:,:)); % i,k,j
wolffd@0 104 %ver_counts = squeeze(sum(marg(:,2,2,:,:),1)); % sum over i
wolffd@0 105 ver_counts = squeeze(sum(marg(:,2,2,:,:),1)) + squeeze(sum(marg(:,1,2,:,:),1)); % sum over i,b
wolffd@0 106 end
wolffd@0 107 else % d == D, so no F from below
wolffd@0 108 % marg(QD(t-1), FD(t-1), Qps(t), QD(t))
wolffd@0 109 % FD(t-1) P(QD(t)=j | QD(t-1)=i, Qps(t)=k)
wolffd@0 110 % 1 transprob(i,k,j)
wolffd@0 111 % 2 startprob(k,j)
wolffd@0 112 if obs_Qps & obs_self
wolffd@0 113 marg = myreshape(fmarginal.T, [1 2 1 1]);
wolffd@0 114 k = 1;
wolffd@0 115 hor_counts = zeros(Qsz, Qpsz, Qsz);
wolffd@0 116 hor_counts(oldself_val, Qps_val, self_val) = marg(1, 1, k,1);
wolffd@0 117 ver_counts = zeros(Qpsz, Qsz);
wolffd@0 118 ver_counts(Qps_val, self_val) = marg(1, 2, k,1);
wolffd@0 119 elseif obs_Qps & ~obs_self
wolffd@0 120 marg = myreshape(fmarginal.T, [Qsz 2 1 Qsz]);
wolffd@0 121 k = 1;
wolffd@0 122 hor_counts = zeros(Qsz, Qpsz, Qsz);
wolffd@0 123 hor_counts(:, Qps_val, :) = marg(:, 1, k,:);
wolffd@0 124 ver_counts = zeros(Qpsz, Qsz);
wolffd@0 125 ver_counts(Qps_val, :) = sum(marg(:, 2, k, :), 1);
wolffd@0 126 elseif ~obs_Qps & obs_self
wolffd@0 127 error('not yet implemented')
wolffd@0 128 else % everything is hidden
wolffd@0 129 marg = reshape(fmarginal.T, [Qsz 2 Qpsz Qsz]);
wolffd@0 130 hor_counts = squeeze(marg(:,1,:,:));
wolffd@0 131 ver_counts = squeeze(sum(marg(:,2,:,:),1)); % sum over i
wolffd@0 132 end
wolffd@0 133 end
wolffd@0 134 end
wolffd@0 135
wolffd@0 136 CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts);
wolffd@0 137
wolffd@0 138 if ~isempty(CPD.sub_CPD_start)
wolffd@0 139 CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts);
wolffd@0 140 end
wolffd@0 141