matthiasm@8: function pot = linear_gaussian_to_cpot(mu, Sigma, W, domain, ns, cnodes, evidence) matthiasm@8: % LINEAR_GAUSSIAN_TO_CPOT Convert a linear Gaussian CPD to a canonical potential. matthiasm@8: % pot = linear_gaussian_to_cpot(mu, Sigma, W, domain, ns, cnodes, evidence) matthiasm@8: % matthiasm@8: % We include any cts evidence, but ignore any discrete evidence. matthiasm@8: % (Use gaussian_CPD_params_given_dps to use discrete evidence to select mu, Sigma, W.) matthiasm@8: matthiasm@8: odom = domain(~isemptycell(evidence(domain))); matthiasm@8: hdom = domain(isemptycell(evidence(domain))); matthiasm@8: cobs = myintersect(cnodes, odom); matthiasm@8: chid = myintersect(cnodes, hdom); matthiasm@8: cvals = cat(1, evidence{cobs}); matthiasm@8: matthiasm@8: %[g,h,K] = gaussian_to_canonical(mu, Sigma, W); matthiasm@8: Sinv = inv(Sigma); matthiasm@8: g = -0.5*mu'*Sinv*mu + log(normal_coef(Sigma)); matthiasm@8: if isempty(W) | (size(W,2)==0) % no cts parents matthiasm@8: h = Sinv*mu; matthiasm@8: K = Sinv; matthiasm@8: else matthiasm@8: h = [-W'*Sinv*mu; Sinv*mu]; matthiasm@8: K = [W'*Sinv*W -W'*Sinv'; matthiasm@8: -Sinv*W Sinv]; matthiasm@8: end matthiasm@8: matthiasm@8: if ~isempty(cvals) matthiasm@8: %[g, h, K] = enter_evidence_canonical(g, h, K, chid, cobs, cvals(:), ns); matthiasm@8: [hx, hy, KXX, KXY, KYX, KYY] = partition_matrix_vec(h, K, chid, cobs, ns); matthiasm@8: y = cvals(:); matthiasm@8: g = g + hy'*y - 0.5*y'*KYY*y; matthiasm@8: if length(hx)==0 % isempty(X) % i.e., we have instantiated everything away matthiasm@8: h = []; matthiasm@8: K = []; matthiasm@8: else matthiasm@8: h = hx - KXY*y; matthiasm@8: K = KXX; matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: ns(odom) = 0; matthiasm@8: pot = cpot(domain, ns(domain), g, h, K); matthiasm@8: matthiasm@8: