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 / general / add_evidence_to_gmarginal.m @ 8:b5b38998ef3b
History | View | Annotate | Download (2.23 KB)
| 1 |
function fullm = add_evidence_to_gmarginal(fmarginal, evidence, ns, cnodes) |
|---|---|
| 2 |
% ADD_EVIDENCE_TO_GMARGINAL 'pump up' observed nodes back to their original size. |
| 3 |
% function fullm = add_evidence_to_gmarginal(fmarginal, evidence, ns, cnodes) |
| 4 |
% |
| 5 |
% We introduce 0s into the array in positions which are incompatible with the evidence. |
| 6 |
% for both discrete and continuous nodes. |
| 7 |
% |
| 8 |
% See also add_ev_to_dmarginal |
| 9 |
|
| 10 |
dom = fmarginal.domain; |
| 11 |
fullm.domain = fmarginal.domain; |
| 12 |
|
| 13 |
% Find out which values of the discrete parents (if any) are compatible with |
| 14 |
% the discrete evidence (if any). |
| 15 |
dnodes = mysetdiff(1:length(ns), cnodes); |
| 16 |
ddom = myintersect(dom, dnodes); |
| 17 |
cdom = myintersect(dom, cnodes); |
| 18 |
odom = dom(~isemptycell(evidence(dom))); |
| 19 |
hdom = dom(isemptycell(evidence(dom))); |
| 20 |
|
| 21 |
% Find the entries in the big table that are compatible with the discrete evidence. |
| 22 |
% (We will put the probabilities from the small inferred table into these positions.) |
| 23 |
% We could use add_ev_to_dmarginal to do this. |
| 24 |
dobs = myintersect(ddom, odom); |
| 25 |
dvals = cat(1, evidence{dobs});
|
| 26 |
ens = ns; % effective node sizes |
| 27 |
ens(dobs) = 1; |
| 28 |
S = prod(ens(ddom)); |
| 29 |
subs = ind2subv(ens(ddom), 1:S); |
| 30 |
mask = find_equiv_posns(dobs, ddom); |
| 31 |
%subs(mask) = dvals; % bug fix by P. Brutti |
| 32 |
for i=1:length(mask), |
| 33 |
subs(:,mask(i)) = dvals(i); |
| 34 |
end |
| 35 |
supportedQs = subv2ind(ns(ddom), subs); |
| 36 |
|
| 37 |
if isempty(ddom) |
| 38 |
Qarity = 1; |
| 39 |
else |
| 40 |
Qarity = prod(ns(ddom)); |
| 41 |
end |
| 42 |
fullm.T = zeros(Qarity, 1); |
| 43 |
fullm.T(supportedQs) = fmarginal.T(:); |
| 44 |
fullm.T = myreshape(fullm.T, ns(ddom)); |
| 45 |
|
| 46 |
|
| 47 |
if isempty(cdom) |
| 48 |
fullm.mu = []; |
| 49 |
fullm.sigma = []; |
| 50 |
return; |
| 51 |
end |
| 52 |
|
| 53 |
% Now put the hidden cts parts into their right blocks, |
| 54 |
% leaving the observed cts parts as 0. |
| 55 |
cobs = myintersect(cdom, odom); |
| 56 |
chid = myintersect(cdom, hdom); |
| 57 |
cvals = cat(1, evidence{cobs});
|
| 58 |
n = sum(ns(cdom)); |
| 59 |
fullm.mu = zeros(n,Qarity); |
| 60 |
fullm.Sigma = zeros(n,n,Qarity); |
| 61 |
|
| 62 |
if ~isempty(chid) |
| 63 |
chid_blocks = block(find_equiv_posns(chid, cdom), ns(cdom)); |
| 64 |
end |
| 65 |
if ~isempty(cobs) |
| 66 |
cobs_blocks = block(find_equiv_posns(cobs, cdom), ns(cdom)); |
| 67 |
end |
| 68 |
|
| 69 |
for i=1:length(supportedQs) |
| 70 |
Q = supportedQs(i); |
| 71 |
if ~isempty(chid) |
| 72 |
fullm.mu(chid_blocks, Q) = fmarginal.mu(:, i); |
| 73 |
fullm.Sigma(chid_blocks, chid_blocks, Q) = fmarginal.Sigma(:,:,i); |
| 74 |
end |
| 75 |
if ~isempty(cobs) |
| 76 |
fullm.mu(cobs_blocks, Q) = cvals(:); |
| 77 |
end |
| 78 |
end |