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