Daniel@0: function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv) Daniel@0: % UPDATE_ESS Update the Expected Sufficient Statistics of a hhmm Q node. Daniel@0: % function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, idden_bitv) Daniel@0: Daniel@0: % Figure out the node numbers associated with each parent Daniel@0: % 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)]. Daniel@0: % so self = Q3(t), old_self = Q3(t-1), CPD.Qps = [1 2], Qps = [Q1(t) Q2(t)] Daniel@0: dom = fmarginal.domain; Daniel@0: self = dom(end); Daniel@0: old_self = dom(1); Daniel@0: Qps = dom(length(dom)-length(CPD.Qps):end-1); Daniel@0: Daniel@0: Qsz = CPD.Qsizes(CPD.d); Daniel@0: Qpsz = prod(CPD.Qsizes(CPD.Qps)); Daniel@0: Daniel@0: % If some of the Q nodes are observed (which happens during supervised training) Daniel@0: % the counts will only be non-zero in positions Daniel@0: % consistent with the evidence. We put the computed marginal responsibilities Daniel@0: % into the appropriate slots of the big counts array. Daniel@0: % (Recall that observed discrete nodes only have a single effective value.) Daniel@0: % (A more general, but much slower, way is to call add_evidence_to_dmarginal.) Daniel@0: % We assume the F nodes are never observed. Daniel@0: Daniel@0: obs_self = ~hidden_bitv(self); Daniel@0: obs_Qps = (~isempty(Qps)) & (~any(hidden_bitv(Qps))); % we assume that all or none of the Q parents are observed Daniel@0: Daniel@0: if obs_self Daniel@0: self_val = evidence{self}; Daniel@0: oldself_val = evidence{old_self}; Daniel@0: end Daniel@0: Daniel@0: if obs_Qps Daniel@0: Qps_val = subv2ind(Qpsz, cat(1, evidence{Qps})); Daniel@0: if Qps_val == 0 Daniel@0: keyboard Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if CPD.d==1 % no Qps from above Daniel@0: if ~CPD.F1toQ1 % no F from self Daniel@0: % marg(Q1(t-1), F2(t-1), Q1(t)) Daniel@0: % F2(t-1) P(Q1(t)=j | Q1(t-1)=i) Daniel@0: % 1 delta(i,j) Daniel@0: % 2 transprob(i,j) Daniel@0: if obs_self Daniel@0: hor_counts = zeros(Qsz, Qsz); Daniel@0: hor_counts(oldself_val, self_val) = fmarginal.T(2); Daniel@0: else Daniel@0: marg = reshape(fmarginal.T, [Qsz 2 Qsz]); Daniel@0: hor_counts = squeeze(marg(:,2,:)); Daniel@0: end Daniel@0: else Daniel@0: % marg(Q1(t-1), F2(t-1), F1(t-1), Q1(t)) Daniel@0: % F2(t-1) F1(t-1) P(Qd(t)=j| Qd(t-1)=i) Daniel@0: % ------------------------------------------------------ Daniel@0: % 1 1 delta(i,j) Daniel@0: % 2 1 transprob(i,j) Daniel@0: % 1 2 impossible Daniel@0: % 2 2 startprob(j) Daniel@0: if obs_self Daniel@0: marg = myreshape(fmarginal.T, [1 2 2 1]); Daniel@0: hor_counts = zeros(Qsz, Qsz); Daniel@0: hor_counts(oldself_val, self_val) = marg(1,2,1,1); Daniel@0: ver_counts = zeros(Qsz, 1); Daniel@0: %ver_counts(self_val) = marg(1,2,2,1); Daniel@0: ver_counts(self_val) = marg(1,2,2,1) + marg(1,1,2,1); Daniel@0: else Daniel@0: marg = reshape(fmarginal.T, [Qsz 2 2 Qsz]); Daniel@0: hor_counts = squeeze(marg(:,2,1,:)); Daniel@0: %ver_counts = squeeze(sum(marg(:,2,2,:),1)); % sum over i Daniel@0: ver_counts = squeeze(sum(marg(:,2,2,:),1)) + squeeze(sum(marg(:,1,2,:),1)); % sum i,b Daniel@0: end Daniel@0: end % F1toQ1 Daniel@0: else % d ~= 1 Daniel@0: if CPD.d < CPD.D % general case Daniel@0: % marg(Qd(t-1), Fd+1(t-1), Fd(t-1), Qps(t), Qd(t)) Daniel@0: % Fd+1(t-1) Fd(t-1) P(Qd(t)=j| Qd(t-1)=i, Qps(t)=k) Daniel@0: % ------------------------------------------------------ Daniel@0: % 1 1 delta(i,j) Daniel@0: % 2 1 transprob(i,k,j) Daniel@0: % 1 2 impossible Daniel@0: % 2 2 startprob(k,j) Daniel@0: if obs_Qps & obs_self Daniel@0: marg = myreshape(fmarginal.T, [1 2 2 1 1]); Daniel@0: k = 1; Daniel@0: hor_counts = zeros(Qsz, Qpsz, Qsz); Daniel@0: hor_counts(oldself_val, Qps_val, self_val) = marg(1, 2,1, k,1); Daniel@0: ver_counts = zeros(Qpsz, Qsz); Daniel@0: %ver_counts(Qps_val, self_val) = marg(1, 2,2, k,1); Daniel@0: ver_counts(Qps_val, self_val) = marg(1, 2,2, k,1) + marg(1, 1,2, k,1); Daniel@0: elseif obs_Qps & ~obs_self Daniel@0: marg = myreshape(fmarginal.T, [Qsz 2 2 1 Qsz]); Daniel@0: k = 1; Daniel@0: hor_counts = zeros(Qsz, Qpsz, Qsz); Daniel@0: hor_counts(:, Qps_val, :) = marg(:, 2,1, k,:); Daniel@0: ver_counts = zeros(Qpsz, Qsz); Daniel@0: %ver_counts(Qps_val, :) = sum(marg(:, 2,2, k,:), 1); Daniel@0: ver_counts(Qps_val, :) = sum(marg(:, 2,2, k,:), 1) + sum(marg(:, 1,2, k,:), 1); Daniel@0: elseif ~obs_Qps & obs_self Daniel@0: error('not yet implemented') Daniel@0: else % everything is hidden Daniel@0: marg = reshape(fmarginal.T, [Qsz 2 2 Qpsz Qsz]); Daniel@0: hor_counts = squeeze(marg(:,2,1,:,:)); % i,k,j Daniel@0: %ver_counts = squeeze(sum(marg(:,2,2,:,:),1)); % sum over i Daniel@0: ver_counts = squeeze(sum(marg(:,2,2,:,:),1)) + squeeze(sum(marg(:,1,2,:,:),1)); % sum over i,b Daniel@0: end Daniel@0: else % d == D, so no F from below Daniel@0: % marg(QD(t-1), FD(t-1), Qps(t), QD(t)) Daniel@0: % FD(t-1) P(QD(t)=j | QD(t-1)=i, Qps(t)=k) Daniel@0: % 1 transprob(i,k,j) Daniel@0: % 2 startprob(k,j) Daniel@0: if obs_Qps & obs_self Daniel@0: marg = myreshape(fmarginal.T, [1 2 1 1]); Daniel@0: k = 1; Daniel@0: hor_counts = zeros(Qsz, Qpsz, Qsz); Daniel@0: hor_counts(oldself_val, Qps_val, self_val) = marg(1, 1, k,1); Daniel@0: ver_counts = zeros(Qpsz, Qsz); Daniel@0: ver_counts(Qps_val, self_val) = marg(1, 2, k,1); Daniel@0: elseif obs_Qps & ~obs_self Daniel@0: marg = myreshape(fmarginal.T, [Qsz 2 1 Qsz]); Daniel@0: k = 1; Daniel@0: hor_counts = zeros(Qsz, Qpsz, Qsz); Daniel@0: hor_counts(:, Qps_val, :) = marg(:, 1, k,:); Daniel@0: ver_counts = zeros(Qpsz, Qsz); Daniel@0: ver_counts(Qps_val, :) = sum(marg(:, 2, k, :), 1); Daniel@0: elseif ~obs_Qps & obs_self Daniel@0: error('not yet implemented') Daniel@0: else % everything is hidden Daniel@0: marg = reshape(fmarginal.T, [Qsz 2 Qpsz Qsz]); Daniel@0: hor_counts = squeeze(marg(:,1,:,:)); Daniel@0: ver_counts = squeeze(sum(marg(:,2,:,:),1)); % sum over i Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts); Daniel@0: Daniel@0: if ~isempty(CPD.sub_CPD_start) Daniel@0: CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts); Daniel@0: end Daniel@0: