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