To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _FullBNT / BNT / CPDs / @hhmmQ_CPD / Old / update_ess3.m @ 8:b5b38998ef3b
History | View | Annotate | Download (2.54 KB)
| 1 |
function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, hidden_bitv) |
|---|---|
| 2 |
% UPDATE_ESS Update the Expected Sufficient Statistics of a hhmm Q node. |
| 3 |
% function CPD = update_ess(CPD, fmarginal, evidence, ns, cnodes, idden_bitv) |
| 4 |
% |
| 5 |
% we assume if one of the Qps is observed, all of them are |
| 6 |
% We assume the F nodes are already hidden |
| 7 |
|
| 8 |
% Figure out the node numbers associated with each parent |
| 9 |
dom = fmarginal.domain; |
| 10 |
self = dom(CPD.self_ndx); |
| 11 |
old_self = dom(CPD.old_self_ndx); |
| 12 |
%Fself = dom(CPD.Fself_ndx); |
| 13 |
%Fbelow = dom(CPD.Fbelow_ndx); |
| 14 |
Qps = dom(CPD.Qps_ndx); |
| 15 |
|
| 16 |
Qsz = CPD.Qsz; |
| 17 |
Qpsz = CPD.Qpsz; |
| 18 |
|
| 19 |
|
| 20 |
% hor_counts(old_self, Qps, self), |
| 21 |
% fmarginal(old_self, Fbelow, Fself, Qps, self) |
| 22 |
% hor_counts(i,k,j) = fmarginal(i,2,1,k,j) % below has finished, self has not |
| 23 |
% ver_counts(i,k,j) = fmarginal(i,2,2,k,j) % below has finished, and so has self (reset) |
| 24 |
% Since any of i,j,k may be observed, we write |
| 25 |
% hor_counts(ndx{:}) = fmarginal(...)
|
| 26 |
% where e.g., ndx = {1, ':', 2} if Qps is hidden but we observe old_self=1, self=2.
|
| 27 |
|
| 28 |
% ndx{i,k,j}
|
| 29 |
if hidden_bitv(old_self) |
| 30 |
ndx{1} = ':';
|
| 31 |
else |
| 32 |
ndx{1} = evidence{old_self};
|
| 33 |
end |
| 34 |
if hidden_bitv(Qps) |
| 35 |
ndx{2} = ':';
|
| 36 |
else |
| 37 |
ndx{2} = subv2ind(Qpsz, cat(1, evidence{Qps}));
|
| 38 |
end |
| 39 |
if hidden_bitv(self) |
| 40 |
ndx{3} = ':';
|
| 41 |
else |
| 42 |
ndx{3} = evidence{self};
|
| 43 |
end |
| 44 |
|
| 45 |
fmarg = add_ev_to_dmarginal(fmarginal, evidence, ns); |
| 46 |
% marg(Qold(t-1), Fbelow(t-1), Fself(t-1), Qps(t), Qself(t)) |
| 47 |
hor_counts = zeros(Qsz, Qpsz, Qsz); |
| 48 |
ver_counts = zeros(Qpsz, Qsz); |
| 49 |
|
| 50 |
if ~isempty(CPD.Fbelow_ndx) |
| 51 |
if ~isempty(CPD.Fself_ndx) % general case |
| 52 |
fmarg.T = myreshape(fmarg.T, [Qsz 2 2 Qpsz Qsz]); |
| 53 |
marg_ndx = {ndx{1}, 2, 1, ndx{2}, ndx{3}};
|
| 54 |
hor_counts(ndx{:}) = fmarg.T(marg_ndx{:});
|
| 55 |
ver_counts(ndx{2:3}) = ... % sum over Fbelow and Qold=i
|
| 56 |
sum(fmarg.T({ndx{1}, 1, 2, ndx{2}, ndx{3}}),1) + ..
|
| 57 |
sum(fmarg.T({ndx{1}, 2, 2, ndx{2}, ndx{3}}),1);
|
| 58 |
else % no F from self, hence no startprob |
| 59 |
fmarg.T = myreshape(fmarg.T, [Qsz 2 Qpsz Qsz]); |
| 60 |
hor_counts(ndx{:}) = fmarg.T({ndx{1}, 2, ndx{2}, ndx{3}});
|
| 61 |
end |
| 62 |
else % no F signal from below |
| 63 |
if ~isempty(CPD.Fself_ndx) % self F |
| 64 |
fmarg.T = myreshape(fmarg.T, [Qsz 2 Qpsz Qsz]); |
| 65 |
hor_counts(ndx{:}) = fmarg.T({ndx{1}, 1, ndx{2}, ndx{3}});
|
| 66 |
ver_counts(ndx{2:3}) = ... % sum over Qold=i
|
| 67 |
sum(fmarg.T({ndx{1}, 2, ndx{2}, ndx{3}}),1);
|
| 68 |
else % no F from self |
| 69 |
error('An hhmmQ node without any F parents is just a tabular_CPD')
|
| 70 |
end |
| 71 |
end |
| 72 |
|
| 73 |
|
| 74 |
CPD.sub_CPD_trans = update_ess_simple(CPD.sub_CPD_trans, hor_counts); |
| 75 |
|
| 76 |
if ~isempty(CPD.sub_CPD_start) |
| 77 |
CPD.sub_CPD_start = update_ess_simple(CPD.sub_CPD_start, ver_counts); |
| 78 |
end |
| 79 |
|
| 80 |
|