wolffd@0: function pot = CPD_to_scgpot(CPD, domain, ns, cnodes, evidence) wolffd@0: % CPD_TO_CGPOT Convert a Gaussian CPD to a CG potential, incorporating any evidence wolffd@0: % pot = CPD_to_cgpot(CPD, domain, ns, cnodes, evidence) wolffd@0: wolffd@0: self = CPD.self; wolffd@0: dnodes = mysetdiff(1:length(ns), cnodes); wolffd@0: odom = domain(~isemptycell(evidence(domain))); wolffd@0: cdom = myintersect(cnodes, domain); wolffd@0: cheaddom = myintersect(self, domain); wolffd@0: ctaildom = mysetdiff(cdom,cheaddom); wolffd@0: ddom = myintersect(dnodes, domain); wolffd@0: cobs = myintersect(cdom, odom); wolffd@0: dobs = myintersect(ddom, odom); wolffd@0: ens = ns; % effective node size wolffd@0: ens(cobs) = 0; wolffd@0: ens(dobs) = 1; wolffd@0: wolffd@0: % Extract the params compatible with the observations (if any) on the discrete parents (if any) wolffd@0: % parents are all but the last domain element wolffd@0: ps = domain(1:end-1); wolffd@0: dps = myintersect(ps, ddom); wolffd@0: dops = myintersect(dps, odom); wolffd@0: wolffd@0: map = find_equiv_posns(dops, dps); wolffd@0: dpvals = cat(1, evidence{dops}); wolffd@0: index = mk_multi_index(length(dps), map, dpvals); wolffd@0: wolffd@0: dpsize = prod(ens(dps)); wolffd@0: cpsize = size(CPD.weights(:,:,1), 2); % cts parents size wolffd@0: ss = size(CPD.mean, 1); % self size wolffd@0: % the reshape acts like a squeeze wolffd@0: m = reshape(CPD.mean(:, index{:}), [ss dpsize]); wolffd@0: C = reshape(CPD.cov(:, :, index{:}), [ss ss dpsize]); wolffd@0: W = reshape(CPD.weights(:, :, index{:}), [ss cpsize dpsize]); wolffd@0: wolffd@0: wolffd@0: % Convert each conditional Gaussian to a canonical potential wolffd@0: pot = cell(1, dpsize); wolffd@0: for i=1:dpsize wolffd@0: %pot{i} = linear_gaussian_to_scgcpot(m(:,i), C(:,:,i), W(:,:,i), cdom, ns, cnodes, evidence); wolffd@0: pot{i} = scgcpot(ss, cpsize, 1, m(:,i), W(:,:,i), C(:,:,i)); wolffd@0: end wolffd@0: wolffd@0: pot = scgpot(ddom, cheaddom, ctaildom, ens, pot); wolffd@0: wolffd@0: wolffd@0: function pot = linear_gaussian_to_scgcpot(mu, Sigma, W, domain, ns, cnodes, evidence) wolffd@0: % LINEAR_GAUSSIAN_TO_CPOT Convert a linear Gaussian CPD to a stable conditional potential element. wolffd@0: % pot = linear_gaussian_to_cpot(mu, Sigma, W, domain, ns, cnodes, evidence) wolffd@0: wolffd@0: p = 1; wolffd@0: A = mu; wolffd@0: B = W; wolffd@0: C = Sigma; wolffd@0: ns(odom) = 0; wolffd@0: %pot = scgcpot(, ns(domain), p, A, B, C); wolffd@0: wolffd@0: