wolffd@0: function fullm = add_evidence_to_gmarginal(fmarginal, evidence, ns, cnodes) wolffd@0: % ADD_EVIDENCE_TO_GMARGINAL 'pump up' observed nodes back to their original size. wolffd@0: % function fullm = add_evidence_to_gmarginal(fmarginal, evidence, ns, cnodes) wolffd@0: % wolffd@0: % We introduce 0s into the array in positions which are incompatible with the evidence. wolffd@0: % for both discrete and continuous nodes. wolffd@0: % wolffd@0: % See also add_ev_to_dmarginal wolffd@0: wolffd@0: dom = fmarginal.domain; wolffd@0: fullm.domain = fmarginal.domain; wolffd@0: wolffd@0: % Find out which values of the discrete parents (if any) are compatible with wolffd@0: % the discrete evidence (if any). wolffd@0: dnodes = mysetdiff(1:length(ns), cnodes); wolffd@0: ddom = myintersect(dom, dnodes); wolffd@0: cdom = myintersect(dom, cnodes); wolffd@0: odom = dom(~isemptycell(evidence(dom))); wolffd@0: hdom = dom(isemptycell(evidence(dom))); wolffd@0: wolffd@0: % Find the entries in the big table that are compatible with the discrete evidence. wolffd@0: % (We will put the probabilities from the small inferred table into these positions.) wolffd@0: % We could use add_ev_to_dmarginal to do this. wolffd@0: dobs = myintersect(ddom, odom); wolffd@0: dvals = cat(1, evidence{dobs}); wolffd@0: ens = ns; % effective node sizes wolffd@0: ens(dobs) = 1; wolffd@0: S = prod(ens(ddom)); wolffd@0: subs = ind2subv(ens(ddom), 1:S); wolffd@0: mask = find_equiv_posns(dobs, ddom); wolffd@0: %subs(mask) = dvals; % bug fix by P. Brutti wolffd@0: for i=1:length(mask), wolffd@0: subs(:,mask(i)) = dvals(i); wolffd@0: end wolffd@0: supportedQs = subv2ind(ns(ddom), subs); wolffd@0: wolffd@0: if isempty(ddom) wolffd@0: Qarity = 1; wolffd@0: else wolffd@0: Qarity = prod(ns(ddom)); wolffd@0: end wolffd@0: fullm.T = zeros(Qarity, 1); wolffd@0: fullm.T(supportedQs) = fmarginal.T(:); wolffd@0: fullm.T = myreshape(fullm.T, ns(ddom)); wolffd@0: wolffd@0: wolffd@0: if isempty(cdom) wolffd@0: fullm.mu = []; wolffd@0: fullm.sigma = []; wolffd@0: return; wolffd@0: end wolffd@0: wolffd@0: % Now put the hidden cts parts into their right blocks, wolffd@0: % leaving the observed cts parts as 0. wolffd@0: cobs = myintersect(cdom, odom); wolffd@0: chid = myintersect(cdom, hdom); wolffd@0: cvals = cat(1, evidence{cobs}); wolffd@0: n = sum(ns(cdom)); wolffd@0: fullm.mu = zeros(n,Qarity); wolffd@0: fullm.Sigma = zeros(n,n,Qarity); wolffd@0: wolffd@0: if ~isempty(chid) wolffd@0: chid_blocks = block(find_equiv_posns(chid, cdom), ns(cdom)); wolffd@0: end wolffd@0: if ~isempty(cobs) wolffd@0: cobs_blocks = block(find_equiv_posns(cobs, cdom), ns(cdom)); wolffd@0: end wolffd@0: wolffd@0: for i=1:length(supportedQs) wolffd@0: Q = supportedQs(i); wolffd@0: if ~isempty(chid) wolffd@0: fullm.mu(chid_blocks, Q) = fmarginal.mu(:, i); wolffd@0: fullm.Sigma(chid_blocks, chid_blocks, Q) = fmarginal.Sigma(:,:,i); wolffd@0: end wolffd@0: if ~isempty(cobs) wolffd@0: fullm.mu(cobs_blocks, Q) = cvals(:); wolffd@0: end wolffd@0: end