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

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