annotate _FullBNT/BNT/general/linear_gaussian_to_cpot.m @ 8:b5b38998ef3b

added all that other stuff
author matthiasm
date Fri, 11 Apr 2014 15:54:25 +0100
parents
children
rev   line source
matthiasm@8 1 function pot = linear_gaussian_to_cpot(mu, Sigma, W, domain, ns, cnodes, evidence)
matthiasm@8 2 % LINEAR_GAUSSIAN_TO_CPOT Convert a linear Gaussian CPD to a canonical potential.
matthiasm@8 3 % pot = linear_gaussian_to_cpot(mu, Sigma, W, domain, ns, cnodes, evidence)
matthiasm@8 4 %
matthiasm@8 5 % We include any cts evidence, but ignore any discrete evidence.
matthiasm@8 6 % (Use gaussian_CPD_params_given_dps to use discrete evidence to select mu, Sigma, W.)
matthiasm@8 7
matthiasm@8 8 odom = domain(~isemptycell(evidence(domain)));
matthiasm@8 9 hdom = domain(isemptycell(evidence(domain)));
matthiasm@8 10 cobs = myintersect(cnodes, odom);
matthiasm@8 11 chid = myintersect(cnodes, hdom);
matthiasm@8 12 cvals = cat(1, evidence{cobs});
matthiasm@8 13
matthiasm@8 14 %[g,h,K] = gaussian_to_canonical(mu, Sigma, W);
matthiasm@8 15 Sinv = inv(Sigma);
matthiasm@8 16 g = -0.5*mu'*Sinv*mu + log(normal_coef(Sigma));
matthiasm@8 17 if isempty(W) | (size(W,2)==0) % no cts parents
matthiasm@8 18 h = Sinv*mu;
matthiasm@8 19 K = Sinv;
matthiasm@8 20 else
matthiasm@8 21 h = [-W'*Sinv*mu; Sinv*mu];
matthiasm@8 22 K = [W'*Sinv*W -W'*Sinv';
matthiasm@8 23 -Sinv*W Sinv];
matthiasm@8 24 end
matthiasm@8 25
matthiasm@8 26 if ~isempty(cvals)
matthiasm@8 27 %[g, h, K] = enter_evidence_canonical(g, h, K, chid, cobs, cvals(:), ns);
matthiasm@8 28 [hx, hy, KXX, KXY, KYX, KYY] = partition_matrix_vec(h, K, chid, cobs, ns);
matthiasm@8 29 y = cvals(:);
matthiasm@8 30 g = g + hy'*y - 0.5*y'*KYY*y;
matthiasm@8 31 if length(hx)==0 % isempty(X) % i.e., we have instantiated everything away
matthiasm@8 32 h = [];
matthiasm@8 33 K = [];
matthiasm@8 34 else
matthiasm@8 35 h = hx - KXY*y;
matthiasm@8 36 K = KXX;
matthiasm@8 37 end
matthiasm@8 38 end
matthiasm@8 39
matthiasm@8 40 ns(odom) = 0;
matthiasm@8 41 pot = cpot(domain, ns(domain), g, h, K);
matthiasm@8 42
matthiasm@8 43