annotate toolboxes/FullBNT-1.0.7/bnt/general/add_evidence_to_gmarginal.m @ 0:e9a9cd732c1e tip

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