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