annotate toolboxes/FullBNT-1.0.7/bnt/general/linear_gaussian_to_cpot.m @ 0:e9a9cd732c1e tip

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