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